mylloon.fr/static/js/cours.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-11-01 12:59:40 +01:00
/**
* Build the filetree
* @param {HTMLElement} parent Root element of the filetree
* @param {{name: string, is_dir: boolean, children: any[]}} data FileNode
2023-11-01 13:12:33 +01:00
* @param {string} location Current location, used for links creation
2023-11-01 12:59:40 +01:00
*/
2023-11-01 13:12:33 +01:00
const buildFileTree = (parent, data, location) => {
2023-11-01 12:59:40 +01:00
const ul = document.createElement("ul");
data.forEach((item) => {
const li = document.createElement("li");
li.classList.add(item.is_dir ? "directory" : "file");
if (item.is_dir) {
2023-11-01 13:28:19 +01:00
// Directory
li.textContent = item.name;
2023-11-01 12:59:40 +01:00
li.classList.add("collapsed");
2023-11-01 15:40:25 +01:00
// Toggle collapsing on click
2023-11-01 12:59:40 +01:00
li.addEventListener("click", function (e) {
if (e.target === li) {
li.classList.toggle("collapsed");
}
});
2023-11-01 13:28:19 +01:00
} else {
// File
const url = window.location.href.split("?")[0];
const a = document.createElement("a");
a.text = item.name;
a.href = `${url}?q=${location}${item.name}`;
li.appendChild(a);
2023-11-01 12:59:40 +01:00
}
ul.appendChild(li);
if (item.children && item.children.length > 0) {
2023-11-01 13:12:33 +01:00
buildFileTree(
li,
item.children,
item.is_dir ? location + `${item.name}/` : location
);
2023-11-01 12:59:40 +01:00
}
});
parent.appendChild(ul);
};
2023-11-01 16:54:29 +01:00
/**
* 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;
}
}
}
};
2023-11-01 03:36:03 +01:00
window.addEventListener("load", () => {
2023-11-01 15:40:25 +01:00
// Build the filetree
2023-11-01 12:59:40 +01:00
const fileTreeElement = document.getElementsByTagName("aside")[0];
2023-11-01 13:02:35 +01:00
const dataElement = fileTreeElement.getElementsByTagName("span")[0];
2023-11-01 12:59:40 +01:00
buildFileTree(
fileTreeElement,
2023-11-01 13:12:33 +01:00
JSON.parse(dataElement.getAttribute("data-json")).children,
""
2023-11-01 12:59:40 +01:00
);
dataElement.remove();
2023-11-01 16:54:29 +01:00
// 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);
2023-11-01 03:36:03 +01:00
});