-
Notifications
You must be signed in to change notification settings - Fork 77
/
ui-grid.base.js
15529 lines (13622 loc) · 571 KB
/
ui-grid.base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* ui-grid - v4.3.1 - 2018-03-14
* Copyright (c) 2018 ; License: MIT
*/
(function () {
'use strict';
angular.module('ui.grid.i18n', []);
angular.module('ui.grid', ['ui.grid.i18n']);
})();
(function () {
'use strict';
/**
* @ngdoc object
* @name ui.grid.service:uiGridConstants
* @description Constants for use across many grid features
*
*/
angular.module('ui.grid').constant('uiGridConstants', {
LOG_DEBUG_MESSAGES: true,
LOG_WARN_MESSAGES: true,
LOG_ERROR_MESSAGES: true,
CUSTOM_FILTERS: /CUSTOM_FILTERS/g,
COL_FIELD: /COL_FIELD/g,
MODEL_COL_FIELD: /MODEL_COL_FIELD/g,
TOOLTIP: /title=\"TOOLTIP\"/g,
DISPLAY_CELL_TEMPLATE: /DISPLAY_CELL_TEMPLATE/g,
TEMPLATE_REGEXP: /<.+>/,
FUNC_REGEXP: /(\([^)]*\))?$/,
DOT_REGEXP: /\./g,
APOS_REGEXP: /'/g,
BRACKET_REGEXP: /^(.*)((?:\s*\[\s*\d+\s*\]\s*)|(?:\s*\[\s*"(?:[^"\\]|\\.)*"\s*\]\s*)|(?:\s*\[\s*'(?:[^'\\]|\\.)*'\s*\]\s*))(.*)$/,
COL_CLASS_PREFIX: 'ui-grid-col',
ENTITY_BINDING: '$$this',
events: {
GRID_SCROLL: 'uiGridScroll',
COLUMN_MENU_SHOWN: 'uiGridColMenuShown',
ITEM_DRAGGING: 'uiGridItemDragStart', // For any item being dragged
COLUMN_HEADER_CLICK: 'uiGridColumnHeaderClick'
},
// copied from http://www.lsauer.com/2011/08/javascript-keymap-keycodes-in-json.html
keymap: {
TAB: 9,
STRG: 17,
CAPSLOCK: 20,
CTRL: 17,
CTRLRIGHT: 18,
CTRLR: 18,
SHIFT: 16,
RETURN: 13,
ENTER: 13,
BACKSPACE: 8,
BCKSP: 8,
ALT: 18,
ALTR: 17,
ALTRIGHT: 17,
SPACE: 32,
WIN: 91,
MAC: 91,
FN: null,
PG_UP: 33,
PG_DOWN: 34,
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
ESC: 27,
DEL: 46,
F1: 112,
F2: 113,
F3: 114,
F4: 115,
F5: 116,
F6: 117,
F7: 118,
F8: 119,
F9: 120,
F10: 121,
F11: 122,
F12: 123
},
/**
* @ngdoc object
* @name ASC
* @propertyOf ui.grid.service:uiGridConstants
* @description Used in {@link ui.grid.class:GridOptions.columnDef#properties_sort columnDef.sort} and
* {@link ui.grid.class:GridOptions.columnDef#properties_sortDirectionCycle columnDef.sortDirectionCycle}
* to configure the sorting direction of the column
*/
ASC: 'asc',
/**
* @ngdoc object
* @name DESC
* @propertyOf ui.grid.service:uiGridConstants
* @description Used in {@link ui.grid.class:GridOptions.columnDef#properties_sort columnDef.sort} and
* {@link ui.grid.class:GridOptions.columnDef#properties_sortDirectionCycle columnDef.sortDirectionCycle}
* to configure the sorting direction of the column
*/
DESC: 'desc',
/**
* @ngdoc object
* @name filter
* @propertyOf ui.grid.service:uiGridConstants
* @description Used in {@link ui.grid.class:GridOptions.columnDef#properties_filter columnDef.filter}
* to configure filtering on the column
*
* `SELECT` and `INPUT` are used with the `type` property of the filter, the rest are used to specify
* one of the built-in conditions.
*
* Available `condition` options are:
* - `uiGridConstants.filter.STARTS_WITH`
* - `uiGridConstants.filter.ENDS_WITH`
* - `uiGridConstants.filter.CONTAINS`
* - `uiGridConstants.filter.GREATER_THAN`
* - `uiGridConstants.filter.GREATER_THAN_OR_EQUAL`
* - `uiGridConstants.filter.LESS_THAN`
* - `uiGridConstants.filter.LESS_THAN_OR_EQUAL`
* - `uiGridConstants.filter.NOT_EQUAL`
*
*
* Available `type` options are:
* - `uiGridConstants.filter.SELECT` - use a dropdown box for the cell header filter field
* - `uiGridConstants.filter.INPUT` - use a text box for the cell header filter field
*/
filter: {
STARTS_WITH: 2,
ENDS_WITH: 4,
EXACT: 8,
CONTAINS: 16,
GREATER_THAN: 32,
GREATER_THAN_OR_EQUAL: 64,
LESS_THAN: 128,
LESS_THAN_OR_EQUAL: 256,
NOT_EQUAL: 512,
SELECT: 'select',
INPUT: 'input'
},
/**
* @ngdoc object
* @name aggregationTypes
* @propertyOf ui.grid.service:uiGridConstants
* @description Used in {@link ui.grid.class:GridOptions.columnDef#properties_aggregationType columnDef.aggregationType}
* to specify the type of built-in aggregation the column should use.
*
* Available options are:
* - `uiGridConstants.aggregationTypes.sum` - add the values in this column to produce the aggregated value
* - `uiGridConstants.aggregationTypes.count` - count the number of rows to produce the aggregated value
* - `uiGridConstants.aggregationTypes.avg` - average the values in this column to produce the aggregated value
* - `uiGridConstants.aggregationTypes.min` - use the minimum value in this column as the aggregated value
* - `uiGridConstants.aggregationTypes.max` - use the maximum value in this column as the aggregated value
*/
aggregationTypes: {
sum: 2,
count: 4,
avg: 8,
min: 16,
max: 32
},
/**
* @ngdoc array
* @name CURRENCY_SYMBOLS
* @propertyOf ui.grid.service:uiGridConstants
* @description A list of all presently circulating currency symbols that was copied from
* https://en.wikipedia.org/wiki/Currency_symbol#List_of_presently-circulating_currency_symbols
*
* Can be used on {@link ui.grid.class:rowSorter} to create a number string regex that ignores currency symbols.
*/
CURRENCY_SYMBOLS: ['¤', '؋', 'Ar', 'Ƀ', '฿', 'B/.', 'Br', 'Bs.', 'Bs.F.', 'GH₵', '¢', 'c', 'Ch.', '₡', 'C$', 'D', 'ден',
'دج', '.د.ب', 'د.ع', 'JD', 'د.ك', 'ل.د', 'дин', 'د.ت', 'د.م.', 'د.إ', 'Db', '$', '₫', 'Esc', '€', 'ƒ', 'Ft', 'FBu',
'FCFA', 'CFA', 'Fr', 'FRw', 'G', 'gr', '₲', 'h', '₴', '₭', 'Kč', 'kr', 'kn', 'MK', 'ZK', 'Kz', 'K', 'L', 'Le', 'лв',
'E', 'lp', 'M', 'KM', 'MT', '₥', 'Nfk', '₦', 'Nu.', 'UM', 'T$', 'MOP$', '₱', 'Pt.', '£', 'ج.م.', 'LL', 'LS', 'P', 'Q',
'q', 'R', 'R$', 'ر.ع.', 'ر.ق', 'ر.س', '៛', 'RM', 'p', 'Rf.', '₹', '₨', 'SRe', 'Rp', '₪', 'Ksh', 'Sh.So.', 'USh', 'S/',
'SDR', 'сом', '৳ ', 'WS$', '₮', 'VT', '₩', '¥', 'zł'],
/**
* @ngdoc object
* @name scrollDirection
* @propertyOf ui.grid.service:uiGridConstants
* @description Set on {@link ui.grid.class:Grid#properties_scrollDirection Grid.scrollDirection},
* to indicate the direction the grid is currently scrolling in
*
* Available options are:
* - `uiGridConstants.scrollDirection.UP` - set when the grid is scrolling up
* - `uiGridConstants.scrollDirection.DOWN` - set when the grid is scrolling down
* - `uiGridConstants.scrollDirection.LEFT` - set when the grid is scrolling left
* - `uiGridConstants.scrollDirection.RIGHT` - set when the grid is scrolling right
* - `uiGridConstants.scrollDirection.NONE` - set when the grid is not scrolling, this is the default
*/
scrollDirection: {
UP: 'up',
DOWN: 'down',
LEFT: 'left',
RIGHT: 'right',
NONE: 'none'
},
/**
* @ngdoc object
* @name dataChange
* @propertyOf ui.grid.service:uiGridConstants
* @description Used with {@link ui.grid.core.api:PublicApi#methods_notifyDataChange PublicApi.notifyDataChange},
* {@link ui.grid.class:Grid#methods_callDataChangeCallbacks Grid.callDataChangeCallbacks},
* and {@link ui.grid.class:Grid#methods_registerDataChangeCallback Grid.registerDataChangeCallback}
* to specify the type of the event(s).
*
* Available options are:
* - `uiGridConstants.dataChange.ALL` - listeners fired on any of these events, fires listeners on all events.
* - `uiGridConstants.dataChange.EDIT` - fired when the data in a cell is edited
* - `uiGridConstants.dataChange.ROW` - fired when a row is added or removed
* - `uiGridConstants.dataChange.COLUMN` - fired when the column definitions are modified
* - `uiGridConstants.dataChange.OPTIONS` - fired when the grid options are modified
*/
dataChange: {
ALL: 'all',
EDIT: 'edit',
ROW: 'row',
COLUMN: 'column',
OPTIONS: 'options'
},
/**
* @ngdoc object
* @name scrollbars
* @propertyOf ui.grid.service:uiGridConstants
* @description Used with {@link ui.grid.class:GridOptions#properties_enableHorizontalScrollbar GridOptions.enableHorizontalScrollbar}
* and {@link ui.grid.class:GridOptions#properties_enableVerticalScrollbar GridOptions.enableVerticalScrollbar}
* to specify the scrollbar policy for that direction.
*
* Available options are:
* - `uiGridConstants.scrollbars.NEVER` - never show scrollbars in this direction
* - `uiGridConstants.scrollbars.ALWAYS` - always show scrollbars in this direction
* - `uiGridConstants.scrollbars.WHEN_NEEDED` - shows scrollbars in this direction when needed
*/
scrollbars: {
NEVER: 0,
ALWAYS: 1,
WHEN_NEEDED: 2
}
});
})();
angular.module('ui.grid').directive('uiGridCell', ['$compile', '$parse', 'gridUtil', 'uiGridConstants', function ($compile, $parse, gridUtil, uiGridConstants) {
var uiGridCell = {
priority: 0,
scope: false,
require: '?^uiGrid',
compile: function() {
return {
pre: function($scope, $elm, $attrs, uiGridCtrl) {
function compileTemplate() {
var compiledElementFn = $scope.col.compiledElementFn;
compiledElementFn($scope, function(clonedElement, scope) {
$elm.append(clonedElement);
});
}
// If the grid controller is present, use it to get the compiled cell template function
if (uiGridCtrl && $scope.col.compiledElementFn) {
compileTemplate();
}
// No controller, compile the element manually (for unit tests)
else {
if ( uiGridCtrl && !$scope.col.compiledElementFn ){
// gridUtil.logError('Render has been called before precompile. Please log a ui-grid issue');
$scope.col.getCompiledElementFn()
.then(function (compiledElementFn) {
compiledElementFn($scope, function(clonedElement, scope) {
$elm.append(clonedElement);
});
}).catch(angular.noop);
}
else {
var html = $scope.col.cellTemplate
.replace(uiGridConstants.MODEL_COL_FIELD, 'row.entity.' + gridUtil.preEval($scope.col.field))
.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
var cellElement = $compile(html)($scope);
$elm.append(cellElement);
}
}
},
post: function($scope, $elm, $attrs, uiGridCtrl) {
var initColClass = $scope.col.getColClass(false);
$elm.addClass(initColClass);
var classAdded;
var updateClass = function( grid ){
var contents = $elm;
if ( classAdded ){
contents.removeClass( classAdded );
classAdded = null;
}
if (angular.isFunction($scope.col.cellClass)) {
classAdded = $scope.col.cellClass($scope.grid, $scope.row, $scope.col, $scope.rowRenderIndex, $scope.colRenderIndex);
}
else {
classAdded = $scope.col.cellClass;
}
contents.addClass(classAdded);
};
if ($scope.col.cellClass) {
updateClass();
}
// Register a data change watch that would get triggered whenever someone edits a cell or modifies column defs
var dataChangeDereg = $scope.grid.registerDataChangeCallback( updateClass, [uiGridConstants.dataChange.COLUMN, uiGridConstants.dataChange.EDIT]);
// watch the col and row to see if they change - which would indicate that we've scrolled or sorted or otherwise
// changed the row/col that this cell relates to, and we need to re-evaluate cell classes and maybe other things
var cellChangeFunction = function( n, o ){
if ( n !== o ) {
if ( classAdded || $scope.col.cellClass ){
updateClass();
}
// See if the column's internal class has changed
var newColClass = $scope.col.getColClass(false);
if (newColClass !== initColClass) {
$elm.removeClass(initColClass);
$elm.addClass(newColClass);
initColClass = newColClass;
}
}
};
// TODO(c0bra): Turn this into a deep array watch
/* shouldn't be needed any more given track by col.name
var colWatchDereg = $scope.$watch( 'col', cellChangeFunction );
*/
var rowWatchDereg = $scope.$watch( 'row', cellChangeFunction );
var deregisterFunction = function() {
dataChangeDereg();
// colWatchDereg();
rowWatchDereg();
};
$scope.$on( '$destroy', deregisterFunction );
$elm.on( '$destroy', deregisterFunction );
}
};
}
};
return uiGridCell;
}]);
(function(){
angular.module('ui.grid')
.service('uiGridColumnMenuService', [ 'i18nService', 'uiGridConstants', 'gridUtil',
function ( i18nService, uiGridConstants, gridUtil ) {
/**
* @ngdoc service
* @name ui.grid.service:uiGridColumnMenuService
*
* @description Services for working with column menus, factored out
* to make the code easier to understand
*/
var service = {
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name initialize
* @description Sets defaults, puts a reference to the $scope on
* the uiGridController
* @param {$scope} $scope the $scope from the uiGridColumnMenu
* @param {controller} uiGridCtrl the uiGridController for the grid
* we're on
*
*/
initialize: function( $scope, uiGridCtrl ){
$scope.grid = uiGridCtrl.grid;
// Store a reference to this link/controller in the main uiGrid controller
// to allow showMenu later
uiGridCtrl.columnMenuScope = $scope;
// Save whether we're shown or not so the columns can check
$scope.menuShown = false;
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name setColMenuItemWatch
* @description Setup a watch on $scope.col.menuItems, and update
* menuItems based on this. $scope.col needs to be set by the column
* before calling the menu.
* @param {$scope} $scope the $scope from the uiGridColumnMenu
* @param {controller} uiGridCtrl the uiGridController for the grid
* we're on
*
*/
setColMenuItemWatch: function ( $scope ){
var deregFunction = $scope.$watch('col.menuItems', function (n) {
if (typeof(n) !== 'undefined' && n && angular.isArray(n)) {
n.forEach(function (item) {
if (typeof(item.context) === 'undefined' || !item.context) {
item.context = {};
}
item.context.col = $scope.col;
});
$scope.menuItems = $scope.defaultMenuItems.concat(n);
}
else {
$scope.menuItems = $scope.defaultMenuItems;
}
});
$scope.$on( '$destroy', deregFunction );
},
/**
* @ngdoc boolean
* @name enableSorting
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description (optional) True by default. When enabled, this setting adds sort
* widgets to the column header, allowing sorting of the data in the individual column.
*/
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name sortable
* @description determines whether this column is sortable
* @param {$scope} $scope the $scope from the uiGridColumnMenu
*
*/
sortable: function( $scope ) {
if ( $scope.grid.options.enableSorting && typeof($scope.col) !== 'undefined' && $scope.col && $scope.col.enableSorting) {
return true;
}
else {
return false;
}
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name isActiveSort
* @description determines whether the requested sort direction is current active, to
* allow highlighting in the menu
* @param {$scope} $scope the $scope from the uiGridColumnMenu
* @param {string} direction the direction that we'd have selected for us to be active
*
*/
isActiveSort: function( $scope, direction ){
return (typeof($scope.col) !== 'undefined' && typeof($scope.col.sort) !== 'undefined' &&
typeof($scope.col.sort.direction) !== 'undefined' && $scope.col.sort.direction === direction);
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name suppressRemoveSort
* @description determines whether we should suppress the removeSort option
* @param {$scope} $scope the $scope from the uiGridColumnMenu
*
*/
suppressRemoveSort: function( $scope ) {
if ($scope.col && $scope.col.suppressRemoveSort) {
return true;
}
else {
return false;
}
},
/**
* @ngdoc boolean
* @name enableHiding
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description (optional) True by default. When set to false, this setting prevents a user from hiding the column
* using the column menu or the grid menu.
*/
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name hideable
* @description determines whether a column can be hidden, by checking the enableHiding columnDef option
* @param {$scope} $scope the $scope from the uiGridColumnMenu
*
*/
hideable: function( $scope ) {
if (typeof($scope.col) !== 'undefined' && $scope.col && $scope.col.colDef && $scope.col.colDef.enableHiding === false ) {
return false;
}
else {
return true;
}
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name getDefaultMenuItems
* @description returns the default menu items for a column menu
* @param {$scope} $scope the $scope from the uiGridColumnMenu
*
*/
getDefaultMenuItems: function( $scope ){
return [
{
title: function(){return i18nService.getSafeText('sort.ascending');},
icon: 'ui-grid-icon-sort-alt-up',
action: function($event) {
$event.stopPropagation();
$scope.sortColumn($event, uiGridConstants.ASC);
},
shown: function () {
return service.sortable( $scope );
},
active: function() {
return service.isActiveSort( $scope, uiGridConstants.ASC);
}
},
{
title: function(){return i18nService.getSafeText('sort.descending');},
icon: 'ui-grid-icon-sort-alt-down',
action: function($event) {
$event.stopPropagation();
$scope.sortColumn($event, uiGridConstants.DESC);
},
shown: function() {
return service.sortable( $scope );
},
active: function() {
return service.isActiveSort( $scope, uiGridConstants.DESC);
}
},
{
title: function(){return i18nService.getSafeText('sort.remove');},
icon: 'ui-grid-icon-cancel',
action: function ($event) {
$event.stopPropagation();
$scope.unsortColumn();
},
shown: function() {
return service.sortable( $scope ) &&
typeof($scope.col) !== 'undefined' && (typeof($scope.col.sort) !== 'undefined' &&
typeof($scope.col.sort.direction) !== 'undefined') && $scope.col.sort.direction !== null &&
!service.suppressRemoveSort( $scope );
}
},
{
title: function(){return i18nService.getSafeText('column.hide');},
icon: 'ui-grid-icon-cancel',
shown: function() {
return service.hideable( $scope );
},
action: function ($event) {
$event.stopPropagation();
$scope.hideColumn();
}
}
];
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name getColumnElementPosition
* @description gets the position information needed to place the column
* menu below the column header
* @param {$scope} $scope the $scope from the uiGridColumnMenu
* @param {GridCol} column the column we want to position below
* @param {element} $columnElement the column element we want to position below
* @returns {hash} containing left, top, offset, height, width
*
*/
getColumnElementPosition: function( $scope, column, $columnElement ){
var positionData = {};
positionData.left = $columnElement[0].offsetLeft;
positionData.top = $columnElement[0].offsetTop;
positionData.parentLeft = $columnElement[0].offsetParent.offsetLeft;
// Get the grid scrollLeft
positionData.offset = 0;
if (column.grid.options.offsetLeft) {
positionData.offset = column.grid.options.offsetLeft;
}
positionData.height = gridUtil.elementHeight($columnElement, true);
positionData.width = gridUtil.elementWidth($columnElement, true);
return positionData;
},
/**
* @ngdoc method
* @methodOf ui.grid.service:uiGridColumnMenuService
* @name repositionMenu
* @description Reposition the menu below the new column. If the menu has no child nodes
* (i.e. it's not currently visible) then we guess it's width at 100, we'll be called again
* later to fix it
* @param {$scope} $scope the $scope from the uiGridColumnMenu
* @param {GridCol} column the column we want to position below
* @param {hash} positionData a hash containing left, top, offset, height, width
* @param {element} $elm the column menu element that we want to reposition
* @param {element} $columnElement the column element that we want to reposition underneath
*
*/
repositionMenu: function( $scope, column, positionData, $elm, $columnElement ) {
var menu = $elm[0].querySelectorAll('.ui-grid-menu');
// It's possible that the render container of the column we're attaching to is
// offset from the grid (i.e. pinned containers), we need to get the difference in the offsetLeft
// between the render container and the grid
var renderContainerElm = gridUtil.closestElm($columnElement, '.ui-grid-render-container');
var renderContainerOffset = renderContainerElm.getBoundingClientRect().left - $scope.grid.element[0].getBoundingClientRect().left;
var containerScrollLeft = renderContainerElm.querySelectorAll('.ui-grid-viewport')[0].scrollLeft;
// repositionMenu is now always called after it's visible in the DOM,
// allowing us to simply get the width every time the menu is opened
var myWidth = gridUtil.elementWidth(menu, true);
var paddingRight = column.lastMenuPaddingRight ? column.lastMenuPaddingRight : ( $scope.lastMenuPaddingRight ? $scope.lastMenuPaddingRight : 10);
if ( menu.length !== 0 ){
var mid = menu[0].querySelectorAll('.ui-grid-menu-mid');
if ( mid.length !== 0 ) {
// TODO(c0bra): use padding-left/padding-right based on document direction (ltr/rtl), place menu on proper side
// Get the column menu right padding
paddingRight = parseInt(gridUtil.getStyles(angular.element(menu)[0])['paddingRight'], 10);
$scope.lastMenuPaddingRight = paddingRight;
column.lastMenuPaddingRight = paddingRight;
}
}
var left = positionData.left + renderContainerOffset - containerScrollLeft + positionData.parentLeft + positionData.width + paddingRight;
if (left < positionData.offset + myWidth) {
left = Math.max(positionData.left - containerScrollLeft + positionData.parentLeft - paddingRight + myWidth, positionData.offset + myWidth);
}
$elm.css('left', left + 'px');
$elm.css('top', (positionData.top + positionData.height) + 'px');
}
};
return service;
}])
.directive('uiGridColumnMenu', ['$timeout', 'gridUtil', 'uiGridConstants', 'uiGridColumnMenuService', '$document',
function ($timeout, gridUtil, uiGridConstants, uiGridColumnMenuService, $document) {
/**
* @ngdoc directive
* @name ui.grid.directive:uiGridColumnMenu
* @description Provides the column menu framework, leverages uiGridMenu underneath
*
*/
var uiGridColumnMenu = {
priority: 0,
scope: true,
require: '^uiGrid',
templateUrl: 'ui-grid/uiGridColumnMenu',
replace: true,
link: function ($scope, $elm, $attrs, uiGridCtrl) {
uiGridColumnMenuService.initialize( $scope, uiGridCtrl );
$scope.defaultMenuItems = uiGridColumnMenuService.getDefaultMenuItems( $scope );
// Set the menu items for use with the column menu. The user can later add additional items via the watch
$scope.menuItems = $scope.defaultMenuItems;
uiGridColumnMenuService.setColMenuItemWatch( $scope );
/**
* @ngdoc method
* @methodOf ui.grid.directive:uiGridColumnMenu
* @name showMenu
* @description Shows the column menu. If the menu is already displayed it
* calls the menu to ask it to hide (it will animate), then it repositions the menu
* to the right place whilst hidden (it will make an assumption on menu width),
* then it asks the menu to show (it will animate), then it repositions the menu again
* once we can calculate it's size.
* @param {GridCol} column the column we want to position below
* @param {element} $columnElement the column element we want to position below
*/
$scope.showMenu = function(column, $columnElement, event) {
// Swap to this column
$scope.col = column;
// Get the position information for the column element
var colElementPosition = uiGridColumnMenuService.getColumnElementPosition( $scope, column, $columnElement );
if ($scope.menuShown) {
// we want to hide, then reposition, then show, but we want to wait for animations
// we set a variable, and then rely on the menu-hidden event to call the reposition and show
$scope.colElement = $columnElement;
$scope.colElementPosition = colElementPosition;
$scope.hideThenShow = true;
$scope.$broadcast('hide-menu', { originalEvent: event });
} else {
$scope.menuShown = true;
$scope.colElement = $columnElement;
$scope.colElementPosition = colElementPosition;
$scope.$broadcast('show-menu', { originalEvent: event });
}
};
/**
* @ngdoc method
* @methodOf ui.grid.directive:uiGridColumnMenu
* @name hideMenu
* @description Hides the column menu.
* @param {boolean} broadcastTrigger true if we were triggered by a broadcast
* from the menu itself - in which case don't broadcast again as we'll get
* an infinite loop
*/
$scope.hideMenu = function( broadcastTrigger ) {
$scope.menuShown = false;
if ( !broadcastTrigger ){
$scope.$broadcast('hide-menu');
}
};
$scope.$on('menu-hidden', function() {
$elm[0].removeAttribute('style');
if ( $scope.hideThenShow ){
delete $scope.hideThenShow;
$scope.$broadcast('show-menu');
$scope.menuShown = true;
} else {
$scope.hideMenu( true );
if ($scope.col) {
//Focus on the menu button
gridUtil.focus.bySelector($document, '.ui-grid-header-cell.' + $scope.col.getColClass()+ ' .ui-grid-column-menu-button', $scope.col.grid, false);
}
}
});
$scope.$on('menu-shown', function() {
$timeout( function() {
uiGridColumnMenuService.repositionMenu( $scope, $scope.col, $scope.colElementPosition, $elm, $scope.colElement );
//automatically set the focus to the first button element in the now open menu.
gridUtil.focus.bySelector($document, '.ui-grid-menu-items .ui-grid-menu-item', true);
delete $scope.colElementPosition;
delete $scope.columnElement;
});
});
/* Column methods */
$scope.sortColumn = function (event, dir) {
event.stopPropagation();
$scope.grid.sortColumn($scope.col, dir, true)
.then(function () {
$scope.grid.refresh();
$scope.hideMenu();
}).catch(angular.noop);
};
$scope.unsortColumn = function () {
$scope.col.unsort();
$scope.grid.refresh();
$scope.hideMenu();
};
// Since we are hiding this column the default hide action will fail so we need to focus somewhere else.
var setFocusOnHideColumn = function(){
$timeout(function() {
// Get the UID of the first
var focusToGridMenu = function(){
return gridUtil.focus.byId('grid-menu', $scope.grid);
};
var thisIndex;
$scope.grid.columns.some(function(element, index){
if (angular.equals(element, $scope.col)) {
thisIndex = index;
return true;
}
});
var previousVisibleCol;
// Try and find the next lower or nearest column to focus on
$scope.grid.columns.some(function(element, index){
if (!element.visible){
return false;
} // This columns index is below the current column index
else if ( index < thisIndex){
previousVisibleCol = element;
} // This elements index is above this column index and we haven't found one that is lower
else if ( index > thisIndex && !previousVisibleCol) {
// This is the next best thing
previousVisibleCol = element;
// We've found one so use it.
return true;
} // We've reached an element with an index above this column and the previousVisibleCol variable has been set
else if (index > thisIndex && previousVisibleCol) {
// We are done.
return true;
}
});
// If found then focus on it
if (previousVisibleCol){
var colClass = previousVisibleCol.getColClass();
gridUtil.focus.bySelector($document, '.ui-grid-header-cell.' + colClass+ ' .ui-grid-header-cell-primary-focus', true).then(angular.noop, function(reason){
if (reason !== 'canceled'){ // If this is canceled then don't perform the action
//The fallback action is to focus on the grid menu
return focusToGridMenu();
}
}).catch(angular.noop);
} else {
// Fallback action to focus on the grid menu
focusToGridMenu();
}
});
};
$scope.hideColumn = function () {
$scope.col.colDef.visible = false;
$scope.col.visible = false;
$scope.grid.queueGridRefresh();
$scope.hideMenu();
$scope.grid.api.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
$scope.grid.api.core.raise.columnVisibilityChanged( $scope.col );
// We are hiding so the default action of focusing on the button that opened this menu will fail.
setFocusOnHideColumn();
};
},
controller: ['$scope', function ($scope) {
var self = this;
$scope.$watch('menuItems', function (n) {
self.menuItems = n;
});
}]
};
return uiGridColumnMenu;
}]);
})();
(function(){
'use strict';
angular.module('ui.grid').directive('uiGridFilter', ['$compile', '$templateCache', 'i18nService', 'gridUtil', function ($compile, $templateCache, i18nService, gridUtil) {
return {
compile: function() {
return {
pre: function ($scope, $elm, $attrs, controllers) {
$scope.col.updateFilters = function( filterable ){
$elm.children().remove();
if ( filterable ) {
var template = $scope.col.filterHeaderTemplate;
if (template === undefined && $scope.col.providedFilterHeaderTemplate !== '') {
if ($scope.col.filterHeaderTemplatePromise) {
$scope.col.filterHeaderTemplatePromise.then(function () {
template = $scope.col.filterHeaderTemplate;
$elm.append($compile(template)($scope));
});
}
}
else {
$elm.append($compile(template)($scope));
}
}
};
$scope.$on( '$destroy', function() {
delete $scope.col.updateFilters;
});
},
post: function ($scope, $elm, $attrs, controllers){
$scope.aria = i18nService.getSafeText('headerCell.aria');
$scope.removeFilter = function(colFilter, index){
colFilter.term = null;
//Set the focus to the filter input after the action disables the button
gridUtil.focus.bySelector($elm, '.ui-grid-filter-input-' + index);
};
}
};
}
};
}]);
})();
(function () {
'use strict';
angular.module('ui.grid').directive('uiGridFooterCell', ['$timeout', 'gridUtil', 'uiGridConstants', '$compile',
function ($timeout, gridUtil, uiGridConstants, $compile) {
var uiGridFooterCell = {
priority: 0,
scope: {
col: '=',
row: '=',
renderIndex: '='
},
replace: true,
require: '^uiGrid',
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
var template = $scope.col.footerCellTemplate;
if (template === undefined && $scope.col.providedFooterCellTemplate !== '') {
if ($scope.col.footerCellTemplatePromise) {
$scope.col.footerCellTemplatePromise.then(function () {
template = $scope.col.footerCellTemplate;
$elm.append($compile(template)($scope));
});
}
}
else {
$elm.append($compile(template)($scope));
}
},
post: function ($scope, $elm, $attrs, uiGridCtrl) {
//$elm.addClass($scope.col.getColClass(false));
$scope.grid = uiGridCtrl.grid;
var initColClass = $scope.col.getColClass(false);
$elm.addClass(initColClass);
// apply any footerCellClass
var classAdded;
var updateClass = function( grid ){
var contents = $elm;
if ( classAdded ){
contents.removeClass( classAdded );
classAdded = null;
}
if (angular.isFunction($scope.col.footerCellClass)) {
classAdded = $scope.col.footerCellClass($scope.grid, $scope.row, $scope.col, $scope.rowRenderIndex, $scope.colRenderIndex);
}
else {
classAdded = $scope.col.footerCellClass;
}
contents.addClass(classAdded);
};
if ($scope.col.footerCellClass) {
updateClass();
}
$scope.col.updateAggregationValue();
// Watch for column changes so we can alter the col cell class properly
/* shouldn't be needed any more, given track by col.name
$scope.$watch('col', function (n, o) {
if (n !== o) {
// See if the column's internal class has changed
var newColClass = $scope.col.getColClass(false);
if (newColClass !== initColClass) {
$elm.removeClass(initColClass);
$elm.addClass(newColClass);
initColClass = newColClass;
}