trilium/public/javascripts/tree_utils.js

73 lines
1.5 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-05 10:18:36 +08:00
function getParentEncryption(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);
}
2017-11-05 10:18:36 +08:00
function activateNode(noteId) {
const node = treeUtils.getNodeByKey(noteId);
2017-11-05 10:18:36 +08:00
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 = treeUtils.getNodeByKey(noteId);
2017-11-05 10:18:36 +08:00
if (note === null) {
return "[unknown]";
}
2017-11-05 10:18:36 +08:00
// why?
if (note.data.is_clone) {
return null;
}
2017-11-05 10:18:36 +08:00
const path = [];
while (note) {
2017-11-11 11:55:19 +08:00
path.push(note.title);
2017-11-05 10:18:36 +08:00
note = note.getParent();
}
// remove "root" element
path.pop();
2017-11-05 10:18:36 +08:00
return path.reverse().join(" > ");
}
2017-11-05 10:18:36 +08:00
return {
getParentKey,
getParentEncryption,
getNodeByKey,
activateNode,
getNoteTitle,
getFullName
};
})();