History

Set favicon to the icon of the currently shown docs

Jasper van Merle committed 7 years ago

Commit c36478c1 parent d640a8e5 files · +58 −25Browse files

Showing the changes to assets/javascripts (5 of 5 files). Show all

Unified · Split

5 files
  1. assets/javascripts/lib/favicon.coffee +53 −0
  2. assets/javascripts/views/content/content.coffee +3 −0
  3. assets/javascripts/views/content/entry_page.coffee +1 −0
  4. assets/javascripts/views/content/type_page.coffee +1 −0
  5. assets/javascripts/views/sidebar/doc_list.coffee +0 −25
assets/javascripts/lib/favicon.coffee+53−0
@@ -0,0 +1,53 @@
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
assets/javascripts/views/content/content.coffee+3−0
@@ -153,6 +153,9 @@ class app.views.Content extends app.View · 152 unmodified lines
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
assets/javascripts/views/content/entry_page.coffee+1−0
@@ -40,6 +40,7 @@ class app.views.EntryPage extends app.View · 39 unmodified lines
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
assets/javascripts/views/content/type_page.coffee+1−0
@@ -9,6 +9,7 @@ class app.views.TypePage extends app.View · 8 unmodified lines
99
1010 render: (@type) ->
1111 @html @tmpl('typePage', @type)
12+ setFaviconForDoc(@type.doc)
1213 return
1314
1415 getTitle: ->
assets/javascripts/views/sidebar/doc_list.coffee+0−25
@@ -94,8 +94,6 @@ class app.views.DocList extends app.View · 93 unmodified lines
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 · 11 unmodified lines
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