Skip to content

Build your personal knowledge base with Trilium Notes

split tree_cache and entities from note_tree

azivnerf52d7e3c287c
diff --git a/src/public/javascripts/entities/branch.js b/src/public/javascripts/entities/branch.js
new file mode 100644
index 0000000000..375c676c6c
--- /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 0000000000..fc1457e3a4
--- /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 1fd0cb8fd1..e2173c504c 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.<string, NoteShort>} */
- this.notes = {};
- for (const noteRow of noteRows) {
- const note = new NoteShort(this, noteRow);
-
- this.notes[note.noteId] = note;
- }
-
- /** @type {Object.<string, Branch>} */
- 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 0000000000..387956cd2c
--- /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.<string, NoteShort>} */
+ this.notes = {};
+ for (const noteRow of noteRows) {
+ const note = new NoteShort(this, noteRow);
+
+ this.notes[note.noteId] = note;
+ }
+
+ /** @type {Object.<string, Branch>} */
+ 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