Skip to content

Commit

Permalink
feat(sideMenu): add menu open and close events
Browse files Browse the repository at this point in the history
  • Loading branch information
rbutera authored and perrygovier committed May 18, 2015
1 parent 7679690 commit dbd5881
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion js/angular/controller/sideMenuController.js
Expand Up @@ -8,7 +8,8 @@ IonicModule
'$ionicHistory',
'$ionicScrollDelegate',
'IONIC_BACK_PRIORITY',
function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $ionicHistory, $ionicScrollDelegate, IONIC_BACK_PRIORITY) {
'$rootScope',
function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $ionicHistory, $ionicScrollDelegate, IONIC_BACK_PRIORITY, $rootScope) {
var self = this;
var rightShowing, leftShowing, isDragging;
var startX, lastX, offsetX, isAsideExposed;
Expand Down Expand Up @@ -63,8 +64,10 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io
self.content.enableAnimation();
if (!shouldOpen) {
self.openPercentage(0);
$rootScope.$emit('$ionicSideMenuClose', 'left');
} else {
self.openPercentage(100);
$rootScope.$emit('$ionicSideMenuOpen', 'left');
}
};

Expand All @@ -80,8 +83,10 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io
self.content.enableAnimation();
if (!shouldOpen) {
self.openPercentage(0);
$rootScope.$emit('$ionicSideMenuClose', 'right');
} else {
self.openPercentage(-100);
$rootScope.$emit('$ionicSideMenuOpen', 'right');
}
};

Expand All @@ -98,6 +103,8 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io
*/
self.close = function() {
self.openPercentage(0);
$rootScope.$emit('$ionicSideMenuClose', 'left');
$rootScope.$emit('$ionicSideMenuClose', 'right');
};

/**
Expand Down
37 changes: 37 additions & 0 deletions test/unit/angular/controller/sideMenuController.unit.js
Expand Up @@ -322,6 +322,43 @@ describe('$ionicSideMenus controller', function() {
expect(deregSpy).toHaveBeenCalled();
}));

it('should emit $ionicSideMenuOpen on open and $ionicSideMenuClose on close', inject(function($rootScope){
// create spies and event listeners
var openSpy = jasmine.createSpy('openSpy');
$rootScope.$on('$ionicSideMenuOpen', openSpy);
var closeSpy = jasmine.createSpy('openSpy');
$rootScope.$on('$ionicSideMenuClose', closeSpy);

// left open
ctrl.toggleLeft();
expect(ctrl.getOpenPercentage()).toEqual(100);
ctrl.$scope.$apply();
expect(openSpy).toHaveBeenCalled();
expect(openSpy.mostRecentCall.args[1]).toEqual("left");

// left close
ctrl.toggleLeft();
expect(ctrl.getOpenPercentage()).toEqual(0);
ctrl.$scope.$apply();
expect(closeSpy).toHaveBeenCalled();
expect(closeSpy.mostRecentCall.args[1]).toEqual("left");

// right open
ctrl.toggleRight();
expect(ctrl.getOpenPercentage()).toEqual(-100);
ctrl.$scope.$apply();
expect(openSpy).toHaveBeenCalled();
expect(openSpy.mostRecentCall.args[1]).toEqual("right");

// right close
ctrl.toggleRight();
expect(ctrl.getOpenPercentage()).toEqual(0);
ctrl.$scope.$apply();
expect(closeSpy).toHaveBeenCalled();
expect(closeSpy.mostRecentCall.args[1]).toEqual("right");
}));


it('should deregister back button action on $destroy', inject(function($ionicPlatform) {
var openAmount = 0;
var deregSpy = jasmine.createSpy('deregister');
Expand Down

1 comment on commit dbd5881

@maximilianloy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only fires $ionicSideMenuOpen and $ionicSideMenuClose when pressing the side-menu button (ion-nav-button). The same event should be triggered when the user slides-open the menu.

Please sign in to comment.