Skip to content
This repository was archived by the owner on May 4, 2022. It is now read-only.

feat(sidemenu): leaving content active when menu is open #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions js/angular/controller/sideMenuController.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,21 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io
*/
self.openPercentage = function(percentage) {
var p = percentage / 100;
var leaveContentActive = false;

if (self.left && percentage >= 0) {
self.openAmount(self.left.width * p);
leaveContentActive = self.left.leaveContentActive;
} else if (self.right && percentage < 0) {
self.openAmount(self.right.width * p);
leaveContentActive = self.right.leaveContentActive;
}

// add the CSS class "menu-open" if the percentage does not
// equal 0, otherwise remove the class from the body element
$ionicBody.enableClass((percentage !== 0), 'menu-open');
// add the CSS class "menu-open" if don't want to leave content active and the
// percentage does not equal 0, otherwise remove the class from the body element
$ionicBody.enableClass((!leaveContentActive && percentage !== 0), 'menu-open');

self.content.setCanScroll(percentage == 0);
self.content.setCanScroll(leaveContentActive || percentage == 0);
};

/*
Expand Down
12 changes: 9 additions & 3 deletions js/angular/directive/sideMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* <ion-side-menu
* side="left"
* width="myWidthValue + 20"
* is-enabled="shouldLeftSideMenuBeEnabled()">
* is-enabled="shouldLeftSideMenuBeEnabled()"
* leave-content-active="shouldLeftSideMenuLeaveMainContentActive()">
* </ion-side-menu>
* ```
* For a complete side menu example, see the
Expand All @@ -22,6 +23,7 @@
* @param {string} side Which side the side menu is currently on. Allowed values: 'left' or 'right'.
* @param {boolean=} is-enabled Whether this side menu is enabled.
* @param {number=} width How many pixels wide the side menu should be. Defaults to 275.
* @param {boolean=} leave-content-active Whether this menu should leave main content active when menu is opened. Defaults to false.
*/
IonicModule
.directive('ionSideMenu', function() {
Expand All @@ -32,6 +34,7 @@ IonicModule
compile: function(element, attr) {
angular.isUndefined(attr.isEnabled) && attr.$set('isEnabled', 'true');
angular.isUndefined(attr.width) && attr.$set('width', '275');
angular.isUndefined(attr.leaveContentActive) && attr.$set('leaveContentActive', 'false');

element.addClass('menu menu-' + attr.side);

Expand All @@ -41,7 +44,8 @@ IonicModule
var sideMenu = sideMenuCtrl[$scope.side] = new ionic.views.SideMenu({
width: attr.width,
el: $element[0],
isEnabled: true
isEnabled: true,
leaveContentActive: false
});

$scope.$watch($attr.width, function(val) {
Expand All @@ -53,8 +57,10 @@ IonicModule
$scope.$watch($attr.isEnabled, function(val) {
sideMenu.setIsEnabled(!!val);
});
$scope.$watch($attr.leaveContentActive, function(val) {
sideMenu.setLeaveContentActive(!!val);
});
};
}
};
});

4 changes: 4 additions & 0 deletions js/views/sideMenuView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
this.el = opts.el;
this.isEnabled = (typeof opts.isEnabled === 'undefined') ? true : opts.isEnabled;
this.setWidth(opts.width);
this.leaveContentActive = (typeof opts.leaveContentActive === 'undefined') ? false : opts.leaveContentActive;
},
getFullWidth: function() {
return this.width;
Expand All @@ -31,6 +32,9 @@
if(this.el.style.zIndex !== '-1') {
this.el.style.zIndex = '-1';
}
},
setLeaveContentActive: function(leaveContentActive) {
this.leaveContentActive = leaveContentActive;
}
});

Expand Down
12 changes: 12 additions & 0 deletions test/unit/angular/controller/sideMenuController.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ describe('$ionicSideMenus controller', function() {
expect(ctrl.right.isEnabled).toEqual(true);
});

// Menu leaveContentActive
it('should set leaveContentActive', function() {
ctrl.left.setLeaveContentActive(true);
ctrl.right.setLeaveContentActive(true);
expect(ctrl.left.leaveContentActive).toEqual(true);
expect(ctrl.right.leaveContentActive).toEqual(true);
ctrl.left.setLeaveContentActive(false);
ctrl.right.setLeaveContentActive(false);
expect(ctrl.left.leaveContentActive).toEqual(false);
expect(ctrl.right.leaveContentActive).toEqual(false);
});

// Menu widths
it('should init widths', function() {
expect(ctrl.left.width).toEqual(270);
Expand Down
1 change: 1 addition & 0 deletions test/unit/views/sideMenu.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ describe('SideMenu', function() {
it('Should init', function() {
expect(menu.width).toEqual(270);
expect(menu.isEnabled).toEqual(true);
expect(menu.leaveContentActive).toEqual(false);
});
});