mirror of https://github.com/freeCodeCamp/devdocs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
6 years ago
|
defaultUrl = null
|
||
|
currentSlug = null
|
||
|
|
||
|
imageCache = {}
|
||
|
urlCache = {}
|
||
|
|
||
|
withImage = (url, action) ->
|
||
|
if imageCache[url]
|
||
|
action(imageCache[url])
|
||
|
else
|
||
|
img = new Image()
|
||
6 years ago
|
img.crossOrigin = 'anonymous'
|
||
6 years ago
|
img.src = url
|
||
|
img.onload = () =>
|
||
|
imageCache[url] = img
|
||
|
action(img)
|
||
|
|
||
|
@setFaviconForDoc = (doc) ->
|
||
|
return if currentSlug == doc.slug
|
||
|
|
||
|
favicon = $('link[rel="icon"]')
|
||
|
|
||
6 years ago
|
if defaultUrl == null
|
||
|
defaultUrl = favicon.href
|
||
|
|
||
6 years ago
|
if urlCache[doc.slug]
|
||
|
favicon.href = urlCache[doc.slug]
|
||
|
currentSlug = doc.slug
|
||
|
return
|
||
|
|
||
5 years ago
|
iconEl = $("._icon-#{doc.slug.split('~')[0]}")
|
||
|
return if iconEl == null
|
||
|
|
||
|
styles = window.getComputedStyle(iconEl, ':before')
|
||
|
|
||
|
backgroundPositionX = styles['background-position-x']
|
||
|
backgroundPositionY = styles['background-position-y']
|
||
|
return if backgroundPositionX == undefined || backgroundPositionY == undefined
|
||
6 years ago
|
|
||
6 years ago
|
bgUrl = app.config.favicon_spritesheet
|
||
|
sourceSize = 16
|
||
5 years ago
|
sourceX = Math.abs(parseInt(backgroundPositionX.slice(0, -2)))
|
||
|
sourceY = Math.abs(parseInt(backgroundPositionY.slice(0, -2)))
|
||
6 years ago
|
|
||
6 years ago
|
withImage(bgUrl, (docImg) ->
|
||
|
withImage(defaultUrl, (defaultImg) ->
|
||
|
size = defaultImg.width
|
||
6 years ago
|
|
||
6 years ago
|
canvas = document.createElement('canvas')
|
||
|
ctx = canvas.getContext('2d')
|
||
6 years ago
|
|
||
6 years ago
|
canvas.width = size
|
||
|
canvas.height = size
|
||
|
ctx.drawImage(defaultImg, 0, 0)
|
||
6 years ago
|
|
||
6 years ago
|
docIconPercentage = 65
|
||
|
destinationCoords = size / 100 * (100 - docIconPercentage)
|
||
|
destinationSize = size / 100 * docIconPercentage
|
||
5 years ago
|
|
||
6 years ago
|
ctx.drawImage(docImg, sourceX, sourceY, sourceSize, sourceSize, destinationCoords, destinationCoords, destinationSize, destinationSize)
|
||
6 years ago
|
|
||
5 years ago
|
try
|
||
|
urlCache[doc.slug] = canvas.toDataURL()
|
||
|
favicon.href = urlCache[doc.slug]
|
||
|
|
||
|
currentSlug = doc.slug
|
||
|
catch error
|
||
|
Raven.captureException error, { level: 'info' }
|
||
|
@resetFavicon()
|
||
6 years ago
|
)
|
||
6 years ago
|
)
|
||
|
|
||
|
@resetFavicon = () ->
|
||
|
if defaultUrl != null and currentSlug != null
|
||
|
$('link[rel="icon"]').href = defaultUrl
|
||
|
currentSlug = null
|