[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,127 +38,126 @@ define(
* @constructor
*/
function DialogService(overlayService, $q, $log) {
var overlay,
dialogVisible = false;
// Stop showing whatever overlay is currently active
// (e.g. because the user hit cancel)
function dismiss() {
if (overlay) {
overlay.dismiss();
}
dialogVisible = false;
}
function getDialogResponse(key, model, resultGetter) {
// We will return this result as a promise, because user
// input is asynchronous.
var deferred = $q.defer(),
overlayModel;
// Confirm function; this will be passed in to the
// overlay-dialog template and associated with a
// OK button click
function confirm(value) {
// Pass along the result
deferred.resolve(resultGetter ? resultGetter() : value);
// Stop showing the dialog
dismiss();
}
// Cancel function; this will be passed in to the
// overlay-dialog template and associated with a
// Cancel or X button click
function cancel() {
deferred.reject();
dismiss();
}
// Add confirm/cancel callbacks
model.confirm = confirm;
model.cancel = cancel;
if (dialogVisible) {
// Only one dialog should be shown at a time.
// The application design should be such that
// we never even try to do this.
$log.warn([
"Dialog already showing; ",
"unable to show ",
model.name
].join(""));
deferred.reject();
} else {
// Add the overlay using the OverlayService, which
// will handle actual insertion into the DOM
overlay = overlayService.createOverlay(
key,
model
);
// Track that a dialog is already visible, to
// avoid spawning multiple dialogs at once.
dialogVisible = true;
}
return deferred.promise;
}
function getUserInput(formModel, value) {
var overlayModel = {
title: formModel.name,
message: formModel.message,
structure: formModel,
value: value
};
// Provide result from the model
function resultGetter() {
return overlayModel.value;
}
// Show the overlay-dialog
return getDialogResponse(
"overlay-dialog",
overlayModel,
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,
* which will be shown as buttons.
*
* @param dialogModel a description of the dialog to show
* @memberof platform/commonUI/dialog.DialogService#
*/
getUserChoice: getUserChoice
};
this.overlayService = overlayService;
this.$q = $q;
this.$log = $log;
this.overlay = undefined;
this.dialogVisible = false;
}
// Stop showing whatever overlay is currently active
// (e.g. because the user hit cancel)
DialogService.prototype.dismiss = function () {
var overlay = this.overlay;
if (overlay) {
overlay.dismiss();
}
this.dialogVisible = false;
};
DialogService.prototype.getDialogResponse = function (key, model, resultGetter) {
// We will return this result as a promise, because user
// input is asynchronous.
var deferred = this.$q.defer(),
self = this;
// Confirm function; this will be passed in to the
// overlay-dialog template and associated with a
// OK button click
function confirm(value) {
// Pass along the result
deferred.resolve(resultGetter ? resultGetter() : value);
// Stop showing the dialog
self.dismiss();
}
// Cancel function; this will be passed in to the
// overlay-dialog template and associated with a
// Cancel or X button click
function cancel() {
deferred.reject();
self.dismiss();
}
// Add confirm/cancel callbacks
model.confirm = confirm;
model.cancel = cancel;
if (this.dialogVisible) {
// Only one dialog should be shown at a time.
// The application design should be such that
// we never even try to do this.
this.$log.warn([
"Dialog already showing; ",
"unable to show ",
model.name
].join(""));
deferred.reject();
} else {
// Add the overlay using the OverlayService, which
// will handle actual insertion into the DOM
this.overlay = this.overlayService.createOverlay(
key,
model
);
// Track that a dialog is already visible, to
// avoid spawning multiple dialogs at once.
this.dialogVisible = true;
}
return deferred.promise;
};
/**
* 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 = {
title: formModel.name,
message: formModel.message,
structure: formModel,
value: value
};
// Provide result from the model
function resultGetter() {
return overlayModel.value;
}
// Show the overlay-dialog
return this.getDialogResponse(
"overlay-dialog",
overlayModel,
resultGetter
);
};
/**
* Request that the user chooses from a set of options,
* which will be shown as buttons.
*
* @param dialogModel a description of the dialog to show
* @return {Promise} a promise for the user's choice
*/
DialogService.prototype.getUserChoice = function (dialogModel) {
// Show the overlay-options dialog
return this.getDialogResponse(
"overlay-options",
{ dialog: dialogModel }
);
};
return DialogService;
}
);