[Code Style] Use prototypes in Dialog bundle

WTD-1482.
This commit is contained in:
Victor Woeltjen 2015-08-10 12:53:55 -07:00
parent f8a0ddb484
commit efc42aa8f2
2 changed files with 157 additions and 161 deletions

View File

@ -38,23 +38,28 @@ define(
* @constructor * @constructor
*/ */
function DialogService(overlayService, $q, $log) { function DialogService(overlayService, $q, $log) {
var overlay, this.overlayService = overlayService;
dialogVisible = false; this.$q = $q;
this.$log = $log;
this.overlay = undefined;
this.dialogVisible = false;
}
// Stop showing whatever overlay is currently active // Stop showing whatever overlay is currently active
// (e.g. because the user hit cancel) // (e.g. because the user hit cancel)
function dismiss() { DialogService.prototype.dismiss = function () {
var overlay = this.overlay;
if (overlay) { if (overlay) {
overlay.dismiss(); overlay.dismiss();
} }
dialogVisible = false; this.dialogVisible = false;
} };
function getDialogResponse(key, model, resultGetter) { DialogService.prototype.getDialogResponse = function (key, model, resultGetter) {
// We will return this result as a promise, because user // We will return this result as a promise, because user
// input is asynchronous. // input is asynchronous.
var deferred = $q.defer(), var deferred = this.$q.defer(),
overlayModel; self = this;
// Confirm function; this will be passed in to the // Confirm function; this will be passed in to the
// overlay-dialog template and associated with a // overlay-dialog template and associated with a
@ -64,7 +69,7 @@ define(
deferred.resolve(resultGetter ? resultGetter() : value); deferred.resolve(resultGetter ? resultGetter() : value);
// Stop showing the dialog // Stop showing the dialog
dismiss(); self.dismiss();
} }
// Cancel function; this will be passed in to the // Cancel function; this will be passed in to the
@ -72,18 +77,18 @@ define(
// Cancel or X button click // Cancel or X button click
function cancel() { function cancel() {
deferred.reject(); deferred.reject();
dismiss(); self.dismiss();
} }
// Add confirm/cancel callbacks // Add confirm/cancel callbacks
model.confirm = confirm; model.confirm = confirm;
model.cancel = cancel; model.cancel = cancel;
if (dialogVisible) { if (this.dialogVisible) {
// Only one dialog should be shown at a time. // Only one dialog should be shown at a time.
// The application design should be such that // The application design should be such that
// we never even try to do this. // we never even try to do this.
$log.warn([ this.$log.warn([
"Dialog already showing; ", "Dialog already showing; ",
"unable to show ", "unable to show ",
model.name model.name
@ -92,20 +97,31 @@ define(
} else { } else {
// Add the overlay using the OverlayService, which // Add the overlay using the OverlayService, which
// will handle actual insertion into the DOM // will handle actual insertion into the DOM
overlay = overlayService.createOverlay( this.overlay = this.overlayService.createOverlay(
key, key,
model model
); );
// Track that a dialog is already visible, to // Track that a dialog is already visible, to
// avoid spawning multiple dialogs at once. // avoid spawning multiple dialogs at once.
dialogVisible = true; this.dialogVisible = true;
} }
return deferred.promise; return deferred.promise;
} };
function getUserInput(formModel, value) { /**
* Request user input via a window-modal dialog.
*
* @param {FormModel} formModel a description of the form
* to be shown (see platform/forms)
* @param {object} value the initial state of the form
* @returns {Promise} a promise for the form value that the
* user has supplied; this may be rejected if
* user input cannot be obtained (for instance,
* because the user cancelled the dialog)
*/
DialogService.prototype.getUserInput = function (formModel, value) {
var overlayModel = { var overlayModel = {
title: formModel.name, title: formModel.name,
message: formModel.message, message: formModel.message,
@ -119,45 +135,28 @@ define(
} }
// Show the overlay-dialog // Show the overlay-dialog
return getDialogResponse( return this.getDialogResponse(
"overlay-dialog", "overlay-dialog",
overlayModel, overlayModel,
resultGetter resultGetter
); );
} };
function getUserChoice(dialogModel) {
// Show the overlay-options dialog
return getDialogResponse(
"overlay-options",
{ dialog: dialogModel }
);
}
return {
/**
* Request user input via a window-modal dialog.
*
* @param {FormModel} formModel a description of the form
* to be shown (see platform/forms)
* @param {object} value the initial state of the form
* @returns {Promise} a promsie for the form value that the
* user has supplied; this may be rejected if
* user input cannot be obtained (for instance,
* because the user cancelled the dialog)
* @memberof platform/commonUI/dialog.DialogService#
*/
getUserInput: getUserInput,
/** /**
* Request that the user chooses from a set of options, * Request that the user chooses from a set of options,
* which will be shown as buttons. * which will be shown as buttons.
* *
* @param dialogModel a description of the dialog to show * @param dialogModel a description of the dialog to show
* @memberof platform/commonUI/dialog.DialogService# * @return {Promise} a promise for the user's choice
*/ */
getUserChoice: getUserChoice DialogService.prototype.getUserChoice = function (dialogModel) {
// Show the overlay-options dialog
return this.getDialogResponse(
"overlay-options",
{ dialog: dialogModel }
);
}; };
}
return DialogService; return DialogService;
} }

View File

@ -47,9 +47,27 @@ define(
* @constructor * @constructor
*/ */
function OverlayService($document, $compile, $rootScope) { function OverlayService($document, $compile, $rootScope) {
function createOverlay(key, overlayModel) { this.$document = $document;
this.$compile = $compile;
this.$rootScope = $rootScope;
}
/**
* Add a new overlay to the document. This will be
* prepended to the document body; the overlay's
* template (as pointed to by the `key` argument) is
* responsible for having a useful z-order, and for
* blocking user interactions if appropriate.
*
* @param {string} key the symbolic key which identifies
* the template of the overlay to be shown
* @param {object} overlayModel the model to pass to the
* included overlay template (this will be passed
* in via ng-model)
*/
OverlayService.prototype.createOverlay = function (key, overlayModel) {
// Create a new scope for this overlay // Create a new scope for this overlay
var scope = $rootScope.$new(), var scope = this.$rootScope.$new(),
element; element;
// Stop showing the overlay; additionally, release the scope // Stop showing the overlay; additionally, release the scope
@ -67,34 +85,13 @@ define(
scope.key = key; scope.key = key;
// Create the overlay element and add it to the document's body // Create the overlay element and add it to the document's body
element = $compile(TEMPLATE)(scope); element = this.$compile(TEMPLATE)(scope);
$document.find('body').prepend(element); this.$document.find('body').prepend(element);
return { return {
dismiss: dismiss dismiss: dismiss
}; };
}
return {
/**
* Add a new overlay to the document. This will be
* prepended to the document body; the overlay's
* template (as pointed to by the `key` argument) is
* responsible for having a useful z-order, and for
* blocking user interactions if appropriate.
*
* @param {string} key the symbolic key which identifies
* the template of the overlay to be shown
* @param {object} overlayModel the model to pass to the
* included overlay template (this will be passed
* in via ng-model)
* @memberof platform/commonUI/dialog.OverlayService#
*/
createOverlay: createOverlay
}; };
}
return OverlayService; return OverlayService;
} }