Skip to content

Commit b87bcb3

Browse files
committed
feat($ionicPopup): on android, make back button close popup
Fixes #1222
1 parent 5e56c2d commit b87bcb3

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

Diff for: js/angular/service/actionSheet.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
107107
};
108108

109109
// Support Android back button to close
110-
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(function(){
111-
hideSheet();
112-
}, 300);
110+
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(
111+
function(){
112+
hideSheet();
113+
},
114+
PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET
115+
);
113116

114117
scope.cancel = function() {
115118
hideSheet(true);

Diff for: js/angular/service/platform.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
2+
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
3+
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 500;
14
/**
25
* @ngdoc service
36
* @name $ionicPlatform

Diff for: js/angular/service/popup.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,15 @@ var POPUP_TPL =
9898
*/
9999
IonicModule
100100
.factory('$ionicPopup', [
101-
'$animate',
102101
'$ionicTemplateLoader',
103102
'$ionicBackdrop',
104-
'$log',
105103
'$q',
106104
'$timeout',
107105
'$rootScope',
108106
'$document',
109107
'$compile',
110-
function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $rootScope, $document, $compile) {
108+
'$ionicPlatform',
109+
function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $document, $compile, $ionicPlatform) {
111110
//TODO allow this to be configured
112111
var config = {
113112
stackPushDelay: 50
@@ -348,6 +347,10 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
348347
});
349348
}
350349

350+
function onHardwareBackButton(e) {
351+
popupStack[0] && popupStack[0].responseDeferred.resolve();
352+
}
353+
351354
function showPopup(options) {
352355
var popupPromise = $ionicPopup._createPopup(options);
353356
var previousPopup = popupStack[0];
@@ -363,6 +366,10 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
363366
//Add popup-open & backdrop if this is first popup
364367
document.body.classList.add('popup-open');
365368
$ionicBackdrop.retain();
369+
$ionicPopup._backButtonActionDone = $ionicPlatform.registerBackButtonAction(
370+
onHardwareBackButton,
371+
PLATFORM_BACK_BUTTON_PRIORITY_POPUP
372+
);
366373
}
367374
popupStack.unshift(popup);
368375
popup.show();
@@ -386,6 +393,7 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
386393
//Remove popup-open & backdrop if this is last popup
387394
document.body.classList.remove('popup-open');
388395
$ionicBackdrop.release();
396+
($ionicPopup._backButtonActionDone || angular.noop)();
389397
}
390398

391399
return result;

Diff for: js/angular/service/viewService.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ function($rootScope, $state, $location, $document, $animate, $ionicPlatform, $io
7979
e.preventDefault();
8080
return false;
8181
}
82-
$ionicPlatform.registerBackButtonAction(onHardwareBackButton, 100);
82+
$ionicPlatform.registerBackButtonAction(
83+
onHardwareBackButton,
84+
PLATFORM_BACK_BUTTON_PRIORITY_VIEW
85+
);
8386

8487
}])
8588

Diff for: test/unit/angular/service/popup.unit.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,21 @@ describe('$ionicPopup service', function() {
153153
afterEach(function() {
154154
document.body.classList.remove('popup-open');
155155
});
156-
it('should add popup-open and retain backdrop if no previous popup', inject(function($ionicBackdrop, $timeout) {
156+
157+
it('should add popup-open and retain backdrop and register back button action if no previous popup', inject(function($ionicBackdrop, $timeout, $ionicPlatform) {
158+
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn('actionReturn');
157159
spyOn($ionicBackdrop, 'retain');
158160
$ionicPopup.show();
159161
$timeout.flush();
160162
expect(angular.element(document.body).hasClass('popup-open')).toBe(true);
161163
expect($ionicBackdrop.retain).toHaveBeenCalled();
164+
expect($ionicPlatform.registerBackButtonAction).toHaveBeenCalledWith(
165+
jasmine.any(Function),
166+
PLATFORM_BACK_BUTTON_PRIORITY_POPUP
167+
);
168+
expect($ionicPopup._backButtonActionDone).toBe('actionReturn');
162169
}));
170+
163171
it('should hide previous popup if exists and not popup-open & backdrop', inject(function($ionicBackdrop, $timeout) {
164172
var previousPopup = { hide: jasmine.createSpy('hide') };
165173
spyOn($ionicBackdrop, 'retain');
@@ -231,19 +239,21 @@ describe('$ionicPopup service', function() {
231239
expect(previousPopup.show).toHaveBeenCalled();
232240
}));
233241

234-
it('should release backdrop and remove popup-open if no previous', inject(function($q, $timeout, $ionicBackdrop) {
242+
it('should release backdrop and remove popup-open and deregister back if no previous', inject(function($q, $timeout, $ionicBackdrop, $ionicPlatform) {
235243
var fakePopup = {
236244
show: jasmine.createSpy('show'),
237245
remove: jasmine.createSpy('remove'),
238246
responseDeferred: $q.defer()
239247
};
240-
document.body.classList.add('popup-open');
248+
var backDoneSpy = jasmine.createSpy('backDone');
249+
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn(backDoneSpy);
241250
spyOn($ionicBackdrop, 'release');
242251
spyOn($ionicPopup, '_createPopup').andReturn($q.when(fakePopup));
243252
$ionicPopup.show();
244253
fakePopup.responseDeferred.resolve();
245254
$timeout.flush();
246255
expect($ionicBackdrop.release).toHaveBeenCalled();
256+
expect(backDoneSpy).toHaveBeenCalled();
247257
expect(document.body.classList.contains('popup-open')).toBe(false);
248258
}));
249259
});

0 commit comments

Comments
 (0)