From 4d95eb0762bc48b64956f2b28d0a669252491bbb Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 28 Nov 2017 15:17:11 -0500 Subject: [PATCH] some fixes in note moving --- public/javascripts/note_tree.js | 6 ++-- public/javascripts/tree_changes.js | 53 ++++++++++++++++++------------ 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index 818837700..6554e89d7 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -344,17 +344,17 @@ const noteTree = (function() { const beforeNode = node.getPrevSibling(); if (beforeNode !== null) { - treeChanges.moveBeforeNode(node, beforeNode); + treeChanges.moveBeforeNode(node, beforeNode, false); } }, "shift+down": node => { let afterNode = node.getNextSibling(); if (afterNode !== null) { - treeChanges.moveAfterNode(node, afterNode); + treeChanges.moveAfterNode(node, afterNode, false); } }, "shift+left": node => { - treeChanges.moveNodeUp(node); + treeChanges.moveNodeUpInHierarchy(node); }, "shift+right": node => { let toNode = node.getPrevSibling(); diff --git a/public/javascripts/tree_changes.js b/public/javascripts/tree_changes.js index 4c94df2b0..df209aaff 100644 --- a/public/javascripts/tree_changes.js +++ b/public/javascripts/tree_changes.js @@ -1,7 +1,7 @@ "use strict"; const treeChanges = (function() { - async function moveBeforeNode(node, beforeNode) { + async function moveBeforeNode(node, beforeNode, changeInPath = true) { await $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id, type: 'PUT', @@ -10,10 +10,14 @@ const treeChanges = (function() { node.moveTo(beforeNode, 'before'); + if (changeInPath) { + recentNotes.removeRecentNote(noteTree.getCurrentNotePath()); + } + noteTree.setCurrentNotePathToHash(node); } - async function moveAfterNode(node, afterNode) { + async function moveAfterNode(node, afterNode, changeInPath = true) { await $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id, type: 'PUT', @@ -22,6 +26,10 @@ const treeChanges = (function() { node.moveTo(afterNode, 'after'); + if (changeInPath) { + recentNotes.removeRecentNote(noteTree.getCurrentNotePath()); + } + noteTree.setCurrentNotePathToHash(node); } @@ -55,6 +63,8 @@ const treeChanges = (function() { toNode.folder = true; toNode.renderTitle(); + recentNotes.removeRecentNote(noteTree.getCurrentNotePath()); + noteTree.setCurrentNotePathToHash(node); } @@ -88,7 +98,7 @@ const treeChanges = (function() { node.getParent().renderTitle(); } - recentNotes.removeRecentNote(node.note_tree_id); + recentNotes.removeRecentNote(noteTree.getCurrentNotePath()); let next = node.getNextSibling(); if (!next) { @@ -103,24 +113,25 @@ const treeChanges = (function() { noteTree.setCurrentNotePathToHash(next); } - async function moveNodeUp(node) { - if (node.getParent() !== null) { - $.ajax({ - url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id, - 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); - } - }); + async function moveNodeUpInHierarchy(node) { + if (node.getParent() === null) { + return; } + + await $.ajax({ + url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id, + type: 'PUT', + contentType: "application/json", + }); + + if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { + node.getParent().folder = false; + node.getParent().renderTitle(); + } + + node.moveTo(node.getParent(), 'after'); + + noteTree.setCurrentNotePathToHash(node); } return { @@ -128,7 +139,7 @@ const treeChanges = (function() { moveAfterNode, moveToNode, deleteNode, - moveNodeUp, + moveNodeUpInHierarchy, cloneNoteAfter, cloneNoteTo };