mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
lazy loading of tree
This commit is contained in:
parent
2f2969b2a1
commit
7b2c79b754
@ -6,6 +6,8 @@ const noteTree = (function() {
|
|||||||
let startNoteId = null;
|
let startNoteId = null;
|
||||||
let treeLoadTime = null;
|
let treeLoadTime = null;
|
||||||
let clipboardNoteId = null;
|
let clipboardNoteId = null;
|
||||||
|
let notesMap = {};
|
||||||
|
let parentToNotes = {};
|
||||||
|
|
||||||
function getTreeLoadTime() {
|
function getTreeLoadTime() {
|
||||||
return treeLoadTime;
|
return treeLoadTime;
|
||||||
@ -19,8 +21,16 @@ const noteTree = (function() {
|
|||||||
clipboardNoteId = cbNoteId;
|
clipboardNoteId = cbNoteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareNoteTree(notes) {
|
function prepareNoteTree() {
|
||||||
for (const note of notes) {
|
return prepareNoteTreeInner(parentToNotes['root']);
|
||||||
|
}
|
||||||
|
|
||||||
|
function prepareNoteTreeInner(noteIds) {
|
||||||
|
const noteList = [];
|
||||||
|
|
||||||
|
for (const noteId of noteIds) {
|
||||||
|
const note = notesMap[noteId];
|
||||||
|
|
||||||
glob.allNoteIds.push(note.note_id);
|
glob.allNoteIds.push(note.note_id);
|
||||||
|
|
||||||
note.title = note.note_title;
|
note.title = note.note_title;
|
||||||
@ -28,19 +38,25 @@ const noteTree = (function() {
|
|||||||
if (note.is_protected) {
|
if (note.is_protected) {
|
||||||
note.extraClasses = "protected";
|
note.extraClasses = "protected";
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
if (note.is_clone) {
|
|
||||||
note.title += " (clone)";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
note.key = note.note_id;
|
note.key = note.note_id;
|
||||||
note.expanded = note.is_expanded;
|
note.expanded = note.is_expanded;
|
||||||
|
|
||||||
if (note.children && note.children.length > 0) {
|
if (parentToNotes[noteId] && parentToNotes[noteId].length > 0) {
|
||||||
prepareNoteTree(note.children);
|
note.folder = true;
|
||||||
|
|
||||||
|
if (note.expanded) {
|
||||||
|
note.children = prepareNoteTreeInner(parentToNotes[noteId], notesMap, parentToNotes);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
note.lazy = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noteList.push(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return noteList;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExpandedToServer(note_id, is_expanded) {
|
function setExpandedToServer(note_id, is_expanded) {
|
||||||
@ -179,6 +195,16 @@ const noteTree = (function() {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
lazyLoad: function(event, data){
|
||||||
|
const node = data.node;
|
||||||
|
|
||||||
|
if (parentToNotes[node.key]) {
|
||||||
|
data.result = prepareNoteTreeInner(parentToNotes[node.key]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("No children. This shouldn't happen.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -186,15 +212,16 @@ const noteTree = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function reload() {
|
async function reload() {
|
||||||
const treeResp = await loadTree();
|
const notesMap = await loadTree();
|
||||||
|
|
||||||
// this will also reload the note content
|
// this will also reload the note content
|
||||||
await treeEl.fancytree('getTree').reload(treeResp.notes);
|
await treeEl.fancytree('getTree').reload(notesMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadTree() {
|
function loadTree() {
|
||||||
return $.get(baseApiUrl + 'tree').then(resp => {
|
return $.get(baseApiUrl + 'tree').then(resp => {
|
||||||
const notes = resp.notes;
|
notesMap = resp.notes_map;
|
||||||
|
parentToNotes = resp.parent_to_notes;
|
||||||
startNoteId = resp.start_note_id;
|
startNoteId = resp.start_note_id;
|
||||||
treeLoadTime = resp.tree_load_time;
|
treeLoadTime = resp.tree_load_time;
|
||||||
|
|
||||||
@ -202,16 +229,11 @@ const noteTree = (function() {
|
|||||||
startNoteId = document.location.hash.substr(1); // strip initial #
|
startNoteId = document.location.hash.substr(1); // strip initial #
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareNoteTree(notes);
|
return prepareNoteTree();
|
||||||
|
|
||||||
return {
|
|
||||||
notes: notes,
|
|
||||||
startNoteId: startNoteId
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$(() => loadTree().then(resp => initFancyTree(resp.notes)));
|
$(() => loadTree().then(notesMap => initFancyTree(notesMap)));
|
||||||
|
|
||||||
function collapseTree() {
|
function collapseTree() {
|
||||||
treeEl.fancytree("getRootNode").visit(node => {
|
treeEl.fancytree("getRootNode").visit(node => {
|
||||||
|
@ -43,11 +43,6 @@ const treeUtils = (function() {
|
|||||||
return "[unknown]";
|
return "[unknown]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// why?
|
|
||||||
if (note.data.is_clone) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = [];
|
const path = [];
|
||||||
|
|
||||||
while (note) {
|
while (note) {
|
||||||
|
@ -21,8 +21,8 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
|
|||||||
+ "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 "
|
+ "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 "
|
||||||
+ "order by note_pid, note_pos");
|
+ "order by note_pid, note_pos");
|
||||||
|
|
||||||
const root_notes = [];
|
const parentToNotes = {};
|
||||||
const notes_map = {};
|
const notesMap = {};
|
||||||
|
|
||||||
const dataKey = protected_session.getDataKey(req);
|
const dataKey = protected_session.getDataKey(req);
|
||||||
|
|
||||||
@ -31,30 +31,17 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
|
|||||||
note.note_title = data_encryption.decryptCbcString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title);
|
note.note_title = data_encryption.decryptCbcString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
note.children = [];
|
if (!parentToNotes[note.note_pid]) {
|
||||||
|
parentToNotes[note.note_pid] = [];
|
||||||
if (note.note_pid === "root") {
|
|
||||||
root_notes.push(note);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
notes_map[note.note_id] = note;
|
notesMap[note.note_id] = note;
|
||||||
}
|
parentToNotes[note.note_pid].push(note.note_id);
|
||||||
|
|
||||||
for (const note of notes) {
|
|
||||||
if (note.note_pid !== "root") {
|
|
||||||
const parent = notes_map[note.note_pid];
|
|
||||||
|
|
||||||
if (!parent) {
|
|
||||||
log.error("Parent " + note.note_pid + ' has not been found');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
parent.children.push(note);
|
|
||||||
parent.folder = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
notes: root_notes,
|
notes_map: notesMap,
|
||||||
|
parent_to_notes: parentToNotes,
|
||||||
start_note_id: await options.getOption('start_node'),
|
start_note_id: await options.getOption('start_node'),
|
||||||
tree_load_time: utils.nowTimestamp()
|
tree_load_time: utils.nowTimestamp()
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user