[Dialog] Refactor dialogService

Refactor dialogService to remove redundant code after changes for
WTD-1033.
This commit is contained in:
Victor Woeltjen 2015-03-24 17:20:59 -07:00
parent 27af3a6b88
commit 8c35f9eb81
2 changed files with 39 additions and 63 deletions

View File

@ -16,7 +16,7 @@
href='' href=''
class="btn lg" class="btn lg"
title="{{option.description}}" title="{{option.description}}"
ng-click="ngModel.click(option.key)" ng-click="ngModel.confirm(option.key)"
ng-class="{ major: $first, subtle: !$first }"> ng-class="{ major: $first, subtle: !$first }">
{{option.name}} {{option.name}}
</a> </a>

View File

@ -26,7 +26,7 @@ define(
dialogVisible = false; dialogVisible = false;
} }
function getUserInput(formModel, value) { function getDialogResponse(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 = $q.defer(),
@ -35,9 +35,9 @@ define(
// 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
// OK button click // OK button click
function confirm() { function confirm(value) {
// Pass along the result // Pass along the result
deferred.resolve(overlayModel.value); deferred.resolve(resultGetter(value));
// Stop showing the dialog // Stop showing the dialog
dismiss(); dismiss();
@ -51,6 +51,10 @@ define(
dismiss(); dismiss();
} }
// Add confirm/cancel callbacks
model.confirm = confirm;
model.cancel = cancel;
if (dialogVisible) { if (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
@ -58,84 +62,56 @@ define(
$log.warn([ $log.warn([
"Dialog already showing; ", "Dialog already showing; ",
"unable to show ", "unable to show ",
formModel.name model.name
].join("")); ].join(""));
deferred.reject(); deferred.reject();
} else { } else {
// To be passed to the overlay-dialog template, // Add the overlay using the OverlayService, which
// via ng-model // will handle actual insertion into the DOM
overlayModel = { 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, title: formModel.name,
message: formModel.message, message: formModel.message,
structure: formModel, structure: formModel,
value: value, value: value
confirm: confirm,
cancel: cancel
}; };
// Add the overlay using the OverlayService, which // Provide result from the model
// will handle actual insertion into the DOM function resultGetter() {
overlay = overlayService.createOverlay( return overlayModel.value;
"overlay-dialog",
overlayModel
);
// Track that a dialog is already visible, to
// avoid spawning multiple dialogs at once.
dialogVisible = true;
} }
return deferred.promise; return getDialogResponse(
"overlay-dialog",
overlayModel,
resultGetter
);
} }
function getUserChoice(dialogModel) { function getUserChoice(dialogModel) {
// We will return this result as a promise, because user // We just want to pass back the result from the template
// input is asynchronous. function echo(value) {
var deferred = $q.defer(); return value;
// 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(value);
// Stop showing the dialog
dismiss();
} }
// Cancel function; this will be passed in to the return getDialogResponse(
// overlay-dialog template and associated with a
// Cancel or X button click
function cancel() {
deferred.reject();
dismiss();
}
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 ",
dialogModel.name
].join(""));
deferred.reject();
} else {
// Add the overlay using the OverlayService, which
// will handle actual insertion into the DOM
overlay = overlayService.createOverlay(
"overlay-options", "overlay-options",
{ dialog: dialogModel, click: confirm, cancel: cancel } { dialog: dialogModel },
echo
); );
// Track that a dialog is already visible, to
// avoid spawning multiple dialogs at once.
dialogVisible = true;
}
return deferred.promise;
} }
return { return {