Skip to content

Commit 5f15eab

Browse files
vwonggmportuga
authored andcommitted
feat(core): expose GridMenuTemplate
1 parent 138d149 commit 5f15eab

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
4343
templateUrl: 'ui-grid/uiGridMenu',
4444
replace: false,
4545
link: function ($scope, $elm, $attrs, uiGridCtrl) {
46-
4746
$scope.dynamicStyles = '';
47+
if (uiGridCtrl && uiGridCtrl.grid && uiGridCtrl.grid.options && uiGridCtrl.grid.options.gridMenuTemplate) {
48+
var gridMenuTemplate = uiGridCtrl.grid.options.gridMenuTemplate;
49+
gridUtil.getTemplate(gridMenuTemplate).then(function (contents) {
50+
var template = angular.element(contents);
51+
var newElm = $compile(template)($scope);
52+
$elm.replaceWith(newElm);
53+
});
54+
}
4855

4956
var setupHeightStyle = function(gridHeight) {
5057
//menu appears under header row, so substract that height from it's total
@@ -61,7 +68,7 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
6168
setupHeightStyle(uiGridCtrl.grid.gridHeight);
6269
uiGridCtrl.grid.api.core.on.gridDimensionChanged($scope, function(oldGridHeight, oldGridWidth, newGridHeight, newGridWidth) {
6370
setupHeightStyle(newGridHeight);
64-
});
71+
});
6572
}
6673

6774
$scope.i18n = {

src/js/core/factories/GridOptions.js

+9
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,15 @@ angular.module('ui.grid')
494494
*/
495495
baseOptions.rowTemplate = baseOptions.rowTemplate || 'ui-grid/ui-grid-row';
496496

497+
/**
498+
* @ngdoc string
499+
* @name gridMenuTemplate
500+
* @propertyOf ui.grid.class:GridOptions
501+
* @description 'ui-grid/uiGridMenu' by default. When provided, this setting uses a
502+
* custom grid menu template.
503+
*/
504+
baseOptions.gridMenuTemplate = baseOptions.gridMenuTemplate || 'ui-grid/uiGridMenu';
505+
497506
/**
498507
* @ngdoc object
499508
* @name appScopeProvider

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

+29
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,33 @@ describe('ui-grid-menu', function() {
268268
expect(hideSpy).toHaveBeenCalled();
269269
});
270270
});
271+
272+
describe('custom gridMenu templates', function () {
273+
var $timeout;
274+
var customGridMenu = '<div ui-grid-menu-custom menu-items="items"></div>';
275+
276+
beforeEach(inject(function (_$timeout_) {
277+
$timeout = _$timeout_;
278+
}));
279+
beforeEach( function() {
280+
recompile = function () {
281+
var element = angular.element('<div ui-grid="gridOptions"></div>');
282+
$scope.gridOptions = {};
283+
$scope.gridOptions.gridMenuTemplate = customGridMenu;
284+
$scope.gridOptions.onRegisterApi = function(gridApi) {
285+
$scope.grid = gridApi.grid;
286+
};
287+
$timeout(function () {
288+
$compile(element)($scope);
289+
});
290+
$timeout.flush();
291+
};
292+
293+
recompile();
294+
});
295+
296+
it('should have gridMenuTemplate defined in grid options', function() {
297+
expect($scope.grid.options.gridMenuTemplate).toEqual(customGridMenu);
298+
});
299+
});
271300
});

test/unit/core/factories/GridOptions.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('GridOptions factory', function () {
5151
footerTemplate: 'ui-grid/ui-grid-footer',
5252
gridFooterTemplate: 'ui-grid/ui-grid-grid-footer',
5353
rowTemplate: 'ui-grid/ui-grid-row',
54+
gridMenuTemplate: 'ui-grid/uiGridMenu',
5455
appScopeProvider: null
5556
});
5657
});
@@ -96,6 +97,7 @@ describe('GridOptions factory', function () {
9697
footerTemplate: 'testFooter',
9798
gridFooterTemplate: 'testGridFooter',
9899
rowTemplate: 'testRow',
100+
gridMenuTemplate: 'testGridMenu',
99101
extraOption: 'testExtraOption',
100102
appScopeProvider : 'anotherRef'
101103
};
@@ -139,6 +141,7 @@ describe('GridOptions factory', function () {
139141
footerTemplate: 'testFooter',
140142
gridFooterTemplate: 'testGridFooter',
141143
rowTemplate: 'testRow',
144+
gridMenuTemplate: 'testGridMenu',
142145
extraOption: 'testExtraOption',
143146
appScopeProvider : 'anotherRef'
144147
});
@@ -185,6 +188,7 @@ describe('GridOptions factory', function () {
185188
footerTemplate: 'testFooter',
186189
gridFooterTemplate: 'testGridFooter',
187190
rowTemplate: 'testRow',
191+
gridMenuTemplate: 'testGridMenu',
188192
extraOption: 'testExtraOption'
189193
};
190194
expect( GridOptions.initialize(options) ).toEqual({
@@ -227,6 +231,7 @@ describe('GridOptions factory', function () {
227231
footerTemplate: 'testFooter',
228232
gridFooterTemplate: 'testGridFooter',
229233
rowTemplate: 'testRow',
234+
gridMenuTemplate: 'testGridMenu',
230235
extraOption: 'testExtraOption',
231236
appScopeProvider : null
232237
});

0 commit comments

Comments
 (0)