"use strict"; const noteTree = (function() { const noteDetailEl = $('#note-detail'); const treeEl = $("#tree"); const parentListEl = $("#parent-list"); let startNoteTreeId = null; let treeLoadTime = null; let clipboardNoteTreeId = null; let notesMap = {}; let parentToChildren = {}; let childToParents = {}; let counter = 1; let noteTreeIdToKey = {}; let parentChildToNoteTreeId = {}; let noteIdToTitle = {}; function getNoteTreeIdFromKey(key) { const node = treeUtils.getNodeByKey(key); return node.note_tree_id; } function getKeyFromNoteTreeId(noteTreeId) { return noteTreeIdToKey[noteTreeId]; } function getTreeLoadTime() { return treeLoadTime; } function getClipboardNoteTreeId() { return clipboardNoteTreeId; } function setClipboardNoteTreeId(cbNoteId) { clipboardNoteTreeId = cbNoteId; } function getNoteTreeId(parentNoteId, childNoteId) { const key = parentNoteId + "-" + childNoteId; const noteTreeId = parentChildToNoteTreeId[key]; if (!noteTreeId) { console.trace(); throw new Error("Can't find note tree id for parent=" + parentNoteId + ", child=" + childNoteId); } return noteTreeId; } function getNoteTitle(notePath) { const noteId = treeUtils.getNoteIdFromNotePath(notePath); const title = noteIdToTitle[noteId]; if (!title) { throw new Error("Can't find title for noteId='" + noteId + "'"); } return title; } function prepareNoteTree(notes) { parentToChildren = {}; childToParents = {}; notesMap = {}; for (const note of notes) { notesMap[note.note_tree_id] = note; noteIdToTitle[note.note_id] = note.note_title; delete note.note_title; // this should not be used. Use noteIdToTitle instead const key = note.note_pid + "-" + note.note_id; parentChildToNoteTreeId[key] = note.note_tree_id; if (!parentToChildren[note.note_pid]) { parentToChildren[note.note_pid] = []; } parentToChildren[note.note_pid].push(note.note_id); if (!childToParents[note.note_id]) { childToParents[note.note_id] = []; } childToParents[note.note_id].push(note.note_pid); } return prepareNoteTreeInner('root'); } function prepareNoteTreeInner(parentNoteId) { const childNoteIds = parentToChildren[parentNoteId]; if (!childNoteIds) { console.log("No children for " + noteId + ". This shouldn't happen."); return; } const noteList = []; for (const childNoteId of childNoteIds) { const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); const note = notesMap[noteTreeId]; note.title = noteIdToTitle[note.note_id]; note.extraClasses = ""; if (note.is_protected) { note.extraClasses += ",protected"; } if (childToParents[childNoteId].length > 1) { note.extraClasses += ",multiple-parents"; } if (note.extraClasses.startsWith(",")) { note.extraClasses = note.extraClasses.substr(1); } note.key = counter++ + ""; // key needs to be string note.refKey = note.note_id; note.expanded = note.is_expanded; noteTreeIdToKey[noteTreeId] = note.key; if (parentToChildren[note.note_id] && parentToChildren[note.note_id].length > 0) { note.folder = true; if (note.expanded) { note.children = prepareNoteTreeInner(note.note_id); } else { note.lazy = true; } } noteList.push(note); } return noteList; } async function activateNode(notePath) { const path = notePath.split("/").reverse(); const effectivePath = []; let childNoteId = null; let i = 0; while (true) { const parentNoteId = i < path.length ? path[i] : null; i++; if (childNoteId !== null) { const parents = childToParents[childNoteId]; if (parentNoteId === null || !parents.includes(parentNoteId)) { console.log("Did not find parent " + parentNoteId + " for child " + childNoteId); if (parents.length > 0) { if (parents[0] === 'root') { console.log("Reached root."); break; } childNoteId = parents[0]; effectivePath.push(childNoteId); console.log("Choosing parent " + childNoteId + " instead."); continue; } else { console.log("No parents, can't activate node."); return; } } } effectivePath.push(parentNoteId); childNoteId = parentNoteId; } const noteId = treeUtils.getNoteIdFromNotePath(notePath); const runPath = effectivePath.reverse(); let parentNoteId = 'root'; for (const childNoteId of runPath) { const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); const node = treeUtils.getNodeByNoteTreeId(noteTreeId); if (childNoteId === noteId) { await node.setActive(); } else { await node.setExpanded(); } parentNoteId = childNoteId; } } function showParentList(noteId, node) { const parents = childToParents[noteId]; if (parents.length <= 1) { parentListEl.hide(); } else { parentListEl.show(); parentListEl.empty(); const list = $("