From f52d7e3c287cfbddd715300f0ea685cb45eafcc3 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 25 Mar 2018 12:29:00 -0400 Subject: [PATCH] split tree_cache and entities from note_tree --- src/public/javascripts/entities/branch.js | 22 +++ src/public/javascripts/entities/note_short.js | 45 ++++++ src/public/javascripts/note_tree.js | 131 +----------------- src/public/javascripts/tree_cache.js | 68 +++++++++ 4 files changed, 137 insertions(+), 129 deletions(-) create mode 100644 src/public/javascripts/entities/branch.js create mode 100644 src/public/javascripts/entities/note_short.js create mode 100644 src/public/javascripts/tree_cache.js diff --git a/src/public/javascripts/entities/branch.js b/src/public/javascripts/entities/branch.js new file mode 100644 index 000000000..375c676c6 --- /dev/null +++ b/src/public/javascripts/entities/branch.js @@ -0,0 +1,22 @@ +class Branch { + constructor(treeCache, row) { + this.treeCache = treeCache; + this.branchId = row.branchId; + this.noteId = row.noteId; + this.note = null; + this.parentNoteId = row.parentNoteId; + this.notePosition = row.notePosition; + this.prefix = row.prefix; + this.isExpanded = row.isExpanded; + } + + async getNote() { + return this.treeCache.getNote(this.noteId); + } + + get toString() { + return `Branch(branchId=${this.branchId})`; + } +} + +export default Branch; \ No newline at end of file diff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.js new file mode 100644 index 000000000..fc1457e3a --- /dev/null +++ b/src/public/javascripts/entities/note_short.js @@ -0,0 +1,45 @@ +class NoteShort { + constructor(treeCache, row) { + this.treeCache = treeCache; + this.noteId = row.noteId; + this.title = row.title; + this.isProtected = row.isProtected; + this.type = row.type; + this.mime = row.mime; + this.hideInAutocomplete = row.hideInAutocomplete; + } + + async getBranches() { + const branches = []; + + for (const parent of this.treeCache.parents[this.noteId]) { + branches.push(await this.treeCache.getBranchByChildParent(this.noteId, p.noteId)); + } + + return branches; + } + + async getChildBranches() { + const branches = []; + + for (const child of this.treeCache.children[this.noteId]) { + branches.push(await this.treeCache.getBranchByChildParent(child.noteId, this.noteId)); + } + + return branches; + } + + async getParentNotes() { + return this.treeCache.parents[this.noteId] || []; + } + + async getChildNotes() { + return this.treeCache.children[this.noteId] || []; + } + + get toString() { + return `Note(noteId=${this.noteId}, title=${this.title})`; + } +} + +export default NoteShort; \ No newline at end of file diff --git a/src/public/javascripts/note_tree.js b/src/public/javascripts/note_tree.js index 1fd0cb8fd..e2173c504 100644 --- a/src/public/javascripts/note_tree.js +++ b/src/public/javascripts/note_tree.js @@ -11,134 +11,7 @@ import treeUtils from './tree_utils.js'; import utils from './utils.js'; import server from './server.js'; import recentNotes from './dialogs/recent_notes.js'; - -class TreeCache { - constructor(noteRows, branchRows) { - this.parents = []; - this.children = []; - this.childParentToBranch = {}; - - /** @type {Object.} */ - this.notes = {}; - for (const noteRow of noteRows) { - const note = new NoteShort(this, noteRow); - - this.notes[note.noteId] = note; - } - - /** @type {Object.} */ - this.branches = {}; - for (const branchRow of branchRows) { - const branch = new Branch(this, branchRow); - - this.addBranch(branch); - } - } - - getNote(noteId) { - return this.notes[noteId]; - } - - addBranch(branch) { - this.branches[branch.branchId] = branch; - - this.parents[branch.noteId] = this.parents[branch.noteId] || []; - this.parents[branch.noteId].push(this.notes[branch.parentNoteId]); - - this.children[branch.parentNoteId] = this.children[branch.parentNoteId] || []; - this.children[branch.parentNoteId].push(this.notes[branch.noteId]); - - this.childParentToBranch[branch.noteId + '-' + branch.parentNoteId] = branch; - } - - add(note, branch) { - this.notes[note.noteId] = note; - - this.addBranch(branch); - } - - getBranch(branchId) { - return this.branches[branchId]; - } - - getBranchByChildParent(childNoteId, parentNoteId) { - const key = (childNoteId + '-' + parentNoteId); - const branch = this.childParentToBranch[key]; - - if (!branch) { - utils.throwError("Cannot find branch for child-parent=" + key); - } - - return branch; - } -} - -class NoteShort { - constructor(treeCache, row) { - this.treeCache = treeCache; - this.noteId = row.noteId; - this.title = row.title; - this.isProtected = row.isProtected; - this.type = row.type; - this.mime = row.mime; - this.hideInAutocomplete = row.hideInAutocomplete; - } - - async getBranches() { - const branches = []; - - for (const parent of this.treeCache.parents[this.noteId]) { - branches.push(await this.treeCache.getBranchByChildParent(this.noteId, p.noteId)); - } - - return branches; - } - - async getChildBranches() { - const branches = []; - - for (const child of this.treeCache.children[this.noteId]) { - branches.push(await this.treeCache.getBranchByChildParent(child.noteId, this.noteId)); - } - - return branches; - } - - async getParentNotes() { - return this.treeCache.parents[this.noteId] || []; - } - - async getChildNotes() { - return this.treeCache.children[this.noteId] || []; - } - - get toString() { - return `Note(noteId=${this.noteId}, title=${this.title})`; - } -} - -class Branch { - constructor(treeCache, row) { - this.treeCache = treeCache; - this.branchId = row.branchId; - this.noteId = row.noteId; - this.note = null; - this.parentNoteId = row.parentNoteId; - this.notePosition = row.notePosition; - this.prefix = row.prefix; - this.isExpanded = row.isExpanded; - } - - async getNote() { - return this.treeCache.getNote(this.noteId); - } - - get toString() { - return `Branch(branchId=${this.branchId})`; - } -} - -let treeCache; +import treeCache from './tree_cache.js'; const $tree = $("#tree"); const $parentList = $("#parent-list"); @@ -244,7 +117,7 @@ function setParentChildRelation(branchId, parentNoteId, childNoteId) { async function prepareBranch(noteRows, branchRows) { utils.assertArguments(noteRows); - treeCache = new TreeCache(noteRows, branchRows); + treeCache.load(noteRows, branchRows); return await prepareBranchInner(treeCache.getNote('root')); } diff --git a/src/public/javascripts/tree_cache.js b/src/public/javascripts/tree_cache.js new file mode 100644 index 000000000..387956cd2 --- /dev/null +++ b/src/public/javascripts/tree_cache.js @@ -0,0 +1,68 @@ +import utils from "./utils.js"; +import Branch from "./entities/branch.js"; +import NoteShort from "./entities/note_short.js"; + +class TreeCache { + load(noteRows, branchRows) { + this.parents = []; + this.children = []; + this.childParentToBranch = {}; + + /** @type {Object.} */ + this.notes = {}; + for (const noteRow of noteRows) { + const note = new NoteShort(this, noteRow); + + this.notes[note.noteId] = note; + } + + /** @type {Object.} */ + this.branches = {}; + for (const branchRow of branchRows) { + const branch = new Branch(this, branchRow); + + this.addBranch(branch); + } + } + + getNote(noteId) { + return this.notes[noteId]; + } + + addBranch(branch) { + this.branches[branch.branchId] = branch; + + this.parents[branch.noteId] = this.parents[branch.noteId] || []; + this.parents[branch.noteId].push(this.notes[branch.parentNoteId]); + + this.children[branch.parentNoteId] = this.children[branch.parentNoteId] || []; + this.children[branch.parentNoteId].push(this.notes[branch.noteId]); + + this.childParentToBranch[branch.noteId + '-' + branch.parentNoteId] = branch; + } + + add(note, branch) { + this.notes[note.noteId] = note; + + this.addBranch(branch); + } + + getBranch(branchId) { + return this.branches[branchId]; + } + + getBranchByChildParent(childNoteId, parentNoteId) { + const key = (childNoteId + '-' + parentNoteId); + const branch = this.childParentToBranch[key]; + + if (!branch) { + utils.throwError("Cannot find branch for child-parent=" + key); + } + + return branch; + } +} + +const treeCache = new TreeCache(); + +export default treeCache; \ No newline at end of file