2018-03-27 11:18:50 +08:00
|
|
|
import noteDetailService from "./note_detail.js";
|
|
|
|
import utils from "./utils.js";
|
|
|
|
import Branch from "../entities/branch.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import treeCache from "./tree_cache.js";
|
|
|
|
import messagingService from "./messaging.js";
|
|
|
|
|
2018-04-17 11:13:33 +08:00
|
|
|
async function prepareTree(noteRows, branchRows, relations) {
|
|
|
|
utils.assertArguments(noteRows, branchRows, relations);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2018-04-17 11:13:33 +08:00
|
|
|
treeCache.load(noteRows, branchRows, relations);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2018-05-27 04:16:34 +08:00
|
|
|
return [ await prepareNode(await treeCache.getBranch('root')) ];
|
2018-03-27 11:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareBranch(note) {
|
|
|
|
if (note.type === 'search') {
|
|
|
|
return await prepareSearchBranch(note);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return await prepareRealBranch(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 04:16:34 +08:00
|
|
|
async function prepareNode(branch) {
|
|
|
|
const note = await branch.getNote();
|
|
|
|
const title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
|
|
|
|
|
|
|
|
const node = {
|
|
|
|
noteId: note.noteId,
|
|
|
|
parentNoteId: branch.parentNoteId,
|
|
|
|
branchId: branch.branchId,
|
|
|
|
isProtected: note.isProtected,
|
|
|
|
title: utils.escapeHtml(title),
|
|
|
|
extraClasses: await getExtraClasses(note),
|
|
|
|
refKey: note.noteId,
|
|
|
|
expanded: note.type !== 'search' && branch.isExpanded
|
|
|
|
};
|
|
|
|
|
|
|
|
if (note.hasChildren() || note.type === 'search') {
|
|
|
|
node.folder = true;
|
|
|
|
|
|
|
|
if (node.expanded && note.type !== 'search') {
|
|
|
|
node.children = await prepareRealBranch(note);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.lazy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-03-27 11:18:50 +08:00
|
|
|
async function prepareRealBranch(parentNote) {
|
|
|
|
utils.assertArguments(parentNote);
|
|
|
|
|
|
|
|
const childBranches = await parentNote.getChildBranches();
|
|
|
|
|
|
|
|
if (!childBranches) {
|
|
|
|
messagingService.logError(`No children for ${parentNote}. This shouldn't happen.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const noteList = [];
|
|
|
|
|
|
|
|
for (const branch of childBranches) {
|
2018-05-27 04:16:34 +08:00
|
|
|
const node = await prepareNode(branch);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
|
|
|
noteList.push(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return noteList;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareSearchBranch(note) {
|
|
|
|
const fullNote = await noteDetailService.loadNote(note.noteId);
|
2018-06-05 11:21:45 +08:00
|
|
|
const results = await server.get('search/' + encodeURIComponent(fullNote.jsonContent.searchString));
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2018-06-06 10:47:47 +08:00
|
|
|
const noteIds = results.map(res => res.noteId);
|
|
|
|
|
|
|
|
// force to load all the notes at once instead of one by one
|
|
|
|
await treeCache.getNotes(noteIds);
|
|
|
|
|
2018-06-05 11:21:45 +08:00
|
|
|
for (const result of results) {
|
2018-06-06 10:47:47 +08:00
|
|
|
const origBranch = await treeCache.getBranch(result.branchId);
|
|
|
|
|
2018-03-27 11:18:50 +08:00
|
|
|
const branch = new Branch(treeCache, {
|
|
|
|
branchId: "virt" + utils.randomString(10),
|
2018-06-05 11:21:45 +08:00
|
|
|
noteId: result.noteId,
|
2018-03-27 11:18:50 +08:00
|
|
|
parentNoteId: note.noteId,
|
2018-06-06 10:47:47 +08:00
|
|
|
prefix: origBranch.prefix,
|
2018-03-27 11:18:50 +08:00
|
|
|
virtual: true
|
|
|
|
});
|
|
|
|
|
|
|
|
treeCache.addBranch(branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await prepareRealBranch(fullNote);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getExtraClasses(note) {
|
|
|
|
utils.assertArguments(note);
|
|
|
|
|
|
|
|
const extraClasses = [];
|
|
|
|
|
2018-05-27 04:16:34 +08:00
|
|
|
if (note.noteId === 'root') {
|
|
|
|
extraClasses.push("tree-root");
|
|
|
|
}
|
|
|
|
|
2018-03-27 11:18:50 +08:00
|
|
|
if (note.isProtected) {
|
|
|
|
extraClasses.push("protected");
|
|
|
|
}
|
|
|
|
|
2018-04-17 11:34:56 +08:00
|
|
|
if (note.getParentNoteIds().length > 1) {
|
2018-03-27 11:18:50 +08:00
|
|
|
extraClasses.push("multiple-parents");
|
|
|
|
}
|
|
|
|
|
|
|
|
extraClasses.push(note.type);
|
|
|
|
|
|
|
|
return extraClasses.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
prepareTree,
|
|
|
|
prepareBranch,
|
|
|
|
getExtraClasses
|
|
|
|
}
|