Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.js #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions expanding-list-web-component/main.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
// 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();
}

connectedCallback() {
// 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"));
const uls = this.querySelectorAll('ul');
const lis = this.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";
});
for (const ul of uls) {
ul.style.display = 'none';
}

// Look through each li element in the ul
lis.forEach((li) => {
for (const li of lis) {
// 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
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");
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");
const newSpan = document.createElement('span');

// Copy text from li to span, set cursor style
newSpan.textContent = childText.textContent;
newSpan.style.cursor = "pointer";
newSpan.style.cursor = 'pointer';

// Add click handler to this span
newSpan.addEventListener("click", (e) => {
const onClick = (e) => {
// next sibling to the span should be the ul
const nextul = e.target.nextElementSibling;
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");
if (nextUl.style.display === 'block') {
nextUl.style.display = 'none';
nextUl.parentNode.setAttribute('class', 'closed');
} else {
nextul.style.display = "block";
nextul.parentNode.setAttribute("class", "open");
nextUl.style.display = 'block';
nextUl.parentNode.setAttribute('class', 'open');
}
});
};

newSpan.addEventListener('click', onClick);

// Add the span and remove the bare text node from the li
childText.parentNode.insertBefore(newSpan, childText);
childText.parentNode.removeChild(childText);
childText.parentNode?.insertBefore(newSpan, childText);
childText.parentNode?.removeChild(childText);
}
});
}
}
}

// Define the new element
customElements.define("expanding-list", ExpandingList, { extends: "ul" });
customElements.define('expanding-list', ExpandingList, { extends: 'ul' });