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-19 06:05:50 +08:00
|
|
|
function getParentNoteTreeId(node) {
|
|
|
|
return node.note_pid;
|
2017-11-05 10:18:36 +08:00
|
|
|
}
|
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-19 06:05:50 +08:00
|
|
|
function getNodeByKey(key) {
|
|
|
|
return treeEl.fancytree('getNodeByKey', key);
|
2017-10-02 11:07:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-19 06:05:50 +08:00
|
|
|
function getNodeByNoteTreeId(noteTreeId) {
|
|
|
|
const key = noteTree.getKeyFromNoteTreeId(noteTreeId);
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-19 06:05:50 +08:00
|
|
|
return getNodeByKey(key);
|
|
|
|
}
|
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
function getNoteIdFromNotePath(notePath) {
|
|
|
|
const path = notePath.split("/");
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
return path[path.length - 1];
|
|
|
|
}
|
2017-09-19 08:48:02 +08:00
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
function getFullNameForPath(notePath) {
|
|
|
|
const path = notePath.split("/");
|
|
|
|
const titlePath = path.map(noteId => noteTree.getNoteTitle(noteId));
|
2017-10-12 07:41:45 +08:00
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
return titlePath.join(" > ");
|
2017-09-19 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2017-11-19 06:05:50 +08:00
|
|
|
function getFullName(noteTreeId) {
|
2017-11-20 08:39:39 +08:00
|
|
|
let note = noteTree.getByNoteTreeId(noteTreeId);
|
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-20 08:39:39 +08:00
|
|
|
note = noteTree.getByNoteTreeId(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-19 21:47:22 +08:00
|
|
|
function getNotePath(node) {
|
|
|
|
const path = [];
|
|
|
|
|
|
|
|
while (node) {
|
2017-11-20 00:28:46 +08:00
|
|
|
if (node.data.note_id) {
|
|
|
|
path.push(node.data.note_id);
|
2017-11-19 21:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.reverse().join("/");
|
|
|
|
}
|
|
|
|
|
2017-11-20 11:31:30 +08:00
|
|
|
async function addAsChild(parentNotePath, childNotePath) {
|
|
|
|
const parentNoteId = treeUtils.getNoteIdFromNotePath(parentNotePath);
|
|
|
|
const childNoteId = treeUtils.getNoteIdFromNotePath(childNotePath);
|
|
|
|
|
|
|
|
await $.ajax({
|
|
|
|
url: baseApiUrl + 'tree/' + parentNoteId + '/addChild/' + childNoteId,
|
|
|
|
type: 'PUT',
|
|
|
|
error: () => showError("Error adding child.")
|
|
|
|
});
|
|
|
|
|
|
|
|
await noteTree.reload();
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:18:36 +08:00
|
|
|
return {
|
2017-11-19 06:05:50 +08:00
|
|
|
getParentNoteTreeId,
|
2017-11-15 12:01:23 +08:00
|
|
|
getParentProtectedStatus,
|
2017-11-05 10:18:36 +08:00
|
|
|
getNodeByKey,
|
2017-11-19 06:05:50 +08:00
|
|
|
getNodeByNoteTreeId,
|
2017-11-19 21:47:22 +08:00
|
|
|
getFullName,
|
2017-11-20 01:06:48 +08:00
|
|
|
getFullNameForPath,
|
|
|
|
getNotePath,
|
2017-11-20 11:31:30 +08:00
|
|
|
getNoteIdFromNotePath,
|
|
|
|
addAsChild
|
2017-11-05 10:18:36 +08:00
|
|
|
};
|
|
|
|
})();
|