diff options
Diffstat (limited to 'static/script.js')
-rw-r--r-- | static/script.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..36e2b18 --- /dev/null +++ b/static/script.js @@ -0,0 +1,59 @@ +(function() { + 'use strict'; + + var init = function() { + var toggle = document.createElement('button'); + + var clickToggle = function() { + toggle.open = !toggle.open; + var open = toggle.open ? 'open' : ''; + var secs = document.querySelectorAll('section>details'); + for (var i=0; i<secs.length; i++) { + secs[i].open = open; + } + toggle.textContent = (toggle.open ? 'Collapse' : 'Expand') + ' all sections'; + }; + + toggle.addEventListener('click', clickToggle); + + var hashchange = function() { + var selected = []; + if (location.hash != '' && location.hash != '#') { + var h = location.hash.slice(1); + var el = document.getElementById(h); + while (el) { + if (el.tagName == 'DETAILS') { + selected.push(el); + } + el = el.parentNode; + } + } + + if (selected.length > 0) { + toggle.open = true; + clickToggle(); + for (var i=0; i<selected.length; i++) { + selected[i].open = 'open'; + } + selected[0].scrollIntoView(); + } else { + toggle.open = false; + clickToggle(); + } + }; + window.addEventListener('hashchange', hashchange); + + hashchange(); + if (document.querySelectorAll('main section').length > 0) { + var main = document.querySelector('main'); + main.insertBefore(toggle, main.firstChild); + } + + }; + + if (document.readyState !== 'loading') { + init(); + } else { + document.addEventListener('DOMContentLoaded', init); + } +})(); |