Skip to content

Commit 1010355

Browse files
committed
feat(sideMenu): make android back button close side menu
Closes #1264
1 parent 425ba24 commit 1010355

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

js/angular/controller/sideMenuController.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ IonicModule
33
'$scope',
44
'$attrs',
55
'$ionicSideMenuDelegate',
6-
function($scope, $attrs, $ionicSideMenuDelegate) {
6+
'$ionicPlatform',
7+
function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform) {
8+
var self = this;
79
angular.extend(this, ionic.controllers.SideMenuController.prototype);
810

11+
this.$scope = $scope;
12+
913
ionic.controllers.SideMenuController.call(this, {
1014
left: { width: 275 },
1115
right: { width: 275 }
@@ -28,6 +32,21 @@ function($scope, $attrs, $ionicSideMenuDelegate) {
2832

2933
$scope.sideMenuContentTranslateX = 0;
3034

35+
36+
var deregisterBackButtonAction = angular.noop;
37+
var closeSideMenu = angular.bind(this, this.close);
38+
$scope.$watch(function() {
39+
return self.getOpenAmount() !== 0;
40+
}, function(isOpen) {
41+
deregisterBackButtonAction();
42+
if (isOpen) {
43+
deregisterBackButtonAction = $ionicPlatform.registerBackButtonAction(
44+
closeSideMenu,
45+
PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU
46+
);
47+
}
48+
});
49+
3150
var deregisterInstance = $ionicSideMenuDelegate._registerInstance(
3251
this, $attrs.delegateHandle
3352
);

js/angular/service/platform.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
2+
var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;
23
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
34
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;
45
var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
describe('$ionicSideMenus controller', function() {
2+
beforeEach(module('ionic'));
3+
4+
function setup(locals, props) {
5+
var ctrl;
6+
inject(function($controller, $rootScope) {
7+
var scope = $rootScope.$new();
8+
ctrl = $controller('$ionicSideMenus', angular.extend({
9+
$scope: scope,
10+
$attrs: {}
11+
}, locals || {}));
12+
angular.extend(ctrl, props || {});
13+
});
14+
return ctrl;
15+
}
16+
17+
it('should register with backButton on open and dereg on close', inject(function($ionicPlatform) {
18+
var openAmount = 0;
19+
var deregSpy = jasmine.createSpy('deregister');
20+
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn(deregSpy);
21+
22+
var ctrl = setup();
23+
spyOn(ctrl, 'getOpenAmount').andCallFake(function() { return openAmount; });
24+
25+
expect($ionicPlatform.registerBackButtonAction).not.toHaveBeenCalled();
26+
openAmount = 1;
27+
ctrl.$scope.$apply();
28+
expect($ionicPlatform.registerBackButtonAction).toHaveBeenCalledWith(
29+
jasmine.any(Function),
30+
PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU
31+
);
32+
expect(deregSpy).not.toHaveBeenCalled();
33+
openAmount = 0;
34+
ctrl.$scope.$apply();
35+
expect(deregSpy).toHaveBeenCalled();
36+
}));
37+
});

0 commit comments

Comments
 (0)