Skip to content

Commit bfefc69

Browse files
committed
feat(sideMenu): allow and watch attrs width & is-enabled
1 parent 39ad3e0 commit bfefc69

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

js/ext/angular/src/directive/ionicSideMenu.js

+21-29
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,14 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture', 'ionic.service.vie
2323
.directive('sideMenus', function() {
2424
return {
2525
restrict: 'ECA',
26-
controller: ['$scope', function($scope) {
26+
controller: ['$scope', '$attrs', function($scope, $attrs) {
2727
var _this = this;
2828

2929
angular.extend(this, ionic.controllers.SideMenuController.prototype);
3030

3131
ionic.controllers.SideMenuController.call(this, {
32-
// Our quick implementation of the left side menu
33-
left: {
34-
width: 275,
35-
},
36-
37-
// Our quick implementation of the right side menu
38-
right: {
39-
width: 275,
40-
}
32+
left: { width: 275 },
33+
right: { width: 275 }
4134
});
4235

4336
$scope.sideMenuContentTranslateX = 0;
@@ -158,30 +151,29 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture', 'ionic.service.vie
158151
replace: true,
159152
transclude: true,
160153
scope: true,
161-
template: '<div class="menu menu-{{side}}"></div>',
154+
template: '<div class="menu menu-{{side}}" ng-transclude></div>',
162155
compile: function(element, attr, transclude) {
156+
angular.isUndefined(attr.isEnabled) && attr.$set('isEnabled', 'true');
157+
angular.isUndefined(attr.width) && attr.$set('width', '275');
158+
163159
return function($scope, $element, $attr, sideMenuCtrl) {
164160
$scope.side = $attr.side;
165161

166-
if($scope.side == 'left') {
167-
sideMenuCtrl.left.isEnabled = true;
168-
sideMenuCtrl.left.pushDown = function() {
169-
$element[0].style.zIndex = -1;
170-
};
171-
sideMenuCtrl.left.bringUp = function() {
172-
$element[0].style.zIndex = 0;
173-
};
174-
} else if($scope.side == 'right') {
175-
sideMenuCtrl.right.isEnabled = true;
176-
sideMenuCtrl.right.pushDown = function() {
177-
$element[0].style.zIndex = -1;
178-
};
179-
sideMenuCtrl.right.bringUp = function() {
180-
$element[0].style.zIndex = 0;
181-
};
182-
}
162+
var sideMenu = sideMenuCtrl[$scope.side] = new ionic.views.SideMenu({
163+
width: 275,
164+
el: $element[0],
165+
isEnabled: true
166+
});
183167

184-
$element.append(transclude($scope));
168+
$scope.$watch($attr.width, function(val) {
169+
var numberVal = +val;
170+
if (numberVal && numberVal == val) {
171+
sideMenu.setWidth(+val);
172+
}
173+
});
174+
$scope.$watch($attr.isEnabled, function(val) {
175+
sideMenu.setIsEnabled(!!val);
176+
});
185177
};
186178
}
187179
};

js/ext/angular/test/directive/ionicSideMenu.unit.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ describe('Ionic Side Menu Content Directive', function () {
3838
}));
3939
});
4040

41-
describe('Ionic Side Menu Directive', function () {
41+
ddescribe('Ionic Side Menu Directive', function () {
4242
var element, scope, sideMenuCtrl;
4343

4444
beforeEach(module('ionic.ui.sideMenu'));
4545

4646
beforeEach(inject(function (_$compile_, _$rootScope_) {
4747
var $compile = _$compile_;
48-
var $rootScope = _$rootScope_;
48+
var $rootScope = _$rootScope_.$new();
49+
50+
$rootScope.widthVal = 250;
51+
$rootScope.enabledVal = true;
4952

5053
var sideMenus = $compile('<side-menus>')($rootScope);
5154

5255
sideMenuCtrl = sideMenus.controller('sideMenus');
5356

54-
element = angular.element('<side-menu side="left">').appendTo(sideMenus);
57+
element = angular.element('<side-menu side="left" is-enabled="enabledVal" width="widthVal">').appendTo(sideMenus);
5558
$compile(element)($rootScope);
5659

5760
scope = element.scope();
@@ -63,4 +66,18 @@ describe('Ionic Side Menu Directive', function () {
6366
expect(sideMenuCtrl.left.pushDown).not.toBe(undefined);
6467
expect(sideMenuCtrl.left.bringUp).not.toBe(undefined);
6568
});
69+
70+
it('should watch isEnabled', function() {
71+
expect(sideMenuCtrl.left.isEnabled).toBe(true);
72+
scope.$apply('enabledVal = false');
73+
expect(sideMenuCtrl.left.isEnabled).toBe(false);
74+
});
75+
76+
it('should watch width', function() {
77+
expect(sideMenuCtrl.left.width).toBe(250);
78+
expect(sideMenuCtrl.left.el.style.width).toBe('250px');
79+
scope.$apply('widthVal = 222');
80+
expect(sideMenuCtrl.left.width).toBe(222);
81+
expect(sideMenuCtrl.left.el.style.width).toBe('222px');
82+
});
6683
});

js/ext/angular/test/sideMenu.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ <h1 class="title">Slide me</h1>
1919
</header>
2020
<content has-header="true">
2121
<toggle ng-model="$root.$draggy">Hello</toggle>
22+
<input type="range" ng-model="$root.menuWidth" min="0" max="300">
2223
<h1>Content</h1>
2324
</content>
2425
</pane>
25-
<side-menu side="left">
26+
<side-menu side="left" width="$root.menuWidth || 200">
2627
<header class="bar bar-header bar-assertive">
2728
<h1 class="title">Left</h1>
2829
</header>

js/views/sideMenuView.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
ionic.views.SideMenu = ionic.views.View.inherit({
1010
initialize: function(opts) {
1111
this.el = opts.el;
12-
this.width = opts.width;
1312
this.isEnabled = opts.isEnabled || true;
13+
this.setWidth(opts.width);
1414
},
1515

1616
getFullWidth: function() {
1717
return this.width;
1818
},
19+
setWidth: function(width) {
20+
this.width = width;
21+
this.el.style.width = width + 'px';
22+
},
1923
setIsEnabled: function(isEnabled) {
2024
this.isEnabled = isEnabled;
2125
},

0 commit comments

Comments
 (0)