mirror of
https://github.com/zadam/trilium.git
synced 2025-10-30 01:36:24 +01:00
moved services into the service directory
This commit is contained in:
68
src/public/javascripts/services/tree_cache.js
Normal file
68
src/public/javascripts/services/tree_cache.js
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user