Nashboard/static/js/mainpage.js

190 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-12-14 12:10:00 +03:00
if (!localStorage.getItem("SessionID")) {
window.location.href = "/login.html";
}
const Q = (selector) => document.querySelector(selector);
2022-12-14 02:27:39 +03:00
window.addEventListener("load", async () => {
await refreshFolder();
});
function mapFolder(folder, element) {
element.innerHTML = "";
if (folder.Folder.Name != "root") {
document.getElementById("title").textContent = folder.Folder.Name;
const uplevel = {
id: folder.Folder.ParentID,
Name: "Go back",
Persistent: true,
};
const clone = createFolderElement(uplevel);
element.appendChild(clone);
} else {
document.getElementById("title").textContent = "Dashboard Prototype";
2022-12-14 02:27:39 +03:00
}
for (let f of folder.ChildFolders) {
2022-12-14 12:10:00 +03:00
const clone = createFolderElement(f);
element.appendChild(clone);
2022-12-14 12:10:00 +03:00
}
for (let b of folder.ChildBookmarks) {
2022-12-14 12:10:00 +03:00
const clone = createBookmarkElement(b);
element.appendChild(clone);
2022-12-14 02:27:39 +03:00
}
element.appendChild(newFolderItem(folder));
element.appendChild(newBookmarkItem(folder));
2022-12-14 12:10:00 +03:00
feather.replace();
}
2022-12-14 12:10:00 +03:00
function createFolderElement(folder) {
const template = Q("#folder");
2022-12-14 12:10:00 +03:00
const clone = template.content.firstElementChild.cloneNode(true);
clone.querySelector("#folder_name").textContent = folder.Name;
clone.addEventListener("click", async () => {
console.log(folder);
const newFolder = await getFolder(folder.id);
mapFolder(newFolder.folder, document.getElementById("bookmarks"));
2022-12-14 12:10:00 +03:00
});
for (let el of clone.querySelectorAll("[data-feather-t]")) {
el.setAttribute("data-feather", el.getAttribute("data-feather-t"));
}
if (folder.Persistent) {
clone.querySelector(".xmark").remove();
} else {
clone.querySelector(".xmark").addEventListener("click", async (e) => {
e.stopPropagation();
await delFolder(folder);
});
}
2022-12-14 12:10:00 +03:00
return clone;
}
function createBookmarkElement(bookmark) {
const template = Q("#bookmark");
const tagtempl = Q("#booktag");
2022-12-14 12:10:00 +03:00
const clone = template.content.firstElementChild.cloneNode(true);
clone.querySelector("#bookmark_name").textContent = bookmark.Name;
const link = clone.querySelector("#bookmark_link");
link.setAttribute("href", bookmark.Url);
link.textContent = bookmark.Url;
clone.addEventListener("click", async () => {
window.location.href = bookmark.Url;
});
2022-12-14 12:10:00 +03:00
for (let tag of bookmark.Tags) {
const t = tagtempl.content.cloneNode(true);
const el = t.querySelector("span");
el.addEventListener("onclick", () => alert(tag));
el.textContent = "#" + tag;
clone.querySelector("#bookmark_tags").appendChild(t);
}
for (let el of clone.querySelectorAll("[data-feather-t]")) {
el.setAttribute("data-feather", el.getAttribute("data-feather-t"));
}
clone.querySelector(".xmark").addEventListener("click", async (e) => {
2022-12-14 12:10:00 +03:00
e.stopPropagation();
await delBookmark(bookmark);
});
return clone;
}
function newFolderItem(folder) {
const template = document.getElementById("newFolder");
const clone = template.content.firstElementChild.cloneNode(true);
clone.addEventListener("click", () => {
const folderForm = document.getElementById("folder_form");
const el = folderForm.content.firstElementChild.cloneNode(true);
el.querySelector("form").setAttribute("folderid", folder.Folder.id);
const m = Q("#modal");
m.appendChild(el);
m.classList.add("show");
// console.log("New folder called for " + folder.Folder.Name);
2022-12-14 12:10:00 +03:00
});
for (let el of clone.querySelectorAll("[data-feather-t]")) {
el.setAttribute("data-feather", el.getAttribute("data-feather-t"));
}
2022-12-14 12:10:00 +03:00
return clone;
}
function newBookmarkItem(folder) {
const template = document.getElementById("newBookmark");
const clone = template.content.firstElementChild.cloneNode(true);
clone.addEventListener("click", () => {
const bookmarkForm = document.getElementById("bookmark_form");
const el = bookmarkForm.content.firstElementChild.cloneNode(true);
el.querySelector("form").setAttribute("folderid", folder.Folder.id);
const m = Q("#modal");
m.appendChild(el);
m.classList.add("show");
});
for (let el of clone.querySelectorAll("[data-feather-t]")) {
el.setAttribute("data-feather", el.getAttribute("data-feather-t"));
}
return clone;
}
function closeModal(event) {
const m = Q("#modal");
if (event) if (event.target != m) return;
m.classList.remove("show");
m.innerHTML = "";
}
async function newFolder(event) {
event.preventDefault();
const form = event.target;
const parentID = form.getAttribute("folderid");
const name = form.querySelector("#folder_name").value;
console.log(name);
const result = await createFolder(parentID, name);
if (result.success) {
await refreshFolder(parentID);
closeModal();
}
}
async function newBookmark(event) {
event.preventDefault();
const form = event.target;
const parentID = form.getAttribute("folderid");
const name = form.querySelector("#bookmark_name").value;
const url = form.querySelector("#bookmark_url").value;
const tags = form.querySelector("#bookmark_tags").value.split(",");
console.log(name);
const result = await createBookmark(parentID, {
Name: name,
Url: url,
FolderID: parentID,
Tags: tags,
});
if (result.success) {
await refreshFolder(parentID);
closeModal();
}
}
async function refreshFolder(id) {
const folder = await getFolder(id);
const bm = document.getElementById("bookmarks");
if (!folder.success) {
bm.textContent = "Нет закладок";
return;
}
mapFolder(folder.folder, bm);
}
async function delFolder(folder) {
if (!confirm(`Delete folder "${folder.Name}"?`)) return;
const result = await deleteFolder(folder.id);
if (result.success) {
await refreshFolder(folder.ParentID);
}
}
async function delBookmark(bookmark) {
if (!confirm(`Delete bookmark "${bookmark.Name}"?`)) return;
const result = await deleteBookmark(bookmark.id);
if (result.success) {
await refreshFolder(bookmark.FolderID);
}
}