refactoring of note loading

This commit is contained in:
zadam
2019-09-04 22:13:22 +02:00
parent fdc86bab50
commit a17b8a053e
5 changed files with 268 additions and 240 deletions

View File

@@ -45,11 +45,13 @@ class TabContext {
this.state = state;
}
initTabContent() {
async initTabContent() {
if (this.initialized) {
return;
}
this.initialized = true;
this.$tabContent = $(".note-tab-content-template").clone();
this.$tabContent.removeClass('note-tab-content-template');
this.$tabContent.attr('data-tab-id', this.tabId);
@@ -110,7 +112,13 @@ class TabContext {
this.$unprotectButton = this.$tabContent.find(".unprotect-button");
this.$unprotectButton.click(protectedSessionService.unprotectNoteAndSendToServer);
this.initialized = true;
const type = this.getComponentType();
if (!(type in this.components)) {
const clazz = await import(componentClasses[type]);
this.components[type] = new clazz.default(this);
}
}
async setNote(note, notePath) {
@@ -124,21 +132,9 @@ class TabContext {
return;
}
await this.initComponent();
// after loading new note make sure editor is scrolled to the top
this.getComponent().scrollToTop();
if (utils.isDesktop()) {
// needs to happen after loading the note itself because it references active noteId
this.attributes.refreshAttributes();
await this.showChildrenOverview();
} else {
// mobile usually doesn't need attributes so we just invalidate
this.attributes.invalidateAttributes();
}
this.setupClasses();
this.setCurrentNotePathToHash();
@@ -148,10 +144,6 @@ class TabContext {
try {
this.$noteTitle.val(this.note.title);
if (utils.isDesktop()) {
this.noteType.update();
}
await this.renderComponent();
} finally {
this.noteChangeDisabled = false;
@@ -173,18 +165,29 @@ class TabContext {
this.showPaths();
if (utils.isDesktop()) {
this.attributes.refreshAttributes();
this.noteType.update();
this.showChildrenOverview();
} else {
// mobile usually doesn't need attributes so we just invalidate
this.attributes.invalidateAttributes();
}
if (this.sidebar) {
this.sidebar.noteLoaded(); // load async
}
await bundleService.executeRelationBundles(this.note, 'runOnNoteView', this);
bundleService.executeRelationBundles(this.note, 'runOnNoteView', this);
}
async show() {
if (!this.initialized) {
this.initTabContent();
await this.initTabContent();
await this.initComponent();
this.$tabContent.show(); // show immediately so that user can see something
if (this.note) {
await this.setNote(this.note, this.notePath);
@@ -192,14 +195,14 @@ class TabContext {
}
this.$tabContent.show();
this.setCurrentNotePathToHash();
this.setTitleBar();
this.getComponent().show();
}
async renderComponent() {
for (const componentType in this.components) {
if (componentType !== this.note.type) {
if (componentType !== this.getComponentType()) {
this.components[componentType].cleanup();
}
}
@@ -209,12 +212,10 @@ class TabContext {
this.$noteTitle.show(); // this can be hidden by empty detail
this.$noteTitle.removeAttr("readonly"); // this can be set by protected session service
await this.initComponent();
this.getComponent().show();
await this.getComponent().render();
}
setTitleBar() {
if (!this.$tabContent.is(":visible")) {
return;
@@ -269,16 +270,6 @@ class TabContext {
this.$unprotectButton.prop("disabled", !this.note.isProtected || !protectedSessionHolder.isProtectedSessionAvailable());
}
async initComponent() {
const type = this.getComponentType();
if (!(type in this.components)) {
const clazz = await import(componentClasses[type]);
this.components[type] = new clazz.default(this);
}
}
getComponent() {
const type = this.getComponentType();