From 0ec6d4baa86b879760dcbc6edc459cdeceec9f71 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 23 Nov 2017 19:29:25 -0500 Subject: [PATCH] simplification of internal structure/methods in note_tree --- public/javascripts/context_menu.js | 6 +++--- public/javascripts/note_tree.js | 24 +++++++++--------------- public/javascripts/tree_utils.js | 7 ------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js index 18d596c3d..19fee4fc5 100644 --- a/public/javascripts/context_menu.js +++ b/public/javascripts/context_menu.js @@ -8,7 +8,7 @@ const contextMenu = (function() { function pasteAfter(node) { if (clipboardMode === 'cut') { - const subjectNode = treeUtils.getNodeByNoteTreeId(clipboardId); + const subjectNode = treeUtils.getNodeByKey(clipboardId); treeChanges.moveAfterNode(subjectNode, node); } @@ -25,7 +25,7 @@ const contextMenu = (function() { function pasteInto(node) { if (clipboardMode === 'cut') { - const subjectNode = treeUtils.getNodeByNoteTreeId(clipboardId); + const subjectNode = treeUtils.getNodeByKey(clipboardId); treeChanges.moveToNode(subjectNode, node); } @@ -46,7 +46,7 @@ const contextMenu = (function() { } function cut(node) { - clipboardId = node.data.note_tree_id; + clipboardId = node.key; clipboardMode = 'cut'; } diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index f27730baf..3c071eba7 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -8,10 +8,10 @@ const noteTree = (function() { let startNoteTreeId = null; let treeLoadTime = null; let notesMap = {}; + let parentToChildren = {}; let childToParents = {}; - let counter = 1; - let noteTreeIdToKey = {}; + let parentChildToNoteTreeId = {}; let noteIdToTitle = {}; @@ -21,10 +21,6 @@ const noteTree = (function() { return node.note_tree_id; } - function getKeyFromNoteTreeId(noteTreeId) { - return noteTreeIdToKey[noteTreeId]; - } - function getTreeLoadTime() { return treeLoadTime; } @@ -120,12 +116,9 @@ const noteTree = (function() { node.extraClasses = node.extraClasses.substr(1); } - node.key = counter++ + ""; // key needs to be string node.refKey = note.note_id; node.expanded = note.is_expanded; - noteTreeIdToKey[noteTreeId] = node.key; - if (parentToChildren[note.note_id] && parentToChildren[note.note_id].length > 0) { node.folder = true; @@ -189,9 +182,7 @@ const noteTree = (function() { let parentNoteId = 'root'; for (const childNoteId of runPath) { - const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); - - const node = treeUtils.getNodeByNoteTreeId(noteTreeId); + const node = getNodes(childNoteId).find(node => node.data.note_pid === parentNoteId); if (childNoteId === noteId) { await node.setActive(); @@ -204,6 +195,10 @@ const noteTree = (function() { } } + function getNodes(noteId) { + return getTree().getNodesByRef(noteId); + } + function showParentList(noteId, node) { const parents = childToParents[noteId]; @@ -467,7 +462,7 @@ const noteTree = (function() { $(document).bind('keydown', 'alt+c', collapseTree); function scrollToCurrentNote() { - const node = treeUtils.getNodeByNoteTreeId(noteEditor.getCurrentNoteId()); + const node = noteTree.getCurrentNode(); if (node) { node.makeVisible({scrollIntoView: true}); @@ -568,7 +563,7 @@ const noteTree = (function() { const noteId = getCurrentNoteId(); if (noteId) { - return getTree().getNodesByRef(noteId); + return getNodes(noteId); } else { return []; @@ -683,7 +678,6 @@ const noteTree = (function() { collapseTree, scrollToCurrentNote, toggleSearch, - getKeyFromNoteTreeId, getNoteTreeIdFromKey, setCurrentNoteTreeBasedOnProtectedStatus, getCurrentNode, diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js index 144e0b260..d580f8975 100644 --- a/public/javascripts/tree_utils.js +++ b/public/javascripts/tree_utils.js @@ -11,12 +11,6 @@ const treeUtils = (function() { return treeEl.fancytree('getNodeByKey', key); } - function getNodeByNoteTreeId(noteTreeId) { - const key = noteTree.getKeyFromNoteTreeId(noteTreeId); - - return getNodeByKey(key); - } - function getNoteIdFromNotePath(notePath) { const path = notePath.split("/"); @@ -47,7 +41,6 @@ const treeUtils = (function() { return { getParentProtectedStatus, getNodeByKey, - getNodeByNoteTreeId, getFullNameForPath, getNotePath, getNoteIdFromNotePath