[Dialog Service] Dismiss individual dialogs. Fixes #254
This commit is contained in:
@@ -39,25 +39,28 @@ define(
|
||||
this.overlayService = overlayService;
|
||||
this.$q = $q;
|
||||
this.$log = $log;
|
||||
this.overlay = undefined;
|
||||
this.dialogVisible = false;
|
||||
this.activeOverlay = undefined;
|
||||
}
|
||||
|
||||
// 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();
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
DialogService.prototype.dismissOverlay = function (overlay) {
|
||||
//Dismiss the overlay
|
||||
overlay.dismiss();
|
||||
|
||||
//If dialog is the current active one, dismiss it
|
||||
if (overlay === this.activeOverlay) {
|
||||
this.activeOverlay = undefined;
|
||||
}
|
||||
this.dialogVisible = false;
|
||||
};
|
||||
|
||||
DialogService.prototype.getDialogResponse = function (key, model, resultGetter, typeClass) {
|
||||
// We will return this result as a promise, because user
|
||||
// input is asynchronous.
|
||||
var deferred = this.$q.defer(),
|
||||
self = this;
|
||||
self = this,
|
||||
overlay;
|
||||
|
||||
// Confirm function; this will be passed in to the
|
||||
// overlay-dialog template and associated with a
|
||||
@@ -65,9 +68,7 @@ define(
|
||||
function confirm(value) {
|
||||
// Pass along the result
|
||||
deferred.resolve(resultGetter ? resultGetter() : value);
|
||||
|
||||
// Stop showing the dialog
|
||||
self.dismiss();
|
||||
self.dismissOverlay(overlay);
|
||||
}
|
||||
|
||||
// Cancel function; this will be passed in to the
|
||||
@@ -75,7 +76,7 @@ define(
|
||||
// Cancel or X button click
|
||||
function cancel() {
|
||||
deferred.reject();
|
||||
self.dismiss();
|
||||
self.dismissOverlay(overlay);
|
||||
}
|
||||
|
||||
// Add confirm/cancel callbacks
|
||||
@@ -85,15 +86,11 @@ define(
|
||||
if (this.canShowDialog(model)) {
|
||||
// Add the overlay using the OverlayService, which
|
||||
// will handle actual insertion into the DOM
|
||||
this.overlay = this.overlayService.createOverlay(
|
||||
overlay = this.activeOverlay = this.overlayService.createOverlay(
|
||||
key,
|
||||
model,
|
||||
typeClass || "t-dialog"
|
||||
);
|
||||
|
||||
// Track that a dialog is already visible, to
|
||||
// avoid spawning multiple dialogs at once.
|
||||
this.dialogVisible = true;
|
||||
} else {
|
||||
deferred.reject();
|
||||
}
|
||||
@@ -156,7 +153,7 @@ define(
|
||||
* otherwise
|
||||
*/
|
||||
DialogService.prototype.canShowDialog = function (dialogModel) {
|
||||
if (this.dialogVisible) {
|
||||
if (this.activeOverlay) {
|
||||
// Only one dialog should be shown at a time.
|
||||
// The application design should be such that
|
||||
// we never even try to do this.
|
||||
@@ -183,6 +180,11 @@ define(
|
||||
* button is clicked
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef DialogHandle
|
||||
* @property {function} dismiss a function to dismiss the given dialog
|
||||
*/
|
||||
|
||||
/**
|
||||
* A description of the model options that may be passed to the
|
||||
* showBlockingMessage method. Note that the DialogModel desribed
|
||||
@@ -222,21 +224,26 @@ define(
|
||||
* the user can take if necessary
|
||||
* @param {DialogModel} dialogModel defines options for the dialog
|
||||
* @param {typeClass} string tells overlayService that this overlay should use appropriate CSS class
|
||||
* @returns {boolean}
|
||||
* @returns {boolean | {DialogHandle}}
|
||||
*/
|
||||
DialogService.prototype.showBlockingMessage = function (dialogModel) {
|
||||
if (this.canShowDialog(dialogModel)) {
|
||||
// Add the overlay using the OverlayService, which
|
||||
// will handle actual insertion into the DOM
|
||||
this.overlay = this.overlayService.createOverlay(
|
||||
"overlay-blocking-message",
|
||||
dialogModel,
|
||||
"t-dialog-sm"
|
||||
);
|
||||
// Track that a dialog is already visible, to
|
||||
// avoid spawning multiple dialogs at once.
|
||||
this.dialogVisible = true;
|
||||
return true;
|
||||
var self = this,
|
||||
overlay = this.overlayService.createOverlay(
|
||||
"overlay-blocking-message",
|
||||
dialogModel,
|
||||
"t-dialog-sm"
|
||||
);
|
||||
|
||||
this.activeOverlay = overlay;
|
||||
|
||||
return {
|
||||
dismiss: function () {
|
||||
self.dismissOverlay(overlay);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ define(
|
||||
it("invokes the overlay service with the correct parameters when" +
|
||||
" a blocking dialog is requested", function () {
|
||||
var dialogModel = {};
|
||||
expect(dialogService.showBlockingMessage(dialogModel)).toBe(true);
|
||||
expect(dialogService.showBlockingMessage(dialogModel)).not.toBe(false);
|
||||
expect(mockOverlayService.createOverlay).toHaveBeenCalledWith(
|
||||
"overlay-blocking-message",
|
||||
dialogModel,
|
||||
@@ -130,6 +130,45 @@ define(
|
||||
);
|
||||
});
|
||||
|
||||
describe("the blocking message dialog", function () {
|
||||
var dialogModel = {};
|
||||
var dialogHandle;
|
||||
|
||||
beforeEach(function () {
|
||||
dialogHandle = dialogService.showBlockingMessage(dialogModel);
|
||||
});
|
||||
|
||||
it("returns a handle to the dialog", function () {
|
||||
expect(dialogHandle).not.toBe(undefined);
|
||||
});
|
||||
|
||||
it("dismissing the dialog dismisses the overlay", function () {
|
||||
dialogHandle.dismiss();
|
||||
expect(mockOverlay.dismiss).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("individual dialogs can be dismissed", function () {
|
||||
var secondDialogHandle,
|
||||
secondMockOverlay;
|
||||
|
||||
dialogHandle.dismiss();
|
||||
|
||||
secondMockOverlay = jasmine.createSpyObj(
|
||||
"overlay",
|
||||
["dismiss"]
|
||||
);
|
||||
mockOverlayService.createOverlay.andReturn(secondMockOverlay);
|
||||
secondDialogHandle = dialogService.showBlockingMessage(dialogModel);
|
||||
|
||||
//Dismiss the first dialog. It should only dismiss if it
|
||||
// is active
|
||||
dialogHandle.dismiss();
|
||||
expect(secondMockOverlay.dismiss).not.toHaveBeenCalled();
|
||||
secondDialogHandle.dismiss();
|
||||
expect(secondMockOverlay.dismiss).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user