uncollapse when necessary
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-11-01 16:54:29 +01:00
parent fe9a0c750b
commit a738d492d2
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -44,6 +44,45 @@ const buildFileTree = (parent, data, location) => {
parent.appendChild(ul); parent.appendChild(ul);
}; };
/**
* Uncollapse elements from the deepest element
* @param {HTMLLIElement} element Element to uncollapse
*/
const uncollapse = (element) => {
if (element) {
element.classList.remove("collapsed");
uncollapse(element.parentElement.closest("li"));
}
};
/**
* Find the deepest opened directory
* @param {string[]} path Current path we are looking at, init with fullpath
* @param {NodeListOf<ChildNode>} options Options we have, init with list root
* @returns
*/
const deepestNodeOpened = (path, options) => {
// Iterate over possible options
for (let i = 0; i < options.length; ++i) {
// If the directory and the current path match
if (path[0] === options[i].firstChild.nodeValue) {
if (path.length === 1) {
// We found it
return options[i];
}
// Continue the search
let returned_value = deepestNodeOpened(
path.slice(1),
options[i].querySelector("ul").childNodes
);
if (returned_value) {
return returned_value;
}
}
}
};
window.addEventListener("load", () => { window.addEventListener("load", () => {
// Build the filetree // Build the filetree
const fileTreeElement = document.getElementsByTagName("aside")[0]; const fileTreeElement = document.getElementsByTagName("aside")[0];
@ -55,4 +94,14 @@ window.addEventListener("load", () => {
"" ""
); );
dataElement.remove(); dataElement.remove();
// Open nested openeded directories
const fullpath = window.location.href.split("?")[1].substring(2);
const path = fullpath.substring(0, fullpath.lastIndexOf("/"));
const last_openeded = deepestNodeOpened(
path.split("/"),
fileTreeElement.querySelector("ul").childNodes
);
uncollapse(last_openeded);
}); });