-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathoptions.js
168 lines (144 loc) · 4.96 KB
/
options.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Loop through all the option buttons and add click to collapse/expand.
*/
function makeOptionsCollapsible() {
document.querySelectorAll('.option-button').forEach((button) => {
button.addEventListener('click', () => {
const isOpened = button.getAttribute('aria-expanded') === "true";
if (isOpened ? closeOption(button) : openOption(button));
});
});
document.querySelectorAll('button.toggle-children').forEach((button) => {
button.addEventListener('click', () => {
const isOpened = button.getAttribute('aria-expanded') === "true";
button.parentElement.querySelectorAll('li>div>button.option-button').forEach((childButton) => {
if (isOpened ? closeOption(childButton) : openOption(childButton));
});
button.setAttribute('aria-expanded', !isOpened);
});
});
}
/**
* Open an option.
*/
function openOption(button) {
button.setAttribute('aria-expanded', "true");
var content = getNextSibling(button, '.collapsible-content');
$(content).slideDown();
}
/**
* Close an option and all children, and reset the expand all button.
*/
function closeOption(button) {
button.setAttribute('aria-expanded', "false");
var content = getNextSibling(button, '.collapsible-content');
$(content).slideUp();
/* reset toggle children button */
var toggleChildrenButton = content.querySelector('button.toggle-children');
if (toggleChildrenButton) {
toggleChildrenButton.setAttribute('aria-expanded', 'false');
}
/* close children too */
content.querySelectorAll('.option-button').forEach((childButton) => {
if (childButton.getAttribute('aria-expanded') === "true") {
closeOption(childButton);
}
});
}
/**
* Get next sibling of a given selector.
* credit: https://gomakethings.com/finding-the-next-and-previous-sibling-elements-that-match-a-selector-with-vanilla-js/
*/
function getNextSibling(elem, selector) {
// Get the next sibling element
var sibling = elem.nextElementSibling;
// If there's no selector, return the first sibling
if (!selector) return sibling;
// If the sibling matches our selector, use it
// If not, jump to the next sibling and continue the loop
while (sibling) {
if (sibling.matches(selector)) return sibling;
sibling = sibling.nextElementSibling
}
};
/**
* Get previous sibling of a given selector.
* credit: https://gomakethings.com/finding-the-next-and-previous-sibling-elements-that-match-a-selector-with-vanilla-js/
*/
function getPrevSibling(elem, selector) {
// Get the next sibling element
var sibling = elem.previousElementSibling;
// If there's no selector, return the first sibling
if (!selector) return sibling;
// If the sibling matches our selector, use it
// If not, jump to the next sibling and continue the loop
while (sibling) {
if (sibling.matches(selector)) return sibling;
sibling = sibling.previousElementSibling;
}
};
/**
* If an anchor is in the URL, open the options ancestry and scroll to it.
*/
function goToAnchor() {
var pathOnly = window.location.origin + window.location.pathname;
var pathWithAnchor = window.location.href;
var anchor = pathWithAnchor.slice(pathWithAnchor.indexOf(pathOnly) + pathOnly.length);
if (anchor !== "") {
var content = document.querySelector(anchor);
openOptionRecursively(content);
setTimeout(
() => {
content.scrollIntoView({behavior: "smooth", block: "center"});
},
500
);
}
}
/**
* Recursively open options start with the furthest ancestor.
*/
function openOptionRecursively(content) {
var button = getPrevSibling(content, '.option-button');
var parentContent = button.closest('.collapsible-content');
if (parentContent !== null) {
openOptionRecursively(parentContent);
}
button.click();
}
/**
* Add copy to clipboard capability to Options anchors.
*/
function addClipboardToAnchors() {
var clipboard = new ClipboardJS('.option-link', {
text: function(trigger) {
var dialog_elem = document.querySelector('.md-dialog');
dialog_elem.classList.toggle('md-dialog--active');
var dialog_child = dialog_elem.firstElementChild;
dialog_child.innerText = 'Copied to clipboard';
setTimeout(() => {
dialog_elem.classList.toggle('md-dialog--active');
}, "2000");
return window.location.origin + window.location.pathname + trigger.getAttribute('href');
}
});
}
function initializePage() {
document
.querySelectorAll('[aria-expanded="true"].option-button')
.forEach((elem) => {
elem.setAttribute('aria-expanded', 'false');
getNextSibling(elem, '.collapsible-content').style.display = "none";
});
document
.querySelectorAll('.option-button')
.forEach(elem => elem.style.setProperty('--icon-visibility', 'visible'));
document
.querySelectorAll('[aria-expanded="true"].toggle-children')
.forEach((elem) => {
elem.setAttribute('aria-expanded', 'false');
elem.style.display = "block"
});
addClipboardToAnchors();
goToAnchor();
}