Notes/public/javascripts/tree_utils.js

68 lines
1.5 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
2017-11-04 22:18:36 -04:00
function getParentKey(node) {
return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key;
}
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
}
2017-11-04 22:18:36 -04:00
function getNodeByKey(noteId) {
return treeEl.fancytree('getNodeByKey', noteId);
}
2017-11-04 22:18:36 -04:00
function activateNode(noteId) {
const node = treeUtils.getNodeByKey(noteId);
2017-11-04 22:18:36 -04:00
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;
}
2017-11-04 22:18:36 -04:00
function getFullName(noteId) {
let note = treeUtils.getNodeByKey(noteId);
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) {
2017-11-10 22:55:19 -05:00
path.push(note.title);
2017-11-04 22:18:36 -04:00
note = note.getParent();
}
// remove "root" element
path.pop();
2017-11-04 22:18:36 -04:00
return path.reverse().join(" > ");
}
2017-11-04 22:18:36 -04:00
return {
getParentKey,
2017-11-14 23:01:23 -05:00
getParentProtectedStatus,
2017-11-04 22:18:36 -04:00
getNodeByKey,
activateNode,
getNoteTitle,
getFullName
};
})();