Skip to content

Commit df10da9

Browse files
JSlainmportuga
authored andcommitted
Issue #4085: Menu Items's title can now be a function (the returned value get displayed). Default Menu Items titles now are functions that return translated value, so translation changes at runtime.
1 parent d1aee28 commit df10da9

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

misc/site/index.html

+12-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ <h4 class="feature-heading">Basic Example</h4>
160160

161161
<h4 class="feature-heading">Complex Example</h4>
162162
<div class="grid" ui-grid="gridOptionsComplex" ui-grid-edit ui-grid-resize-columns></div>
163+
164+
165+
<label>Language: </label>
166+
<select ng-options="language for language in languages" ng-model="currentLanguage" ng-change="changeLanguage()"></select>
163167
</div>
164168
</div>
165169
</div>
@@ -182,10 +186,10 @@ <h4 class="feature-heading">Complex Example</h4>
182186
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
183187
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= latestAngular %>/angular.js"></script>
184188
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= latestAngular %>/angular-animate.js"></script>
185-
<script src="//ui-grid.info/release/ui-grid-unstable.min.js"></script>
189+
<script src="/release/ui-grid.js"></script>
186190
<script>
187191
angular.module('demo', ['ngAnimate', 'ui.grid', 'ui.grid.edit', 'ui.grid.resizeColumns'])
188-
.run(function($rootScope, uiGridConstants) {
192+
.run(function($rootScope, uiGridConstants, i18nService) {
189193
$rootScope.gridOptionsSimple = {
190194
data: [
191195
{
@@ -456,6 +460,12 @@ <h4 class="feature-heading">Complex Example</h4>
456460
}
457461
]
458462
};
463+
464+
$rootScope.languages = i18nService.getAllLangs();
465+
$rootScope.currentLanguage = i18nService.getCurrentLang();
466+
$rootScope.changeLanguage = function(){
467+
i18nService.setCurrentLang($rootScope.currentLanguage);
468+
};
459469
});
460470
</script>
461471

misc/tutorial/104_i18n.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ For an example using angular-translate to translate your headers, refer to http:
5454
<p>Using Filter:</p>
5555
<p>{{"groupPanel.description" | t}}</p>
5656

57-
<p>Click the header menu to see language. NOTE: TODO: header text does not change after grid is rendered. </p>
57+
<p>Click the header menu to see language.</p>
5858

5959
<div ui-grid="gridOptions" class="grid"></div>
6060
</div>

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function ( i18nService, uiGridConstants, gridUtil ) {
162162
getDefaultMenuItems: function( $scope ){
163163
return [
164164
{
165-
title: i18nService.getSafeText('sort.ascending'),
165+
title: function(){return i18nService.getSafeText('sort.ascending');},
166166
icon: 'ui-grid-icon-sort-alt-up',
167167
action: function($event) {
168168
$event.stopPropagation();
@@ -176,7 +176,7 @@ function ( i18nService, uiGridConstants, gridUtil ) {
176176
}
177177
},
178178
{
179-
title: i18nService.getSafeText('sort.descending'),
179+
title: function(){return i18nService.getSafeText('sort.descending');},
180180
icon: 'ui-grid-icon-sort-alt-down',
181181
action: function($event) {
182182
$event.stopPropagation();
@@ -190,7 +190,7 @@ function ( i18nService, uiGridConstants, gridUtil ) {
190190
}
191191
},
192192
{
193-
title: i18nService.getSafeText('sort.remove'),
193+
title: function(){return i18nService.getSafeText('sort.remove');},
194194
icon: 'ui-grid-icon-cancel',
195195
action: function ($event) {
196196
$event.stopPropagation();
@@ -204,7 +204,7 @@ function ( i18nService, uiGridConstants, gridUtil ) {
204204
}
205205
},
206206
{
207-
title: i18nService.getSafeText('column.hide'),
207+
title: function(){return i18nService.getSafeText('column.hide');},
208208
icon: 'ui-grid-icon-cancel',
209209
shown: function() {
210210
return service.hideable( $scope );

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

+10
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants, i18
301301
}
302302
};
303303

304+
$scope.label = function(){
305+
var toBeDisplayed = $scope.name;
306+
307+
if (typeof($scope.name) === 'function'){
308+
toBeDisplayed = $scope.name.call();
309+
}
310+
311+
return toBeDisplayed;
312+
};
313+
304314
$scope.i18n = i18nService.get();
305315
}
306316
};

src/templates/ui-grid/uiGridMenuItem.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
aria-hidden='true'>
1414
&nbsp;
1515
</i>
16-
{{ name }}
16+
{{ label() }}
1717
</button>

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

+22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe('ui-grid-menu', function() {
2929
{
3030
title: 'Blah 4',
3131
shown: function () { return false; }
32+
},
33+
{
34+
title: function(){return 'Blah 5';}
3235
}
3336
];
3437

@@ -297,4 +300,23 @@ describe('ui-grid-menu', function() {
297300
expect($scope.grid.options.gridMenuTemplate).toEqual(customGridMenu);
298301
});
299302
});
303+
304+
describe('title displayed', function(){
305+
beforeEach(function(){
306+
$scope.$broadcast('show-menu');
307+
$scope.$digest();
308+
});
309+
310+
it('should accept to display some text directly', function(){
311+
var item = menu.find('.ui-grid-menu-item').first();
312+
expect(item.text().trim()).toBe('Blah 1');
313+
});
314+
315+
316+
it('should accept to display the return value of a called function', function(){
317+
var item = menu.find('.ui-grid-menu-item').last();
318+
expect(item.text().trim()).toBe('Blah 5');
319+
});
320+
});
300321
});
322+

0 commit comments

Comments
 (0)