fixes in working with top level nodes and root node

This commit is contained in:
azivner 2017-12-23 12:19:15 -05:00
parent 14f44b3271
commit 8bc12a2654
4 changed files with 34 additions and 14 deletions

View File

@ -222,6 +222,7 @@ const noteTree = (function() {
assertArguments(notePath); assertArguments(notePath);
const runPath = getRunPath(notePath); const runPath = getRunPath(notePath);
const noteId = treeUtils.getNoteIdFromNotePath(notePath); const noteId = treeUtils.getNoteIdFromNotePath(notePath);
let parentNoteId = 'root'; let parentNoteId = 'root';
@ -277,10 +278,14 @@ const noteTree = (function() {
if (parents.length > 0) { if (parents.length > 0) {
console.log(now(), "Available parents:", parents); console.log(now(), "Available parents:", parents);
const pathToRoot = getSomeNotePath(parents[0]).split("/").reverse(); const someNotePath = getSomeNotePath(parents[0]);
for (const noteId of pathToRoot) { if (someNotePath) { // in case it's root the path may be empty
effectivePath.push(noteId); const pathToRoot = someNotePath.split("/").reverse();
for (const noteId of pathToRoot) {
effectivePath.push(noteId);
}
} }
break; break;

View File

@ -58,7 +58,7 @@ const treeChanges = (function() {
await server.remove('notes/' + node.data.note_tree_id); 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().folder = false;
node.getParent().renderTitle(); node.getParent().renderTitle();
} }
@ -69,27 +69,30 @@ const treeChanges = (function() {
next = node.getPrevSibling(); next = node.getPrevSibling();
} }
if (!next) { if (!next && !isTopLevelNode(node)) {
next = node.getParent(); next = node.getParent();
} }
node.remove(); node.remove();
// activate next element after this one is deleted so we don't lose focus if (next) {
next.setActive(); // activate next element after this one is deleted so we don't lose focus
next.setActive();
noteTree.setCurrentNotePathToHash(next);
}
noteTree.setCurrentNotePathToHash(next);
noteTree.reload(); noteTree.reload();
} }
async function moveNodeUpInHierarchy(node) { async function moveNodeUpInHierarchy(node) {
if (node.getParent() === null) { if (isTopLevelNode(node)) {
return; return;
} }
await server.put('notes/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id); 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().folder = false;
node.getParent().renderTitle(); node.getParent().renderTitle();
} }
@ -98,11 +101,13 @@ const treeChanges = (function() {
} }
function changeNode(node, func) { 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); noteTree.removeParentChildRelation(node.data.parent_note_id, node.data.note_id);
func(node); 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); noteTree.setParentChildRelation(node.data.note_tree_id, node.data.parent_note_id, node.data.note_id);

View File

@ -4,7 +4,7 @@ const treeUtils = (function() {
const treeEl = $("#tree"); const treeEl = $("#tree");
function getParentProtectedStatus(node) { 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) { function getNodeByKey(key) {
@ -20,7 +20,7 @@ const treeUtils = (function() {
function getNotePath(node) { function getNotePath(node) {
const path = []; const path = [];
while (node) { while (node && !isRootNode(node)) {
if (node.data.note_id) { if (node.data.note_id) {
path.push(node.data.note_id); path.push(node.data.note_id);
} }

View File

@ -75,7 +75,9 @@ function isElectron() {
function assertArguments() { function assertArguments() {
for (const i in arguments) { 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) { if (!expr) {
throwError(message); throwError(message);
} }
}
function isTopLevelNode(node) {
return isRootNode(node.getParent());
}
function isRootNode(node) {
return node.key === "root_1";
} }