Skip to content

Commit

Permalink
feat(sideMenu): make android back button close side menu
Browse files Browse the repository at this point in the history
Closes #1264
  • Loading branch information
ajoslin committed May 6, 2014
1 parent 425ba24 commit 1010355
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
21 changes: 20 additions & 1 deletion js/angular/controller/sideMenuController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ IonicModule
'$scope',
'$attrs',
'$ionicSideMenuDelegate',
function($scope, $attrs, $ionicSideMenuDelegate) {
'$ionicPlatform',
function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform) {
var self = this;
angular.extend(this, ionic.controllers.SideMenuController.prototype);

this.$scope = $scope;

ionic.controllers.SideMenuController.call(this, {
left: { width: 275 },
right: { width: 275 }
Expand All @@ -28,6 +32,21 @@ function($scope, $attrs, $ionicSideMenuDelegate) {

$scope.sideMenuContentTranslateX = 0;


var deregisterBackButtonAction = angular.noop;
var closeSideMenu = angular.bind(this, this.close);
$scope.$watch(function() {
return self.getOpenAmount() !== 0;
}, function(isOpen) {
deregisterBackButtonAction();
if (isOpen) {
deregisterBackButtonAction = $ionicPlatform.registerBackButtonAction(
closeSideMenu,
PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU
);
}
});

var deregisterInstance = $ionicSideMenuDelegate._registerInstance(
this, $attrs.delegateHandle
);
Expand Down
1 change: 1 addition & 0 deletions js/angular/service/platform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;
var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;
Expand Down
37 changes: 37 additions & 0 deletions test/unit/angular/controller/sideMenuController.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('$ionicSideMenus controller', function() {
beforeEach(module('ionic'));

function setup(locals, props) {
var ctrl;
inject(function($controller, $rootScope) {
var scope = $rootScope.$new();
ctrl = $controller('$ionicSideMenus', angular.extend({
$scope: scope,
$attrs: {}
}, locals || {}));
angular.extend(ctrl, props || {});
});
return ctrl;
}

it('should register with backButton on open and dereg on close', inject(function($ionicPlatform) {
var openAmount = 0;
var deregSpy = jasmine.createSpy('deregister');
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn(deregSpy);

var ctrl = setup();
spyOn(ctrl, 'getOpenAmount').andCallFake(function() { return openAmount; });

expect($ionicPlatform.registerBackButtonAction).not.toHaveBeenCalled();
openAmount = 1;
ctrl.$scope.$apply();
expect($ionicPlatform.registerBackButtonAction).toHaveBeenCalledWith(
jasmine.any(Function),
PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU
);
expect(deregSpy).not.toHaveBeenCalled();
openAmount = 0;
ctrl.$scope.$apply();
expect(deregSpy).toHaveBeenCalled();
}));
});

0 comments on commit 1010355

Please sign in to comment.