tree change listener and others

This commit is contained in:
zadam 2020-01-29 21:38:58 +01:00
parent 439b45b0b8
commit b65631be7e
10 changed files with 140 additions and 49 deletions

View File

@ -1,5 +1,7 @@
export class LoadResults { export class LoadResults {
constructor() { constructor(treeCache) {
this.treeCache = treeCache;
this.noteIdToSourceId = {}; this.noteIdToSourceId = {};
this.sourceIdToNoteIds = {}; this.sourceIdToNoteIds = {};
@ -25,14 +27,34 @@ export class LoadResults {
this.branchIdToSourceId[branchId].push(sourceId); this.branchIdToSourceId[branchId].push(sourceId);
} }
getBranches() {
}
addNoteReordering(parentNoteId, sourceId) { addNoteReordering(parentNoteId, sourceId) {
} }
getNoteReorderings() {
}
addAttribute(attributeId, sourceId) { addAttribute(attributeId, sourceId) {
} }
getAttributes() {
}
addNoteRevision(noteRevisionId, noteId, sourceId) {
}
hasNoteRevisionForNote(noteId) {
}
getNoteIds() { getNoteIds() {
return Object.keys(this.noteIdToSourceId); return Object.keys(this.noteIdToSourceId);
} }

View File

@ -412,7 +412,7 @@ async function createNote(node, parentNoteId, target, extraOptions = {}) {
await newParentNode.load(true); // force reload to show up new note await newParentNode.load(true); // force reload to show up new note
await appContext.getMainNoteTree().checkFolderStatus(newParentNode); await appContext.getMainNoteTree().updateNode(newParentNode);
} }
return {note, branch}; return {note, branch};

View File

@ -230,10 +230,11 @@ class TreeCache {
return child.parentToBranch[parentNoteId]; return child.parentToBranch[parentNoteId];
} }
// FIXME does not actually belong here
async processSyncRows(syncRows) { async processSyncRows(syncRows) {
const noteIdsToReload = []; const noteIdsToReload = [];
const loadResults = new LoadResults(); const loadResults = new LoadResults(this);
syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => { syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => {
noteIdsToReload.push(sync.parentNoteId); noteIdsToReload.push(sync.parentNoteId);
@ -261,6 +262,11 @@ class TreeCache {
loadResults.addAttribute(sync.entityId, sync.sourceId); loadResults.addAttribute(sync.entityId, sync.sourceId);
}); });
// missing reloading the relation target note
syncRows.filter(sync => sync.entityName === 'note_revisions').forEach(sync => {
loadResults.addNoteRevision(sync.entityId, sync.noteId, sync.sourceId);
});
await this.reloadNotes(noteIdsToReload); await this.reloadNotes(noteIdsToReload);
const appContext = (await import('./app_context.js')).default; const appContext = (await import('./app_context.js')).default;

View File

@ -88,11 +88,9 @@ class AttributesWidget extends StandardWidget {
} }
} }
syncDataListener({data}) { entitiesReloadedListener({loadResults}) {
if (data.find(sd => sd.entityName === 'attributes' && sd.noteId === this.tabContext.note.noteId)) { if (loadResults.getAttributes().find(attr => attr.type === 'relation' && attr.noteId === this.noteId)) {
// no need to invalidate attributes since the Attribute class listens to this as well this.refresh();
// (and is guaranteed to run first)
this.doRenderBody();
} }
} }
} }

View File

@ -54,13 +54,9 @@ class LinkMapWidget extends StandardWidget {
} }
} }
syncDataListener({data}) { entitiesReloadedListener({loadResults}) {
if (data.find(sd => sd.entityName === 'attributes' && sd.noteId === this.tabContext.note.noteId)) { if (loadResults.getAttributes().find(attr => attr.type === 'relation' && (attr.noteId === this.noteId || attr.value === this.noteId))) {
// no need to invalidate attributes since the Attribute class listens to this as well this.refresh();
// (and is guaranteed to run first)
if (this.linkMapService) {
this.linkMapService.loadNotesAndRelations();
}
} }
} }
} }

View File

@ -220,9 +220,9 @@ export default class NoteDetailWidget extends TabAwareWidget {
this.refresh(); this.refresh();
} }
entitiesReloadedListener(data) { entitiesReloadedListener({loadResults}) {
super.entitiesReloadedListener(data); if (loadResults.isNoteReloaded(this.noteId, this.componentId)) {
this.refreshWithNote(this.note, this.notePath);
return false; // stop propagation to children }
} }
} }

View File

@ -55,6 +55,12 @@ class NoteRevisionsWidget extends StandardWidget {
} }
} }
entitiesReloadedListener({loadResults}) {
if (loadResults.hasNoteRevisionForNote(this.noteId)) {
this.refresh();
}
}
syncDataListener({data}) { syncDataListener({data}) {
if (data.find(sd => sd.entityName === 'note_revisions' && sd.noteId === this.tabContext.note.noteId)) { if (data.find(sd => sd.entityName === 'note_revisions' && sd.noteId === this.tabContext.note.noteId)) {
this.refresh(); this.refresh();

View File

@ -331,7 +331,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
await parentNode.setExpanded(true, expandOpts); await parentNode.setExpanded(true, expandOpts);
} }
await this.checkFolderStatus(parentNode); await this.updateNode(parentNode);
let foundChildNode = this.findChildNode(parentNode, childNoteId); let foundChildNode = this.findChildNode(parentNode, childNoteId);
@ -372,12 +372,16 @@ export default class NoteTreeWidget extends TabAwareWidget {
return this.getNodeFromPath(notePath, true, expandOpts); return this.getNodeFromPath(notePath, true, expandOpts);
} }
async checkFolderStatus(node) { async updateNode(node) {
const note = await treeCache.getNote(node.data.noteId); const note = await treeCache.getNote(node.data.noteId);
const branch = treeCache.getBranch(node.data.branchId);
node.data.isProtected = note.isProtected;
node.data.noteType = note.type;
node.folder = note.type === 'search' || note.getChildNoteIds().length > 0; node.folder = note.type === 'search' || note.getChildNoteIds().length > 0;
node.icon = await treeBuilder.getIcon(note); node.icon = await treeBuilder.getIcon(note);
node.extraClasses = await treeBuilder.getExtraClasses(note); node.extraClasses = await treeBuilder.getExtraClasses(note);
node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
node.renderTitle(); node.renderTitle();
} }
@ -428,20 +432,93 @@ export default class NoteTreeWidget extends TabAwareWidget {
} }
async entitiesReloadedListener({loadResults}) { async entitiesReloadedListener({loadResults}) {
const activeNode = this.getActiveNode(); const noteIdsToUpdate = new Set();
const activateNotePath = activeNode ? await treeService.getNotePath(activeNode) : null; const noteIdsToReload = new Set();
for (const noteId of loadResults.getNoteIds()) { for (const attr of loadResults.getAttributes()) {
for (const node of this.getNodesByNoteId(noteId)) { if (attr.type === 'label' && ['iconClass', 'cssClass'].includes(attr.name)) {
const branch = treeCache.getBranch(node.data.branchId, true); if (attr.isInheritable) {
noteIdsToReload.add(attr.noteId);
}
else {
noteIdsToUpdate.add(attr.noteId);
}
}
else if (attr.type === 'relation' && attr.name === 'template') {
// missing handling of things inherited from template
noteIdsToReload.add(attr.noteId);
}
}
for (const branch of loadResults.getBranches()) {
for (const node of this.getNodesByBranchId(branch.branchId)) {
if (branch.isDeleted) {
if (node.isActive()) {
let newActive = node.getNextSibling();
if (!newActive) {
newActive = node.getPrevSibling();
}
if (!newActive) {
newActive = node.getParent();
}
await newActive.setActive(true, {noEvents: true});
}
if (!branch) {
node.remove(); node.remove();
} }
else { else {
noteIdsToUpdate.add(branch.noteId);
}
}
if (!branch.isDeleted) {
for (const parentNode of this.getNodesByNoteId(branch.parentNoteId)) {
if (!parentNode.isLoaded()) {
continue;
}
const found = parentNode.getChildren().find(child => child.data.noteId === branch.noteId);
if (!found) {
noteIdsToReload.add(branch.parentNoteId);
}
}
}
}
const activeNode = this.getActiveNode();
const activateNotePath = activeNode ? await treeService.getNotePath(activeNode) : null;
for (const noteId of noteIdsToReload) {
for (const node of this.getNodesByNoteId(noteId)) {
await node.load(true); await node.load(true);
await this.checkFolderStatus(node); await this.updateNode(node);
}
}
for (const noteId of noteIdsToReload) {
for (const node of this.getNodesByNoteId(noteId)) {
await this.updateNode(node);
}
}
for (const {parentNoteId} of loadResults.getNoteReorderings()) {
for (const node of this.getNodesByNoteId(parentNoteId)) {
if (node.isLoaded()) {
node.sortChildren((nodeA, nodeB) => {
const branchA = treeCache.branches[nodeA.data.branchId];
const branchB = treeCache.branches[nodeB.data.branchId];
if (!branchA || !branchB) {
return 0;
}
return branchA.notePosition - branchB.notePosition;
});
} }
} }
} }
@ -500,20 +577,6 @@ export default class NoteTreeWidget extends TabAwareWidget {
await server.put('branches/' + branchId + '/expanded/' + expandedNum); await server.put('branches/' + branchId + '/expanded/' + expandedNum);
} }
// async reloadNotesListener({noteIds, activateNotePath = null}) {
// if (noteIds.length === 0) {
// return;
// }
//
// await treeCache.reloadNotes(noteIds);
//
// if (!activateNotePath) {
// activateNotePath = appContext.getActiveTabNotePath();
// }
//
// appContext.trigger('entitiesReloaded', { noteIds, activateNotePath });
// }
async reloadTreeListener() { async reloadTreeListener() {
const notes = await treeService.loadTreeData(); const notes = await treeService.loadTreeData();

View File

@ -59,10 +59,4 @@ export default class TabAwareWidget extends BasicWidget {
this.activeTabChanged(); this.activeTabChanged();
} }
entitiesReloadedListener({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId, this.componentId)) {
this.refreshWithNote(this.note, this.notePath);
}
}
} }

View File

@ -49,6 +49,12 @@ class WhatLinksHereWidget extends StandardWidget {
this.$body.empty().append($list); this.$body.empty().append($list);
} }
entitiesReloadedListener({loadResults}) {
if (loadResults.getAttributes().find(attr => attr.type === 'relation' && attr.value === this.noteId)) {
this.refresh();
}
}
} }
export default WhatLinksHereWidget; export default WhatLinksHereWidget;