2017-11-05 10:10:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const treeChanges = (function() {
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveBeforeNode(nodesToMove, beforeNode) {
|
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-02 11:28:19 +08:00
|
|
|
const resp = await server.put('notes/' + nodeToMove.data.note_tree_id + '/move-before/' + beforeNode.data.note_tree_id);
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before'));
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveAfterNode(nodesToMove, afterNode) {
|
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-02 11:28:19 +08:00
|
|
|
const resp = await server.put('notes/' + nodeToMove.data.note_tree_id + '/move-after/' + afterNode.data.note_tree_id);
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => node.moveTo(afterNode, 'after'));
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveToNode(nodesToMove, toNode) {
|
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-02 11:28:19 +08:00
|
|
|
const resp = await server.put('notes/' + nodeToMove.data.note_tree_id + '/move-to/' + toNode.data.note_id);
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => {
|
|
|
|
// first expand which will force lazy load and only then move the node
|
|
|
|
// if this is not expanded before moving, then lazy load won't happen because it already contains node
|
|
|
|
toNode.setExpanded(true);
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
node.moveTo(toNode);
|
2017-12-28 11:44:27 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
toNode.folder = true;
|
|
|
|
toNode.renderTitle();
|
|
|
|
});
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2017-12-22 10:54:25 +08:00
|
|
|
async function cloneNoteTo(childNoteId, parentNoteId, prefix) {
|
|
|
|
const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId, {
|
|
|
|
prefix: prefix
|
|
|
|
});
|
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) {
|
2017-12-27 08:54:43 +08:00
|
|
|
if (!confirm('Are you sure you want to delete note "' + node.title + '" and all its sub-notes?')) {
|
2017-11-20 12:12:39 +08:00
|
|
|
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-12-24 01:19:15 +08:00
|
|
|
if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2017-11-20 12:12:39 +08:00
|
|
|
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-12-24 01:19:15 +08:00
|
|
|
if (!next && !isTopLevelNode(node)) {
|
2017-11-20 12:12:39 +08:00
|
|
|
next = node.getParent();
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
|
|
|
node.remove();
|
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
if (next) {
|
|
|
|
// activate next element after this one is deleted so we don't lose focus
|
|
|
|
next.setActive();
|
|
|
|
|
|
|
|
noteTree.setCurrentNotePathToHash(next);
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2017-12-10 10:42:22 +08:00
|
|
|
noteTree.reload();
|
2017-12-27 08:54:43 +08:00
|
|
|
|
|
|
|
showMessage("Note has been deleted.");
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 04:17:11 +08:00
|
|
|
async function moveNodeUpInHierarchy(node) {
|
2017-12-24 01:19:15 +08:00
|
|
|
if (isTopLevelNode(node)) {
|
2017-11-29 04:17:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
const resp = await server.put('notes/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id);
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2017-11-29 04:17:11 +08:00
|
|
|
node.getParent().folder = false;
|
|
|
|
node.getParent().renderTitle();
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2017-12-18 05:28:13 +08:00
|
|
|
changeNode(node, node => node.moveTo(node.getParent(), 'after'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeNode(node, func) {
|
2017-12-24 01:19:15 +08:00
|
|
|
assertArguments(node.data.parent_note_id, node.data.note_id);
|
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
noteTree.removeParentChildRelation(node.data.parent_note_id, node.data.note_id);
|
2017-12-18 05:28:13 +08:00
|
|
|
|
|
|
|
func(node);
|
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
node.data.parent_note_id = isTopLevelNode(node) ? 'root' : node.getParent().data.note_id;
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
noteTree.setParentChildRelation(node.data.note_tree_id, node.data.parent_note_id, node.data.note_id);
|
2017-11-29 04:17:11 +08:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
})();
|