Basic cours support #44

Merged
Anri merged 67 commits from cours into main 2024-04-01 18:11:49 +02:00
Showing only changes of commit a738d492d2 - Show all commits

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);
}); });