mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat($ionicModal): add hardwareBackButtonClose as option, default true
Closes #1397
This commit is contained in:
19
js/angular/service/modal.js
vendored
19
js/angular/service/modal.js
vendored
@@ -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);
|
||||
|
||||
1
js/angular/service/platform.js
vendored
1
js/angular/service/platform.js
vendored
@@ -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;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
unfocusOnHide: true,
|
||||
focusFirstDelay: 600,
|
||||
backdropClickToClose: true,
|
||||
hardwareBackButtonClose: true,
|
||||
}, opts);
|
||||
|
||||
ionic.extend(this, opts);
|
||||
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user