|
|
@ -1,13 +1,12 @@ |
|
|
|
/* eslint-env browser */ |
|
|
|
(function () { |
|
|
|
|
|
|
|
// Space optimisations
|
|
|
|
var doc = document; |
|
|
|
var find = doc.querySelector.bind(doc); |
|
|
|
var create = doc.createElement.bind(doc); |
|
|
|
|
|
|
|
// Run callback when DOM is ready
|
|
|
|
function DOMReady(cb) { |
|
|
|
|
|
|
|
function domReady(cb) { |
|
|
|
// Run now if DOM has already loaded as we're loading async
|
|
|
|
if (['interactive', 'complete'].indexOf(doc.readyState) >= 0) { |
|
|
|
cb(); |
|
|
@ -22,7 +21,7 @@ |
|
|
|
var supports = { |
|
|
|
test: function (features) { |
|
|
|
var self = this; |
|
|
|
if(!features || !features.length) { |
|
|
|
if (!features || features.length < 1) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return features.every(function (feature) { |
|
|
@ -35,7 +34,7 @@ |
|
|
|
localStorage.setItem('test', 'test'); |
|
|
|
localStorage.removeItem('test'); |
|
|
|
return true; |
|
|
|
} catch (e) { |
|
|
|
} catch (err) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
})(), |
|
|
@ -43,10 +42,10 @@ |
|
|
|
var div = create('div'); |
|
|
|
div.innerHTML = '<svg/>'; |
|
|
|
return ( |
|
|
|
typeof SVGRect != 'undefined' |
|
|
|
&& div.firstChild |
|
|
|
&& div.firstChild.namespaceURI |
|
|
|
) == 'http://www.w3.org/2000/svg'; |
|
|
|
typeof SVGRect !== 'undefined' && |
|
|
|
div.firstChild && |
|
|
|
div.firstChild.namespaceURI |
|
|
|
) === 'http://www.w3.org/2000/svg'; |
|
|
|
})(), |
|
|
|
querySelector: typeof doc.querySelector === 'function', |
|
|
|
classList: (function () { |
|
|
@ -123,7 +122,7 @@ |
|
|
|
var menuHTML = ''; |
|
|
|
var heartedNodes = favouriteNodes.getHeartedNodes(); |
|
|
|
var nodeHashes = Object.keys(heartedNodes); |
|
|
|
if(nodeHashes.length) { |
|
|
|
if (nodeHashes.length > 0) { |
|
|
|
menuHTML += '<ul>'; |
|
|
|
nodeHashes.forEach(function (node) { |
|
|
|
menuHTML += '<li><a href="/node/' + node + '">' + heartedNodes[node] + '</a></li>'; |
|
|
@ -132,12 +131,12 @@ |
|
|
|
} else { |
|
|
|
menuHTML += '<div class="empty">Click the heart next to a node\'s title on it\'s own page to save it here for easy access :)</div>'; |
|
|
|
} |
|
|
|
return menu.innerHTML = menuHTML; |
|
|
|
menu.innerHTML = menuHTML; |
|
|
|
return menu.innerHTML; |
|
|
|
}, |
|
|
|
|
|
|
|
// Load SVG, run callback when loaded
|
|
|
|
loadSVG: function (cb) { |
|
|
|
|
|
|
|
// Get heart SVG
|
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
xhr.open('GET', favouriteNodes.heartPath); |
|
|
@ -149,10 +148,8 @@ |
|
|
|
|
|
|
|
// Initiate node favouriting
|
|
|
|
init: function () { |
|
|
|
|
|
|
|
// Start loading heart SVG before DOM
|
|
|
|
favouriteNodes.loadSVG(function (svg) { |
|
|
|
|
|
|
|
// Create heart SVG elem
|
|
|
|
var div = create('div'); |
|
|
|
div.innerHTML = svg; |
|
|
@ -166,7 +163,6 @@ |
|
|
|
|
|
|
|
// Add click handler
|
|
|
|
heartEl.addEventListener('click', function () { |
|
|
|
|
|
|
|
// Heart/unheart node
|
|
|
|
var node = favouriteNodes.getCurrentNode(); |
|
|
|
if (favouriteNodes.isHearted(node)) { |
|
|
@ -179,7 +175,7 @@ |
|
|
|
}); |
|
|
|
|
|
|
|
// Then inject into DOM when it's ready
|
|
|
|
DOMReady(function() { |
|
|
|
domReady(function () { |
|
|
|
var headerHeight = find('.title').offsetHeight; |
|
|
|
var headerBoxShadow = 3; |
|
|
|
|
|
|
@ -213,7 +209,6 @@ |
|
|
|
// If current node is hearted
|
|
|
|
var node = favouriteNodes.getCurrentNode(); |
|
|
|
if (favouriteNodes.isHearted(node)) { |
|
|
|
|
|
|
|
// Heart it again so we get the new name if it's updated
|
|
|
|
favouriteNodes.heart(node); |
|
|
|
} |
|
|
@ -222,12 +217,11 @@ |
|
|
|
|
|
|
|
// Service worker
|
|
|
|
if (supports.test(['serviceWorker', 'querySelector', 'classList'])) { |
|
|
|
|
|
|
|
// Register service worker
|
|
|
|
navigator.serviceWorker.register('/sw.js'); |
|
|
|
|
|
|
|
// Show cache message on stale pages
|
|
|
|
DOMReady(function() { |
|
|
|
domReady(function () { |
|
|
|
if (window.cacheDate) { |
|
|
|
var offlineMessage = create('div'); |
|
|
|
offlineMessage.classList.add('cache-message'); |
|
|
@ -248,14 +242,13 @@ |
|
|
|
|
|
|
|
// Add ios class to body on iOS devices
|
|
|
|
if (supports.test(['classList'])) { |
|
|
|
DOMReady(function() { |
|
|
|
domReady(function () { |
|
|
|
if ( |
|
|
|
/iPad|iPhone|iPod/.test(navigator.userAgent) |
|
|
|
&& !window.MSStream |
|
|
|
/iPad|iPhone|iPod/.test(navigator.userAgent) && |
|
|
|
!window.MSStream |
|
|
|
) { |
|
|
|
doc.body.classList.add('ios'); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
})(); |
|
|
|