Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(panel): Adding a hook on close success.
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Louie committed Oct 12, 2016
1 parent 4fb1767 commit e204658
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/components/panel/panel.js
Expand Up @@ -126,6 +126,9 @@ angular
* outside the panel to close it. Defaults to false.
* - `escapeToClose` - `{boolean=}`: Whether the user can press escape to
* close the panel. Defaults to false.
* - `postClose` - `{function=}`: Function that is called after close
* is done. The first parameter passed into this function is the closeEvent
* and the 2nd is the current panelRef.
* - `trapFocus` - `{boolean=}`: Whether focus should be trapped within the
* panel. If `trapFocus` is true, the user will not be able to interact
* with the rest of the page until the panel is dismissed. Defaults to
Expand Down Expand Up @@ -1153,20 +1156,24 @@ MdPanelRef.prototype.open = function() {

/**
* Closes the panel.
* @param {string} closeEvent The event type that triggered the close.
* @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
* the panel is closed and animations finish.
*/
MdPanelRef.prototype.close = function() {
MdPanelRef.prototype.close = function(closeEvent) {
var self = this;

return this._$q(function(resolve, reject) {
self._callInterceptors(MdPanelRef.interceptorTypes.CLOSE).then(function() {
var done = self._done(resolve, self);
var detach = self._simpleBind(self.detach, self);
var postClose = self.config['postClose'] || angular.noop;
postClose = angular.bind(self, postClose, self, closeEvent);

self.hide()
.then(detach)
.then(done)
.then(postClose)
.catch(reject);
}, reject);
});
Expand Down Expand Up @@ -1704,7 +1711,7 @@ MdPanelRef.prototype._configureEscapeToClose = function() {
ev.stopPropagation();
ev.preventDefault();

self.close();
self.close("escapeToClose");
}
};

Expand Down Expand Up @@ -1747,7 +1754,7 @@ MdPanelRef.prototype._configureClickOutsideToClose = function() {
ev.stopPropagation();
ev.preventDefault();

self.close();
self.close("clickOutsideToClose");
}
};

Expand Down
46 changes: 46 additions & 0 deletions src/components/panel/panel.spec.js
Expand Up @@ -1043,6 +1043,7 @@ describe('$mdPanel', function() {
clickPanelContainer();

expect(myButton).toBeFocused();

});

it('escapeToClose', function() {
Expand All @@ -1055,10 +1056,55 @@ describe('$mdPanel', function() {
pressEscape();

expect(myButton).toBeFocused();

});
});
});

describe('postClose logic:', function() {
it('postClose when provided', function () {
var obj = {
postClose: function() {},
};

spyOn(obj, 'postClose').and.callThrough();
var postCloseConfig = angular.extend({ 'postClose': obj.postClose }, DEFAULT_CONFIG);

openPanel(postCloseConfig);

closePanel();
expect(obj.postClose).toHaveBeenCalledWith(panelRef, undefined);
});

it('clickOutsideToClose', function() {
var obj = {
postClose: function() {},
};

spyOn(obj, 'postClose').and.callThrough();
var postCloseConfig = angular.extend({ 'postClose': obj.postClose }, DEFAULT_CONFIG);

openPanel(postCloseConfig);

clickPanelContainer();
expect(obj.postClose).toHaveBeenCalledWith(panelRef, 'clickOutsideToClose');
});

it('escapeToClose', function() {
var obj = {
postClose: function() {},
};

spyOn(obj, 'postClose').and.callThrough();
var postCloseConfig = angular.extend({ 'postClose': obj.postClose }, DEFAULT_CONFIG);

openPanel(postCloseConfig);
pressEscape();

expect(obj.postClose).toHaveBeenCalledWith(panelRef, 'escapeToClose');
});
});

describe('grouping logic:', function() {
it('should create a group using the newPanelGroup method', function() {
$mdPanel.newPanelGroup('test');
Expand Down

0 comments on commit e204658

Please sign in to comment.