Added Android back button support to action sheet, modal

This commit is contained in:
Max Lynch
2014-01-13 12:03:06 -06:00
parent 2008a72204
commit cd13cf396d
6 changed files with 112 additions and 15 deletions

View File

@@ -153,10 +153,11 @@ angular.module('ionic.ui.service.slideBoxDelegate', [])
})(ionic);
;
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.ui.actionSheet', 'ngAnimate'])
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.service.platform', 'ionic.ui.actionSheet', 'ngAnimate'])
.factory('$ionicActionSheet', ['$rootScope', '$document', '$compile', '$animate', '$timeout', '$ionicTemplateLoader',
function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoader) {
.factory('$ionicActionSheet', ['$rootScope', '$document', '$compile', '$animate', '$timeout',
'$ionicTemplateLoader', '$ionicPlatform',
function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoader, $ionicPlatform) {
return {
/**
@@ -191,6 +192,17 @@ angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ioni
});
};
var onHardwareBackButton = function() {
hideSheet();
};
scope.$on('$destroy', function() {
$ionicPlatform.offHardwareBackButton(onHardwareBackButton);
});
// Support Android back button to close
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
scope.cancel = function() {
hideSheet(true);
};
@@ -295,10 +307,10 @@ angular.module('ionic.service.loading', ['ionic.ui.loading'])
};
}]);
;
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ngAnimate'])
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.service.platform', 'ngAnimate'])
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$animate', '$q', '$ionicTemplateLoader', function($rootScope, $document, $compile, $animate, $q, $ionicTemplateLoader) {
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$animate', '$q', '$ionicPlatform', '$ionicTemplateLoader', function($rootScope, $document, $compile, $animate, $q, $ionicPlatform, $ionicTemplateLoader) {
var ModalView = ionic.views.Modal.inherit({
initialize: function(opts) {
ionic.views.Modal.prototype.initialize.call(this, opts);
@@ -317,6 +329,18 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ngAnimate'
$animate.addClass(element, this.animation, function() {
});
}
var onHardwareBackButton = function() {
_this.hide();
};
_this.scope.$on('$destroy', function() {
$ionicPlatform.offHardwareBackButton(onHardwareBackButton);
});
// Support Android back button to close
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
},
// Hide the modal
hide: function() {

View File

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,8 @@
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.ui.actionSheet', 'ngAnimate'])
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.service.platform', 'ionic.ui.actionSheet', 'ngAnimate'])
.factory('$ionicActionSheet', ['$rootScope', '$document', '$compile', '$animate', '$timeout', '$ionicTemplateLoader',
function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoader) {
.factory('$ionicActionSheet', ['$rootScope', '$document', '$compile', '$animate', '$timeout',
'$ionicTemplateLoader', '$ionicPlatform',
function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoader, $ionicPlatform) {
return {
/**
@@ -36,6 +37,17 @@ angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ioni
});
};
var onHardwareBackButton = function() {
hideSheet();
};
scope.$on('$destroy', function() {
$ionicPlatform.offHardwareBackButton(onHardwareBackButton);
});
// Support Android back button to close
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
scope.cancel = function() {
hideSheet(true);
};

View File

@@ -1,7 +1,7 @@
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ngAnimate'])
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.service.platform', 'ngAnimate'])
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$animate', '$q', '$ionicTemplateLoader', function($rootScope, $document, $compile, $animate, $q, $ionicTemplateLoader) {
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$animate', '$q', '$ionicPlatform', '$ionicTemplateLoader', function($rootScope, $document, $compile, $animate, $q, $ionicPlatform, $ionicTemplateLoader) {
var ModalView = ionic.views.Modal.inherit({
initialize: function(opts) {
ionic.views.Modal.prototype.initialize.call(this, opts);
@@ -20,6 +20,18 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ngAnimate'
$animate.addClass(element, this.animation, function() {
});
}
var onHardwareBackButton = function() {
_this.hide();
};
_this.scope.$on('$destroy', function() {
$ionicPlatform.offHardwareBackButton(onHardwareBackButton);
});
// Support Android back button to close
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
},
// Hide the modal
hide: function() {

View File

@@ -0,0 +1,29 @@
describe('Ionic ActionSheet Service', function() {
var sheet, timeout;
beforeEach(module('ionic.service.actionSheet'));
beforeEach(inject(function($ionicActionSheet, $timeout) {
sheet = $ionicActionSheet;
timeout = $timeout;
}));
it('Should show', function() {
var s = sheet.show();
expect(s.el.classList.contains('active')).toBe(true);
});
it('Should handle hardware back button', function() {
// Fake cordova
window.device = {};
var s = sheet.show();
timeout.flush();
ionic.trigger('backbutton', {
target: document
});
expect(s.el.classList.contains('active')).toBe(false);
});
});

View File

@@ -1,11 +1,12 @@
describe('Ionic Modal', function() {
var modal, q;
var modal, q, timeout;
beforeEach(module('ionic.service.modal'));
beforeEach(inject(function($ionicModal, $q, $templateCache) {
beforeEach(inject(function($ionicModal, $q, $templateCache, $timeout) {
q = $q;
modal = $ionicModal;
timeout = $timeout;
$templateCache.put('modal.html', '<div class="modal"></div>');
}));
@@ -18,7 +19,7 @@ describe('Ionic Modal', function() {
expect(modalInstance.el.classList.contains('slide-in-up')).toBe(true);
});
xit('Should show for dynamic template', function() {
it('Should show for dynamic template', function() {
var template = '<div class="modal"></div>';
var done = false;
@@ -27,11 +28,30 @@ describe('Ionic Modal', function() {
done = true;
modalInstance.show();
expect(modalInstance.el.classList.contains('modal')).toBe(true);
expect(modalInstance.el.classList.contains('active')).toBe(true);
});
timeout.flush();
waitsFor(function() {
return done;
}, "Modal should be loaded", 100);
});
it('Should close on hardware back button', function() {
var template = '<div class="modal"></div>';
var modalInstance = modal.fromTemplate(template);
modalInstance.show();
timeout.flush();
expect(modalInstance.el.classList.contains('active')).toBe(true);
ionic.trigger('backbutton', {
target: document
});
expect(modalInstance.el.classList.contains('active')).toBe(false);
});
});