trilium/public/javascripts/tree_changes.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-11-05 10:10:41 +08:00
"use strict";
const treeChanges = (function() {
2017-11-20 12:12:39 +08:00
async function moveBeforeNode(node, beforeNode) {
await $.ajax({
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id,
2017-11-05 10:10:41 +08:00
type: 'PUT',
2017-11-20 12:12:39 +08:00
contentType: "application/json"
2017-11-05 10:10:41 +08:00
});
2017-11-20 12:12:39 +08:00
node.moveTo(beforeNode, 'before');
noteTree.setCurrentNotePathToHash(node);
2017-11-05 10:10:41 +08:00
}
2017-11-20 12:12:39 +08:00
async function moveAfterNode(node, afterNode) {
await $.ajax({
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id,
2017-11-05 10:10:41 +08:00
type: 'PUT',
2017-11-20 12:12:39 +08:00
contentType: "application/json"
2017-11-05 10:10:41 +08:00
});
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) {
await $.ajax({
url: baseApiUrl + 'notes/' + noteId + '/cloneAfter/' + afterNoteTreeId,
type: 'PUT',
error: () => showError("Error cloning note.")
});
await noteTree.reload();
}
2017-11-20 12:12:39 +08:00
async function moveToNode(node, toNode) {
await $.ajax({
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveTo/' + toNode.data.note_id,
2017-11-05 10:10:41 +08:00
type: 'PUT',
2017-11-20 12:12:39 +08:00
contentType: "application/json"
});
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) {
await $.ajax({
url: baseApiUrl + 'notes/' + childNoteId + '/cloneTo/' + parentNoteId,
type: 'PUT',
error: () => showError("Error cloning note.")
});
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
2017-11-20 12:12:39 +08:00
await $.ajax({
url: baseApiUrl + 'notes/' + node.data.note_tree_id,
type: 'DELETE'
});
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
recentNotes.removeRecentNote(node.note_tree_id);
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-20 12:12:39 +08:00
async function moveNodeUp(node) {
2017-11-05 10:10:41 +08:00
if (node.getParent() !== null) {
$.ajax({
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id,
2017-11-05 10:10:41 +08:00
type: 'PUT',
contentType: "application/json",
success: () => {
if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
node.moveTo(node.getParent(), 'after');
noteTree.setCurrentNotePathToHash(node);
2017-11-05 10:10:41 +08:00
}
});
}
}
return {
moveBeforeNode,
moveAfterNode,
moveToNode,
deleteNode,
moveNodeUp,
cloneNoteAfter,
cloneNoteTo
2017-11-05 10:10:41 +08:00
};
})();