Permalink
Browse files

feat(dialog-service): refactor consolidate open methods in one

  • Loading branch information...
StrahilKazlachev committed Oct 20, 2016
1 parent 0df452d commit 676ee58753cee99fc1fb2043115c46e164818f66
Showing with 61 additions and 39 deletions.
  1. +40 −39 src/dialog-service.js
  2. +21 −0 src/interfaces.js
View
@@ -4,8 +4,8 @@ import {CompositionEngine, ViewSlot} from 'aurelia-templating';
import {DialogController} from './dialog-controller';
import {Renderer} from './renderer';
import {invokeLifecycle} from './lifecycle';
-import {DialogResult} from './dialog-result';
import {dialogOptions} from './dialog-options';
+import {DialogSettings, OpenDialogResult, CloseDialogResult} from './interfaces';
/**
* A service allowing for the creation of dialogs.
@@ -34,32 +34,53 @@ export class DialogService {
* @param settings Dialog settings for this dialog instance.
* @return Promise A promise that settles when the dialog is closed.
*/
- open(settings?: Object): Promise<DialogResult> {
- return this.openAndYieldController(settings)
- .then((controller) => controller.result);
- }
-
- /**
- * Opens a new dialog.
- * @param settings Dialog settings for this dialog instance.
- * @return Promise A promise that settles when the dialog is opened.
- * Resolves to the controller of the dialog.
- */
- openAndYieldController(settings?: Object): Promise<DialogController> {
+ open(settings?: DialogSettings) {
let childContainer = this.container.createChild();
let dialogController;
- let promise = new Promise((resolve, reject) => {
+ let closeResult: Promise<CloseDialogResult> = new Promise((resolve, reject) => {
dialogController = new DialogController(childContainer.get(Renderer), _createSettings(settings), resolve, reject);
});
childContainer.registerInstance(DialogController, dialogController);
- dialogController.result = promise;
- dialogController.result.then(() => {
+
+ closeResult.then(() => {
_removeController(this, dialogController);
}, () => {
_removeController(this, dialogController);
});
- return _openDialog(this, childContainer, dialogController)
- .then(() => dialogController);
+
+ let openResult: Promise<OpenDialogResult> = _getViewModel(this.container, this.compositionEngine, childContainer, dialogController)
+ .then((instruction) => {
+ dialogController.viewModel = instruction.viewModel;
+ dialogController.slot = instruction.viewSlot;
+
+ return invokeLifecycle(dialogController.viewModel, 'canActivate', dialogController.settings.model).then(canActivate => {
+ if (canActivate) {
+ return this.compositionEngine.compose(instruction).then(controller => {
+ this.controllers.push(dialogController);
+ this.hasActiveDialog = !!this.controllers.length;
+ dialogController.controller = controller;
+ dialogController.view = controller.view;
+
+ return dialogController.renderer.showDialog(dialogController).then(() => {
+ return {
+ wasCancelled: false,
+ controller: dialogController,
+ closeResult
+ };
+ });
+ });
+ }
+
+ return {
+ wasCancelled: true // see aurelia/dialog#223
+ };
+ });
+ });
+
+ return {
+ openResult,
+ closeResult
+ };
}
}
@@ -69,7 +90,7 @@ function _createSettings(settings) {
return settings;
}
-function _openDialog(service, childContainer, dialogController) {
+function _getViewModel(container, compositionEngine, childCOntainer, dialogController) {
let host = dialogController.renderer.getDialogContainer();
let instruction = {
container: service.container,
@@ -81,26 +102,6 @@ function _openDialog(service, childContainer, dialogController) {
host: host
};
- return _getViewModel(instruction, service.compositionEngine).then(returnedInstruction => {
- dialogController.viewModel = returnedInstruction.viewModel;
- dialogController.slot = returnedInstruction.viewSlot;
-
- return invokeLifecycle(dialogController.viewModel, 'canActivate', dialogController.settings.model).then(canActivate => {
- if (canActivate) {
- return service.compositionEngine.compose(returnedInstruction).then(controller => {
- service.controllers.push(dialogController);
- service.hasActiveDialog = !!service.controllers.length;
- dialogController.controller = controller;
- dialogController.view = controller.view;
-
- return dialogController.renderer.showDialog(dialogController);
- });
- }
- });
- });
-}
-
-function _getViewModel(instruction, compositionEngine) {
if (typeof instruction.viewModel === 'function') {
instruction.viewModel = Origin.get(instruction.viewModel).moduleId;
}
View
@@ -0,0 +1,21 @@
+interface DialogSettings {
+ viewModel: any;
+ view?: any;
+ model?: any;
+ lock?: boolean;
+ startingZIndex?: number;
+ centerHorizontalOnly?: boolean;
+ ignoreTransitions?: boolean;
+ position?: (modalContainer: Element, modalOverlay: Element) => void;
+}
+
+interface CloseDialogResult {
+ wasCancelled: boolean;
+ output?: any;
+}
+
+interface OpenDialogResult {
+ wasCancelled: boolean; // see aurelia/dialog#223
+ controller?: DialogController;
+ closeResult?: Promise<CloseDialogResult>;
+}

0 comments on commit 676ee58

Please sign in to comment.