-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
angular-slickgrid.component.ts
1535 lines (1348 loc) · 72.3 KB
/
angular-slickgrid.component.ts
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
import {
AfterViewInit,
ApplicationRef,
ChangeDetectorRef,
Component,
ContentChild,
ElementRef,
EventEmitter,
Inject,
Input,
OnDestroy,
Optional,
Output,
TemplateRef,
} from '@angular/core';
import {
AutocompleterEditor,
BackendService,
BackendServiceApi,
BackendServiceOption,
BasePaginationComponent,
Column,
DataViewOption,
EventSubscription,
ExternalResource,
isColumnDateType,
ItemMetadata,
Locale,
Metrics,
Pagination,
PaginationMetadata,
RxJsFacade,
SelectEditor,
SlickDataView,
SlickEventHandler,
SlickGrid,
} from '@slickgrid-universal/common';
import {
ExtensionName,
ExtensionUtility,
SlickGroupItemMetadataProvider,
// services
BackendUtilityService,
CollectionService,
EventNamingStyle,
ExtensionService,
FilterFactory,
FilterService,
GridEventService,
GridService,
GridStateService,
HeaderGroupingService,
PaginationService,
ResizerService,
SharedService,
SlickgridConfig,
SortService,
TreeDataService,
// utilities
autoAddEditorFormatterToColumnsWithEditor,
emptyElement,
GridStateType,
unsubscribeAll,
} from '@slickgrid-universal/common';
import { EventPubSubService } from '@slickgrid-universal/event-pub-sub';
import { SlickEmptyWarningComponent } from '@slickgrid-universal/empty-warning-component';
import { SlickFooterComponent } from '@slickgrid-universal/custom-footer-component';
import { SlickPaginationComponent } from '@slickgrid-universal/pagination-component';
import { RxJsResource } from '@slickgrid-universal/rxjs-observable';
import { extend } from '@slickgrid-universal/utils';
import { TranslateService } from '@ngx-translate/core';
import { dequal } from 'dequal/lite';
import { Observable } from 'rxjs';
import { Constants } from '../constants';
import type { AngularGridInstance, ExternalTestingDependencies, GridOption } from './../models/index';
import { GlobalGridOptions } from './../global-grid-options';
import { TranslaterService } from '../services/translater.service';
// Services
import { AngularUtilService } from '../services/angularUtil.service';
import { SlickRowDetailView } from '../extensions/slickRowDetailView';
import { ContainerService } from '../services/container.service';
const WARN_NO_PREPARSE_DATE_SIZE = 10000; // data size to warn user when pre-parse isn't enabled
@Component({
selector: 'angular-slickgrid',
templateUrl: './angular-slickgrid.component.html',
providers: [
// make everything transient (non-singleton)
AngularUtilService,
TranslaterService,
]
})
export class AngularSlickgridComponent<TData = any> implements AfterViewInit, OnDestroy {
protected _dataset?: TData[] | null;
protected _columnDefinitions!: Column[];
protected _currentDatasetLength = 0;
protected _darkMode = false;
protected _eventHandler: SlickEventHandler = new SlickEventHandler();
protected _eventPubSubService!: EventPubSubService;
protected _angularGridInstances: AngularGridInstance | undefined;
protected _hideHeaderRowAfterPageLoad = false;
protected _isAutosizeColsCalled = false;
protected _isGridInitialized = false;
protected _isDatasetInitialized = false;
protected _isDatasetHierarchicalInitialized = false;
protected _isPaginationInitialized = false;
protected _isLocalGrid = true;
protected _paginationOptions: Pagination | undefined;
protected _registeredResources: ExternalResource[] = [];
protected _scrollEndCalled = false;
dataView!: SlickDataView;
slickGrid!: SlickGrid;
groupingDefinition: any = {};
groupItemMetadataProvider?: SlickGroupItemMetadataProvider;
backendServiceApi?: BackendServiceApi;
locales!: Locale;
metrics?: Metrics;
showPagination = false;
serviceList: any[] = [];
totalItems = 0;
paginationData?: {
gridOptions: GridOption;
paginationService: PaginationService;
};
subscriptions: EventSubscription[] = [];
// components / plugins
slickEmptyWarning?: SlickEmptyWarningComponent;
slickFooter?: SlickFooterComponent;
slickPagination?: BasePaginationComponent;
paginationComponent: BasePaginationComponent | undefined;
slickRowDetailView?: SlickRowDetailView;
// services
backendUtilityService!: BackendUtilityService;
collectionService: CollectionService;
extensionService: ExtensionService;
extensionUtility: ExtensionUtility;
filterFactory!: FilterFactory;
filterService: FilterService;
gridEventService: GridEventService;
gridService: GridService;
gridStateService: GridStateService;
headerGroupingService: HeaderGroupingService;
paginationService: PaginationService;
resizerService!: ResizerService;
rxjs?: RxJsFacade;
sharedService: SharedService;
sortService: SortService;
treeDataService: TreeDataService;
@Input() customDataView: any;
@Input() gridId = '';
@Input() gridOptions!: GridOption;
@Input()
get paginationOptions(): Pagination | undefined {
return this._paginationOptions;
}
set paginationOptions(newPaginationOptions: Pagination | undefined) {
if (newPaginationOptions && this._paginationOptions) {
this._paginationOptions = { ...this.gridOptions.pagination, ...this._paginationOptions, ...newPaginationOptions };
} else {
this._paginationOptions = newPaginationOptions;
}
this.gridOptions.pagination = this._paginationOptions ?? this.gridOptions.pagination;
this.paginationService.updateTotalItems(this.gridOptions.pagination?.totalItems ?? 0, true);
}
@Input()
set columnDefinitions(columnDefinitions: Column[]) {
this._columnDefinitions = columnDefinitions;
if (this._isGridInitialized) {
this.updateColumnDefinitionsList(columnDefinitions);
}
if (columnDefinitions.length > 0) {
this.copyColumnWidthsReference(columnDefinitions);
}
}
get columnDefinitions(): Column[] {
return this._columnDefinitions;
}
// make the columnDefinitions a 2-way binding so that plugin adding cols
// are synched on user's side as well (RowMove, RowDetail, RowSelections)
@Output() columnDefinitionsChange = new EventEmitter(true);
@Input()
get dataset(): any[] {
return (this.customDataView ? this.slickGrid?.getData?.() : this.dataView?.getItems()) || [];
}
set dataset(newDataset: any[]) {
const prevDatasetLn = this._currentDatasetLength;
const isDatasetEqual = dequal(newDataset, this._dataset || []);
let data = newDataset;
// when Tree Data is enabled and we don't yet have the hierarchical dataset filled, we can force a convert+sort of the array
if (this.slickGrid && this.gridOptions?.enableTreeData && Array.isArray(newDataset) && (newDataset.length > 0 || newDataset.length !== prevDatasetLn || !isDatasetEqual)) {
this._isDatasetHierarchicalInitialized = false;
data = this.sortTreeDataset(newDataset, !isDatasetEqual); // if dataset changed, then force a refresh anyway
}
this._dataset = data;
this.refreshGridData(data || []);
this._currentDatasetLength = (newDataset || []).length;
// expand/autofit columns on first page load
// we can assume that if the prevDataset was empty then we are on first load
if (this.slickGrid && this.gridOptions?.autoFitColumnsOnFirstLoad && prevDatasetLn === 0 && !this._isAutosizeColsCalled) {
this.slickGrid.autosizeColumns();
this._isAutosizeColsCalled = true;
}
this.suggestDateParsingWhenHelpful();
}
@Input()
get datasetHierarchical(): any[] | undefined {
return this.sharedService.hierarchicalDataset;
}
set datasetHierarchical(newHierarchicalDataset: any[] | undefined) {
const isDatasetEqual = dequal(newHierarchicalDataset, this.sharedService?.hierarchicalDataset ?? []);
const prevFlatDatasetLn = this._currentDatasetLength;
this.sharedService.hierarchicalDataset = newHierarchicalDataset;
if (newHierarchicalDataset && this.columnDefinitions && this.filterService?.clearFilters) {
this.filterService.clearFilters();
}
// when a hierarchical dataset is set afterward, we can reset the flat dataset and call a tree data sort that will overwrite the flat dataset
if (newHierarchicalDataset && this.slickGrid && this.sortService?.processTreeDataInitialSort) {
this.sortService.processTreeDataInitialSort();
// we also need to reset/refresh the Tree Data filters because if we inserted new item(s) then it might not show up without doing this refresh
// however we need to queue our process until the flat dataset is ready, so we can queue a microtask to execute the DataView refresh only after everything is ready
queueMicrotask(() => {
const flatDatasetLn = this.dataView.getItemCount();
if (flatDatasetLn > 0 && (flatDatasetLn !== prevFlatDatasetLn || !isDatasetEqual)) {
this.filterService.refreshTreeDataFilters();
}
});
this._isDatasetHierarchicalInitialized = true;
}
}
get elementRef(): ElementRef {
return this.elm;
}
get backendService(): BackendService | undefined {
return this.gridOptions?.backendServiceApi?.service;
}
get eventHandler(): SlickEventHandler {
return this._eventHandler;
}
get gridContainerElement(): HTMLElement | null {
return document.querySelector(`#${this.gridOptions.gridContainerId || ''}`);
}
/** GETTER to know if dataset was initialized or not */
get isDatasetInitialized(): boolean {
return this._isDatasetInitialized;
}
/** SETTER to change if dataset was initialized or not (stringly used for unit testing purposes) */
set isDatasetInitialized(isInitialized: boolean) {
this._isDatasetInitialized = isInitialized;
}
set isDatasetHierarchicalInitialized(isInitialized: boolean) {
this._isDatasetHierarchicalInitialized = isInitialized;
}
get registeredResources(): ExternalResource[] {
return this._registeredResources;
}
@ContentChild('slickgridHeader', { static: true }) slickgridHeader?: TemplateRef<any>;
@ContentChild('slickgridFooter', { static: true }) slickgridFooter?: TemplateRef<any>;
constructor(
protected readonly angularUtilService: AngularUtilService,
protected readonly appRef: ApplicationRef,
protected readonly cd: ChangeDetectorRef,
protected readonly containerService: ContainerService,
protected readonly elm: ElementRef,
@Optional() protected readonly translate: TranslateService,
@Optional() protected readonly translaterService: TranslaterService,
@Inject('config') protected forRootConfig: GridOption,
@Inject('externalService') externalServices: ExternalTestingDependencies
) {
const slickgridConfig = new SlickgridConfig();
// initialize and assign all Service Dependencies
this._eventPubSubService = externalServices?.eventPubSubService ?? new EventPubSubService(this.elm.nativeElement);
this._eventPubSubService.eventNamingStyle = EventNamingStyle.camelCase;
this.backendUtilityService = externalServices?.backendUtilityService ?? new BackendUtilityService();
this.gridEventService = externalServices?.gridEventService ?? new GridEventService();
this.sharedService = externalServices?.sharedService ?? new SharedService();
this.collectionService = externalServices?.collectionService ?? new CollectionService(this.translaterService);
this.extensionUtility = externalServices?.extensionUtility ?? new ExtensionUtility(this.sharedService, this.backendUtilityService, this.translaterService);
this.filterFactory = new FilterFactory(slickgridConfig, this.translaterService, this.collectionService);
this.filterService = externalServices?.filterService ?? new FilterService(this.filterFactory as any, this._eventPubSubService, this.sharedService, this.backendUtilityService);
this.resizerService = externalServices?.resizerService ?? new ResizerService(this._eventPubSubService);
this.sortService = externalServices?.sortService ?? new SortService(this.collectionService, this.sharedService, this._eventPubSubService, this.backendUtilityService);
this.treeDataService = externalServices?.treeDataService ?? new TreeDataService(this._eventPubSubService, this.sharedService, this.sortService);
this.paginationService = externalServices?.paginationService ?? new PaginationService(this._eventPubSubService, this.sharedService, this.backendUtilityService);
this.extensionService = externalServices?.extensionService ?? new ExtensionService(
this.extensionUtility,
this.filterService,
this._eventPubSubService,
this.sharedService,
this.sortService,
this.treeDataService,
this.translaterService,
() => this.gridService
);
this.gridStateService = externalServices?.gridStateService ?? new GridStateService(this.extensionService, this.filterService, this._eventPubSubService, this.sharedService, this.sortService, this.treeDataService);
this.gridService = externalServices?.gridService ?? new GridService(this.gridStateService, this.filterService, this._eventPubSubService, this.paginationService, this.sharedService, this.sortService, this.treeDataService);
this.headerGroupingService = externalServices?.headerGroupingService ?? new HeaderGroupingService(this.extensionUtility);
this.serviceList = [
this.containerService,
this.extensionService,
this.filterService,
this.gridEventService,
this.gridService,
this.gridStateService,
this.headerGroupingService,
this.paginationService,
this.resizerService,
this.sortService,
this.treeDataService,
];
// register all Service instances in the container
this.containerService.registerInstance('ExtensionUtility', this.extensionUtility);
this.containerService.registerInstance('FilterService', this.filterService);
this.containerService.registerInstance('CollectionService', this.collectionService);
this.containerService.registerInstance('ExtensionService', this.extensionService);
this.containerService.registerInstance('GridEventService', this.gridEventService);
this.containerService.registerInstance('GridService', this.gridService);
this.containerService.registerInstance('GridStateService', this.gridStateService);
this.containerService.registerInstance('HeaderGroupingService', this.headerGroupingService);
this.containerService.registerInstance('PaginationService', this.paginationService);
this.containerService.registerInstance('ResizerService', this.resizerService);
this.containerService.registerInstance('SharedService', this.sharedService);
this.containerService.registerInstance('SortService', this.sortService);
this.containerService.registerInstance('EventPubSubService', this._eventPubSubService);
this.containerService.registerInstance('PubSubService', this._eventPubSubService);
this.containerService.registerInstance('TranslaterService', this.translaterService);
this.containerService.registerInstance('TreeDataService', this.treeDataService);
}
ngAfterViewInit() {
if (!this.gridOptions || !this.columnDefinitions) {
throw new Error('Using `<angular-slickgrid>` requires [gridOptions] and [columnDefinitions], it seems that you might have forgot to provide them since at least of them is undefined.');
}
this.initialization(this._eventHandler);
this._isGridInitialized = true;
// recheck the empty warning message after grid is shown so that it works in every use case
if (this.gridOptions?.enableEmptyDataWarningMessage && Array.isArray(this.dataset)) {
const finalTotalCount = this.dataset.length;
this.displayEmptyDataWarning(finalTotalCount < 1);
}
// add dark mode CSS class when enabled
if (this.gridOptions.darkMode) {
this.setDarkMode(true);
}
this.suggestDateParsingWhenHelpful();
}
ngOnDestroy(): void {
this._eventPubSubService.publish('onBeforeGridDestroy', this.slickGrid);
this.destroy();
this._eventPubSubService.publish('onAfterGridDestroyed', true);
}
destroy(shouldEmptyDomElementContainer = false) {
// dispose of all Services
this.serviceList.forEach((service: any) => {
if (typeof service?.dispose === 'function') {
service.dispose();
}
});
this.serviceList = [];
// dispose backend service when defined and a dispose method exists
this.backendService?.dispose?.();
// dispose all registered external resources
this.disposeExternalResources();
// dispose the Components
this.slickEmptyWarning?.dispose();
this.slickFooter?.dispose();
this.slickPagination?.dispose();
if (this._eventHandler?.unsubscribeAll) {
this._eventHandler.unsubscribeAll();
}
this._eventPubSubService?.unsubscribeAll();
if (this.dataView) {
this.dataView.setItems([]);
this.dataView.destroy();
}
if (this.slickGrid?.destroy) {
this.slickGrid.destroy(shouldEmptyDomElementContainer);
}
if (this.backendServiceApi) {
for (const prop of Object.keys(this.backendServiceApi)) {
delete this.backendServiceApi[prop as keyof BackendServiceApi];
}
this.backendServiceApi = undefined;
}
for (const prop of Object.keys(this.columnDefinitions)) {
(this.columnDefinitions as any)[prop] = null;
}
for (const prop of Object.keys(this.sharedService)) {
(this.sharedService as any)[prop] = null;
}
// we could optionally also empty the content of the grid container DOM element
if (shouldEmptyDomElementContainer) {
this.emptyGridContainerElm();
}
// also unsubscribe all RxJS subscriptions
this.subscriptions = unsubscribeAll(this.subscriptions);
this._dataset = null;
this.datasetHierarchical = undefined;
this._columnDefinitions = [];
this._angularGridInstances = undefined;
this.slickGrid = undefined as any;
}
disposeExternalResources() {
if (Array.isArray(this._registeredResources)) {
while (this._registeredResources.length > 0) {
const res = this._registeredResources.pop();
if (res?.dispose) {
res.dispose();
}
}
}
this._registeredResources = [];
}
emptyGridContainerElm() {
const gridContainerId = this.gridOptions?.gridContainerId ?? 'grid1';
const gridContainerElm = document.querySelector(`#${gridContainerId}`);
emptyElement(gridContainerElm);
}
/**
* Define our internal Post Process callback, it will execute internally after we get back result from the Process backend call
* Currently ONLY available with the GraphQL Backend Service.
* The behavior is to refresh the Dataset & Pagination without requiring the user to create his own PostProcess every time
*/
createBackendApiInternalPostProcessCallback(gridOptions: GridOption) {
const backendApi = gridOptions?.backendServiceApi;
if (backendApi?.service) {
const backendApiService = backendApi.service;
// internalPostProcess only works (for now) with a GraphQL Service, so make sure it is of that type
if (typeof backendApiService.getDatasetName === 'function') {
backendApi.internalPostProcess = (processResult: any) => {
const datasetName = (backendApi && backendApiService && typeof backendApiService.getDatasetName === 'function') ? backendApiService.getDatasetName() : '';
if (processResult?.data[datasetName]) {
const data = 'nodes' in processResult.data[datasetName] ? (processResult as any).data[datasetName].nodes : (processResult as any).data[datasetName];
const totalCount = 'totalCount' in processResult.data[datasetName] ? (processResult as any).data[datasetName].totalCount : (processResult as any).data[datasetName].length;
this.refreshGridData(data, totalCount || 0);
}
};
}
}
}
initialization(eventHandler: SlickEventHandler) {
this.gridOptions.translater = this.translaterService;
this._eventHandler = eventHandler;
this._isAutosizeColsCalled = false;
// when detecting a frozen grid, we'll automatically enable the mousewheel scroll handler so that we can scroll from both left/right frozen containers
if (this.gridOptions && ((this.gridOptions.frozenRow !== undefined && this.gridOptions.frozenRow >= 0) || this.gridOptions.frozenColumn !== undefined && this.gridOptions.frozenColumn >= 0) && this.gridOptions.enableMouseWheelScrollHandler === undefined) {
this.gridOptions.enableMouseWheelScrollHandler = true;
}
this._eventPubSubService.eventNamingStyle = this.gridOptions?.eventNamingStyle ?? EventNamingStyle.camelCase;
this._eventPubSubService.publish('onBeforeGridCreate', true);
// make sure the dataset is initialized (if not it will throw an error that it cannot getLength of null)
this._dataset = this._dataset || [];
this.gridOptions = this.mergeGridOptions(this.gridOptions);
this._paginationOptions = this.gridOptions?.pagination;
this.locales = this.gridOptions?.locales ?? Constants.locales;
this.backendServiceApi = this.gridOptions?.backendServiceApi;
this._isLocalGrid = !this.backendServiceApi; // considered a local grid if it doesn't have a backend service set
// unless specified, we'll create an internal postProcess callback (currently only available for GraphQL)
if (this.gridOptions.backendServiceApi && !this.gridOptions.backendServiceApi?.disableInternalPostProcess) {
this.createBackendApiInternalPostProcessCallback(this.gridOptions);
}
if (!this.customDataView) {
const dataviewInlineFilters = this.gridOptions?.dataView?.inlineFilters ?? false;
let dataViewOptions: Partial<DataViewOption> = { ...this.gridOptions.dataView, inlineFilters: dataviewInlineFilters };
if (this.gridOptions.draggableGrouping || this.gridOptions.enableGrouping) {
this.groupItemMetadataProvider = new SlickGroupItemMetadataProvider();
this.sharedService.groupItemMetadataProvider = this.groupItemMetadataProvider;
dataViewOptions = { ...dataViewOptions, groupItemMetadataProvider: this.groupItemMetadataProvider };
}
this.dataView = new SlickDataView<TData>(dataViewOptions, this._eventPubSubService);
this._eventPubSubService.publish('onDataviewCreated', this.dataView);
}
// get any possible Services that user want to register which don't require SlickGrid to be instantiated
// RxJS Resource is in this lot because it has to be registered before anything else and doesn't require SlickGrid to be initialized
this.preRegisterResources();
// prepare and load all SlickGrid editors, if an async editor is found then we'll also execute it.
this._columnDefinitions = this.loadSlickGridEditors(this._columnDefinitions || []);
// if the user wants to automatically add a Custom Editor Formatter, we need to call the auto add function again
if (this.gridOptions.autoAddCustomEditorFormatter) {
autoAddEditorFormatterToColumnsWithEditor(this._columnDefinitions, this.gridOptions.autoAddCustomEditorFormatter);
}
// save reference for all columns before they optionally become hidden/visible
this.sharedService.allColumns = this._columnDefinitions;
this.sharedService.visibleColumns = this._columnDefinitions;
// before certain extentions/plugins potentially adds extra columns not created by the user itself (RowMove, RowDetail, RowSelections)
// we'll subscribe to the event and push back the change to the user so they always use full column defs array including extra cols
this.subscriptions.push(
this._eventPubSubService.subscribe<{ columns: Column[]; grid: SlickGrid; }>('onPluginColumnsChanged', data => {
this._columnDefinitions = data.columns;
this.columnDefinitionsChange.emit(this._columnDefinitions);
})
);
// after subscribing to potential columns changed, we are ready to create these optional extensions
// when we did find some to create (RowMove, RowDetail, RowSelections), it will automatically modify column definitions (by previous subscribe)
this.extensionService.createExtensionsBeforeGridCreation(this._columnDefinitions, this.gridOptions);
// if user entered some Pinning/Frozen "presets", we need to apply them in the grid options
if (this.gridOptions.presets?.pinning) {
this.gridOptions = { ...this.gridOptions, ...this.gridOptions.presets.pinning };
}
// build SlickGrid Grid, also user might optionally pass a custom dataview (e.g. remote model)
this.slickGrid = new SlickGrid<TData, Column<TData>, GridOption<Column<TData>>>(`#${this.gridId}`, this.customDataView || this.dataView as SlickDataView<TData>, this._columnDefinitions, this.gridOptions, this._eventPubSubService);
this.sharedService.dataView = this.dataView;
this.sharedService.slickGrid = this.slickGrid;
this.sharedService.gridContainerElement = this.elm.nativeElement as HTMLDivElement;
if (this.groupItemMetadataProvider) {
this.slickGrid.registerPlugin(this.groupItemMetadataProvider); // register GroupItemMetadataProvider when Grouping is enabled
}
this.extensionService.bindDifferentExtensions();
this.bindDifferentHooks(this.slickGrid, this.gridOptions, this.dataView);
// when it's a frozen grid, we need to keep the frozen column id for reference if we ever show/hide column from ColumnPicker/GridMenu afterward
const frozenColumnIndex = this.gridOptions.frozenColumn !== undefined ? this.gridOptions.frozenColumn : -1;
if (frozenColumnIndex >= 0 && frozenColumnIndex <= this._columnDefinitions.length) {
this.sharedService.frozenVisibleColumnId = this._columnDefinitions[frozenColumnIndex].id || '';
}
// get any possible Services that user want to register
this.registerResources();
// initialize the SlickGrid grid
this.slickGrid.init();
// initialized the resizer service only after SlickGrid is initialized
// if we don't we end up binding our resize to a grid element that doesn't yet exist in the DOM and the resizer service will fail silently (because it has a try/catch that unbinds the resize without throwing back)
if (this.gridContainerElement) {
this.resizerService.init(this.slickGrid, this.gridContainerElement as HTMLDivElement);
}
// user could show a custom footer with the data metrics (dataset length and last updated timestamp)
if (!this.gridOptions.enablePagination && this.gridOptions.showCustomFooter && this.gridOptions.customFooterOptions && this.gridContainerElement) {
this.slickFooter = new SlickFooterComponent(this.slickGrid, this.gridOptions.customFooterOptions, this._eventPubSubService, this.translaterService);
this.slickFooter.renderFooter(this.gridContainerElement);
}
if (!this.customDataView && this.dataView) {
// load the data in the DataView (unless it's a hierarchical dataset, if so it will be loaded after the initial tree sort)
const initialDataset = this.gridOptions?.enableTreeData ? this.sortTreeDataset(this._dataset) : this._dataset;
this.dataView.beginUpdate();
this.dataView.setItems(initialDataset || [], this.gridOptions.datasetIdPropertyName ?? 'id');
this.dataView.endUpdate();
// if you don't want the items that are not visible (due to being filtered out or being on a different page)
// to stay selected, pass 'false' to the second arg
if (this.slickGrid?.getSelectionModel() && this.gridOptions?.dataView && 'syncGridSelection' in this.gridOptions.dataView) {
// if we are using a Backend Service, we will do an extra flag check, the reason is because it might have some unintended behaviors
// with the BackendServiceApi because technically the data in the page changes the DataView on every page change.
let preservedRowSelectionWithBackend = false;
if (this.gridOptions.backendServiceApi && 'syncGridSelectionWithBackendService' in this.gridOptions.dataView) {
preservedRowSelectionWithBackend = this.gridOptions.dataView.syncGridSelectionWithBackendService as boolean;
}
const syncGridSelection = this.gridOptions.dataView.syncGridSelection;
if (typeof syncGridSelection === 'boolean') {
let preservedRowSelection = syncGridSelection;
if (!this._isLocalGrid) {
// when using BackendServiceApi, we'll be using the "syncGridSelectionWithBackendService" flag BUT "syncGridSelection" must also be set to True
preservedRowSelection = syncGridSelection && preservedRowSelectionWithBackend;
}
this.dataView.syncGridSelection(this.slickGrid, preservedRowSelection);
} else if (typeof syncGridSelection === 'object') {
this.dataView.syncGridSelection(this.slickGrid, syncGridSelection.preserveHidden, syncGridSelection.preserveHiddenOnSelectionChange);
}
}
const datasetLn = this.dataView.getLength() || this._dataset?.length || 0;
if (datasetLn > 0) {
if (!this._isDatasetInitialized && (this.gridOptions.enableCheckboxSelector || this.gridOptions.enableRowSelection)) {
this.loadRowSelectionPresetWhenExists();
}
this.loadFilterPresetsWhenDatasetInitialized();
this._isDatasetInitialized = true;
}
}
// user might want to hide the header row on page load but still have `enableFiltering: true`
// if that is the case, we need to hide the headerRow ONLY AFTER all filters got created & dataView exist
if (this._hideHeaderRowAfterPageLoad) {
this.showHeaderRow(false);
this.sharedService.hideHeaderRowAfterPageLoad = this._hideHeaderRowAfterPageLoad;
}
// publish & dispatch certain events
this._eventPubSubService.publish('onGridCreated', this.slickGrid);
// after the DataView is created & updated execute some processes
if (!this.customDataView) {
this.executeAfterDataviewCreated(this.slickGrid, this.gridOptions);
}
// bind resize ONLY after the dataView is ready
this.bindResizeHook(this.slickGrid, this.gridOptions);
// bind the Backend Service API callback functions only after the grid is initialized
// because the preProcess() and onInit() might get triggered
if (this.gridOptions?.backendServiceApi) {
this.bindBackendCallbackFunctions(this.gridOptions);
}
// local grid, check if we need to show the Pagination
// if so then also check if there's any presets and finally initialize the PaginationService
// a local grid with Pagination presets will potentially have a different total of items, we'll need to get it from the DataView and update our total
if (this.gridOptions?.enablePagination && this._isLocalGrid) {
this.showPagination = true;
this.loadLocalGridPagination(this.dataset);
}
this._angularGridInstances = {
// Slick Grid & DataView objects
dataView: this.dataView,
slickGrid: this.slickGrid,
extensions: this.extensionService?.extensionList,
// public methods
destroy: this.destroy.bind(this),
// return all available Services (non-singleton)
backendService: this.backendService,
eventPubSubService: this._eventPubSubService,
filterService: this.filterService,
gridEventService: this.gridEventService,
gridStateService: this.gridStateService,
gridService: this.gridService,
groupingService: this.headerGroupingService,
headerGroupingService: this.headerGroupingService,
extensionService: this.extensionService,
paginationComponent: this.slickPagination,
paginationService: this.paginationService,
resizerService: this.resizerService,
sortService: this.sortService,
treeDataService: this.treeDataService,
};
// all instances (SlickGrid, DataView & all Services)
this._eventPubSubService.publish('onAngularGridCreated', this._angularGridInstances);
}
/**
* On a Pagination changed, we will trigger a Grid State changed with the new pagination info
* Also if we use Row Selection or the Checkbox Selector with a Backend Service (Odata, GraphQL), we need to reset any selection
*/
paginationChanged(pagination: PaginationMetadata) {
const isSyncGridSelectionEnabled = this.gridStateService?.needToPreserveRowSelection() ?? false;
if (this.slickGrid && !isSyncGridSelectionEnabled && this.gridOptions?.backendServiceApi && (this.gridOptions.enableRowSelection || this.gridOptions.enableCheckboxSelector)) {
this.slickGrid.setSelectedRows([]);
}
const { pageNumber, pageSize } = pagination;
if (this.sharedService) {
if (pageSize !== undefined && pageNumber !== undefined) {
this.sharedService.currentPagination = { pageNumber, pageSize };
}
}
this._eventPubSubService.publish('onGridStateChanged', {
change: { newValues: { pageNumber, pageSize }, type: GridStateType.pagination },
gridState: this.gridStateService.getCurrentGridState()
});
this.cd.markForCheck();
}
/**
* When dataset changes, we need to refresh the entire grid UI & possibly resize it as well
* @param dataset
*/
refreshGridData(dataset: any[], totalCount?: number) {
if (this.gridOptions?.enableEmptyDataWarningMessage && Array.isArray(dataset)) {
const finalTotalCount = totalCount || dataset.length;
this.displayEmptyDataWarning(finalTotalCount < 1);
}
if (Array.isArray(dataset) && this.slickGrid && this.dataView?.setItems) {
this.dataView.setItems(dataset, this.gridOptions.datasetIdPropertyName ?? 'id');
if (!this.gridOptions.backendServiceApi && !this.gridOptions.enableTreeData) {
this.dataView.reSort();
}
if (dataset.length > 0) {
if (!this._isDatasetInitialized) {
this.loadFilterPresetsWhenDatasetInitialized();
if (this.gridOptions.enableCheckboxSelector) {
this.loadRowSelectionPresetWhenExists();
}
}
this._isDatasetInitialized = true;
}
if (dataset) {
this.slickGrid.invalidate();
}
// display the Pagination component only after calling this refresh data first, we call it here so that if we preset pagination page number it will be shown correctly
this.showPagination = !!(this.gridOptions && (this.gridOptions.enablePagination || (this.gridOptions.backendServiceApi && this.gridOptions.enablePagination === undefined)));
if (this._paginationOptions && this.gridOptions?.pagination && this.gridOptions?.backendServiceApi) {
const paginationOptions = this.setPaginationOptionsWhenPresetDefined(this.gridOptions, this._paginationOptions as Pagination);
// when we have a totalCount use it, else we'll take it from the pagination object
// only update the total items if it's different to avoid refreshing the UI
const totalRecords = (totalCount !== undefined) ? totalCount : (this.gridOptions?.pagination?.totalItems);
if (totalRecords !== undefined && totalRecords !== this.totalItems) {
this.totalItems = +totalRecords;
}
// initialize the Pagination Service with new pagination options (which might have presets)
if (!this._isPaginationInitialized) {
this.initializePaginationService(paginationOptions);
} else {
// update the pagination service with the new total
this.paginationService.updateTotalItems(this.totalItems);
}
}
// resize the grid inside a slight timeout, in case other DOM element changed prior to the resize (like a filter/pagination changed)
if (this.slickGrid && this.gridOptions.enableAutoResize) {
const delay = this.gridOptions.autoResize && this.gridOptions.autoResize.delay;
this.resizerService.resizeGrid(delay || 10);
}
}
}
setData(data: TData[], shouldAutosizeColumns = false) {
if (shouldAutosizeColumns) {
this._isAutosizeColsCalled = false;
this._currentDatasetLength = 0;
}
this.dataset = data || [];
}
/**
* Check if there's any Pagination Presets defined in the Grid Options,
* if there are then load them in the paginationOptions object
*/
protected setPaginationOptionsWhenPresetDefined(gridOptions: GridOption, paginationOptions: Pagination): Pagination {
if (gridOptions.presets?.pagination && paginationOptions && !this._isPaginationInitialized) {
if (this.hasBackendInfiniteScroll()) {
console.warn('[Angular-Slickgrid] `presets.pagination` is not supported with Infinite Scroll, reverting to first page.');
} else {
paginationOptions.pageSize = gridOptions.presets.pagination.pageSize;
paginationOptions.pageNumber = gridOptions.presets.pagination.pageNumber;
}
}
return paginationOptions;
}
setDarkMode(dark = false) {
if (dark) {
this.sharedService.gridContainerElement?.classList.add('slick-dark-mode');
} else {
this.sharedService.gridContainerElement?.classList.remove('slick-dark-mode');
}
}
/**
* Dynamically change or update the column definitions list.
* We will re-render the grid so that the new header and data shows up correctly.
* If using i18n, we also need to trigger a re-translate of the column headers
*/
updateColumnDefinitionsList(newColumnDefinitions: Column[]) {
// map the Editor model to editorClass and load editor collectionAsync
newColumnDefinitions = this.loadSlickGridEditors(newColumnDefinitions);
if (this.gridOptions.enableTranslate) {
this.extensionService.translateColumnHeaders(undefined, newColumnDefinitions);
} else {
this.extensionService.renderColumnHeaders(newColumnDefinitions, true);
}
if (this.gridOptions?.enableAutoSizeColumns) {
this.slickGrid.autosizeColumns();
} else if (this.gridOptions?.enableAutoResizeColumnsByCellContent && this.resizerService?.resizeColumnsByCellContent) {
this.resizerService.resizeColumnsByCellContent();
}
}
/**
* Show the filter row displayed on first row, we can optionally pass false to hide it.
* @param showing
*/
showHeaderRow(showing = true) {
this.slickGrid.setHeaderRowVisibility(showing);
if (showing === true && this._isGridInitialized) {
this.slickGrid.setColumns(this.columnDefinitions);
}
return showing;
}
//
// protected functions
// ------------------
/**
* Loop through all column definitions and copy the original optional `width` properties optionally provided by the user.
* We will use this when doing a resize by cell content, if user provided a `width` it won't override it.
*/
protected copyColumnWidthsReference(columnDefinitions: Column[]) {
columnDefinitions.forEach(col => col.originalWidth = col.width);
}
protected displayEmptyDataWarning(showWarning = true) {
this.slickEmptyWarning?.showEmptyDataMessage(showWarning);
}
protected bindDifferentHooks(grid: SlickGrid, gridOptions: GridOption, dataView: SlickDataView) {
// on locale change, we have to manually translate the Headers, GridMenu
if (this.translate?.onLangChange) {
// translate some of them on first load, then on each language change
if (gridOptions.enableTranslate) {
this.extensionService.translateAllExtensions();
}
this.subscriptions.push(
this.translate.onLangChange.subscribe(({ lang }) => {
// publish event of the same name that Slickgrid-Universal uses on a language change event
this._eventPubSubService.publish('onLanguageChange');
if (gridOptions.enableTranslate) {
this.extensionService.translateAllExtensions(lang);
if ((gridOptions.createPreHeaderPanel && gridOptions.createTopHeaderPanel) || (gridOptions.createPreHeaderPanel && !gridOptions.enableDraggableGrouping)) {
this.headerGroupingService.translateHeaderGrouping();
}
}
})
);
}
// if user set an onInit Backend, we'll run it right away (and if so, we also need to run preProcess, internalPostProcess & postProcess)
if (gridOptions.backendServiceApi) {
const backendApi = gridOptions.backendServiceApi;
if (backendApi?.service?.init) {
backendApi.service.init(backendApi.options, gridOptions.pagination, this.slickGrid, this.sharedService);
}
}
if (dataView && grid) {
// on cell click, mainly used with the columnDef.action callback
this.gridEventService.bindOnCellChange(grid);
this.gridEventService.bindOnClick(grid);
// bind external sorting (backend) when available or default onSort (dataView)
if (gridOptions.enableSorting) {
// bind external sorting (backend) unless specified to use the local one
if (gridOptions.backendServiceApi && !gridOptions.backendServiceApi.useLocalSorting) {
this.sortService.bindBackendOnSort(grid);
} else {
this.sortService.bindLocalOnSort(grid);
}
}
// bind external filter (backend) when available or default onFilter (dataView)
if (gridOptions.enableFiltering) {
this.filterService.init(grid);
// bind external filter (backend) unless specified to use the local one
if (gridOptions.backendServiceApi && !gridOptions.backendServiceApi.useLocalFiltering) {
this.filterService.bindBackendOnFilter(grid);
} else {
this.filterService.bindLocalOnFilter(grid);
}
}
// when column are reordered, we need to update the visibleColumn array
this._eventHandler.subscribe(grid.onColumnsReordered, (_e, args) => {
this.sharedService.hasColumnsReordered = true;
this.sharedService.visibleColumns = args.impactedColumns;
});
this._eventHandler.subscribe(grid.onSetOptions, (_e, args) => {
// add/remove dark mode CSS class when enabled
if (args.optionsBefore.darkMode !== args.optionsAfter.darkMode && this.gridContainerElement) {
this.setDarkMode(args.optionsAfter.darkMode);
}
});
// load any presets if any (after dataset is initialized)
this.loadColumnPresetsWhenDatasetInitialized();
this.loadFilterPresetsWhenDatasetInitialized();
// When data changes in the DataView, we need to refresh the metrics and/or display a warning if the dataset is empty
this._eventHandler.subscribe(dataView.onRowCountChanged, () => {
grid.invalidate();
this.handleOnItemCountChanged(dataView.getFilteredItemCount() || 0, dataView.getItemCount() || 0);
});
this._eventHandler.subscribe(dataView.onSetItemsCalled, (_e, args) => {
this.sharedService.isItemsDateParsed = false;
this.handleOnItemCountChanged(dataView.getFilteredItemCount() || 0, args.itemCount);
// when user has resize by content enabled, we'll force a full width calculation since we change our entire dataset
if (args.itemCount > 0 && (this.gridOptions.autosizeColumnsByCellContentOnFirstLoad || this.gridOptions.enableAutoResizeColumnsByCellContent)) {
this.resizerService.resizeColumnsByCellContent(!this.gridOptions?.resizeByContentOnlyOnFirstLoad);
}
});
if (gridOptions?.enableFiltering && !gridOptions.enableRowDetailView) {
this._eventHandler.subscribe(dataView.onRowsChanged, (_e, { calledOnRowCountChanged, rows }) => {
// filtering data with local dataset will not always show correctly unless we call this updateRow/render
// also don't use "invalidateRows" since it destroys the entire row and as bad user experience when updating a row
// see commit: https://github.com/ghiscoding/aurelia-slickgrid/commit/8c503a4d45fba11cbd8d8cc467fae8d177cc4f60
if (!calledOnRowCountChanged && Array.isArray(rows)) {
const ranges = grid.getRenderedRange();
rows
.filter(row => row >= ranges.top && row <= ranges.bottom)
.forEach((row: number) => grid.updateRow(row));
grid.render();
}
});
}
}
// did the user add a colspan callback? If so, hook it into the DataView getItemMetadata
if (gridOptions?.colspanCallback && dataView && dataView.getItem && dataView.getItemMetadata) {
dataView.getItemMetadata = (rowNumber: number) => {
let callbackResult: ItemMetadata | null = null;
if (gridOptions.colspanCallback) {
callbackResult = gridOptions.colspanCallback(dataView.getItem(rowNumber));
}
return callbackResult;
};
}
}
protected bindBackendCallbackFunctions(gridOptions: GridOption) {
const backendApi = gridOptions.backendServiceApi;
const backendApiService = backendApi?.service;
const serviceOptions: BackendServiceOption = backendApiService?.options ?? {};
const isExecuteCommandOnInit = (!serviceOptions) ? false : ((serviceOptions && 'executeProcessCommandOnInit' in serviceOptions) ? serviceOptions['executeProcessCommandOnInit'] : true);
if (backendApiService) {
// update backend filters (if need be) BEFORE the query runs (via the onInit command a few lines below)
// if user entered some any "presets", we need to reflect them all in the grid
if (gridOptions?.presets) {
// Filters "presets"
if (backendApiService.updateFilters && Array.isArray(gridOptions.presets.filters) && gridOptions.presets.filters.length > 0) {
backendApiService.updateFilters(gridOptions.presets.filters, true);
}
// Sorters "presets"
if (backendApiService.updateSorters && Array.isArray(gridOptions.presets.sorters) && gridOptions.presets.sorters.length > 0) {