fix: bind modals to application lifetime

This commit is contained in:
Mo Bitar
2020-09-24 15:32:01 -05:00
parent 65498cad5c
commit 94a82412bd
6 changed files with 59 additions and 57 deletions

View File

@@ -1,4 +1,6 @@
import { AccountSwitcherScope } from './../types'; import { PermissionDialog } from 'snjs/dist/@types/services/component_manager';
import { ComponentModalScope } from './../directives/views/componentModal';
import { AccountSwitcherScope, PermissionsModalScope } from './../types';
import { ComponentGroup } from './component_group'; import { ComponentGroup } from './component_group';
import { EditorGroup } from '@/ui_models/editor_group'; import { EditorGroup } from '@/ui_models/editor_group';
import { InputModalScope } from '@/directives/views/inputModal'; import { InputModalScope } from '@/directives/views/inputModal';
@@ -7,7 +9,7 @@ import {
SNApplication, SNApplication,
platformFromString, platformFromString,
Challenge, Challenge,
ProtectedAction ProtectedAction, SNComponent
} from 'snjs'; } from 'snjs';
import angular from 'angular'; import angular from 'angular';
import { getPlatformString } from '@/utils'; import { getPlatformString } from '@/utils';
@@ -74,6 +76,8 @@ export class WebApplication extends SNApplication {
deviceInterface.setApplication(this); deviceInterface.setApplication(this);
this.editorGroup = new EditorGroup(this); this.editorGroup = new EditorGroup(this);
this.componentGroup = new ComponentGroup(this); this.componentGroup = new ComponentGroup(this);
this.openModalComponent = this.openModalComponent.bind(this);
this.presentPermissionsDialog = this.presentPermissionsDialog.bind(this);
} }
/** @override */ /** @override */
@@ -92,6 +96,8 @@ export class WebApplication extends SNApplication {
(this.scope! as any).application = undefined; (this.scope! as any).application = undefined;
this.scope!.$destroy(); this.scope!.$destroy();
this.scope = undefined; this.scope = undefined;
(this.openModalComponent as any) = undefined;
(this.presentPermissionsDialog as any) = undefined;
/** Allow our Angular directives to be destroyed and any pending digest cycles /** Allow our Angular directives to be destroyed and any pending digest cycles
* to complete before destroying the global application instance and all its services */ * to complete before destroying the global application instance and all its services */
setTimeout(() => { setTimeout(() => {
@@ -99,6 +105,12 @@ export class WebApplication extends SNApplication {
}, 0) }, 0)
} }
onStart() {
super.onStart();
this.componentManager!.openModalComponent = this.openModalComponent;
this.componentManager!.presentPermissionsDialog = this.presentPermissionsDialog;
}
setWebServices(services: WebServices) { setWebServices(services: WebServices) {
this.webServices = services; this.webServices = services;
} }
@@ -150,7 +162,7 @@ export class WebApplication extends SNApplication {
const el = this.$compile!( const el = this.$compile!(
"<password-wizard application='application' type='type'></password-wizard>" "<password-wizard application='application' type='type'></password-wizard>"
)(scope as any); )(scope as any);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
promptForChallenge(challenge: Challenge) { promptForChallenge(challenge: Challenge) {
@@ -162,7 +174,7 @@ export class WebApplication extends SNApplication {
"class='sk-modal' application='application' challenge='challenge'>" + "class='sk-modal' application='application' challenge='challenge'>" +
"</challenge-modal>" "</challenge-modal>"
)(scope); )(scope);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
async presentPrivilegesModal( async presentPrivilegesModal(
@@ -193,7 +205,7 @@ export class WebApplication extends SNApplication {
<privileges-auth-modal application='application' action='action' on-success='onSuccess' <privileges-auth-modal application='application' action='action' on-success='onSuccess'
on-cancel='onCancel' class='sk-modal'></privileges-auth-modal> on-cancel='onCancel' class='sk-modal'></privileges-auth-modal>
`)(scope); `)(scope);
angular.element(document.body).append(el); this.applicationElement.append(el);
this.currentAuthenticationElement = el; this.currentAuthenticationElement = el;
} }
@@ -202,13 +214,17 @@ export class WebApplication extends SNApplication {
const scope: any = this.scope!.$new(true); const scope: any = this.scope!.$new(true);
scope.application = this; scope.application = this;
const el = this.$compile!("<privileges-management-modal application='application' class='sk-modal'></privileges-management-modal>")(scope); const el = this.$compile!("<privileges-management-modal application='application' class='sk-modal'></privileges-management-modal>")(scope);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
authenticationInProgress() { authenticationInProgress() {
return this.currentAuthenticationElement != null; return this.currentAuthenticationElement != null;
} }
get applicationElement() {
return angular.element(document.getElementById(this.identifier)!);
}
presentPasswordModal(callback: () => void) { presentPasswordModal(callback: () => void) {
const scope = this.scope!.$new(true) as InputModalScope; const scope = this.scope!.$new(true) as InputModalScope;
scope.type = "password"; scope.type = "password";
@@ -220,7 +236,7 @@ export class WebApplication extends SNApplication {
`<input-modal type='type' message='message' `<input-modal type='type' message='message'
title='title' callback='callback()'></input-modal>` title='title' callback='callback()'></input-modal>`
)(scope as any); )(scope as any);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
presentRevisionPreviewModal(uuid: string, content: any) { presentRevisionPreviewModal(uuid: string, content: any) {
@@ -232,7 +248,7 @@ export class WebApplication extends SNApplication {
`<revision-preview-modal application='application' uuid='uuid' content='content' `<revision-preview-modal application='application' uuid='uuid' content='content'
class='sk-modal'></revision-preview-modal>` class='sk-modal'></revision-preview-modal>`
)(scope); )(scope);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
public openAccountSwitcher() { public openAccountSwitcher() {
@@ -242,7 +258,29 @@ export class WebApplication extends SNApplication {
"<account-switcher application='application' " "<account-switcher application='application' "
+ "class='sk-modal'></account-switcher>" + "class='sk-modal'></account-switcher>"
)(scope as any); )(scope as any);
angular.element(document.body).append(el); this.applicationElement.append(el);
} }
openModalComponent(component: SNComponent) {
const scope = this.scope!.$new(true) as Partial<ComponentModalScope>;
scope.componentUuid = component.uuid;
scope.application = this;
const el = this.$compile!(
"<component-modal application='application' component-uuid='componentUuid' "
+ "class='sk-modal'></component-modal>"
)(scope as any);
this.applicationElement.append(el);
}
presentPermissionsDialog(dialog: PermissionDialog) {
const scope = this.scope!.$new(true) as PermissionsModalScope;
scope.permissionsString = dialog.permissionsString;
scope.component = dialog.component;
scope.callback = dialog.callback;
const el = this.$compile!(
"<permissions-modal component='component' permissions-string='permissionsString'"
+ " callback='callback' class='sk-modal'></permissions-modal>"
)(scope as any);
this.applicationElement.append(el);
}
} }

View File

@@ -1,10 +1,8 @@
import { ComponentModalScope } from './../../directives/views/componentModal'; import { WebDirective } from '@/types';
import { WebDirective, PermissionsModalScope } from '@/types';
import { getPlatformString } from '@/utils'; import { getPlatformString } from '@/utils';
import template from './application-view.pug'; import template from './application-view.pug';
import { AppStateEvent } from '@/ui_models/app_state'; import { AppStateEvent } from '@/ui_models/app_state';
import { ApplicationEvent, SNComponent } from 'snjs'; import { ApplicationEvent } from 'snjs';
import angular from 'angular';
import { import {
PANEL_NAME_NOTES, PANEL_NAME_NOTES,
PANEL_NAME_TAGS PANEL_NAME_TAGS
@@ -13,10 +11,8 @@ import {
STRING_DEFAULT_FILE_ERROR STRING_DEFAULT_FILE_ERROR
} from '@/strings'; } from '@/strings';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
import { PermissionDialog } from 'snjs/dist/@types/services/component_manager';
class ApplicationViewCtrl extends PureViewCtrl { class ApplicationViewCtrl extends PureViewCtrl {
private $compile?: ng.ICompileService
private $location?: ng.ILocationService private $location?: ng.ILocationService
private $rootScope?: ng.IRootScopeService private $rootScope?: ng.IRootScopeService
public platformString: string public platformString: string
@@ -26,11 +22,9 @@ class ApplicationViewCtrl extends PureViewCtrl {
private tagsCollapsed = false private tagsCollapsed = false
private showingDownloadStatus = false private showingDownloadStatus = false
private uploadSyncStatus: any private uploadSyncStatus: any
private lastAlertShownTimeStamp = 0;
/* @ngInject */ /* @ngInject */
constructor( constructor(
$compile: ng.ICompileService,
$location: ng.ILocationService, $location: ng.ILocationService,
$rootScope: ng.IRootScopeService, $rootScope: ng.IRootScopeService,
$timeout: ng.ITimeoutService $timeout: ng.ITimeoutService
@@ -38,27 +32,21 @@ class ApplicationViewCtrl extends PureViewCtrl {
super($timeout); super($timeout);
this.$location = $location; this.$location = $location;
this.$rootScope = $rootScope; this.$rootScope = $rootScope;
this.$compile = $compile;
this.platformString = getPlatformString(); this.platformString = getPlatformString();
this.state = { appClass: '' }; this.state = { appClass: '' };
this.onDragDrop = this.onDragDrop.bind(this); this.onDragDrop = this.onDragDrop.bind(this);
this.onDragOver = this.onDragOver.bind(this); this.onDragOver = this.onDragOver.bind(this);
this.openModalComponent = this.openModalComponent.bind(this);
this.presentPermissionsDialog = this.presentPermissionsDialog.bind(this);
this.addDragDropHandlers(); this.addDragDropHandlers();
} }
deinit() { deinit() {
this.$location = undefined; this.$location = undefined;
this.$rootScope = undefined; this.$rootScope = undefined;
this.$compile = undefined;
(this.application as any) = undefined; (this.application as any) = undefined;
window.removeEventListener('dragover', this.onDragOver, true); window.removeEventListener('dragover', this.onDragOver, true);
window.removeEventListener('drop', this.onDragDrop, true); window.removeEventListener('drop', this.onDragDrop, true);
(this.onDragDrop as any) = undefined; (this.onDragDrop as any) = undefined;
(this.onDragOver as any) = undefined; (this.onDragOver as any) = undefined;
(this.openModalComponent as any) = undefined;
(this.presentPermissionsDialog as any) = undefined;
super.deinit(); super.deinit();
} }
@@ -74,12 +62,10 @@ class ApplicationViewCtrl extends PureViewCtrl {
} }
}); });
await this.application!.launch(); await this.application!.launch();
} }
async onAppStart() { async onAppStart() {
super.onAppStart(); super.onAppStart();
this.overrideComponentManagerFunctions();
this.application!.componentManager!.setDesktopManager( this.application!.componentManager!.setDesktopManager(
this.application!.getDesktopService() this.application!.getDesktopService()
); );
@@ -225,34 +211,6 @@ class ApplicationViewCtrl extends PureViewCtrl {
} }
} }
openModalComponent(component: SNComponent) {
const scope = this.$rootScope!.$new(true) as Partial<ComponentModalScope>;
scope.componentUuid = component.uuid;
scope.application = this.application;
const el = this.$compile!(
"<component-modal application='application' component-uuid='componentUuid' "
+ "class='sk-modal'></component-modal>"
)(scope as any);
angular.element(document.body).append(el);
}
presentPermissionsDialog(dialog: PermissionDialog) {
const scope = this.$rootScope!.$new(true) as PermissionsModalScope;
scope.permissionsString = dialog.permissionsString;
scope.component = dialog.component;
scope.callback = dialog.callback;
const el = this.$compile!(
"<permissions-modal component='component' permissions-string='permissionsString'"
+ " callback='callback' class='sk-modal'></permissions-modal>"
)(scope as any);
angular.element(document.body).append(el);
}
overrideComponentManagerFunctions() {
this.application!.componentManager!.openModalComponent = this.openModalComponent;
this.application!.componentManager!.presentPermissionsDialog = this.presentPermissionsDialog;
}
addDragDropHandlers() { addDragDropHandlers() {
/** /**
* Disable dragging and dropping of files (but allow text) into main SN interface. * Disable dragging and dropping of files (but allow text) into main SN interface.

View File

@@ -2,4 +2,5 @@ application-view(
ng-repeat='application in self.applications', ng-repeat='application in self.applications',
ng-if='application == self.activeApplication' ng-if='application == self.activeApplication'
application='application' application='application'
ng-attr-id='{{application.identifier}}'
) )

View File

@@ -1,8 +1,9 @@
/// <reference types="angular" /> /// <reference types="angular" />
import { PermissionDialog } from 'snjs/dist/@types/services/component_manager';
import { ComponentGroup } from './component_group'; import { ComponentGroup } from './component_group';
import { EditorGroup } from '@/ui_models/editor_group'; import { EditorGroup } from '@/ui_models/editor_group';
import { PasswordWizardType } from '@/types'; import { PasswordWizardType } from '@/types';
import { SNApplication, Challenge, ProtectedAction } from 'snjs'; import { SNApplication, Challenge, ProtectedAction, SNComponent } from 'snjs';
import { WebDeviceInterface } from '@/web_device_interface'; import { WebDeviceInterface } from '@/web_device_interface';
import { DesktopManager, AutolockService, ArchiveManager, NativeExtManager, StatusManager, ThemeManager, PreferencesManager, KeyboardManager } from '@/services'; import { DesktopManager, AutolockService, ArchiveManager, NativeExtManager, StatusManager, ThemeManager, PreferencesManager, KeyboardManager } from '@/services';
import { AppState } from '@/ui_models/app_state'; import { AppState } from '@/ui_models/app_state';
@@ -29,6 +30,7 @@ export declare class WebApplication extends SNApplication {
constructor(deviceInterface: WebDeviceInterface, identifier: string, $compile: ng.ICompileService, scope: ng.IScope, defaultSyncServerHost: string, bridge: Bridge); constructor(deviceInterface: WebDeviceInterface, identifier: string, $compile: ng.ICompileService, scope: ng.IScope, defaultSyncServerHost: string, bridge: Bridge);
/** @override */ /** @override */
deinit(source: DeinitSource): void; deinit(source: DeinitSource): void;
onStart(): void;
setWebServices(services: WebServices): void; setWebServices(services: WebServices): void;
getAppState(): AppState; getAppState(): AppState;
getDesktopService(): DesktopManager; getDesktopService(): DesktopManager;
@@ -45,8 +47,11 @@ export declare class WebApplication extends SNApplication {
presentPrivilegesModal(action: ProtectedAction, onSuccess?: any, onCancel?: any): Promise<void>; presentPrivilegesModal(action: ProtectedAction, onSuccess?: any, onCancel?: any): Promise<void>;
presentPrivilegesManagementModal(): void; presentPrivilegesManagementModal(): void;
authenticationInProgress(): boolean; authenticationInProgress(): boolean;
get applicationElement(): JQLite;
presentPasswordModal(callback: () => void): void; presentPasswordModal(callback: () => void): void;
presentRevisionPreviewModal(uuid: string, content: any): void; presentRevisionPreviewModal(uuid: string, content: any): void;
openAccountSwitcher(): void; openAccountSwitcher(): void;
openModalComponent(component: SNComponent): void;
presentPermissionsDialog(dialog: PermissionDialog): void;
} }
export {}; export {};

4
package-lock.json generated
View File

@@ -10956,8 +10956,8 @@
"from": "github:standardnotes/sncrypto#8794c88daa967eaae493cd5fdec7506d52b257ad" "from": "github:standardnotes/sncrypto#8794c88daa967eaae493cd5fdec7506d52b257ad"
}, },
"snjs": { "snjs": {
"version": "github:standardnotes/snjs#51715bcd284bfb3be9cf9e7aa7f85377d9880668", "version": "github:standardnotes/snjs#7abfb3eb46db1409dbcb152b3d9d14ee0453c4a6",
"from": "github:standardnotes/snjs#51715bcd284bfb3be9cf9e7aa7f85377d9880668" "from": "github:standardnotes/snjs#7abfb3eb46db1409dbcb152b3d9d14ee0453c4a6"
}, },
"sockjs": { "sockjs": {
"version": "0.3.20", "version": "0.3.20",

View File

@@ -68,6 +68,6 @@
}, },
"dependencies": { "dependencies": {
"sncrypto": "github:standardnotes/sncrypto#8794c88daa967eaae493cd5fdec7506d52b257ad", "sncrypto": "github:standardnotes/sncrypto#8794c88daa967eaae493cd5fdec7506d52b257ad",
"snjs": "github:standardnotes/snjs#51715bcd284bfb3be9cf9e7aa7f85377d9880668" "snjs": "github:standardnotes/snjs#7abfb3eb46db1409dbcb152b3d9d14ee0453c4a6"
} }
} }