2017-11-05 10:10:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const treeChanges = (function() {
|
2017-12-03 23:06:53 +08:00
|
|
|
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');
|
|
|
|
|
2017-12-03 23:06:53 +08:00
|
|
|
noteTree.setCurrentNotePathToHash(node);
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-12-03 23:06:53 +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');
|
|
|
|
|
2017-12-03 23:06:53 +08:00
|
|
|
noteTree.setCurrentNotePathToHash(node);
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-11-23 12:16:54 +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);
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
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 07:16:50 +08:00
|
|
|
|
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
|
|
|
|
2017-11-23 12:16:54 +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);
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
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-29 09:52:38 +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();
|
2017-12-15 11:53:25 +08:00
|
|
|
|
|
|
|
if (!next) {
|
|
|
|
next = node.getPrevSibling();
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:12:39 +08:00
|
|
|
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-12-10 10:42:22 +08:00
|
|
|
noteTree.reload();
|
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,
|
2017-11-23 12:16:54 +08:00
|
|
|
cloneNoteAfter,
|
|
|
|
cloneNoteTo
|
2017-11-05 10:10:41 +08:00
|
|
|
};
|
|
|
|
})();
|