Skip to content

Commit 9ffca1e

Browse files
committed
feat($ionicModal): add hardwareBackButtonClose as option, default true
Closes #1397
1 parent 74a4612 commit 9ffca1e

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ IonicModule
5959
'$ionicPlatform',
6060
'$ionicTemplateLoader',
6161
'$q',
62-
function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $q) {
62+
'$log',
63+
function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $q, $log) {
6364

6465
/**
6566
* @ngdoc controller
@@ -88,6 +89,8 @@ function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTempla
8889
* the modal when shown. Default: false.
8990
* - `{boolean=}` `backdropClickToClose` Whether to close the modal on clicking the backdrop.
9091
* Default: true.
92+
* - `{boolean=}` `hardwareBackButtonClose` Whether the modal can be closed using the hardware
93+
* back button on Android and similar devices. Default: true.
9194
*/
9295
initialize: function(opts) {
9396
ionic.views.Modal.prototype.initialize.call(this, opts);
@@ -104,12 +107,10 @@ function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTempla
104107
var self = this;
105108

106109
if(self.scope.$$destroyed) {
107-
console.error('Cannot call modal.show() after remove(). Please create a new modal instance using $ionicModal.');
110+
$log.error('Cannot call modal.show() after remove(). Please create a new modal instance using $ionicModal.');
108111
return;
109112
}
110113

111-
console.log(self.scope);
112-
113114
var modalEl = jqLite(self.modalEl);
114115

115116
self.el.classList.remove('hide');
@@ -127,9 +128,13 @@ function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTempla
127128
.removeClass('ng-leave ng-leave-active');
128129

129130
self._isShown = true;
130-
self._deregisterBackButton = $ionicPlatform.registerBackButtonAction(function(){
131-
self.hide();
132-
}, 200);
131+
self._deregisterBackButton = self.hardwareBackButtonClose ?
132+
$ionicPlatform.registerBackButtonAction(
133+
angular.bind(self, self.hide),
134+
PLATFORM_BACK_BUTTON_PRIORITY_MODAL
135+
) :
136+
angular.noop;
137+
133138
self._isOpenPromise = $q.defer();
134139

135140
ionic.views.Modal.prototype.show.call(self);

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
22
var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;
3+
var PLATFORM_BACK_BUTTON_PRIORITY_MODAL = 200;
34
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
45
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;
56
var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;

Diff for: js/views/modalView.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
unfocusOnHide: true,
99
focusFirstDelay: 600,
1010
backdropClickToClose: true,
11+
hardwareBackButtonClose: true,
1112
}, opts);
1213

1314
ionic.extend(this, opts);

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Ionic Modal', function() {
8686
expect(instance.scope.$destroy).toHaveBeenCalled();
8787
}));
8888

89-
it('Should close on hardware back button', inject(function($ionicPlatform) {
89+
it('Should close on hardware back button by default', inject(function($ionicPlatform) {
9090
var template = '<div class="modal"></div>';
9191
var instance = modal.fromTemplate(template);
9292
spyOn($ionicPlatform, 'registerBackButtonAction').andCallThrough();
@@ -101,6 +101,27 @@ describe('Ionic Modal', function() {
101101
expect(instance.isShown()).toBe(false);
102102
}));
103103

104+
it('should not close on hardware back button if option', inject(function($ionicPlatform) {
105+
var template = '<div class="modal"></div>';
106+
var instance = modal.fromTemplate(template, {
107+
hardwareBackButtonClose: false
108+
});
109+
spyOn($ionicPlatform, 'registerBackButtonAction');
110+
instance.show();
111+
timeout.flush();
112+
expect($ionicPlatform.registerBackButtonAction).not.toHaveBeenCalled();
113+
}));
114+
115+
it('should call _deregisterBackButton on hide', function() {
116+
var template = '<div class="modal"></div>';
117+
var instance = modal.fromTemplate(template);
118+
instance.show();
119+
timeout.flush();
120+
spyOn(instance, '_deregisterBackButton');
121+
instance.hide();
122+
expect(instance._deregisterBackButton).toHaveBeenCalled();
123+
});
124+
104125
it('should close modal on backdrop click after animate is done', function() {
105126
var template = '<div class="modal"></div>';
106127
var instance = modal.fromTemplate(template);

0 commit comments

Comments
 (0)