Skip to content

Commit

Permalink
feat(dialog-service): refactor consolidate open methods in one
Browse files Browse the repository at this point in the history
  • Loading branch information
StrahilKazlachev committed Oct 20, 2016
1 parent 0df452d commit 676ee58
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
79 changes: 40 additions & 39 deletions src/dialog-service.js
Expand Up @@ -4,8 +4,8 @@ import {CompositionEngine, ViewSlot} from 'aurelia-templating';
import {DialogController} from './dialog-controller'; import {DialogController} from './dialog-controller';
import {Renderer} from './renderer'; import {Renderer} from './renderer';
import {invokeLifecycle} from './lifecycle'; import {invokeLifecycle} from './lifecycle';
import {DialogResult} from './dialog-result';
import {dialogOptions} from './dialog-options'; import {dialogOptions} from './dialog-options';
import {DialogSettings, OpenDialogResult, CloseDialogResult} from './interfaces';


/** /**
* A service allowing for the creation of dialogs. * A service allowing for the creation of dialogs.
Expand Down Expand Up @@ -34,32 +34,53 @@ export class DialogService {
* @param settings Dialog settings for this dialog instance. * @param settings Dialog settings for this dialog instance.
* @return Promise A promise that settles when the dialog is closed. * @return Promise A promise that settles when the dialog is closed.
*/ */
open(settings?: Object): Promise<DialogResult> { open(settings?: DialogSettings) {
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> {
let childContainer = this.container.createChild(); let childContainer = this.container.createChild();
let dialogController; 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); dialogController = new DialogController(childContainer.get(Renderer), _createSettings(settings), resolve, reject);
}); });
childContainer.registerInstance(DialogController, dialogController); childContainer.registerInstance(DialogController, dialogController);
dialogController.result = promise;
dialogController.result.then(() => { closeResult.then(() => {
_removeController(this, dialogController); _removeController(this, dialogController);
}, () => { }, () => {
_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
};
} }
} }


Expand All @@ -69,7 +90,7 @@ function _createSettings(settings) {
return settings; return settings;
} }


function _openDialog(service, childContainer, dialogController) { function _getViewModel(container, compositionEngine, childCOntainer, dialogController) {
let host = dialogController.renderer.getDialogContainer(); let host = dialogController.renderer.getDialogContainer();
let instruction = { let instruction = {
container: service.container, container: service.container,
Expand All @@ -81,26 +102,6 @@ function _openDialog(service, childContainer, dialogController) {
host: host 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') { if (typeof instruction.viewModel === 'function') {
instruction.viewModel = Origin.get(instruction.viewModel).moduleId; instruction.viewModel = Origin.get(instruction.viewModel).moduleId;
} }
Expand Down
21 changes: 21 additions & 0 deletions src/interfaces.js
@@ -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.