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 {
constructor() {
constructor(treeCache) {
this.treeCache = treeCache;
this.noteIdToSourceId = {};
this.sourceIdToNoteIds = {};
@ -25,14 +27,34 @@ export class LoadResults {
this.branchIdToSourceId[branchId].push(sourceId);
}
getBranches() {
}
addNoteReordering(parentNoteId, sourceId) {
}
getNoteReorderings() {
}
addAttribute(attributeId, sourceId) {
}
getAttributes() {
}
addNoteRevision(noteRevisionId, noteId, sourceId) {
}
hasNoteRevisionForNote(noteId) {
}
getNoteIds() {
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 appContext.getMainNoteTree().checkFolderStatus(newParentNode);
await appContext.getMainNoteTree().updateNode(newParentNode);
}
return {note, branch};

View File

@ -230,10 +230,11 @@ class TreeCache {
return child.parentToBranch[parentNoteId];
}
// FIXME does not actually belong here
async processSyncRows(syncRows) {
const noteIdsToReload = [];
const loadResults = new LoadResults();
const loadResults = new LoadResults(this);
syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => {
noteIdsToReload.push(sync.parentNoteId);
@ -261,6 +262,11 @@ class TreeCache {
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);
const appContext = (await import('./app_context.js')).default;

View File

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

View File

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

View File

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

View File

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

View File

@ -331,7 +331,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
await parentNode.setExpanded(true, expandOpts);
}
await this.checkFolderStatus(parentNode);
await this.updateNode(parentNode);
let foundChildNode = this.findChildNode(parentNode, childNoteId);
@ -372,12 +372,16 @@ export default class NoteTreeWidget extends TabAwareWidget {
return this.getNodeFromPath(notePath, true, expandOpts);
}
async checkFolderStatus(node) {
async updateNode(node) {
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.icon = await treeBuilder.getIcon(note);
node.extraClasses = await treeBuilder.getExtraClasses(note);
node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
node.renderTitle();
}
@ -428,20 +432,93 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
async entitiesReloadedListener({loadResults}) {
const activeNode = this.getActiveNode();
const activateNotePath = activeNode ? await treeService.getNotePath(activeNode) : null;
const noteIdsToUpdate = new Set();
const noteIdsToReload = new Set();
for (const noteId of loadResults.getNoteIds()) {
for (const node of this.getNodesByNoteId(noteId)) {
const branch = treeCache.getBranch(node.data.branchId, true);
for (const attr of loadResults.getAttributes()) {
if (attr.type === 'label' && ['iconClass', 'cssClass'].includes(attr.name)) {
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();
}
else {
await node.load(true);
noteIdsToUpdate.add(branch.noteId);
}
}
await this.checkFolderStatus(node);
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 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);
}
// 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() {
const notes = await treeService.loadTreeData();

View File

@ -59,10 +59,4 @@ export default class TabAwareWidget extends BasicWidget {
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);
}
entitiesReloadedListener({loadResults}) {
if (loadResults.getAttributes().find(attr => attr.type === 'relation' && attr.value === this.noteId)) {
this.refresh();
}
}
}
export default WhatLinksHereWidget;