mylloon.fr/static/js/cours.js

45 lines
1.1 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
*/
const buildFileTree = (parent, data) => {
const ul = document.createElement("ul");
data.forEach((item) => {
const li = document.createElement("li");
li.textContent = item.name;
li.classList.add(item.is_dir ? "directory" : "file");
if (item.is_dir) {
li.classList.add("collapsed");
li.addEventListener("click", function (e) {
if (e.target === li) {
li.classList.toggle("collapsed");
}
});
}
ul.appendChild(li);
if (item.children && item.children.length > 0) {
buildFileTree(li, item.children);
}
});
parent.appendChild(ul);
};
2023-11-01 03:36:03 +01:00
window.addEventListener("load", () => {
2023-11-01 12:59:40 +01:00
const fileTreeElement = document.getElementsByTagName("aside")[0];
const dataElement = fileTreeElement.getElementsByTagName("div")[0];
buildFileTree(
fileTreeElement,
JSON.parse(dataElement.getAttribute("data-json")).children
);
console.log(JSON.parse(dataElement.getAttribute("data-json")));
dataElement.remove();
2023-11-01 03:36:03 +01:00
});