forked from w3c/smartcities-workshop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
59 lines (55 loc) · 2.19 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(function () {
var createEl = function (tagName, attrs) {
var anchor = document.createElement(tagName);
Object.keys(attrs).forEach(function (key) {
anchor[key] = attrs[key];
});
return anchor;
};
var createAnchorFromHeading = function (headingEl) {
let href = '#' + (headingEl.id || headingEl.parentNode.id);
return createEl(
"a",
{
className: "ref",
href: href,
textContent: "#",
title: headingEl.textContent
}
);
};
window.addEventListener("load", function () {
Array.prototype.forEach.call(document.querySelectorAll("#main h1[id], #main h2[id], #main h3[id], #main h4[id], #main h5[id], #main section[id]>h2, #main section[id]>h3, #main section[id]>h4"), function (el) {
var a = createAnchorFromHeading(el);
el.classList.add("has-ref");
el.addEventListener("click", function () {
a.click();
});
el.insertBefore(a, el.firstChild);
});
if (document.querySelector("[data-controls]")) {
document.querySelector("[data-controls]").setAttribute('style', '');
document.querySelector("[data-expand]").addEventListener("click", function () {
Array.prototype.forEach.call(document.querySelectorAll("details"), function (el) {
el.open = true;
});
});
document.querySelector("[data-collapse]").addEventListener("click", function () {
Array.prototype.forEach.call(document.querySelectorAll("details"), function (el) {
el.open = false;
});
});
}
if (window.location.hash) {
if (window.location.hash === "#openall") {
document.querySelector("[data-expand]").click();
}
else {
var el = document.querySelector(window.location.hash);
if (el && el.parentNode && (el.parentNode.nodeName === 'DETAILS')) {
el.parentNode.open = true;
}
}
}
});
})();