From d640a8ee221c26ad4612595157e6cbe9f0fd5709 Mon Sep 17 00:00:00 2001 From: Jasper van Merle Date: Mon, 28 Jan 2019 17:51:28 +0100 Subject: [PATCH 1/3] Set favicon to documentation icon on open --- .../javascripts/views/sidebar/doc_list.coffee | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/assets/javascripts/views/sidebar/doc_list.coffee b/assets/javascripts/views/sidebar/doc_list.coffee index c72877a8..d24c2476 100644 --- a/assets/javascripts/views/sidebar/doc_list.coffee +++ b/assets/javascripts/views/sidebar/doc_list.coffee @@ -94,6 +94,8 @@ class app.views.DocList extends app.View $.stopEvent(event) doc = app.docs.findBy 'slug', event.target.getAttribute('data-slug') + @setFaviconForDoc(doc) + if doc and not @lists[doc.slug] @lists[doc.slug] = if doc.types.isEmpty() new app.views.EntryList doc.entries.all() @@ -111,6 +113,29 @@ class app.views.DocList extends app.View delete @lists[doc.slug] return + setFaviconForDoc: (doc) -> + link = $("a._list-item[data-slug='#{doc.slug}']") + styles = window.getComputedStyle(link, ':before') + + bgUrl = styles['background-image'].slice(5, -2) + bgSize = if bgUrl.includes('@2x') then 32 else 16 + bgPositions = styles['background-position'].split(' ') + bgX = parseInt(bgPositions[0].slice(0, -2)) + bgY = parseInt(bgPositions[1].slice(0, -2)) + + img = new Image() + img.src = bgUrl + img.onload = () => + canvas = document.createElement('canvas') + + canvas.width = bgSize + canvas.height = bgSize + canvas.getContext('2d').drawImage(img, bgX, bgY) + + $('link[rel="icon"]').href = canvas.toDataURL() + return + return + select: (model) -> @listSelect.selectByHref model?.fullPath() return From c36478ce8265e480e4a0e759d1836ffdb07ced76 Mon Sep 17 00:00:00 2001 From: Jasper van Merle Date: Fri, 1 Feb 2019 09:30:19 +0100 Subject: [PATCH 2/3] Set favicon to the icon of the currently shown docs --- assets/javascripts/lib/favicon.coffee | 53 +++++++++++++++++++ .../javascripts/views/content/content.coffee | 3 ++ .../views/content/entry_page.coffee | 1 + .../views/content/type_page.coffee | 1 + .../javascripts/views/sidebar/doc_list.coffee | 25 --------- 5 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 assets/javascripts/lib/favicon.coffee diff --git a/assets/javascripts/lib/favicon.coffee b/assets/javascripts/lib/favicon.coffee new file mode 100644 index 00000000..aa319e27 --- /dev/null +++ b/assets/javascripts/lib/favicon.coffee @@ -0,0 +1,53 @@ +defaultUrl = null +currentSlug = null + +imageCache = {} +urlCache = {} + +withImage = (url, action) -> + if imageCache[url] + action(imageCache[url]) + else + img = new Image() + img.src = url + img.onload = () => + imageCache[url] = img + action(img) + +@setFaviconForDoc = (doc) -> + return if currentSlug == doc.slug + + favicon = $('link[rel="icon"]') + + if urlCache[doc.slug] + favicon.href = urlCache[doc.slug] + currentSlug = doc.slug + return + + styles = window.getComputedStyle($("._icon-#{doc.slug}"), ':before') + + bgUrl = styles['background-image'].slice(5, -2) + bgSize = if bgUrl.includes('@2x') then 32 else 16 + bgX = parseInt(styles['background-position-x'].slice(0, -2)) + bgY = parseInt(styles['background-position-y'].slice(0, -2)) + + withImage(bgUrl, (img) -> + canvas = document.createElement('canvas') + + canvas.width = bgSize + canvas.height = bgSize + canvas.getContext('2d').drawImage(img, bgX, bgY) + + if defaultUrl == null + defaultUrl = favicon.href + + urlCache[doc.slug] = canvas.toDataURL() + favicon.href = urlCache[doc.slug] + + currentSlug = doc.slug + ) + +@resetFavicon = () -> + if defaultUrl != null and currentSlug != null + $('link[rel="icon"]').href = defaultUrl + currentSlug = null diff --git a/assets/javascripts/views/content/content.coffee b/assets/javascripts/views/content/content.coffee index 8c5ba874..4e01733e 100644 --- a/assets/javascripts/views/content/content.coffee +++ b/assets/javascripts/views/content/content.coffee @@ -153,6 +153,9 @@ class app.views.Content extends app.View return afterRoute: (route, context) => + if route != 'entry' and route != 'type' + resetFavicon() + switch route when 'root' @show @rootPage diff --git a/assets/javascripts/views/content/entry_page.coffee b/assets/javascripts/views/content/entry_page.coffee index beae4d77..00762fa6 100644 --- a/assets/javascripts/views/content/entry_page.coffee +++ b/assets/javascripts/views/content/entry_page.coffee @@ -40,6 +40,7 @@ class app.views.EntryPage extends app.View if app.disabledDocs.findBy 'slug', @entry.doc.slug @hiddenView = new app.views.HiddenPage @el, @entry + setFaviconForDoc(@entry.doc) @delay @polyfillMathML @trigger 'loaded' return diff --git a/assets/javascripts/views/content/type_page.coffee b/assets/javascripts/views/content/type_page.coffee index 147fa7ed..ef360c14 100644 --- a/assets/javascripts/views/content/type_page.coffee +++ b/assets/javascripts/views/content/type_page.coffee @@ -9,6 +9,7 @@ class app.views.TypePage extends app.View render: (@type) -> @html @tmpl('typePage', @type) + setFaviconForDoc(@type.doc) return getTitle: -> diff --git a/assets/javascripts/views/sidebar/doc_list.coffee b/assets/javascripts/views/sidebar/doc_list.coffee index d24c2476..c72877a8 100644 --- a/assets/javascripts/views/sidebar/doc_list.coffee +++ b/assets/javascripts/views/sidebar/doc_list.coffee @@ -94,8 +94,6 @@ class app.views.DocList extends app.View $.stopEvent(event) doc = app.docs.findBy 'slug', event.target.getAttribute('data-slug') - @setFaviconForDoc(doc) - if doc and not @lists[doc.slug] @lists[doc.slug] = if doc.types.isEmpty() new app.views.EntryList doc.entries.all() @@ -113,29 +111,6 @@ class app.views.DocList extends app.View delete @lists[doc.slug] return - setFaviconForDoc: (doc) -> - link = $("a._list-item[data-slug='#{doc.slug}']") - styles = window.getComputedStyle(link, ':before') - - bgUrl = styles['background-image'].slice(5, -2) - bgSize = if bgUrl.includes('@2x') then 32 else 16 - bgPositions = styles['background-position'].split(' ') - bgX = parseInt(bgPositions[0].slice(0, -2)) - bgY = parseInt(bgPositions[1].slice(0, -2)) - - img = new Image() - img.src = bgUrl - img.onload = () => - canvas = document.createElement('canvas') - - canvas.width = bgSize - canvas.height = bgSize - canvas.getContext('2d').drawImage(img, bgX, bgY) - - $('link[rel="icon"]').href = canvas.toDataURL() - return - return - select: (model) -> @listSelect.selectByHref model?.fullPath() return From 14e49f7014523a807d01df4b04ae470c3da6c2e9 Mon Sep 17 00:00:00 2001 From: Jasper van Merle Date: Sat, 16 Feb 2019 23:41:06 +0100 Subject: [PATCH 3/3] Use GitHub's parent-child style --- assets/javascripts/lib/favicon.coffee | 36 +++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/assets/javascripts/lib/favicon.coffee b/assets/javascripts/lib/favicon.coffee index aa319e27..f42b1281 100644 --- a/assets/javascripts/lib/favicon.coffee +++ b/assets/javascripts/lib/favicon.coffee @@ -19,6 +19,9 @@ withImage = (url, action) -> favicon = $('link[rel="icon"]') + if defaultUrl == null + defaultUrl = favicon.href + if urlCache[doc.slug] favicon.href = urlCache[doc.slug] currentSlug = doc.slug @@ -27,24 +30,31 @@ withImage = (url, action) -> styles = window.getComputedStyle($("._icon-#{doc.slug}"), ':before') bgUrl = styles['background-image'].slice(5, -2) - bgSize = if bgUrl.includes('@2x') then 32 else 16 - bgX = parseInt(styles['background-position-x'].slice(0, -2)) - bgY = parseInt(styles['background-position-y'].slice(0, -2)) + sourceSize = if bgUrl.includes('@2x') then 32 else 16 + sourceX = Math.abs(parseInt(styles['background-position-x'].slice(0, -2))) + sourceY = Math.abs(parseInt(styles['background-position-y'].slice(0, -2))) - withImage(bgUrl, (img) -> - canvas = document.createElement('canvas') + withImage(bgUrl, (docImg) -> + withImage(defaultUrl, (defaultImg) -> + size = defaultImg.width - canvas.width = bgSize - canvas.height = bgSize - canvas.getContext('2d').drawImage(img, bgX, bgY) + canvas = document.createElement('canvas') + ctx = canvas.getContext('2d') - if defaultUrl == null - defaultUrl = favicon.href + canvas.width = size + canvas.height = size + ctx.drawImage(defaultImg, 0, 0) - urlCache[doc.slug] = canvas.toDataURL() - favicon.href = urlCache[doc.slug] + docIconPercentage = 65 + destinationCoords = size / 100 * (100 - docIconPercentage) + destinationSize = size / 100 * docIconPercentage + ctx.drawImage(docImg, sourceX, sourceY, sourceSize, sourceSize, destinationCoords, destinationCoords, destinationSize, destinationSize) - currentSlug = doc.slug + urlCache[doc.slug] = canvas.toDataURL() + favicon.href = urlCache[doc.slug] + + currentSlug = doc.slug + ) ) @resetFavicon = () ->