|
|
|
@ -2,45 +2,82 @@ import utils from "./utils.js";
|
|
|
|
import Branch from "../entities/branch.js";
|
|
|
|
import Branch from "../entities/branch.js";
|
|
|
|
import NoteShort from "../entities/note_short.js";
|
|
|
|
import NoteShort from "../entities/note_short.js";
|
|
|
|
import infoService from "./info.js";
|
|
|
|
import infoService from "./info.js";
|
|
|
|
|
|
|
|
import server from "./server.js";
|
|
|
|
|
|
|
|
|
|
|
|
class TreeCache {
|
|
|
|
class TreeCache {
|
|
|
|
load(noteRows, branchRows) {
|
|
|
|
load(noteRows, branchRows, parentToChildren) {
|
|
|
|
this.parents = [];
|
|
|
|
this.parents = {};
|
|
|
|
this.children = [];
|
|
|
|
this.children = {};
|
|
|
|
this.childParentToBranch = {};
|
|
|
|
this.childParentToBranch = {};
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {Object.<string, NoteShort>} */
|
|
|
|
/** @type {Object.<string, NoteShort>} */
|
|
|
|
this.notes = {};
|
|
|
|
this.notes = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {Object.<string, Branch>} */
|
|
|
|
|
|
|
|
this.branches = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.addResp(noteRows, branchRows, parentToChildren);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addResp(noteRows, branchRows, parentToChildren) {
|
|
|
|
for (const noteRow of noteRows) {
|
|
|
|
for (const noteRow of noteRows) {
|
|
|
|
const note = new NoteShort(this, noteRow);
|
|
|
|
const note = new NoteShort(this, noteRow);
|
|
|
|
|
|
|
|
|
|
|
|
this.notes[note.noteId] = note;
|
|
|
|
this.notes[note.noteId] = note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {Object.<string, Branch>} */
|
|
|
|
|
|
|
|
this.branches = {};
|
|
|
|
|
|
|
|
for (const branchRow of branchRows) {
|
|
|
|
for (const branchRow of branchRows) {
|
|
|
|
const branch = new Branch(this, branchRow);
|
|
|
|
const branch = new Branch(this, branchRow);
|
|
|
|
|
|
|
|
|
|
|
|
this.addBranch(branch);
|
|
|
|
this.addBranch(branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const relation of parentToChildren) {
|
|
|
|
|
|
|
|
this.addBranchRelationship(relation.branchId, relation.childNoteId, relation.parentNoteId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return NoteShort */
|
|
|
|
/** @return NoteShort */
|
|
|
|
async getNote(noteId) {
|
|
|
|
async getNote(noteId) {
|
|
|
|
|
|
|
|
if (this.notes[noteId] === undefined) {
|
|
|
|
|
|
|
|
const resp = await server.post('tree/load', {
|
|
|
|
|
|
|
|
noteIds: [noteId]
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.addResp(resp.notes, resp.branches, resp.parentToChildren);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.notes[noteId]) {
|
|
|
|
|
|
|
|
throw new Error(`Can't find note ${noteId}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.notes[noteId];
|
|
|
|
return this.notes[noteId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addBranch(branch) {
|
|
|
|
addBranch(branch) {
|
|
|
|
this.branches[branch.branchId] = branch;
|
|
|
|
this.branches[branch.branchId] = branch;
|
|
|
|
|
|
|
|
|
|
|
|
this.parents[branch.noteId] = this.parents[branch.noteId] || [];
|
|
|
|
this.addBranchRelationship(branch.branchId, branch.noteId, branch.parentNoteId);
|
|
|
|
this.parents[branch.noteId].push(this.notes[branch.parentNoteId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addBranchRelationship(branchId, childNoteId, parentNoteId) {
|
|
|
|
|
|
|
|
this.addParentChildRelationship(parentNoteId, childNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.childParentToBranch[childNoteId + '-' + parentNoteId] = branchId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.children[branch.parentNoteId] = this.children[branch.parentNoteId] || [];
|
|
|
|
addParentChildRelationship(parentNoteId, childNoteId) {
|
|
|
|
this.children[branch.parentNoteId].push(this.notes[branch.noteId]);
|
|
|
|
this.parents[childNoteId] = this.parents[childNoteId] || [];
|
|
|
|
|
|
|
|
|
|
|
|
this.childParentToBranch[branch.noteId + '-' + branch.parentNoteId] = branch;
|
|
|
|
if (!this.parents[childNoteId].includes(parentNoteId)) {
|
|
|
|
|
|
|
|
this.parents[childNoteId].push(parentNoteId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.children[parentNoteId] = this.children[parentNoteId] || [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.children[parentNoteId].includes(childNoteId)) {
|
|
|
|
|
|
|
|
this.children[parentNoteId].push(childNoteId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
add(note, branch) {
|
|
|
|
add(note, branch) {
|
|
|
|
@ -51,19 +88,34 @@ class TreeCache {
|
|
|
|
|
|
|
|
|
|
|
|
/** @return Branch */
|
|
|
|
/** @return Branch */
|
|
|
|
async getBranch(branchId) {
|
|
|
|
async getBranch(branchId) {
|
|
|
|
|
|
|
|
if (this.branches[branchId] === undefined) {
|
|
|
|
|
|
|
|
const resp = await server.post('tree/load', {
|
|
|
|
|
|
|
|
branchIds: [branchId]
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.addResp(resp.notes, resp.branches, resp.parentToChildren);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.branches[branchId]) {
|
|
|
|
|
|
|
|
throw new Error(`Can't find branch ${branchId}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.branches[branchId];
|
|
|
|
return this.branches[branchId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return Branch */
|
|
|
|
/** @return Branch */
|
|
|
|
async getBranchByChildParent(childNoteId, parentNoteId) {
|
|
|
|
async getBranchByChildParent(childNoteId, parentNoteId) {
|
|
|
|
|
|
|
|
// this will make sure the note and its relationships are loaded
|
|
|
|
|
|
|
|
await this.getNote(parentNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
const key = (childNoteId + '-' + parentNoteId);
|
|
|
|
const key = (childNoteId + '-' + parentNoteId);
|
|
|
|
const branch = this.childParentToBranch[key];
|
|
|
|
const branchId = this.childParentToBranch[key];
|
|
|
|
|
|
|
|
|
|
|
|
if (!branch) {
|
|
|
|
if (!branchId) {
|
|
|
|
infoService.throwError("Cannot find branch for child-parent=" + key);
|
|
|
|
infoService.throwError("Cannot find branch for child-parent=" + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return branch;
|
|
|
|
return await this.getBranch(branchId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Move note from one parent to another. */
|
|
|
|
/* Move note from one parent to another. */
|
|
|
|
@ -78,33 +130,14 @@ class TreeCache {
|
|
|
|
delete treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId]; // this is correct because we know that oldParentId isn't same as newParentId
|
|
|
|
delete treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId]; // this is correct because we know that oldParentId isn't same as newParentId
|
|
|
|
|
|
|
|
|
|
|
|
// remove old associations
|
|
|
|
// remove old associations
|
|
|
|
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== oldParentNoteId);
|
|
|
|
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p !== oldParentNoteId);
|
|
|
|
treeCache.children[oldParentNoteId] = treeCache.children[oldParentNoteId].filter(ch => ch.noteId !== childNoteId);
|
|
|
|
treeCache.children[oldParentNoteId] = treeCache.children[oldParentNoteId].filter(ch => ch !== childNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
// add new associations
|
|
|
|
// add new associations
|
|
|
|
treeCache.parents[childNoteId].push(await treeCache.getNote(newParentNoteId));
|
|
|
|
treeCache.parents[childNoteId].push(newParentNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
treeCache.children[newParentNoteId] = treeCache.children[newParentNoteId] || []; // this might be first child
|
|
|
|
treeCache.children[newParentNoteId] = treeCache.children[newParentNoteId] || []; // this might be first child
|
|
|
|
treeCache.children[newParentNoteId].push(await treeCache.getNote(childNoteId));
|
|
|
|
treeCache.children[newParentNoteId].push(childNoteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeParentChildRelation(parentNoteId, childNoteId) {
|
|
|
|
|
|
|
|
utils.assertArguments(parentNoteId, childNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== parentNoteId);
|
|
|
|
|
|
|
|
treeCache.children[parentNoteId] = treeCache.children[parentNoteId].filter(ch => ch.noteId !== childNoteId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete treeCache.childParentToBranch[childNoteId + '-' + parentNoteId];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async setParentChildRelation(branchId, parentNoteId, childNoteId) {
|
|
|
|
|
|
|
|
treeCache.parents[childNoteId] = treeCache.parents[childNoteId] || [];
|
|
|
|
|
|
|
|
treeCache.parents[childNoteId].push(await treeCache.getNote(parentNoteId));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
treeCache.children[parentNoteId] = treeCache.children[parentNoteId] || [];
|
|
|
|
|
|
|
|
treeCache.children[parentNoteId].push(await treeCache.getNote(childNoteId));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
treeCache.childParentToBranch[childNoteId + '-' + parentNoteId] = await treeCache.getBranch(branchId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|