forked from mdn/web-components-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·63 lines (53 loc) · 2.19 KB
/
main.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
// Create a class for the element
class ExpandingList extends HTMLUListElement {
constructor() {
// Always call super first in constructor
// Return value from super() is a reference to this element
self = super();
// Get ul and li elements that are a child of this custom ul element
// li elements can be containers if they have uls within them
const uls = Array.from(self.querySelectorAll('ul'));
const lis = Array.from(self.querySelectorAll('li'));
// Hide all child uls
// These lists will be shown when the user clicks a higher level container
uls.forEach(ul => {
ul.style.display = 'none';
});
// Look through each li element in the ul
lis.forEach(li => {
// If this li has a ul as a child, decorate it and add a click handler
if (li.querySelectorAll('ul').length > 0) {
// Add an attribute which can be used by the style
// to show an open or closed icon
li.setAttribute('class', 'closed');
// Wrap the li element's text in a new span element
// so we can assign style and event handlers to the span
const childText = li.childNodes[0];
const newSpan = document.createElement('span');
// Copy text from li to span, set cursor style
newSpan.textContent = childText.textContent;
newSpan.style.cursor = 'pointer';
// Add click handler to this span
newSpan.onclick = self.showul;
// Add the span and remove the bare text node from the li
childText.parentNode.insertBefore(newSpan, childText);
childText.parentNode.removeChild(childText);
}
});
}
// li click handler
showul = function (e) {
// next sibling to the span should be the ul
const nextul = e.target.nextElementSibling;
// Toggle visible state and update class attribute on ul
if (nextul.style.display == 'block') {
nextul.style.display = 'none';
nextul.parentNode.setAttribute('class', 'closed');
} else {
nextul.style.display = 'block';
nextul.parentNode.setAttribute('class', 'open');
}
};
}
// Define the new element
customElements.define('expanding-list', ExpandingList, { extends: 'ul' });