mylloon.fr/static/js/cours.js

57 lines
1.5 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");
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 03:36:03 +01:00
window.addEventListener("load", () => {
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 03:36:03 +01:00
});