From 789f62267c19b2b5654de00046244fa5ca172170 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 12 Feb 2020 22:09:25 +0100 Subject: [PATCH] changes in note detail handling --- src/public/javascripts/widgets/note_detail.js | 90 +++++++++---------- .../javascripts/widgets/tab_caching_widget.js | 2 +- .../widgets/type_widgets/relation_map.js | 1 - .../javascripts/widgets/type_widgets/text.js | 3 +- .../widgets/type_widgets/type_widget.js | 7 +- 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/public/javascripts/widgets/note_detail.js b/src/public/javascripts/widgets/note_detail.js index d284fbf4e..0edcbcb0e 100644 --- a/src/public/javascripts/widgets/note_detail.js +++ b/src/public/javascripts/widgets/note_detail.js @@ -4,6 +4,16 @@ import protectedSessionHolder from "../services/protected_session_holder.js"; import SpacedUpdate from "../services/spaced_update.js"; import server from "../services/server.js"; import libraryLoader from "../services/library_loader.js"; +import EmptyTypeWidget from "./type_widgets/empty.js"; +import TextTypeWidget from "./type_widgets/text.js"; +import CodeTypeWidget from "./type_widgets/code.js"; +import FileTypeWidget from "./type_widgets/file.js"; +import ImageTypeWidget from "./type_widgets/image.js"; +import SearchTypeWidget from "./type_widgets/search.js"; +import RenderTypeWidget from "./type_widgets/render.js"; +import RelationMapTypeWidget from "./type_widgets/relation_map.js"; +import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js"; +import BookTypeWidget from "./type_widgets/book.js"; const TPL = `
@@ -16,16 +26,16 @@ const TPL = ` `; const typeWidgetClasses = { - 'empty': "./type_widgets/empty.js", - 'text': "./type_widgets/text.js", - 'code': "./type_widgets/code.js", - 'file': "./type_widgets/file.js", - 'image': "./type_widgets/image.js", - 'search': "./type_widgets/search.js", - 'render': "./type_widgets/render.js", - 'relation-map': "./type_widgets/relation_map.js", - 'protected-session': "./type_widgets/protected_session.js", - 'book': "./type_widgets/book.js" + 'empty': EmptyTypeWidget, + 'text': TextTypeWidget, + 'code': CodeTypeWidget, + 'file': FileTypeWidget, + 'image': ImageTypeWidget, + 'search': SearchTypeWidget, + 'render': RenderTypeWidget, + 'relation-map': RelationMapTypeWidget, + 'protected-session': ProtectedSessionTypeWidget, + 'book': BookTypeWidget }; export default class NoteDetailWidget extends TabAwareWidget { @@ -33,7 +43,6 @@ export default class NoteDetailWidget extends TabAwareWidget { super(appContext); this.typeWidgets = {}; - this.typeWidgetPromises = {}; this.spacedUpdate = new SpacedUpdate(async () => { const {note} = this.tabContext; @@ -77,34 +86,30 @@ export default class NoteDetailWidget extends TabAwareWidget { } async refresh() { - await this.initType(); - - for (const typeWidget of Object.values(this.typeWidgets)) { - if (typeWidget.constructor.getType() !== this.type) { - typeWidget.cleanup(); - typeWidget.toggle(false); - } + if (!this.isEnabled()) { + this.toggle(false); + return; } - this.getTypeWidget().toggle(true); + this.toggle(true); + + this.type = await this.getWidgetType(); + + if (!(this.type in this.typeWidgets)) { + const clazz = typeWidgetClasses[this.type]; + + const typeWidget = this.typeWidgets[this.type] = new clazz(this.appContext); + typeWidget.spacedUpdate = this.spacedUpdate; + + this.children.push(typeWidget); + this.$widget.append(typeWidget.render()); + + typeWidget.eventReceived('setTabContext', {tabContext: this.tabContext}); + } this.setupClasses(); } - async initType() { - let foundType; - - do { - foundType = this.type = await this.getWidgetType(); - - if (!(this.type in this.typeWidgetPromises)) { - this.typeWidgetPromises[this.type] = this.initWidgetType(this.type); - } - - await this.typeWidgetPromises[this.type]; - } while (foundType !== await this.getWidgetType()); - } - setupClasses() { for (const clazz of Array.from(this.$widget[0].classList)) { // create copy to safely iterate over while removing classes if (clazz !== 'note-detail') { @@ -130,18 +135,6 @@ export default class NoteDetailWidget extends TabAwareWidget { return this.typeWidgets[this.type]; } - - async initWidgetType(type) { - const clazz = await import(typeWidgetClasses[type]); - - const typeWidget = this.typeWidgets[type] = new clazz.default(this.appContext); - typeWidget.spacedUpdate = this.spacedUpdate; - - this.children.push(typeWidget); - this.$widget.append(typeWidget.render()); - - typeWidget.eventReceived('setTabContext', {tabContext: this.tabContext}); - } async getWidgetType() { const note = this.note; @@ -228,4 +221,11 @@ export default class NoteDetailWidget extends TabAwareWidget { autoBookDisabledListener() { this.refresh(); } + + async triggerChildren(name, data) { + // done manually in refresh() + if (name !== 'setTabContext') { + await super.triggerChildren(name, data); + } + } } \ No newline at end of file diff --git a/src/public/javascripts/widgets/tab_caching_widget.js b/src/public/javascripts/widgets/tab_caching_widget.js index 8052f6123..494f12cc5 100644 --- a/src/public/javascripts/widgets/tab_caching_widget.js +++ b/src/public/javascripts/widgets/tab_caching_widget.js @@ -20,7 +20,7 @@ export default class TabCachingWidget extends TabAwareWidget { // stop propagation of the event to the children, individual tab widget should not know about tab switching // since they are per-tab if (name !== 'activeTabChanged') { - super.triggerChildren(name, data); + await super.triggerChildren(name, data); } } diff --git a/src/public/javascripts/widgets/type_widgets/relation_map.js b/src/public/javascripts/widgets/type_widgets/relation_map.js index bca4c29c4..58299e625 100644 --- a/src/public/javascripts/widgets/type_widgets/relation_map.js +++ b/src/public/javascripts/widgets/type_widgets/relation_map.js @@ -6,7 +6,6 @@ import contextMenuWidget from "../../services/context_menu.js"; import toastService from "../../services/toast.js"; import attributeAutocompleteService from "../../services/attribute_autocomplete.js"; import TypeWidget from "./type_widget.js"; -import appContext from "../../services/app_context.js"; const uniDirectionalOverlays = [ [ "Arrow", { diff --git a/src/public/javascripts/widgets/type_widgets/text.js b/src/public/javascripts/widgets/type_widgets/text.js index b3de1207e..71033ec13 100644 --- a/src/public/javascripts/widgets/type_widgets/text.js +++ b/src/public/javascripts/widgets/type_widgets/text.js @@ -1,5 +1,4 @@ import libraryLoader from "../../services/library_loader.js"; -import treeService from '../../services/tree.js'; import noteAutocompleteService from '../../services/note_autocomplete.js'; import mimeTypesService from '../../services/mime_types.js'; import TypeWidget from "./type_widget.js"; @@ -137,7 +136,7 @@ export default class TextTypeWidget extends TypeWidget { } } - async doRefresh(note) { + async doRefresh(note) {console.trace("UPDATE###" + this.componentId); this.textEditor.isReadOnly = await note.hasLabel('readOnly'); const noteComplement = await this.tabContext.getNoteComplement(); diff --git a/src/public/javascripts/widgets/type_widgets/type_widget.js b/src/public/javascripts/widgets/type_widgets/type_widget.js index 429da3d49..c5edd622d 100644 --- a/src/public/javascripts/widgets/type_widgets/type_widget.js +++ b/src/public/javascripts/widgets/type_widgets/type_widget.js @@ -19,10 +19,13 @@ export default class TypeWidget extends TabAwareWidget { && (widgetType !== 'protected-session' || !note.isProtected))) { this.toggle(false); - return; + this.cleanup(); } + else { + this.toggle(true); - this.doRefresh(note); + this.doRefresh(note); + } } isActive() {