From 57d19f3302438c7633d85308bed13e0ec96cf942 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 26 Mar 2018 22:11:45 -0400 Subject: [PATCH] refactored moving note in the tree --- src/public/javascripts/services/tree.js | 25 +---------- src/public/javascripts/services/tree_cache.js | 41 +++++++++++++++++++ .../javascripts/services/tree_changes.js | 18 ++++---- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index d7e0669d7..4b374bec9 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -72,25 +72,6 @@ async function setNodeTitleWithPrefix(node) { node.setTitle(utils.escapeHtml(title)); } -function removeParentChildRelation(parentNoteId, childNoteId) { - utils.assertArguments(parentNoteId, childNoteId); - - treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== parentNoteId); - treeCache.children[parentNoteId] = treeCache.children[parentNoteId].filter(ch => ch.noteId !== childNoteId); - - delete treeCache.childParentToBranch[childNoteId + '-' + parentNoteId]; -} - -async function setParentChildRelation(branchId, parentNoteId, childNoteId) { - treeCache.parents[childNoteId] = treeCache.parents[childNoteId] || []; - treeCache.parents[childNoteId].push(await treeCache.getNote(parentNoteId)); - - treeCache.children[parentNoteId] = treeCache.children[parentNoteId] || []; - treeCache.children[parentNoteId].push(await treeCache.getNote(childNoteId)); - - treeCache.childParentToBranch[childNoteId + '-' + parentNoteId] = await treeCache.getBranch(branchId); -} - async function prepareBranch(noteRows, branchRows) { utils.assertArguments(noteRows); @@ -641,8 +622,6 @@ function collapseTree(node = null) { node.visit(node => node.setExpanded(false)); } -$(document).bind('keydown', 'alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument - function scrollToCurrentNote() { const node = getCurrentNode(); @@ -796,6 +775,8 @@ $(window).bind('hashchange', function() { } }); +utils.bindShortcut('alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument + $createTopLevelNoteButton.click(createNewTopLevelNote); $collapseTreeButton.click(collapseTree); $scrollToCurrentNoteButton.click(scrollToCurrentNote); @@ -815,8 +796,6 @@ export default { setPrefix, createNewTopLevelNote, createNote, - removeParentChildRelation, - setParentChildRelation, getSelectedNodes, sortAlphabetically }; \ No newline at end of file diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index ce22d9d40..e2552bd00 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -65,6 +65,47 @@ class TreeCache { return branch; } + + /* Move note from one parent to another. */ + async moveNote(childNoteId, oldParentNoteId, newParentNoteId) { + utils.assertArguments(childNoteId, oldParentNoteId, newParentNoteId); + + if (oldParentNoteId === newParentNoteId) { + return; + } + + treeCache.childParentToBranch[childNoteId + '-' + newParentNoteId] = treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId]; + delete treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId]; // this is correct because we know that oldParentId isn't same as newParentId + + // remove old associations + treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== oldParentNoteId); + treeCache.children[oldParentNoteId] = treeCache.children[oldParentNoteId].filter(ch => ch.noteId !== childNoteId); + + // add new associations + treeCache.parents[childNoteId].push(await treeCache.getNote(newParentNoteId)); + + treeCache.children[newParentNoteId] = treeCache.children[newParentNoteId] || []; // this might be first child + treeCache.children[newParentNoteId].push(await treeCache.getNote(childNoteId)); + } + + removeParentChildRelation(parentNoteId, childNoteId) { + utils.assertArguments(parentNoteId, childNoteId); + + treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== parentNoteId); + treeCache.children[parentNoteId] = treeCache.children[parentNoteId].filter(ch => ch.noteId !== childNoteId); + + delete treeCache.childParentToBranch[childNoteId + '-' + parentNoteId]; + } + + async setParentChildRelation(branchId, parentNoteId, childNoteId) { + treeCache.parents[childNoteId] = treeCache.parents[childNoteId] || []; + treeCache.parents[childNoteId].push(await treeCache.getNote(parentNoteId)); + + treeCache.children[parentNoteId] = treeCache.children[parentNoteId] || []; + treeCache.children[parentNoteId].push(await treeCache.getNote(childNoteId)); + + treeCache.childParentToBranch[childNoteId + '-' + parentNoteId] = await treeCache.getBranch(branchId); + } } const treeCache = new TreeCache(); diff --git a/src/public/javascripts/services/tree_changes.js b/src/public/javascripts/services/tree_changes.js index 56ceb7d05..67716ba65 100644 --- a/src/public/javascripts/services/tree_changes.js +++ b/src/public/javascripts/services/tree_changes.js @@ -2,6 +2,7 @@ import treeService from './tree.js'; import utils from './utils.js'; import server from './server.js'; import infoService from "./info.js"; +import treeCache from "./tree_cache.js"; async function moveBeforeNode(nodesToMove, beforeNode) { for (const nodeToMove of nodesToMove) { @@ -12,7 +13,7 @@ async function moveBeforeNode(nodesToMove, beforeNode) { return; } - changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before')); + await changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before')); } } @@ -27,7 +28,7 @@ async function moveAfterNode(nodesToMove, afterNode) { return; } - changeNode(nodeToMove, node => node.moveTo(afterNode, 'after')); + await changeNode(nodeToMove, node => node.moveTo(afterNode, 'after')); } } @@ -40,7 +41,7 @@ async function moveToNode(nodesToMove, toNode) { return; } - changeNode(nodeToMove, node => { + await 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 // this doesn't work if this isn't a folder yet, that's why we expand second time below @@ -107,19 +108,20 @@ async function moveNodeUpInHierarchy(node) { node.getParent().renderTitle(); } - changeNode(node, node => node.moveTo(node.getParent(), 'after')); + await changeNode(node, node => node.moveTo(node.getParent(), 'after')); } -function changeNode(node, func) { +async function changeNode(node, func) { utils.assertArguments(node.data.parentNoteId, node.data.noteId); - treeService.removeParentChildRelation(node.data.parentNoteId, node.data.noteId); + const childNoteId = node.data.noteId; + const oldParentNoteId = node.data.parentNoteId; func(node); - node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId; + const newParentNoteId = node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId; - treeService.setParentChildRelation(node.data.branchId, node.data.parentNoteId, node.data.noteId); + await treeCache.moveNote(childNoteId, oldParentNoteId, newParentNoteId); treeService.setCurrentNotePathToHash(node); }