Skip to content

Commit

Permalink
feat($ionicModal): add hardwareBackButtonClose as option, default true
Browse files Browse the repository at this point in the history
Closes #1397
  • Loading branch information
ajoslin committed Jun 4, 2014
1 parent 74a4612 commit 9ffca1e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
19 changes: 12 additions & 7 deletions js/angular/service/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ IonicModule
'$ionicPlatform',
'$ionicTemplateLoader',
'$q',
function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $q) {
'$log',
function($rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader, $q, $log) {

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

if(self.scope.$$destroyed) {
console.error('Cannot call modal.show() after remove(). Please create a new modal instance using $ionicModal.');
$log.error('Cannot call modal.show() after remove(). Please create a new modal instance using $ionicModal.');
return;
}

console.log(self.scope);

var modalEl = jqLite(self.modalEl);

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

self._isShown = true;
self._deregisterBackButton = $ionicPlatform.registerBackButtonAction(function(){
self.hide();
}, 200);
self._deregisterBackButton = self.hardwareBackButtonClose ?
$ionicPlatform.registerBackButtonAction(
angular.bind(self, self.hide),
PLATFORM_BACK_BUTTON_PRIORITY_MODAL
) :
angular.noop;

self._isOpenPromise = $q.defer();

ionic.views.Modal.prototype.show.call(self);
Expand Down
1 change: 1 addition & 0 deletions js/angular/service/platform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;
var PLATFORM_BACK_BUTTON_PRIORITY_MODAL = 200;
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;
var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;
Expand Down
1 change: 1 addition & 0 deletions js/views/modalView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
unfocusOnHide: true,
focusFirstDelay: 600,
backdropClickToClose: true,
hardwareBackButtonClose: true,
}, opts);

ionic.extend(this, opts);
Expand Down
23 changes: 22 additions & 1 deletion test/unit/angular/service/modal.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Ionic Modal', function() {
expect(instance.scope.$destroy).toHaveBeenCalled();
}));

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

it('should not close on hardware back button if option', inject(function($ionicPlatform) {
var template = '<div class="modal"></div>';
var instance = modal.fromTemplate(template, {
hardwareBackButtonClose: false
});
spyOn($ionicPlatform, 'registerBackButtonAction');
instance.show();
timeout.flush();
expect($ionicPlatform.registerBackButtonAction).not.toHaveBeenCalled();
}));

it('should call _deregisterBackButton on hide', function() {
var template = '<div class="modal"></div>';
var instance = modal.fromTemplate(template);
instance.show();
timeout.flush();
spyOn(instance, '_deregisterBackButton');
instance.hide();
expect(instance._deregisterBackButton).toHaveBeenCalled();
});

it('should close modal on backdrop click after animate is done', function() {
var template = '<div class="modal"></div>';
var instance = modal.fromTemplate(template);
Expand Down

0 comments on commit 9ffca1e

Please sign in to comment.