From 8bf794f13b97e9ff15229c62f8f286abfbdf9aa4 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 30 Jan 2020 22:38:31 +0100 Subject: [PATCH] wip --- src/public/javascripts/dialogs/attributes.js | 3 -- src/public/javascripts/entities/note_short.js | 38 +++++++++---------- .../javascripts/services/load_results.js | 29 +++++++++----- src/public/javascripts/services/tree_cache.js | 30 +++++++++++---- src/public/javascripts/widgets/attributes.js | 6 ++- src/services/ws.js | 2 + 6 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/public/javascripts/dialogs/attributes.js b/src/public/javascripts/dialogs/attributes.js index 35f2eaf5a..4c80537ae 100644 --- a/src/public/javascripts/dialogs/attributes.js +++ b/src/public/javascripts/dialogs/attributes.js @@ -169,9 +169,6 @@ function AttributesModel() { await showAttributes(attributes); toastService.showMessage("Attributes have been saved."); - - // FIXME detail should be also reloaded - appContext.trigger('reloadTree'); }; function addLastEmptyRow() { diff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.js index 1f4645fae..c6f295e6a 100644 --- a/src/public/javascripts/entities/note_short.js +++ b/src/public/javascripts/entities/note_short.js @@ -18,25 +18,6 @@ class NoteShort { constructor(treeCache, row, branches) { this.treeCache = treeCache; - this.update(row, branches); - } - - update(row, branches) { - /** @param {string} */ - this.noteId = row.noteId; - /** @param {string} */ - this.title = row.title; - /** @param {int} */ - this.contentLength = row.contentLength; - /** @param {boolean} */ - this.isProtected = row.isProtected; - /** @param {string} one of 'text', 'code', 'file' or 'render' */ - this.type = row.type; - /** @param {string} content-type, e.g. "application/json" */ - this.mime = row.mime; - /** @param {boolean} */ - this.isDeleted = row.isDeleted; - /** @type {string[]} */ this.attributes = []; @@ -54,6 +35,25 @@ class NoteShort { /** @type {Object.} */ this.childToBranch = {}; + this.update(row, branches); + } + + update(row, branches = []) { + /** @param {string} */ + this.noteId = row.noteId; + /** @param {string} */ + this.title = row.title; + /** @param {int} */ + this.contentLength = row.contentLength; + /** @param {boolean} */ + this.isProtected = row.isProtected; + /** @param {string} one of 'text', 'code', 'file' or 'render' */ + this.type = row.type; + /** @param {string} content-type, e.g. "application/json" */ + this.mime = row.mime; + /** @param {boolean} */ + this.isDeleted = row.isDeleted; + for (const branch of branches) { if (this.noteId === branch.noteId) { this.parents.push(branch.parentNoteId); diff --git a/src/public/javascripts/services/load_results.js b/src/public/javascripts/services/load_results.js index c134de985..a04fae2f7 100644 --- a/src/public/javascripts/services/load_results.js +++ b/src/public/javascripts/services/load_results.js @@ -5,7 +5,13 @@ export class LoadResults { this.noteIdToSourceId = {}; this.sourceIdToNoteIds = {}; - this.branchIdToSourceId = {}; + this.branches = []; + + this.attributes = []; + + this.noteReorderings = []; + + this.noteRevisions = []; } addNote(noteId, sourceId) { @@ -23,36 +29,39 @@ export class LoadResults { } addBranch(branchId, sourceId) { - this.branchIdToSourceId[branchId] = this.branchIdToSourceId[branchId] || []; - this.branchIdToSourceId[branchId].push(sourceId); + this.branches.push({branchId, sourceId}); } getBranches() { - + return this.branches + .map(row => this.treeCache.branches[row.branchId]) + .filter(branch => !!branch); } addNoteReordering(parentNoteId, sourceId) { - + this.noteReorderings.push(parentNoteId); } getNoteReorderings() { - + return this.noteReorderings; } addAttribute(attributeId, sourceId) { - + this.attributes.push({attributeId, sourceId}); } getAttributes() { - + return this.attributes + .map(row => this.treeCache.attributes[row.attributeId]) + .filter(attr => !!attr); } addNoteRevision(noteRevisionId, noteId, sourceId) { - + this.noteRevisions.push({noteRevisionId, noteId, sourceId}); } hasNoteRevisionForNote(noteId) { - + return !!this.noteRevisions.find(nr => nr.noteId === noteId); } getNoteIds() { diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index ac1a267e6..3c8776473 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -235,29 +235,43 @@ class TreeCache { const loadResults = new LoadResults(this); syncRows.filter(sync => sync.entityName === 'notes').forEach(sync => { + const note = this.notes[sync.entityId]; - - loadResults.addNote(sync.entityId, sync.sourceId); + if (note) { + note.update(sync.entity); + loadResults.addNote(sync.entityId, sync.sourceId); + } }); syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => { - noteIdsToReload.push(sync.parentNoteId); - noteIdsToReload.push(sync.noteId); + const branch = this.branches[sync.entityId]; - loadResults.addBranch(sync.entityId, sync.sourceId); + if (branch) { + branch.update(sync.entity); + loadResults.addBranch(sync.entityId, sync.sourceId); + } }); syncRows.filter(sync => sync.entityName === 'note_reordering').forEach(sync => { - noteIdsToReload.push(sync.entityId); + for (const branchId in sync.positions) { + const branch = this.branches[branchId]; + + if (branch) { + branch.notePosition = sync.positions[branchId]; + } + } loadResults.addNoteReordering(sync.entityId, sync.sourceId); }); // missing reloading the relation target note syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => { - noteIdsToReload.push(sync.noteId); + const attribute = this.attributes[sync.entityId]; - loadResults.addAttribute(sync.entityId, sync.sourceId); + if (attribute) { + attribute.update(sync.entity); + loadResults.addAttribute(sync.entityId, sync.sourceId); + } }); // missing reloading the relation target note diff --git a/src/public/javascripts/widgets/attributes.js b/src/public/javascripts/widgets/attributes.js index b18fb6c4d..2cf8fdd8e 100644 --- a/src/public/javascripts/widgets/attributes.js +++ b/src/public/javascripts/widgets/attributes.js @@ -89,7 +89,11 @@ class AttributesWidget extends StandardWidget { } entitiesReloadedListener({loadResults}) { - if (loadResults.getAttributes().find(attr => attr.type === 'relation' && attr.noteId === this.noteId)) { + console.log("CHECK ATTRS", loadResults.getAttributes()); + + if (loadResults.getAttributes().find(attr => attr.noteId === this.noteId)) { + console.log("JAAAJ"); + this.refresh(); } } diff --git a/src/services/ws.js b/src/services/ws.js index b84d7f5bd..a2964d293 100644 --- a/src/services/ws.js +++ b/src/services/ws.js @@ -83,6 +83,8 @@ async function fillInAdditionalProperties(sync) { sync.noteId = await sql.getValue(`SELECT noteId FROM note_revisions WHERE noteRevisionId = ?`, [sync.entityId]); + } else if (sync.entityName === 'note_reordering') { + sync.positions = await sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [sync.entityId]); } }