diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index 0746cac43..bb0e32805 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -222,6 +222,7 @@ const noteTree = (function() { assertArguments(notePath); const runPath = getRunPath(notePath); + const noteId = treeUtils.getNoteIdFromNotePath(notePath); let parentNoteId = 'root'; @@ -277,10 +278,14 @@ const noteTree = (function() { if (parents.length > 0) { console.log(now(), "Available parents:", parents); - const pathToRoot = getSomeNotePath(parents[0]).split("/").reverse(); + const someNotePath = getSomeNotePath(parents[0]); - for (const noteId of pathToRoot) { - effectivePath.push(noteId); + if (someNotePath) { // in case it's root the path may be empty + const pathToRoot = someNotePath.split("/").reverse(); + + for (const noteId of pathToRoot) { + effectivePath.push(noteId); + } } break; diff --git a/public/javascripts/tree_changes.js b/public/javascripts/tree_changes.js index fcb387e0b..d6c11b93e 100644 --- a/public/javascripts/tree_changes.js +++ b/public/javascripts/tree_changes.js @@ -58,7 +58,7 @@ const treeChanges = (function() { await server.remove('notes/' + node.data.note_tree_id); - if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { + if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) { node.getParent().folder = false; node.getParent().renderTitle(); } @@ -69,27 +69,30 @@ const treeChanges = (function() { next = node.getPrevSibling(); } - if (!next) { + if (!next && !isTopLevelNode(node)) { next = node.getParent(); } node.remove(); - // activate next element after this one is deleted so we don't lose focus - next.setActive(); + if (next) { + // activate next element after this one is deleted so we don't lose focus + next.setActive(); + + noteTree.setCurrentNotePathToHash(next); + } - noteTree.setCurrentNotePathToHash(next); noteTree.reload(); } async function moveNodeUpInHierarchy(node) { - if (node.getParent() === null) { + if (isTopLevelNode(node)) { return; } await server.put('notes/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id); - if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { + if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) { node.getParent().folder = false; node.getParent().renderTitle(); } @@ -98,11 +101,13 @@ const treeChanges = (function() { } function changeNode(node, func) { + assertArguments(node.data.parent_note_id, node.data.note_id); + noteTree.removeParentChildRelation(node.data.parent_note_id, node.data.note_id); func(node); - node.data.parent_note_id = node.getParent() === null ? 'root' : node.getParent().data.note_id; + node.data.parent_note_id = isTopLevelNode(node) ? 'root' : node.getParent().data.note_id; noteTree.setParentChildRelation(node.data.note_tree_id, node.data.parent_note_id, node.data.note_id); diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js index c144065e2..673eaf9fc 100644 --- a/public/javascripts/tree_utils.js +++ b/public/javascripts/tree_utils.js @@ -4,7 +4,7 @@ const treeUtils = (function() { const treeEl = $("#tree"); function getParentProtectedStatus(node) { - return node.getParent() === null ? 0 : node.getParent().data.is_protected; + return isTopLevelNode(node) ? 0 : node.getParent().data.is_protected; } function getNodeByKey(key) { @@ -20,7 +20,7 @@ const treeUtils = (function() { function getNotePath(node) { const path = []; - while (node) { + while (node && !isRootNode(node)) { if (node.data.note_id) { path.push(node.data.note_id); } diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js index 384c5999f..1f67844ad 100644 --- a/public/javascripts/utils.js +++ b/public/javascripts/utils.js @@ -75,7 +75,9 @@ function isElectron() { function assertArguments() { for (const i in arguments) { - assert(arguments[i], `argument ${i} should not be falsy. Argument list: ${arguments}`); + if (!arguments[i]) { + throwError(`Argument idx#${i} should not be falsy: ${arguments[i]}`); + } } } @@ -83,4 +85,12 @@ function assert(expr, message) { if (!expr) { throwError(message); } +} + +function isTopLevelNode(node) { + return isRootNode(node.getParent()); +} + +function isRootNode(node) { + return node.key === "root_1"; } \ No newline at end of file