Skip to content

Commit

Permalink
[# 509] This Commit adds an onOpenCallback to the config options. (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashubham authored and faceleg committed Apr 22, 2017
1 parent f2ab4d4 commit 69fe9ff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ Pass ``false`` to disable template caching. Useful for developing purposes, defa

Give a name for a dialog instance. It is useful for identifying specific dialog if there are multiple dialog boxes opened.

#### ``onOpenCallback {String} | {Function}``

Provide either the name of a function or a function to be called after the dialog is opened. This callback can be used instead of the `ngdialog.opened` event.
It provides with a way to register a hook for when the dialog is appended to the DOM and about to be shown to the user.

##### ``preCloseCallback {String} | {Function}``

Provide either the name of a function or a function to be called before the dialog is closed. If the callback function specified in the option returns ``false`` then the dialog will not be closed. Alternatively, if the callback function returns a promise that gets resolved the dialog will be closed.
Expand Down
47 changes: 30 additions & 17 deletions js/ngDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
closeByNavigation: false,
appendTo: false,
preCloseCallback: false,
onOpenCallback: false,
overlay: true,
cache: true,
trapFocus: true,
Expand Down Expand Up @@ -476,6 +477,7 @@
* - closeByEscape {Boolean} - default true
* - closeByDocument {Boolean} - default true
* - preCloseCallback {String|Function} - user supplied function name/function called before closing dialog (if set)
* - onOpenCallback {String|Function} - user supplied function name/function called after opening dialog (if set)
* - bodyClassName {String} - class added to body at open dialog
* @return {Object} dialog
*/
Expand Down Expand Up @@ -585,27 +587,32 @@

privateMethods.applyAriaAttributes($dialog, options);

if (options.preCloseCallback) {
var preCloseCallback;

if (angular.isFunction(options.preCloseCallback)) {
preCloseCallback = options.preCloseCallback;
} else if (angular.isString(options.preCloseCallback)) {
if (scope) {
if (angular.isFunction(scope[options.preCloseCallback])) {
preCloseCallback = scope[options.preCloseCallback];
} else if (scope.$parent && angular.isFunction(scope.$parent[options.preCloseCallback])) {
preCloseCallback = scope.$parent[options.preCloseCallback];
} else if ($rootScope && angular.isFunction($rootScope[options.preCloseCallback])) {
preCloseCallback = $rootScope[options.preCloseCallback];
[
{ name: '$ngDialogPreCloseCallback', value: options.preCloseCallback },
{ name: '$ngDialogOnOpenCallback', value: options.onOpenCallback }
].forEach(function (option) {
if (option.value) {
var callback;

if (angular.isFunction(option.value)) {
callback = option.value;
} else if (angular.isString(option.value)) {
if (scope) {
if (angular.isFunction(scope[option.value])) {
callback = scope[option.value];
} else if (scope.$parent && angular.isFunction(scope.$parent[option.value])) {
callback = scope.$parent[option.value];
} else if ($rootScope && angular.isFunction($rootScope[option.value])) {
callback = $rootScope[option.value];
}
}
}
}

if (preCloseCallback) {
$dialog.data('$ngDialogPreCloseCallback', preCloseCallback);
if (callback) {
$dialog.data(option.name, callback);
}
}
}
});

scope.closeThisDialog = function (value) {
privateMethods.closeDialog($dialog, value);
Expand Down Expand Up @@ -665,6 +672,11 @@
} else {
$rootScope.$broadcast('ngDialog.opened', $dialog);
}
var onOpenCallback = $dialog.data('$ngDialogOnOpenCallback');
if (onOpenCallback && angular.isFunction(onOpenCallback)) {
onOpenCallback.call($dialog);
}

});

if (!keydownIsBound) {
Expand Down Expand Up @@ -891,6 +903,7 @@
closeByEscape: attrs.ngDialogCloseByEscape === 'false' ? false : (attrs.ngDialogCloseByEscape === 'true' ? true : defaults.closeByEscape),
overlay: attrs.ngDialogOverlay === 'false' ? false : (attrs.ngDialogOverlay === 'true' ? true : defaults.overlay),
preCloseCallback: attrs.ngDialogPreCloseCallback || defaults.preCloseCallback,
onOpenCallback: attrs.ngDialogOnOpenCallback || defaults.onOpenCallback,
bodyClassName: attrs.ngDialogBodyClass || defaults.bodyClassName
});
});
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/ngDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ describe('ngDialog', function () {
});
});

describe('with onOpenCallback', function () {
var elm, open;
beforeEach(inject(function (ngDialog, $timeout, $document) {
open = function (onOpenCallback) {
var id = ngDialog.open({
onOpenCallback: onOpenCallback
}).id;
$timeout.flush();
}
}));

it('onOpenCallback method should be called on opening of the dialog', function () {
var callback = spy();
open(callback);
expect(callback).toHaveBeenCalled();
})
});

describe('with custom height', function () {
var elm, open;
beforeEach(inject(function (ngDialog, $timeout, $document) {
Expand Down

0 comments on commit 69fe9ff

Please sign in to comment.