diff --git a/js/angular/service/modal.js b/js/angular/service/modal.js index 8802622d10b..af8c20bf3d6 100644 --- a/js/angular/service/modal.js +++ b/js/angular/service/modal.js @@ -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 @@ -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); @@ -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'); @@ -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); diff --git a/js/angular/service/platform.js b/js/angular/service/platform.js index 87208297839..053425c2c23 100644 --- a/js/angular/service/platform.js +++ b/js/angular/service/platform.js @@ -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; diff --git a/js/views/modalView.js b/js/views/modalView.js index c3832bdf75c..d9fde78de8f 100644 --- a/js/views/modalView.js +++ b/js/views/modalView.js @@ -8,6 +8,7 @@ unfocusOnHide: true, focusFirstDelay: 600, backdropClickToClose: true, + hardwareBackButtonClose: true, }, opts); ionic.extend(this, opts); diff --git a/test/unit/angular/service/modal.unit.js b/test/unit/angular/service/modal.unit.js index c3e2df40c8e..e77672a2afd 100644 --- a/test/unit/angular/service/modal.unit.js +++ b/test/unit/angular/service/modal.unit.js @@ -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 = ''; var instance = modal.fromTemplate(template); spyOn($ionicPlatform, 'registerBackButtonAction').andCallThrough(); @@ -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 = ''; + 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 = ''; + 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 = ''; var instance = modal.fromTemplate(template);