From 17d030e800dcc773acde34f0fcfc5dede10f02dd Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 12 Dec 2018 20:39:56 +0100 Subject: [PATCH] continuing work on hoisting notes --- src/public/javascripts/services/hoisted_note.js | 3 +++ src/public/javascripts/services/tree.js | 17 ++++++++++------- src/public/javascripts/services/tree_builder.js | 12 ++++++++++-- src/routes/api/tree.js | 4 ++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/public/javascripts/services/hoisted_note.js b/src/public/javascripts/services/hoisted_note.js index 26c863223..2a64ad822 100644 --- a/src/public/javascripts/services/hoisted_note.js +++ b/src/public/javascripts/services/hoisted_note.js @@ -1,5 +1,6 @@ import optionsInit from './options_init.js'; import server from "./server.js"; +import tree from "./tree.js"; let hoistedNoteId; @@ -17,6 +18,8 @@ async function setHoistedNoteId(noteId) { hoistedNoteId = noteId; await server.put('options/hoistedNoteId/' + noteId); + + await tree.reload(); } export default { diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index c7004e0f5..39d64b652 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -14,6 +14,7 @@ import treeBuilder from "./tree_builder.js"; import treeKeyBindings from "./tree_keybindings.js"; import Branch from '../entities/branch.js'; import NoteShort from '../entities/note_short.js'; +import hoistedNoteService from '../services/hoisted_note.js'; const $tree = $("#tree"); const $createTopLevelNoteButton = $("#create-top-level-note-button"); @@ -88,10 +89,11 @@ async function expandToNote(notePath, expandOpts) { const noteId = treeUtils.getNoteIdFromNotePath(notePath); - let parentNoteId = 'none'; + let parentNoteId = null; for (const childNoteId of runPath) { - const node = getNodesByNoteId(childNoteId).find(node => node.data.parentNoteId === parentNoteId); + // for first node (!parentNoteId) it doesn't matter which node is found + const node = getNodesByNoteId(childNoteId).find(node => !parentNoteId || node.data.parentNoteId === parentNoteId); if (!node) { console.error(`Can't find node for noteId=${childNoteId} with parentNoteId=${parentNoteId}`); @@ -143,6 +145,8 @@ async function getRunPath(notePath) { path.push('root'); } + const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); + const effectivePath = []; let childNoteId = null; let i = 0; @@ -195,13 +199,12 @@ async function getRunPath(notePath) { } } - if (parentNoteId === 'none') { + effectivePath.push(parentNoteId); + childNoteId = parentNoteId; + + if (parentNoteId === hoistedNoteId) { break; } - else { - effectivePath.push(parentNoteId); - childNoteId = parentNoteId; - } } return effectivePath.reverse(); diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js index 945ea33e7..63ff8fec7 100644 --- a/src/public/javascripts/services/tree_builder.js +++ b/src/public/javascripts/services/tree_builder.js @@ -12,8 +12,16 @@ async function prepareTree(noteRows, branchRows, relations) { treeCache.load(noteRows, branchRows, relations); const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); - const hoistedNote = await treeCache.getNote(hoistedNoteId); - const hoistedBranch = (await hoistedNote.getBranches())[0]; + + let hoistedBranch; + + if (hoistedNoteId === 'root') { + hoistedBranch = await treeCache.getBranch('root'); + } + else { + const hoistedNote = await treeCache.getNote(hoistedNoteId); + hoistedBranch = (await hoistedNote.getBranches())[0]; + } return [ await prepareNode(hoistedBranch) ]; } diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index 97dbb527c..488731424 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -38,6 +38,7 @@ async function getNotes(noteIds) { async function getRelations(noteIds) { // we need to fetch both parentNoteId and noteId matches because we can have loaded child // of which only some of the parents has been loaded. + // also now with note hoisting, it is possible to have the note displayed without its parent chain being loaded const relations = await sql.getManyRows(`SELECT branchId, noteId AS 'childNoteId', parentNoteId, notePosition FROM branches WHERE isDeleted = 0 AND (parentNoteId IN (???) OR noteId IN (???))`, noteIds); @@ -65,6 +66,9 @@ async function getTree() { ) SELECT branches.* FROM tree JOIN branches USING(noteId) WHERE branches.isDeleted = 0 ORDER BY branches.notePosition`, [hoistedNoteId]); + // we also want root branch in there because all the paths start with root + branches.push(await sql.getRow(`SELECT * FROM branches WHERE branchId = 'root'`)); + const noteIds = Array.from(new Set(branches.map(b => b.noteId))); const notes = await getNotes(noteIds);