trilium/public/javascripts/tree_changes.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-11-05 10:10:41 +08:00
"use strict";
const treeChanges = (function() {
async function moveBeforeNode(node, beforeNode) {
2017-12-02 11:47:23 +08:00
await server.put('notes/' + node.data.note_tree_id + '/move-before/' + beforeNode.data.note_tree_id);
2017-11-20 12:12:39 +08:00
node.moveTo(beforeNode, 'before');
noteTree.setCurrentNotePathToHash(node);
2017-11-05 10:10:41 +08:00
}
async function moveAfterNode(node, afterNode) {
2017-12-02 11:47:23 +08:00
await server.put('notes/' + node.data.note_tree_id + '/move-after/' + afterNode.data.note_tree_id);
2017-11-20 12:12:39 +08:00
node.moveTo(afterNode, 'after');
noteTree.setCurrentNotePathToHash(node);
2017-11-05 10:10:41 +08:00
}
// beware that first arg is noteId and second is noteTreeId!
async function cloneNoteAfter(noteId, afterNoteTreeId) {
2017-12-02 11:47:23 +08:00
const resp = await server.put('notes/' + noteId + '/clone-after/' + afterNoteTreeId);
if (!resp.success) {
alert(resp.message);
return;
}
await noteTree.reload();
}
2017-11-20 12:12:39 +08:00
async function moveToNode(node, toNode) {
2017-12-02 11:47:23 +08:00
await server.put('notes/' + node.data.note_tree_id + '/move-to/' + toNode.data.note_id);
2017-11-05 10:10:41 +08:00
2017-11-20 12:12:39 +08:00
node.moveTo(toNode);
2017-11-05 10:10:41 +08:00
2017-11-20 12:12:39 +08:00
toNode.setExpanded(true);
2017-11-20 12:12:39 +08:00
toNode.folder = true;
toNode.renderTitle();
2017-11-05 10:10:41 +08:00
2017-11-20 12:12:39 +08:00
noteTree.setCurrentNotePathToHash(node);
}
2017-11-05 10:10:41 +08:00
async function cloneNoteTo(childNoteId, parentNoteId) {
2017-12-02 11:47:23 +08:00
const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId);
if (!resp.success) {
alert(resp.message);
return;
}
await noteTree.reload();
}
2017-11-20 12:12:39 +08:00
async function deleteNode(node) {
if (!confirm('Are you sure you want to delete note "' + node.title + '"?')) {
return;
}
2017-11-05 10:10:41 +08:00
await server.remove('notes/' + node.data.note_tree_id);
2017-11-05 10:10:41 +08:00
2017-11-20 12:12:39 +08:00
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
2017-11-05 10:10:41 +08:00
2017-11-20 12:12:39 +08:00
let next = node.getNextSibling();
if (!next) {
next = node.getParent();
2017-11-05 10:10:41 +08:00
}
2017-11-20 12:12:39 +08:00
node.remove();
// activate next element after this one is deleted so we don't lose focus
next.setActive();
noteTree.setCurrentNotePathToHash(next);
2017-11-05 10:10:41 +08:00
}
2017-11-29 04:17:11 +08:00
async function moveNodeUpInHierarchy(node) {
if (node.getParent() === null) {
return;
}
2017-12-02 11:47:23 +08:00
await server.put('notes/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id);
2017-11-29 04:17:11 +08:00
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
2017-11-05 10:10:41 +08:00
}
2017-11-29 04:17:11 +08:00
node.moveTo(node.getParent(), 'after');
noteTree.setCurrentNotePathToHash(node);
2017-11-05 10:10:41 +08:00
}
return {
moveBeforeNode,
moveAfterNode,
moveToNode,
deleteNode,
2017-11-29 04:17:11 +08:00
moveNodeUpInHierarchy,
cloneNoteAfter,
cloneNoteTo
2017-11-05 10:10:41 +08:00
};
})();