From 8c35f9eb8158394f8805a9ee27696a8ccc692a76 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 24 Mar 2015 17:20:59 -0700 Subject: [PATCH] [Dialog] Refactor dialogService Refactor dialogService to remove redundant code after changes for WTD-1033. --- .../dialog/res/templates/overlay-options.html | 2 +- platform/commonUI/dialog/src/DialogService.js | 100 +++++++----------- 2 files changed, 39 insertions(+), 63 deletions(-) diff --git a/platform/commonUI/dialog/res/templates/overlay-options.html b/platform/commonUI/dialog/res/templates/overlay-options.html index 335853a017..6c0b51e991 100644 --- a/platform/commonUI/dialog/res/templates/overlay-options.html +++ b/platform/commonUI/dialog/res/templates/overlay-options.html @@ -16,7 +16,7 @@ href='' class="btn lg" title="{{option.description}}" - ng-click="ngModel.click(option.key)" + ng-click="ngModel.confirm(option.key)" ng-class="{ major: $first, subtle: !$first }"> {{option.name}} diff --git a/platform/commonUI/dialog/src/DialogService.js b/platform/commonUI/dialog/src/DialogService.js index 3dc84f3cb1..956b812bcf 100644 --- a/platform/commonUI/dialog/src/DialogService.js +++ b/platform/commonUI/dialog/src/DialogService.js @@ -26,7 +26,7 @@ define( dialogVisible = false; } - function getUserInput(formModel, value) { + function getDialogResponse(key, model, resultGetter) { // We will return this result as a promise, because user // input is asynchronous. var deferred = $q.defer(), @@ -35,9 +35,9 @@ define( // Confirm function; this will be passed in to the // overlay-dialog template and associated with a // OK button click - function confirm() { + function confirm(value) { // Pass along the result - deferred.resolve(overlayModel.value); + deferred.resolve(resultGetter(value)); // Stop showing the dialog dismiss(); @@ -51,6 +51,10 @@ define( 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 @@ -58,26 +62,15 @@ define( $log.warn([ "Dialog already showing; ", "unable to show ", - formModel.name + model.name ].join("")); deferred.reject(); } else { - // To be passed to the overlay-dialog template, - // via ng-model - overlayModel = { - title: formModel.name, - message: formModel.message, - structure: formModel, - value: value, - confirm: confirm, - cancel: cancel - }; - // Add the overlay using the OverlayService, which // will handle actual insertion into the DOM overlay = overlayService.createOverlay( - "overlay-dialog", - overlayModel + key, + model ); // Track that a dialog is already visible, to @@ -88,54 +81,37 @@ define( 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; + } + + return getDialogResponse( + "overlay-dialog", + overlayModel, + resultGetter + ); + } + function getUserChoice(dialogModel) { - // We will return this result as a promise, because user - // input is asynchronous. - var deferred = $q.defer(); - - // 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(); + // We just want to pass back the result from the template + function echo(value) { + return value; } - // 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(); - } - - 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", - { dialog: dialogModel, click: confirm, cancel: cancel } - ); - - // Track that a dialog is already visible, to - // avoid spawning multiple dialogs at once. - dialogVisible = true; - } - - return deferred.promise; + return getDialogResponse( + "overlay-options", + { dialog: dialogModel }, + echo + ); } return {