Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit db90283

Browse files
Derek Louiejelbourn
authored andcommitted
feat(panel): add hook for close success. (#9819)
1 parent c810d5e commit db90283

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

src/components/panel/panel.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ angular
129129
* outside the panel to close it. Defaults to false.
130130
* - `escapeToClose` - `{boolean=}`: Whether the user can press escape to
131131
* close the panel. Defaults to false.
132+
* - `onCloseSuccess` - `{function(!panelRef, string)=}`: Function that is
133+
* called after the close successfully finishes. The first parameter passed
134+
* into this function is the current panelRef and the 2nd is an optional
135+
* string explaining the close reason. The currently supported closeReasons
136+
* can be found in the MdPanelRef.closeReasons enum. These are by default
137+
* passed along by the panel.
132138
* - `trapFocus` - `{boolean=}`: Whether focus should be trapped within the
133139
* panel. If `trapFocus` is true, the user will not be able to interact
134140
* with the rest of the page until the panel is dismissed. Defaults to
@@ -870,6 +876,18 @@ function MdPanelService($rootElement, $rootScope, $injector, $window) {
870876
* @type {enum}
871877
*/
872878
this.interceptorTypes = MdPanelRef.interceptorTypes;
879+
880+
/**
881+
* Possible values for closing of a panel.
882+
* @type {enum}
883+
*/
884+
this.closeReasons = MdPanelRef.closeReasons;
885+
886+
/**
887+
* Possible values of absolute position.
888+
* @type {enum}
889+
*/
890+
this.absPosition = MdPanelPosition.absPosition;
873891
}
874892

875893

@@ -1200,20 +1218,24 @@ MdPanelRef.prototype.open = function() {
12001218

12011219
/**
12021220
* Closes the panel.
1221+
* @param {string} closeReason The event type that triggered the close.
12031222
* @returns {!angular.$q.Promise<!MdPanelRef>} A promise that is resolved when
12041223
* the panel is closed and animations finish.
12051224
*/
1206-
MdPanelRef.prototype.close = function() {
1225+
MdPanelRef.prototype.close = function(closeReason) {
12071226
var self = this;
12081227

12091228
return this._$q(function(resolve, reject) {
12101229
self._callInterceptors(MdPanelRef.interceptorTypes.CLOSE).then(function() {
12111230
var done = self._done(resolve, self);
12121231
var detach = self._simpleBind(self.detach, self);
1232+
var onCloseSuccess = self.config['onCloseSuccess'] || angular.noop;
1233+
onCloseSuccess = angular.bind(self, onCloseSuccess, self, closeReason);
12131234

12141235
self.hide()
12151236
.then(detach)
12161237
.then(done)
1238+
.then(onCloseSuccess)
12171239
.catch(reject);
12181240
}, reject);
12191241
});
@@ -1802,7 +1824,7 @@ MdPanelRef.prototype._configureEscapeToClose = function() {
18021824
ev.stopPropagation();
18031825
ev.preventDefault();
18041826

1805-
self.close();
1827+
self.close(MdPanelRef.closeReasons.ESCAPE);
18061828
}
18071829
};
18081830

@@ -1845,7 +1867,7 @@ MdPanelRef.prototype._configureClickOutsideToClose = function() {
18451867
ev.stopPropagation();
18461868
ev.preventDefault();
18471869

1848-
self.close();
1870+
self.close(MdPanelRef.closeReasons.CLICK_OUTSIDE);
18491871
}
18501872
};
18511873

@@ -2154,6 +2176,14 @@ MdPanelRef.prototype.removeFromGroup = function(groupName) {
21542176
}
21552177
};
21562178

2179+
/**
2180+
* Possible default closeReasons for the close function.
2181+
* @enum {string}
2182+
*/
2183+
MdPanelRef.closeReasons = {
2184+
CLICK_OUTSIDE: 'clickOutsideToClose',
2185+
ESCAPE: 'escapeToClose',
2186+
};
21572187

21582188
/*****************************************************************************
21592189
* MdPanelPosition *

src/components/panel/panel.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,75 @@ describe('$mdPanel', function() {
566566
expect(PANEL_EL).not.toExist();
567567
});
568568

569+
it('should call onCloseSuccess if provided after the panel finishes ' +
570+
'closing', function() {
571+
var closeReason, closePanelRef;
572+
var onCloseSuccessCalled = false;
573+
574+
var onCloseSuccess = function(panelRef, reason) {
575+
closePanelRef = panelRef;
576+
closeReason = reason;
577+
onCloseSuccessCalled = true;
578+
return $q.when(this);
579+
};
580+
581+
var config = angular.extend(
582+
{'onCloseSuccess': onCloseSuccess }, DEFAULT_CONFIG);
583+
584+
openPanel(config);
585+
closePanel();
586+
587+
expect(onCloseSuccessCalled).toBe(true);
588+
expect(closeReason).toBe(undefined);
589+
expect(closePanelRef).toBe(panelRef);
590+
});
591+
592+
it('should call onCloseSuccess with "clickOutsideToClose" if close ' +
593+
'is triggered by clicking on the panel container', function() {
594+
var closeReason, closePanelRef;
595+
var onCloseSuccessCalled = false;
596+
597+
var onCloseSuccess = function(panelRef, reason) {
598+
closePanelRef = panelRef;
599+
closeReason = reason;
600+
onCloseSuccessCalled = true;
601+
return $q.when(this);
602+
};
603+
604+
var config = angular.extend( {'onCloseSuccess': onCloseSuccess,
605+
clickOutsideToClose: true, }, DEFAULT_CONFIG);
606+
607+
openPanel(config);
608+
clickPanelContainer();
609+
610+
expect(onCloseSuccessCalled).toBe(true);
611+
expect(closeReason).toBe($mdPanel.closeReasons.CLICK_OUTSIDE);
612+
expect(closePanelRef).toBe(panelRef);
613+
});
614+
615+
it('should call onCloseSuccess with "escapeToClose" if close ' +
616+
'is triggered by pressing escape', function() {
617+
var closePanelRef, closeReason;
618+
var onCloseSuccessCalled = false;
619+
620+
var onCloseSuccess = function(panelRef, reason) {
621+
closePanelRef = panelRef;
622+
closeReason = reason;
623+
onCloseSuccessCalled = true;
624+
return $q.when(this);
625+
};
626+
627+
var config = angular.extend( {'onCloseSuccess': onCloseSuccess,
628+
escapeToClose: true }, DEFAULT_CONFIG);
629+
630+
openPanel(config);
631+
pressEscape();
632+
633+
expect(onCloseSuccessCalled).toBe(true);
634+
expect(closeReason).toBe($mdPanel.closeReasons.ESCAPE);
635+
expect(closePanelRef).toBe(panelRef);
636+
});
637+
569638
it('should create and cleanup focus traps', function() {
570639
var config = { template: DEFAULT_TEMPLATE, trapFocus: true };
571640

0 commit comments

Comments
 (0)