trilium/src/public/javascripts/services/tree_builder.js

153 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-03-27 11:18:50 +08:00
import utils from "./utils.js";
import treeCache from "./tree_cache.js";
2019-08-27 02:21:43 +08:00
import ws from "./ws.js";
2018-12-12 04:53:56 +08:00
import hoistedNoteService from "./hoisted_note.js";
2018-03-27 11:18:50 +08:00
async function prepareTree() {
2018-12-12 04:53:56 +08:00
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
2018-12-13 03:39:56 +08:00
let hoistedBranch;
if (hoistedNoteId === 'root') {
hoistedBranch = treeCache.getBranch('root');
2018-12-13 03:39:56 +08:00
}
else {
const hoistedNote = await treeCache.getNote(hoistedNoteId);
hoistedBranch = (await hoistedNote.getBranches())[0];
}
2018-12-12 04:53:56 +08:00
return [ await prepareNode(hoistedBranch) ];
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);
}
}
const NOTE_TYPE_ICONS = {
"file": "jam jam-attachment",
"image": "jam jam-picture",
"code": "jam jam-terminal",
"render": "jam jam-play",
"search": "jam jam-search-folder",
"relation-map": "jam jam-map",
"book": "jam jam-book"
};
async function getIcon(note) {
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
if (note.noteId === 'root') {
return "jam jam-chevrons-right";
}
else if (note.noteId === hoistedNoteId) {
return "jam jam-arrow-up";
}
else if (note.type === 'text') {
if (note.hasChildren()) {
return "jam jam-folder";
}
else {
return "jam jam-file";
}
}
else {
return NOTE_TYPE_ICONS[note.type];
}
}
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 hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
2018-05-27 04:16:34 +08:00
const node = {
noteId: note.noteId,
parentNoteId: branch.parentNoteId,
branchId: branch.branchId,
isProtected: note.isProtected,
title: utils.escapeHtml(title),
extraClasses: await getExtraClasses(note),
icon: await getIcon(note),
2018-05-27 04:16:34 +08:00
refKey: note.noteId,
expanded: branch.isExpanded || hoistedNoteId === note.noteId,
2019-06-29 03:50:15 +08:00
lazy: true,
key: utils.randomString(12) // this should prevent some "duplicate key" errors
2018-05-27 04:16:34 +08:00
};
if (note.hasChildren() || note.type === 'search') {
node.folder = 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) {
2019-08-27 02:21:43 +08:00
ws.logError(`No children for ${parentNote}. This shouldn't happen.`);
2018-03-27 11:18:50 +08:00
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) {
await treeCache.reloadNotes([note.noteId]);
2018-03-27 11:18:50 +08:00
2019-10-28 02:17:32 +08:00
const newNote = await treeCache.getNote(note.noteId);
2018-03-27 11:18:50 +08:00
2019-10-28 02:17:32 +08:00
return await prepareRealBranch(newNote);
2018-03-27 11:18:50 +08:00
}
async function getExtraClasses(note) {
utils.assertArguments(note);
const extraClasses = [];
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");
}
2018-08-13 16:59:31 +08:00
if (note.cssClass) {
extraClasses.push(note.cssClass);
}
extraClasses.push(utils.getNoteTypeClass(note.type));
2018-03-27 11:18:50 +08:00
if (note.mime) { // some notes should not have mime type (e.g. render)
extraClasses.push(utils.getMimeTypeClass(note.mime));
}
2019-10-16 01:16:44 +08:00
if (note.archived) {
extraClasses.push("archived");
}
2018-03-27 11:18:50 +08:00
return extraClasses.join(" ");
}
export default {
prepareTree,
prepareBranch,
2018-11-14 15:42:00 +08:00
getExtraClasses,
getIcon
2018-03-27 11:18:50 +08:00
}