diff --git a/static/js/cours.js b/static/js/cours.js index 2d0c747..a98a7af 100644 --- a/static/js/cours.js +++ b/static/js/cours.js @@ -44,6 +44,45 @@ const buildFileTree = (parent, data, location) => { 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} 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", () => { // Build the filetree const fileTreeElement = document.getElementsByTagName("aside")[0]; @@ -55,4 +94,14 @@ window.addEventListener("load", () => { "" ); 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); });