trilium/public/javascripts/tree_utils.js

86 lines
2 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 10:18:36 +08:00
const treeUtils = (function() {
const treeEl = $("#tree");
2017-11-05 07:28:49 +08:00
2017-11-05 10:18:36 +08:00
function getParentKey(node) {
return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key;
}
2017-11-15 12:01:23 +08:00
function getParentProtectedStatus(node) {
return node.getParent() === null ? 0 : node.getParent().data.is_protected;
2017-11-05 10:18:36 +08:00
}
2017-11-05 10:18:36 +08:00
function getNodeByKey(noteId) {
return treeEl.fancytree('getNodeByKey', noteId);
}
async function activateNode(noteIdToActivate) {
const noteIdPath = [ noteIdToActivate ];
let note = noteTree.getByNoteId(noteIdToActivate);
while (note) {
if (note.note_pid !== 'root') {
noteIdPath.push(note.note_pid);
}
note = noteTree.getByNoteId(note.note_pid);
}
for (const noteId of noteIdPath.reverse()) {
console.log("Activating/expanding " + noteId);
const node = treeUtils.getNodeByKey(noteId);
if (noteId !== noteIdToActivate) {
await node.setExpanded();
}
else {
await node.setActive();
}
}
}
2017-11-05 10:18:36 +08:00
function getNoteTitle(noteId) {
const note = treeUtils.getNodeByKey(noteId);
if (!note) {
return;
}
2017-11-05 10:18:36 +08:00
let noteTitle = note.title;
2017-11-05 10:18:36 +08:00
if (noteTitle.endsWith(" (clone)")) {
noteTitle = noteTitle.substr(0, noteTitle.length - 8);
}
2017-11-05 10:18:36 +08:00
return noteTitle;
}
2017-11-05 10:18:36 +08:00
function getFullName(noteId) {
let note = noteTree.getByNoteId(noteId);
2017-11-05 10:18:36 +08:00
if (note === null) {
return "[unknown]";
}
2017-11-05 10:18:36 +08:00
const path = [];
while (note) {
path.push(note.note_title);
2017-11-05 10:18:36 +08:00
note = noteTree.getByNoteId(note.note_pid);
2017-11-05 10:18:36 +08:00
}
return path.reverse().join(" > ");
}
2017-11-05 10:18:36 +08:00
return {
getParentKey,
2017-11-15 12:01:23 +08:00
getParentProtectedStatus,
2017-11-05 10:18:36 +08:00
getNodeByKey,
activateNode,
getNoteTitle,
getFullName
};
})();