summaryrefslogtreecommitdiffstats
path: root/static/script.js
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-18 14:08:04 +0200
committerclsr <clsr@clsr.net>2017-08-18 14:08:04 +0200
commit1c15fe67c72b4591feaceeffec0951e34a6c2e46 (patch)
treec22393533916300e73799b9fe630c392a48a1c6b /static/script.js
downloadcn-http-1c15fe67c72b4591feaceeffec0951e34a6c2e46.tar.gz
cn-http-1c15fe67c72b4591feaceeffec0951e34a6c2e46.zip
Initial commitv0.1.0
Diffstat (limited to 'static/script.js')
-rw-r--r--static/script.js59
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);
+ }
+})();