2017-11-05 07:38:50 +08:00
|
|
|
"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-09-10 00:06:15 +08:00
|
|
|
|
2017-11-15 12:01:23 +08:00
|
|
|
function getParentProtectedStatus(node) {
|
2017-11-15 10:54:12 +08:00
|
|
|
return node.getParent() === null ? 0 : node.getParent().data.is_protected;
|
2017-11-05 10:18:36 +08:00
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
function getNodeByKey(noteId) {
|
|
|
|
return treeEl.fancytree('getNodeByKey', noteId);
|
2017-10-02 11:07:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 11:52:24 +08:00
|
|
|
async function activateNode(noteIdToActivate) {
|
|
|
|
const noteIdPath = [ noteIdToActivate ];
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-18 11:52:24 +08:00
|
|
|
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-10-02 11:07:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
function getNoteTitle(noteId) {
|
|
|
|
const note = treeUtils.getNodeByKey(noteId);
|
|
|
|
if (!note) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
let noteTitle = note.title;
|
2017-09-19 08:48:02 +08:00
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
if (noteTitle.endsWith(" (clone)")) {
|
|
|
|
noteTitle = noteTitle.substr(0, noteTitle.length - 8);
|
|
|
|
}
|
2017-10-12 07:41:45 +08:00
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
return noteTitle;
|
2017-09-19 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
function getFullName(noteId) {
|
2017-11-18 11:37:11 +08:00
|
|
|
let note = noteTree.getByNoteId(noteId);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
if (note === null) {
|
|
|
|
return "[unknown]";
|
2017-11-04 23:32:05 +08:00
|
|
|
}
|
2017-11-05 10:18:36 +08:00
|
|
|
|
|
|
|
const path = [];
|
|
|
|
|
|
|
|
while (note) {
|
2017-11-18 11:37:11 +08:00
|
|
|
path.push(note.note_title);
|
2017-11-05 10:18:36 +08:00
|
|
|
|
2017-11-18 11:37:11 +08:00
|
|
|
note = noteTree.getByNoteId(note.note_pid);
|
2017-11-05 10:18:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return path.reverse().join(" > ");
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
})();
|