Notes/src/routes/api/tree.js

145 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
2020-12-11 22:06:12 +01:00
const noteCache = require('../../services/note_cache/note_cache');
2020-06-20 12:31:38 +02:00
function getNotesAndBranchesAndAttributes(noteIds) {
2020-12-11 22:06:12 +01:00
noteIds = new Set(noteIds);
const collectedNoteIds = new Set();
const collectedAttributeIds = new Set();
const collectedBranchIds = new Set();
function collectEntityIds(note) {
if (collectedNoteIds.has(note.noteId)) {
return;
}
collectedNoteIds.add(note.noteId);
for (const branch of note.parentBranches) {
collectedBranchIds.add(branch.branchId);
collectEntityIds(branch.parentNote);
}
2020-12-13 23:27:42 +01:00
for (const childNote of note.children) {
const childBranch = noteCache.getBranch(childNote.noteId, note.noteId);
collectedBranchIds.add(childBranch.branchId);
}
2020-12-11 22:06:12 +01:00
for (const attr of note.ownedAttributes) {
collectedAttributeIds.add(attr.attributeId);
if (attr.type === 'relation' && attr.name === 'template') {
collectEntityIds(attr.targetNote);
}
}
}
for (const noteId of noteIds) {
const note = noteCache.notes[noteId];
if (!note) {
continue;
}
collectEntityIds(note);
}
const notes = [];
for (const noteId of collectedNoteIds) {
const note = noteCache.notes[noteId];
notes.push({
noteId: note.noteId,
title: note.title,
isProtected: note.isProtected,
type: note.type,
2020-12-14 23:04:56 +01:00
mime: note.mime
2020-12-11 22:06:12 +01:00
});
}
const branches = [];
2020-12-11 15:27:57 +01:00
if (noteIds.has('root')) {
branches.push({
branchId: 'root',
noteId: 'root',
parentNoteId: 'none',
notePosition: 0,
prefix: '',
isExpanded: true
});
}
2020-12-11 22:06:12 +01:00
for (const branchId of collectedBranchIds) {
const branch = noteCache.branches[branchId];
branches.push({
branchId: branch.branchId,
noteId: branch.noteId,
parentNoteId: branch.parentNoteId,
notePosition: branch.notePosition,
prefix: branch.prefix,
isExpanded: branch.isExpanded
});
}
const attributes = [];
for (const attributeId of collectedAttributeIds) {
const attribute = noteCache.attributes[attributeId];
attributes.push({
attributeId: attribute.attributeId,
noteId: attribute.noteId,
type: attribute.type,
name: attribute.name,
value: attribute.value,
position: attribute.position,
isInheritable: attribute.isInheritable
});
}
return {
branches,
2020-01-25 13:46:55 +01:00
notes,
attributes
};
}
2020-08-26 16:50:16 +02:00
function getTree(req) {
2020-11-22 23:05:02 +01:00
const subTreeNoteId = req.query.subTreeNoteId || 'root';
2020-12-13 23:27:42 +01:00
const collectedNoteIds = new Set([subTreeNoteId]);
2020-12-11 22:06:12 +01:00
function collect(parentNote) {
2020-12-16 14:36:24 +01:00
if (!parentNote) {
console.trace(parentNote);
}
2020-12-13 23:27:42 +01:00
for (const childNote of parentNote.children) {
2020-12-11 22:06:12 +01:00
collectedNoteIds.add(childNote.noteId);
const childBranch = noteCache.getBranch(childNote.noteId, parentNote.noteId);
if (childBranch.isExpanded) {
2020-12-13 23:27:42 +01:00
collect(childBranch.childNote);
2020-12-11 22:06:12 +01:00
}
}
}
collect(noteCache.notes[subTreeNoteId]);
2018-12-11 21:53:56 +01:00
2020-12-11 22:06:12 +01:00
return getNotesAndBranchesAndAttributes(collectedNoteIds);
}
2020-06-20 12:31:38 +02:00
function load(req) {
return getNotesAndBranchesAndAttributes(req.body.noteIds);
2018-03-30 12:57:22 -04:00
}
2018-03-30 12:57:22 -04:00
module.exports = {
getTree,
load
2018-03-30 12:57:22 -04:00
};