Skip to content

Commit a7b3aa9

Browse files
author
Alex Hokanson
committed
feature(ui-grid-menu): grid menu does not extend past the grid bounds
1 parent 5886ca7 commit a7b3aa9

File tree

4 files changed

+92
-36
lines changed

4 files changed

+92
-36
lines changed

src/js/core/directives/ui-grid-menu.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
4646
var self = this;
4747
var menuMid;
4848
var $animate;
49+
var gridMenuMaxHeight;
50+
51+
$scope.dynamicStyles = '';
52+
53+
if (uiGridCtrl) {
54+
// magic number of 30 because the grid menu displays somewhat below
55+
// the top of the grid. It is approximately 30px.
56+
gridMenuMaxHeight = uiGridCtrl.grid.gridHeight - 30;
57+
$scope.dynamicStyles = [
58+
'.grid' + uiGridCtrl.grid.id + ' .ui-grid-menu-mid {',
59+
'max-height: ' + gridMenuMaxHeight + 'px;',
60+
'}'
61+
].join(' ');
62+
}
4963

5064
$scope.i18n = {
5165
close: i18nService.getSafeText('columnMenu.close')

src/less/menu.less

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
right: 0;
1919
.ui-grid-menu-mid {
2020
overflow: scroll;
21-
max-height: 300px;
2221
border: @gridBorderWidth solid @borderColor;
2322
}
2423
}

src/templates/ui-grid/uiGridMenu.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<div
22
class="ui-grid-menu"
33
ng-if="shown">
4+
<style ui-grid-style>
5+
{{dynamicStyles}}
6+
</style>
47
<div
58
class="ui-grid-menu-mid"
69
ng-show="shownMid">

test/unit/core/directives/ui-grid-menu.spec.js

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
describe('ui-grid-menu', function() {
2-
var $scope, $compile, menu, inner, recompile;
2+
var $scope, $compile, $controller, menu, inner, recompile, isolateScope;
33

44
beforeEach(module('ui.grid'));
55

6-
beforeEach(inject(function (_$compile_, $rootScope) {
6+
beforeEach(inject(function (_$compile_, $rootScope, _$controller_) {
77
$scope = $rootScope;
88
$compile = _$compile_;
9+
$controller = _$controller_;
910

1011
$scope.foo = null;
1112

@@ -31,40 +32,79 @@ describe('ui-grid-menu', function() {
3132
}
3233
];
3334

34-
// $scope.isShown = true;
35-
3635
recompile = function () {
3736
menu = angular.element('<div ui-grid-menu menu-items="items"></div>');
38-
$compile(menu)($scope);
37+
menu = $compile(menu)($scope);
3938
$scope.$digest();
39+
isolateScope = menu.isolateScope();
4040
};
4141

4242
recompile();
4343
}));
4444

4545
describe( 'basic show/hide', function() {
46-
46+
4747
it('should hide the menu by default', function () {
4848
expect(menu.find('.ui-grid-menu-inner').length).toEqual(0);
4949
});
50-
50+
5151
// TODO(c0bra): Change to test hide-menu & show-menu events
5252
// it('should be shown when the shown property is a true boolean', function () {
5353
// $scope.isShown = true;
5454
// $scope.$digest();
55-
55+
5656
// expect(inner.hasClass('ng-hide')).toBe(false);
5757
// });
58-
58+
5959
// it('should be shown when the shown property is a function that returns true', function () {
6060
// $scope.isShown = function() { return true; };
6161
// $scope.$digest();
62-
62+
6363
// expect(inner.hasClass('ng-hide')).toBe(false);
6464
// });
65-
66-
65+
66+
67+
});
68+
69+
describe( 'dynamic height', function () {
70+
it('should set $scope.dynamicStyles to empty string if there is not uiGridCtrl', function () {
71+
expect(isolateScope.dynamicStyles).toEqual('');
72+
});
73+
74+
it('should set $scope.dynamicStyles to a style that limits the height of ui-grid-menu-mid', function () {
75+
$scope.gridOptions = {
76+
data: []
77+
};
78+
compileWithGrid();
79+
80+
$scope.$broadcast('show-menu');
81+
82+
expect(isolateScope.dynamicStyles).toBeDefined();
83+
expect(isolateScope.dynamicStyles).toContain('.grid1234 .ui-grid-menu-mid { max-height: 370px; }');
84+
});
85+
86+
function compileWithGrid () {
87+
var grid = angular.element('<div><div id="test-grid-menu" ui-grid-menu menu-items="items"></div></div>');
88+
grid.data('$uiGridController', $controller(function ($scope) {
89+
this.grid = {
90+
gridHeight: 400,
91+
id: '1234',
92+
api: {
93+
core: {
94+
on: {
95+
scrollBegin: angular.noop
96+
}
97+
}
98+
}
99+
};
100+
}, { $scope: $scope.$new() }));
101+
grid = $compile(grid)($scope);
102+
menu = grid.find('#test-grid-menu');
103+
$scope.$digest();
104+
isolateScope = menu.isolateScope();
105+
}
67106
});
107+
68108
describe( 'actions with menu displayed', function() {
69109
beforeEach( function() {
70110
$scope.$broadcast('show-menu');
@@ -74,35 +114,35 @@ describe('ui-grid-menu', function() {
74114

75115
it('should create a list of menu items from the menuItems attribute', function() {
76116
var items = menu.find('.ui-grid-menu-item');
77-
117+
78118
expect(items.length).toEqual($scope.items.length);
79119
});
80-
120+
81121
it("should obey menu item's 'shown' property", function() {
82122
$scope.items[0].shown = function () { return false; };
83123
recompile();
84-
124+
85125
$scope.$broadcast('show-menu');
86126
$scope.$digest();
87127
expect(menu.find('.ui-grid-menu-inner').length).toEqual(1);
88-
128+
89129
var item = menu.find('.ui-grid-menu-item').first();
90130
expect(item.hasClass('ng-hide')).toBe(true);
91131
});
92-
132+
93133
it("should run an item's action when it's clicked", function() {
94134
var item = menu.find('.ui-grid-menu-item').first();
95135
item.trigger('click');
96136
$scope.$digest();
97-
137+
98138
expect($scope.items[0].action).toHaveBeenCalled();
99139
});
100-
140+
101141
it("should run an item's action when it's clicked (part 2)", function() {
102142
var item2 = menu.find('.ui-grid-menu-item:nth(1)').first();
103143
item2.trigger('click');
104144
$scope.$digest();
105-
145+
106146
expect($scope.foo).toEqual('blah');
107147
});
108148

@@ -122,44 +162,44 @@ describe('ui-grid-menu', function() {
122162

123163
it('should add the active class if the item is active', function() {
124164
var item = menu.find('.ui-grid-menu-item:nth(0)').first();
125-
165+
126166
expect(item.hasClass('ui-grid-menu-item-active')).toBe(true, 'item gets active class');
127167
});
128168

129169
it('should add the active class if the active property is a function that returns true', function() {
130170
var item = menu.find('.ui-grid-menu-item:nth(0)').first();
131-
171+
132172
$scope.items[0].active = function() { return true; };
133173
$scope.$digest();
134-
174+
135175
expect(item.hasClass('ui-grid-menu-item-active')).toBe(true);
136176
});
137177

138178
it('should hide a menu item based on its shown property', function() {
139179
var item = menu.find('.ui-grid-menu-item:nth(3)').first();
140-
180+
141181
expect(item.hasClass('ng-hide')).toBe(true);
142182
});
143-
/*
183+
/*
144184
PaulL: commented out as seems to be the cause of the intermittent unit test failures
145-
Will wait to see if they're genuinely gone, then work out why this test causes that
146-
185+
Will wait to see if they're genuinely gone, then work out why this test causes that
186+
147187
it("should throw an exception when an item's 'shown' property is not a function", function () {
148188
$scope.items[0].shown = 'shown goobers';
149-
189+
150190
expect(function() {
151191
recompile();
152192
}).toThrow();
153193
});
154-
194+
155195
it("should throw an exception when an item's 'active' property is not a function", function () {
156196
$scope.items[0].active = 'active goobers';
157-
197+
158198
expect(function() {
159199
recompile();
160200
}).toThrow();
161201
});
162-
*/
202+
*/
163203
});
164204

165205

@@ -173,21 +213,21 @@ describe('ui-grid-menu', function() {
173213
title: 'Blah 1'
174214
}
175215
];
176-
216+
177217
menu = angular.element('<div ui-grid-menu menu-items="items"></div>');
178218
$compile(menu)($scope);
179219
$scope.$digest();
180-
220+
181221
$scope.$broadcast('show-menu');
182222
$scope.$digest();
183-
223+
184224
inner = $(menu).find('.ui-grid-menu-inner').first();
185225
expect(inner.length).toEqual(1);
186226
}));
187227

188228
it("should display a menu item by default if no 'shown' property is passed", function() {
189229
var item = menu.find('.ui-grid-menu-item').first();
190-
230+
191231
expect(item.hasClass('ng-hide')).toBe(false);
192232
});
193233
});

0 commit comments

Comments
 (0)