Notes/public/javascripts/tree_utils.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-04 22:18:36 -04:00
const treeUtils = (function() {
const treeEl = $("#tree");
2017-11-04 19:28:49 -04:00
function getParentNoteTreeId(node) {
return node.note_pid;
2017-11-04 22:18:36 -04:00
}
2017-11-14 23:01:23 -05:00
function getParentProtectedStatus(node) {
return node.getParent() === null ? 0 : node.getParent().data.is_protected;
2017-11-04 22:18:36 -04:00
}
function getNodeByKey(key) {
return treeEl.fancytree('getNodeByKey', key);
}
function getNodeByNoteTreeId(noteTreeId) {
const key = noteTree.getKeyFromNoteTreeId(noteTreeId);
return getNodeByKey(key);
}
async function activateNode(noteTreeIdToActivate) {
const noteTreeIdPath = [ noteTreeIdToActivate ];
let note = noteTree.getByNoteId(noteTreeIdToActivate);
while (note) {
if (note.note_pid !== 'root') {
noteTreeIdPath.push(note.note_pid);
}
note = noteTree.getByNoteId(note.note_pid);
}
for (const noteTreeId of noteTreeIdPath.reverse()) {
const node = treeUtils.getNodeByNoteTreeId(noteTreeId);
if (noteTreeId !== noteTreeIdToActivate) {
await node.setExpanded();
}
else {
await node.setActive();
}
}
}
2017-11-04 22:18:36 -04:00
function getNoteTitle(noteId) {
const note = treeUtils.getNodeByKey(noteId);
if (!note) {
return;
}
2017-11-04 22:18:36 -04:00
let noteTitle = note.title;
2017-11-04 22:18:36 -04:00
if (noteTitle.endsWith(" (clone)")) {
noteTitle = noteTitle.substr(0, noteTitle.length - 8);
}
2017-11-04 22:18:36 -04:00
return noteTitle;
}
function getFullName(noteTreeId) {
let note = noteTree.getByNoteId(noteTreeId);
2017-11-04 22:18:36 -04:00
if (note === null) {
return "[unknown]";
}
2017-11-04 22:18:36 -04:00
const path = [];
while (note) {
path.push(note.note_title);
2017-11-04 22:18:36 -04:00
note = noteTree.getByNoteId(note.note_pid);
2017-11-04 22:18:36 -04:00
}
return path.reverse().join(" > ");
}
2017-11-04 22:18:36 -04:00
return {
getParentNoteTreeId,
2017-11-14 23:01:23 -05:00
getParentProtectedStatus,
2017-11-04 22:18:36 -04:00
getNodeByKey,
getNodeByNoteTreeId,
2017-11-04 22:18:36 -04:00
activateNode,
getNoteTitle,
getFullName
};
})();