Unified · Split
| @@ -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 | |
| @@ -153,6 +153,9 @@ class app.views.Content extends app.View · 152 unmodified lines | ||
| 153 | 153 | return |
| 154 | 154 | |
| 155 | 155 | afterRoute: (route, context) => |
| 156 | + if route != 'entry' and route != 'type' | |
| 157 | + resetFavicon() | |
| 158 | + | |
| 156 | 159 | switch route |
| 157 | 160 | when 'root' |
| 158 | 161 | @show @rootPage |
| @@ -40,6 +40,7 @@ class app.views.EntryPage extends app.View · 39 unmodified lines | ||
| 40 | 40 | if app.disabledDocs.findBy 'slug', @entry.doc.slug |
| 41 | 41 | @hiddenView = new app.views.HiddenPage @el, @entry |
| 42 | 42 | |
| 43 | + setFaviconForDoc(@entry.doc) | |
| 43 | 44 | @delay @polyfillMathML |
| 44 | 45 | @trigger 'loaded' |
| 45 | 46 | return |
| @@ -9,6 +9,7 @@ class app.views.TypePage extends app.View · 8 unmodified lines | ||
| 9 | 9 | |
| 10 | 10 | render: (@type) -> |
| 11 | 11 | @html @tmpl('typePage', @type) |
| 12 | + setFaviconForDoc(@type.doc) | |
| 12 | 13 | return |
| 13 | 14 | |
| 14 | 15 | getTitle: -> |
| @@ -94,8 +94,6 @@ class app.views.DocList extends app.View · 93 unmodified lines | ||
| 94 | 94 | $.stopEvent(event) |
| 95 | 95 | doc = app.docs.findBy 'slug', event.target.getAttribute('data-slug') |
| 96 | 96 | |
| 97 | − @setFaviconForDoc(doc) | |
| 98 | − | |
| 99 | 97 | if doc and not @lists[doc.slug] |
| 100 | 98 | @lists[doc.slug] = if doc.types.isEmpty() |
| 101 | 99 | new app.views.EntryList doc.entries.all() |
| @@ -113,29 +111,6 @@ class app.views.DocList extends app.View · 11 unmodified lines | ||
| 113 | 111 | delete @lists[doc.slug] |
| 114 | 112 | return |
| 115 | 113 | |
| 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 | − | |
| 139 | 114 | select: (model) -> |
| 140 | 115 | @listSelect.selectByHref model?.fullPath() |
| 141 | 116 | return |