-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Grid.js
1639 lines (1349 loc) · 50.1 KB
/
Grid.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
(function(){
angular.module('ui.grid')
.factory('Grid', ['$log', '$q', '$compile', '$parse', 'gridUtil', 'uiGridConstants', 'GridOptions', 'GridColumn', 'GridRow', 'GridApi', 'rowSorter', 'rowSearcher', 'GridRenderContainer', '$timeout',
function($log, $q, $compile, $parse, gridUtil, uiGridConstants, GridOptions, GridColumn, GridRow, GridApi, rowSorter, rowSearcher, GridRenderContainer, $timeout) {
/**
* @ngdoc object
* @name ui.grid.core.api:PublicApi
* @description Public Api for the core grid features
*
*/
/**
* @ngdoc function
* @name ui.grid.class:Grid
* @description Grid is the main viewModel. Any properties or methods needed to maintain state are defined in
* * this prototype. One instance of Grid is created per Grid directive instance.
* @param {object} options Object map of options to pass into the grid. An 'id' property is expected.
*/
var Grid = function Grid(options) {
var self = this;
// Get the id out of the options, then remove it
if (options !== undefined && typeof(options.id) !== 'undefined' && options.id) {
if (!/^[_a-zA-Z0-9-]+$/.test(options.id)) {
throw new Error("Grid id '" + options.id + '" is invalid. It must follow CSS selector syntax rules.');
}
}
else {
throw new Error('No ID provided. An ID must be given when creating a grid.');
}
self.id = options.id;
delete options.id;
// Get default options
self.options = new GridOptions();
// Extend the default options with what we were passed in
angular.extend(self.options, options);
self.headerHeight = self.options.headerRowHeight;
self.footerHeight = self.options.showFooter === true ? self.options.footerRowHeight : 0;
self.rtl = false;
self.gridHeight = 0;
self.gridWidth = 0;
self.columnBuilders = [];
self.rowBuilders = [];
self.rowsProcessors = [];
self.columnsProcessors = [];
self.styleComputations = [];
self.viewportAdjusters = [];
self.rowHeaderColumns = [];
// self.visibleRowCache = [];
// Set of 'render' containers for self grid, which can render sets of rows
self.renderContainers = {};
// Create a
self.renderContainers.body = new GridRenderContainer('body', self);
self.cellValueGetterCache = {};
// Cached function to use with custom row templates
self.getRowTemplateFn = null;
//representation of the rows on the grid.
//these are wrapped references to the actual data rows (options.data)
self.rows = [];
//represents the columns on the grid
self.columns = [];
/**
* @ngdoc boolean
* @name isScrollingVertically
* @propertyOf ui.grid.class:Grid
* @description set to true when Grid is scrolling vertically. Set to false via debounced method
*/
self.isScrollingVertically = false;
/**
* @ngdoc boolean
* @name isScrollingHorizontally
* @propertyOf ui.grid.class:Grid
* @description set to true when Grid is scrolling horizontally. Set to false via debounced method
*/
self.isScrollingHorizontally = false;
var debouncedVertical = gridUtil.debounce(function () {
self.isScrollingVertically = false;
}, 300);
var debouncedHorizontal = gridUtil.debounce(function () {
self.isScrollingHorizontally = false;
}, 300);
/**
* @ngdoc function
* @name flagScrollingVertically
* @methodOf ui.grid.class:Grid
* @description sets isScrollingVertically to true and sets it to false in a debounced function
*/
self.flagScrollingVertically = function() {
self.isScrollingVertically = true;
debouncedVertical();
};
/**
* @ngdoc function
* @name flagScrollingHorizontally
* @methodOf ui.grid.class:Grid
* @description sets isScrollingHorizontally to true and sets it to false in a debounced function
*/
self.flagScrollingHorizontally = function() {
self.isScrollingHorizontally = true;
debouncedHorizontal();
};
self.api = new GridApi(self);
/**
* @ngdoc function
* @name refresh
* @methodOf ui.grid.core.api:PublicApi
* @description Refresh the rendered grid on screen.
*
*/
self.api.registerMethod( 'core', 'refresh', this.refresh );
/**
* @ngdoc function
* @name refreshRows
* @methodOf ui.grid.core.api:PublicApi
* @description Refresh the rendered grid on screen? Note: not functional at present
* @returns {promise} promise that is resolved when render completes?
*
*/
self.api.registerMethod( 'core', 'refreshRows', this.refreshRows );
/**
* @ngdoc function
* @name handleWindowResize
* @methodOf ui.grid.core.api:PublicApi
* @description Trigger a grid resize, normally this would be picked
* up by a watch on window size, but in some circumstances it is necessary
* to call this manually
* @returns {promise} promise that is resolved when render completes?
*
*/
self.api.registerMethod( 'core', 'handleWindowResize', this.handleWindowResize );
/**
* @ngdoc function
* @name addRowHeaderColumn
* @methodOf ui.grid.core.api:PublicApi
* @description adds a row header column to the grid
* @param {object} column def
*
*/
self.api.registerMethod( 'core', 'addRowHeaderColumn', this.addRowHeaderColumn );
/**
* @ngdoc function
* @name sortHandleNulls
* @methodOf ui.grid.core.api:PublicApi
* @description A null handling method that can be used when building custom sort
* functions
* @example
* <pre>
* mySortFn = function(a, b) {
* var nulls = $scope.gridApi.core.sortHandleNulls(a, b);
* if ( nulls !== null ){
* return nulls;
* } else {
* // your code for sorting here
* };
* </pre>
* @param {object} a sort value a
* @param {object} b sort value b
* @returns {number} null if there were no nulls/undefineds, otherwise returns
* a sort value that should be passed back from the sort function
*
*/
self.api.registerMethod( 'core', 'sortHandleNulls', rowSorter.handleNulls );
/**
* @ngdoc function
* @name sortChanged
* @methodOf ui.grid.core.api:PublicApi
* @description The sort criteria on one or more columns has
* changed. Provides as parameters the grid and the output of
* getColumnSorting, which is an array of gridColumns
* that have sorting on them, sorted in priority order.
*
* @param {Grid} grid the grid
* @param {array} sortColumns an array of columns with
* sorts on them, in priority order
*
* @example
* <pre>
* gridApi.core.on.sortChanged( grid, sortColumns );
* </pre>
*/
self.api.registerEvent( 'core', 'sortChanged' );
};
/**
* @ngdoc function
* @name isRTL
* @methodOf ui.grid.class:Grid
* @description Returns true if grid is RightToLeft
*/
Grid.prototype.isRTL = function () {
return this.rtl;
};
/**
* @ngdoc function
* @name registerColumnBuilder
* @methodOf ui.grid.class:Grid
* @description When the build creates columns from column definitions, the columnbuilders will be called to add
* additional properties to the column.
* @param {function(colDef, col, gridOptions)} columnsProcessor function to be called
*/
Grid.prototype.registerColumnBuilder = function registerColumnBuilder(columnBuilder) {
this.columnBuilders.push(columnBuilder);
};
/**
* @ngdoc function
* @name buildColumnDefsFromData
* @methodOf ui.grid.class:Grid
* @description Populates columnDefs from the provided data
* @param {function(colDef, col, gridOptions)} rowBuilder function to be called
*/
Grid.prototype.buildColumnDefsFromData = function (dataRows){
this.options.columnDefs = gridUtil.getColumnsFromData(dataRows, this.options.excludeProperties);
};
/**
* @ngdoc function
* @name registerRowBuilder
* @methodOf ui.grid.class:Grid
* @description When the build creates rows from gridOptions.data, the rowBuilders will be called to add
* additional properties to the row.
* @param {function(colDef, col, gridOptions)} rowBuilder function to be called
*/
Grid.prototype.registerRowBuilder = function registerRowBuilder(rowBuilder) {
this.rowBuilders.push(rowBuilder);
};
/**
* @ngdoc function
* @name getColumn
* @methodOf ui.grid.class:Grid
* @description returns a grid column for the column name
* @param {string} name column name
*/
Grid.prototype.getColumn = function getColumn(name) {
var columns = this.columns.filter(function (column) {
return column.colDef.name === name;
});
return columns.length > 0 ? columns[0] : null;
};
/**
* @ngdoc function
* @name getColDef
* @methodOf ui.grid.class:Grid
* @description returns a grid colDef for the column name
* @param {string} name column.field
*/
Grid.prototype.getColDef = function getColDef(name) {
var colDefs = this.options.columnDefs.filter(function (colDef) {
return colDef.name === name;
});
return colDefs.length > 0 ? colDefs[0] : null;
};
/**
* @ngdoc function
* @name assignTypes
* @methodOf ui.grid.class:Grid
* @description uses the first row of data to assign colDef.type for any types not defined.
*/
/**
* @ngdoc property
* @name type
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description the type of the column, used in sorting. If not provided then the
* grid will guess the type. Add this only if the grid guessing is not to your
* satisfaction. Refer to {@link ui.grid.service:GridUtil.guessType gridUtil.guessType} for
* a list of values the grid knows about.
*
*/
Grid.prototype.assignTypes = function(){
var self = this;
self.options.columnDefs.forEach(function (colDef, index) {
//Assign colDef type if not specified
if (!colDef.type) {
var col = new GridColumn(colDef, index, self);
var firstRow = self.rows.length > 0 ? self.rows[0] : null;
if (firstRow) {
colDef.type = gridUtil.guessType(self.getCellValue(firstRow, col));
}
else {
$log.log('Unable to assign type from data, so defaulting to string');
colDef.type = 'string';
}
}
});
};
/**
* @ngdoc function
* @name addRowHeaderColumn
* @methodOf ui.grid.class:Grid
* @description adds a row header column to the grid
* @param {object} column def
*/
Grid.prototype.addRowHeaderColumn = function addRowHeaderColumn(colDef) {
var self = this;
//self.createLeftContainer();
var rowHeaderCol = new GridColumn(colDef, self.rowHeaderColumns.length, self);
rowHeaderCol.isRowHeader = true;
if (self.isRTL()) {
self.createRightContainer();
rowHeaderCol.renderContainer = 'right';
}
else {
self.createLeftContainer();
rowHeaderCol.renderContainer = 'left';
}
// relies on the default column builder being first in array, as it is instantiated
// as part of grid creation
self.columnBuilders[0](colDef,rowHeaderCol,self.options)
.then(function(){
rowHeaderCol.enableFiltering = false;
rowHeaderCol.enableSorting = false;
rowHeaderCol.disableHiding = true;
self.rowHeaderColumns.push(rowHeaderCol);
self.buildColumns()
.then( function() {
self.preCompileCellTemplates();
self.handleWindowResize();
});
});
};
/**
* @ngdoc function
* @name buildColumns
* @methodOf ui.grid.class:Grid
* @description creates GridColumn objects from the columnDefinition. Calls each registered
* columnBuilder to further process the column
* @returns {Promise} a promise to load any needed column resources
*/
Grid.prototype.buildColumns = function buildColumns() {
$log.debug('buildColumns');
var self = this;
var builderPromises = [];
var headerOffset = self.rowHeaderColumns.length;
var i;
// Remove any columns for which a columnDef cannot be found
// Deliberately don't use forEach, as it doesn't like splice being called in the middle
// Also don't cache columns.length, as it will change during this operation
for ( i=0; i<self.columns.length; i++ ){
if (!self.getColDef(self.columns[i].name)) {
self.columns.splice(i, 1);
i--;
}
}
//add row header columns to the grid columns array _after_ columns without columnDefs have been removed
self.rowHeaderColumns.forEach(function (rowHeaderColumn) {
self.columns.unshift(rowHeaderColumn);
});
// look at each column def, and update column properties to match. If the column def
// doesn't have a column, then splice in a new gridCol
self.options.columnDefs.forEach(function (colDef, index) {
self.preprocessColDef(colDef);
var col = self.getColumn(colDef.name);
if (!col) {
col = new GridColumn(colDef, gridUtil.nextUid(), self);
self.columns.splice(index + headerOffset, 0, col);
}
else {
col.updateColumnDef(colDef);
}
self.columnBuilders.forEach(function (builder) {
builderPromises.push(builder.call(self, colDef, col, self.options));
});
});
return $q.all(builderPromises);
};
/**
* @ngdoc function
* @name preCompileCellTemplates
* @methodOf ui.grid.class:Grid
* @description precompiles all cell templates
*/
Grid.prototype.preCompileCellTemplates = function() {
var self = this;
this.columns.forEach(function (col) {
var html = col.cellTemplate.replace(uiGridConstants.MODEL_COL_FIELD, self.getQualifiedColField(col));
html = html.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
var compiledElementFn = $compile(html);
col.compiledElementFn = compiledElementFn;
if (col.compiledElementFnDefer) {
col.compiledElementFnDefer.resolve(col.compiledElementFn);
}
});
};
/**
* @ngdoc function
* @name getGridQualifiedColField
* @methodOf ui.grid.class:Grid
* @description precompiles all cell templates
* @param {GridColumn} col col object
*/
Grid.prototype.getQualifiedColField = function (col) {
return 'row.entity.' + gridUtil.preEval(col.field);
};
/**
* @ngdoc function
* @name createLeftContainer
* @methodOf ui.grid.class:Grid
* @description creates the left render container if it doesn't already exist
*/
Grid.prototype.createLeftContainer = function() {
if (!this.hasLeftContainer()) {
this.renderContainers.left = new GridRenderContainer('left', this, { disableColumnOffset: true });
}
};
/**
* @ngdoc function
* @name createRightContainer
* @methodOf ui.grid.class:Grid
* @description creates the right render container if it doesn't already exist
*/
Grid.prototype.createRightContainer = function() {
if (!this.hasRightContainer()) {
this.renderContainers.right = new GridRenderContainer('right', this, { disableColumnOffset: true });
}
};
/**
* @ngdoc function
* @name hasLeftContainer
* @methodOf ui.grid.class:Grid
* @description returns true if leftContainer exists
*/
Grid.prototype.hasLeftContainer = function() {
return this.renderContainers.left !== undefined;
};
/**
* @ngdoc function
* @name hasLeftContainer
* @methodOf ui.grid.class:Grid
* @description returns true if rightContainer exists
*/
Grid.prototype.hasRightContainer = function() {
return this.renderContainers.right !== undefined;
};
/**
* undocumented function
* @name preprocessColDef
* @methodOf ui.grid.class:Grid
* @description defaults the name property from field to maintain backwards compatibility with 2.x
* validates that name or field is present
*/
Grid.prototype.preprocessColDef = function preprocessColDef(colDef) {
if (!colDef.field && !colDef.name) {
throw new Error('colDef.name or colDef.field property is required');
}
//maintain backwards compatibility with 2.x
//field was required in 2.x. now name is required
if (colDef.name === undefined && colDef.field !== undefined) {
colDef.name = colDef.field;
}
};
// Return a list of items that exist in the `n` array but not the `o` array. Uses optional property accessors passed as third & fourth parameters
Grid.prototype.newInN = function newInN(o, n, oAccessor, nAccessor) {
var self = this;
var t = [];
for (var i=0; i<n.length; i++) {
var nV = nAccessor ? n[i][nAccessor] : n[i];
var found = false;
for (var j=0; j<o.length; j++) {
var oV = oAccessor ? o[j][oAccessor] : o[j];
if (self.options.rowEquality(nV, oV)) {
found = true;
break;
}
}
if (!found) {
t.push(nV);
}
}
return t;
};
/**
* @ngdoc function
* @name getRow
* @methodOf ui.grid.class:Grid
* @description returns the GridRow that contains the rowEntity
* @param {object} rowEntity the gridOptions.data array element instance
*/
Grid.prototype.getRow = function getRow(rowEntity) {
var rows = this.rows.filter(function (row) {
return row.entity === rowEntity;
});
return rows.length > 0 ? rows[0] : null;
};
/**
* @ngdoc function
* @name modifyRows
* @methodOf ui.grid.class:Grid
* @description creates or removes GridRow objects from the newRawData array. Calls each registered
* rowBuilder to further process the row
*
* Rows are identified using the gridOptions.rowEquality function
*/
Grid.prototype.modifyRows = function modifyRows(newRawData) {
var self = this,
i,
newRow;
if (self.rows.length === 0 && newRawData.length > 0) {
if (self.options.enableRowHashing) {
if (!self.rowHashMap) {
self.createRowHashMap();
}
for (i=0; i<newRawData.length; i++) {
newRow = newRawData[i];
self.rowHashMap.put(newRow, {
i: i,
entity: newRow
});
}
}
self.addRows(newRawData);
//now that we have data, it is save to assign types to colDefs
self.assignTypes();
}
else if (newRawData.length > 0) {
var unfoundNewRows, unfoundOldRows, unfoundNewRowsToFind;
// If row hashing is turned on
if (self.options.enableRowHashing) {
// Array of new rows that haven't been found in the old rowset
unfoundNewRows = [];
// Array of new rows that we explicitly HAVE to search for manually in the old row set. They cannot be looked up by their identity (because it doesn't exist).
unfoundNewRowsToFind = [];
// Map of rows that have been found in the new rowset
var foundOldRows = {};
// Array of old rows that have NOT been found in the new rowset
unfoundOldRows = [];
// Create the row HashMap if it doesn't exist already
if (!self.rowHashMap) {
self.createRowHashMap();
}
var rowhash = self.rowHashMap;
// Make sure every new row has a hash
for (i = 0; i < newRawData.length; i++) {
newRow = newRawData[i];
// Flag this row as needing to be manually found if it didn't come in with a $$hashKey
var mustFind = false;
if (!self.options.getRowIdentity(newRow)) {
mustFind = true;
}
// See if the new row is already in the rowhash
var found = rowhash.get(newRow);
// If so...
if (found) {
// See if it's already being used by as GridRow
if (found.row) {
// If so, mark this new row as being found
foundOldRows[self.options.rowIdentity(newRow)] = true;
}
}
else {
// Put the row in the hashmap with the index it corresponds to
rowhash.put(newRow, {
i: i,
entity: newRow
});
// This row has to be searched for manually in the old row set
if (mustFind) {
unfoundNewRowsToFind.push(newRow);
}
else {
unfoundNewRows.push(newRow);
}
}
}
// Build the list of unfound old rows
for (i = 0; i < self.rows.length; i++) {
var row = self.rows[i];
var hash = self.options.rowIdentity(row.entity);
if (!foundOldRows[hash]) {
unfoundOldRows.push(row);
}
}
}
// Look for new rows
var newRows = unfoundNewRows || [];
// The unfound new rows is either `unfoundNewRowsToFind`, if row hashing is turned on, or straight `newRawData` if it isn't
var unfoundNew = (unfoundNewRowsToFind || newRawData);
// Search for real new rows in `unfoundNew` and concat them onto `newRows`
newRows = newRows.concat(self.newInN(self.rows, unfoundNew, 'entity'));
self.addRows(newRows);
var deletedRows = self.getDeletedRows((unfoundOldRows || self.rows), newRawData);
for (i = 0; i < deletedRows.length; i++) {
if (self.options.enableRowHashing) {
self.rowHashMap.remove(deletedRows[i].entity);
}
self.rows.splice( self.rows.indexOf(deletedRows[i]), 1 );
}
}
// Empty data set
else {
// Reset the row HashMap
self.createRowHashMap();
// Reset the rows length!
self.rows.length = 0;
}
var p1 = $q.when(self.processRowsProcessors(self.rows))
.then(function (renderableRows) {
return self.setVisibleRows(renderableRows);
});
var p2 = $q.when(self.processColumnsProcessors(self.columns))
.then(function (renderableColumns) {
return self.setVisibleColumns(renderableColumns);
});
return $q.all([p1, p2]);
};
Grid.prototype.getDeletedRows = function(oldRows, newRows) {
var self = this;
var olds = oldRows.filter(function (oldRow) {
return !newRows.some(function (newItem) {
return self.options.rowEquality(newItem, oldRow.entity);
});
});
// var olds = self.newInN(newRows, oldRows, null, 'entity');
// dump('olds', olds);
return olds;
};
/**
* Private Undocumented Method
* @name addRows
* @methodOf ui.grid.class:Grid
* @description adds the newRawData array of rows to the grid and calls all registered
* rowBuilders. this keyword will reference the grid
*/
Grid.prototype.addRows = function addRows(newRawData) {
var self = this;
var existingRowCount = self.rows.length;
for (var i=0; i < newRawData.length; i++) {
var newRow = self.processRowBuilders(new GridRow(newRawData[i], i + existingRowCount, self));
if (self.options.enableRowHashing) {
var found = self.rowHashMap.get(newRow.entity);
if (found) {
found.row = newRow;
}
}
self.rows.push(newRow);
}
};
/**
* @ngdoc function
* @name processRowBuilders
* @methodOf ui.grid.class:Grid
* @description processes all RowBuilders for the gridRow
* @param {GridRow} gridRow reference to gridRow
* @returns {GridRow} the gridRow with all additional behavior added
*/
Grid.prototype.processRowBuilders = function processRowBuilders(gridRow) {
var self = this;
self.rowBuilders.forEach(function (builder) {
builder.call(self, gridRow, self.gridOptions);
});
return gridRow;
};
/**
* @ngdoc function
* @name registerStyleComputation
* @methodOf ui.grid.class:Grid
* @description registered a styleComputation function
*
* If the function returns a value it will be appended into the grid's `<style>` block
* @param {function($scope)} styleComputation function
*/
Grid.prototype.registerStyleComputation = function registerStyleComputation(styleComputationInfo) {
this.styleComputations.push(styleComputationInfo);
};
// NOTE (c0bra): We already have rowBuilders. I think these do exactly the same thing...
// Grid.prototype.registerRowFilter = function(filter) {
// // TODO(c0bra): validate filter?
// this.rowFilters.push(filter);
// };
// Grid.prototype.removeRowFilter = function(filter) {
// var idx = this.rowFilters.indexOf(filter);
// if (typeof(idx) !== 'undefined' && idx !== undefined) {
// this.rowFilters.slice(idx, 1);
// }
// };
// Grid.prototype.processRowFilters = function(rows) {
// var self = this;
// self.rowFilters.forEach(function (filter) {
// filter.call(self, rows);
// });
// };
/**
* @ngdoc function
* @name registerRowsProcessor
* @methodOf ui.grid.class:Grid
* @param {function(renderableRows)} rows processor function
* @returns {Array[GridRow]} Updated renderable rows
* @description
Register a "rows processor" function. When the rows are updated,
the grid calls each registered "rows processor", which has a chance
to alter the set of rows (sorting, etc) as long as the count is not
modified.
*/
Grid.prototype.registerRowsProcessor = function registerRowsProcessor(processor) {
if (!angular.isFunction(processor)) {
throw 'Attempt to register non-function rows processor: ' + processor;
}
this.rowsProcessors.push(processor);
};
/**
* @ngdoc function
* @name removeRowsProcessor
* @methodOf ui.grid.class:Grid
* @param {function(renderableRows)} rows processor function
* @description Remove a registered rows processor
*/
Grid.prototype.removeRowsProcessor = function removeRowsProcessor(processor) {
var idx = this.rowsProcessors.indexOf(processor);
if (typeof(idx) !== 'undefined' && idx !== undefined) {
this.rowsProcessors.splice(idx, 1);
}
};
/**
* Private Undocumented Method
* @name processRowsProcessors
* @methodOf ui.grid.class:Grid
* @param {Array[GridRow]} The array of "renderable" rows
* @param {Array[GridColumn]} The array of columns
* @description Run all the registered rows processors on the array of renderable rows
*/
Grid.prototype.processRowsProcessors = function processRowsProcessors(renderableRows) {
var self = this;
// Create a shallow copy of the rows so that we can safely sort them without altering the original grid.rows sort order
var myRenderableRows = renderableRows.slice(0);
// self.rowsProcessors.forEach(function (processor) {
// myRenderableRows = processor.call(self, myRenderableRows, self.columns);
// if (!renderableRows) {
// throw "Processor at index " + i + " did not return a set of renderable rows";
// }
// if (!angular.isArray(renderableRows)) {
// throw "Processor at index " + i + " did not return an array";
// }
// i++;
// });
// Return myRenderableRows with no processing if we have no rows processors
if (self.rowsProcessors.length === 0) {
return $q.when(myRenderableRows);
}
// Counter for iterating through rows processors
var i = 0;
// Promise for when we're done with all the processors
var finished = $q.defer();
// This function will call the processor in self.rowsProcessors at index 'i', and then
// when done will call the next processor in the list, using the output from the processor
// at i as the argument for 'renderedRowsToProcess' on the next iteration.
//
// If we're at the end of the list of processors, we resolve our 'finished' callback with
// the result.
function startProcessor(i, renderedRowsToProcess) {
// Get the processor at 'i'
var processor = self.rowsProcessors[i];
// Call the processor, passing in the rows to process and the current columns
// (note: it's wrapped in $q.when() in case the processor does not return a promise)
return $q.when( processor.call(self, renderedRowsToProcess, self.columns) )
.then(function handleProcessedRows(processedRows) {
// Check for errors
if (!processedRows) {
throw "Processor at index " + i + " did not return a set of renderable rows";
}
if (!angular.isArray(processedRows)) {
throw "Processor at index " + i + " did not return an array";
}
// Processor is done, increment the counter
i++;
// If we're not done with the processors, call the next one
if (i <= self.rowsProcessors.length - 1) {
return startProcessor(i, processedRows);
}
// We're done! Resolve the 'finished' promise
else {
finished.resolve(processedRows);
}
});
}
// Start on the first processor
startProcessor(0, myRenderableRows);
return finished.promise;
};
Grid.prototype.setVisibleRows = function setVisibleRows(rows) {
// $log.debug('setVisibleRows');
var self = this;
//var newVisibleRowCache = [];
// Reset all the render container row caches
for (var i in self.renderContainers) {
var container = self.renderContainers[i];
container.visibleRowCache.length = 0;
}
// rows.forEach(function (row) {
for (var ri = 0; ri < rows.length; ri++) {
var row = rows[ri];
// If the row is visible
if (row.visible) {
// newVisibleRowCache.push(row);
// If the row has a container specified
if (typeof(row.renderContainer) !== 'undefined' && row.renderContainer) {
self.renderContainers[row.renderContainer].visibleRowCache.push(row);
}
// If not, put it into the body container
else {
self.renderContainers.body.visibleRowCache.push(row);
}
}
}
};
/**
* @ngdoc function
* @name registerColumnsProcessor
* @methodOf ui.grid.class:Grid
* @param {function(renderableColumns)} rows processor function
* @returns {Array[GridColumn]} Updated renderable columns
* @description
Register a "columns processor" function. When the columns are updated,
the grid calls each registered "columns processor", which has a chance
to alter the set of columns, as long as the count is not modified.
*/
Grid.prototype.registerColumnsProcessor = function registerColumnsProcessor(processor) {
if (!angular.isFunction(processor)) {
throw 'Attempt to register non-function rows processor: ' + processor;
}
this.columnsProcessors.push(processor);
};
Grid.prototype.removeColumnsProcessor = function removeColumnsProcessor(processor) {
var idx = this.columnsProcessors.indexOf(processor);
if (typeof(idx) !== 'undefined' && idx !== undefined) {
this.columnsProcessors.splice(idx, 1);
}
};
Grid.prototype.processColumnsProcessors = function processColumnsProcessors(renderableColumns) {
var self = this;
// Create a shallow copy of the rows so that we can safely sort them without altering the original grid.rows sort order
var myRenderableColumns = renderableColumns.slice(0);
// Return myRenderableRows with no processing if we have no rows processors
if (self.columnsProcessors.length === 0) {
return $q.when(myRenderableColumns);
}
// Counter for iterating through rows processors
var i = 0;
// Promise for when we're done with all the processors
var finished = $q.defer();
// This function will call the processor in self.rowsProcessors at index 'i', and then
// when done will call the next processor in the list, using the output from the processor
// at i as the argument for 'renderedRowsToProcess' on the next iteration.
//
// If we're at the end of the list of processors, we resolve our 'finished' callback with
// the result.
function startProcessor(i, renderedColumnsToProcess) {
// Get the processor at 'i'
var processor = self.columnsProcessors[i];
// Call the processor, passing in the rows to process and the current columns
// (note: it's wrapped in $q.when() in case the processor does not return a promise)
return $q.when( processor.call(self, renderedColumnsToProcess, self.rows) )