File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ defaultUrl = null
2+ currentSlug = null
3+
4+ imageCache = {}
5+ urlCache = {}
6+
7+ withImage = (url , action ) ->
8+ if imageCache[url]
9+ action (imageCache[url])
10+ else
11+ img = new Image ()
12+ img .src = url
13+ img .onload = () =>
14+ imageCache[url] = img
15+ action (img)
16+
17+ @ setFaviconForDoc = (doc ) ->
18+ return if currentSlug == doc .slug
19+
20+ favicon = $ (' link[rel="icon"]' )
21+
22+ if urlCache[doc .slug ]
23+ favicon .href = urlCache[doc .slug ]
24+ currentSlug = doc .slug
25+ return
26+
27+ styles = window .getComputedStyle ($ (" ._icon-#{ doc .slug } " ), ' :before' )
28+
29+ bgUrl = styles[' background-image' ].slice (5 , - 2 )
30+ bgSize = if bgUrl .includes (' @2x' ) then 32 else 16
31+ bgX = parseInt (styles[' background-position-x' ].slice (0 , - 2 ))
32+ bgY = parseInt (styles[' background-position-y' ].slice (0 , - 2 ))
33+
34+ withImage (bgUrl, (img ) ->
35+ canvas = document .createElement (' canvas' )
36+
37+ canvas .width = bgSize
38+ canvas .height = bgSize
39+ canvas .getContext (' 2d' ).drawImage (img, bgX, bgY)
40+
41+ if defaultUrl == null
42+ defaultUrl = favicon .href
43+
44+ urlCache[doc .slug ] = canvas .toDataURL ()
45+ favicon .href = urlCache[doc .slug ]
46+
47+ currentSlug = doc .slug
48+ )
49+
50+ @ resetFavicon = () ->
51+ if defaultUrl != null and currentSlug != null
52+ $ (' link[rel="icon"]' ).href = defaultUrl
53+ currentSlug = null
Original file line number Diff line number Diff line change @@ -153,6 +153,9 @@ class app.views.Content extends app.View
153153 return
154154
155155 afterRoute : (route , context ) =>
156+ if route != ' entry' and route != ' type'
157+ resetFavicon ()
158+
156159 switch route
157160 when ' root'
158161 @ show @rootPage
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ class app.views.EntryPage extends app.View
4040 if app .disabledDocs .findBy ' slug' , @entry .doc .slug
4141 @hiddenView = new app.views.HiddenPage @el , @entry
4242
43+ setFaviconForDoc (@entry .doc )
4344 @ delay @polyfillMathML
4445 @ trigger ' loaded'
4546 return
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class app.views.TypePage extends app.View
99
1010 render : (@type ) ->
1111 @ html @ tmpl (' typePage' , @type )
12+ setFaviconForDoc (@type .doc )
1213 return
1314
1415 getTitle : ->
Original file line number Diff line number Diff line change @@ -94,8 +94,6 @@ class app.views.DocList extends app.View
9494 $ .stopEvent (event)
9595 doc = app .docs .findBy ' slug' , event .target .getAttribute (' data-slug' )
9696
97- @ setFaviconForDoc (doc)
98-
9997 if doc and not @lists [doc .slug ]
10098 @lists [doc .slug ] = if doc .types .isEmpty ()
10199 new app.views.EntryList doc .entries .all ()
@@ -113,29 +111,6 @@ class app.views.DocList extends app.View
113111 delete @lists [doc .slug ]
114112 return
115113
116- setFaviconForDoc : (doc ) ->
117- link = $ (" a._list-item[data-slug='#{ doc .slug } ']" )
118- styles = window .getComputedStyle (link, ' :before' )
119-
120- bgUrl = styles[' background-image' ].slice (5 , - 2 )
121- bgSize = if bgUrl .includes (' @2x' ) then 32 else 16
122- bgPositions = styles[' background-position' ].split (' ' )
123- bgX = parseInt (bgPositions[0 ].slice (0 , - 2 ))
124- bgY = parseInt (bgPositions[1 ].slice (0 , - 2 ))
125-
126- img = new Image ()
127- img .src = bgUrl
128- img .onload = () =>
129- canvas = document .createElement (' canvas' )
130-
131- canvas .width = bgSize
132- canvas .height = bgSize
133- canvas .getContext (' 2d' ).drawImage (img, bgX, bgY)
134-
135- $ (' link[rel="icon"]' ).href = canvas .toDataURL ()
136- return
137- return
138-
139114 select : (model ) ->
140115 @listSelect .selectByHref model ? .fullPath ()
141116 return
You can’t perform that action at this time.
0 commit comments