-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
controlAndPlugin.service.ts
833 lines (754 loc) · 32.1 KB
/
controlAndPlugin.service.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
import { Injectable } from '@angular/core';
import {
CellArgs,
ColumnSort,
CustomGridMenu,
Column,
DelimiterType,
FileType,
GraphqlResult,
GridMenu,
GridOption,
HeaderButtonOnCommandArgs,
HeaderMenu,
HeaderMenuItem,
HeaderMenuOnCommandArgs,
HeaderMenuOnBeforeMenuShowArgs,
} from './../models/index';
import { TranslateService } from '@ngx-translate/core';
import { castToPromise, sanitizeHtmlToText } from './../services/utilities';
import { FilterService } from './filter.service';
import { ExportService } from './export.service';
import { SortService } from './sort.service';
// using external non-typed js libraries
declare var Slick: any;
declare var $: any;
@Injectable()
export class ControlAndPluginService {
private _dataView: any;
private _grid: any;
visibleColumns: Column[];
areVisibleColumnDifferent = false;
// controls & plugins
autoTooltipPlugin: any;
checkboxSelectorPlugin: any;
columnPickerControl: any;
headerButtonsPlugin: any;
headerMenuPlugin: any;
gridMenuControl: any;
rowSelectionPlugin: any;
undoRedoBuffer: any;
constructor(
private exportService: ExportService,
private filterService: FilterService,
private sortService: SortService,
private translate: TranslateService
) {}
/** Getter for the Grid Options pulled through the Grid Object */
private get _gridOptions(): GridOption {
return (this._grid && this._grid.getOptions) ? this._grid.getOptions() : {};
}
/** Getter for the Column Definitions pulled through the Grid Object */
private get _columnDefinitions(): Column[] {
return (this._grid && this._grid.getColumns) ? this._grid.getColumns() : [];
}
/** Auto-resize all the column in the grid to fit the grid width */
autoResizeColumns() {
this._grid.autosizeColumns();
}
/**
* Attach/Create different Controls or Plugins after the Grid is created
* @param grid
* @param options
* @param dataView
*/
attachDifferentControlOrPlugins(grid: any, dataView: any, groupItemMetadataProvider: any) {
this._grid = grid;
this._dataView = dataView;
this.visibleColumns = this._columnDefinitions;
// Column Picker Plugin
if (this._gridOptions.enableColumnPicker) {
this.columnPickerControl = this.createColumnPicker(this._grid, this._columnDefinitions);
}
// Grid Menu Plugin
if (this._gridOptions.enableGridMenu) {
this.gridMenuControl = this.createGridMenu(this._grid, this._columnDefinitions);
}
// Auto Tooltip Plugin
if (this._gridOptions.enableAutoTooltip) {
this.autoTooltipPlugin = new Slick.AutoTooltips(this._gridOptions.autoTooltipOptions || {});
this._grid.registerPlugin(this.autoTooltipPlugin);
}
// Grouping Plugin
// register the group item metadata provider to add expand/collapse group handlers
if (this._gridOptions.enableGrouping) {
const groupItemMetaProvider = groupItemMetadataProvider || {};
this._grid.registerPlugin(groupItemMetaProvider);
}
// Checkbox Selector Plugin
if (this._gridOptions.enableCheckboxSelector) {
// when enabling the Checkbox Selector Plugin, we need to also watch onClick events to perform certain actions
// the selector column has to be create BEFORE the grid (else it behaves oddly), but we can only watch grid events AFTER the grid is created
this._grid.registerPlugin(this.checkboxSelectorPlugin);
// this also requires the Row Selection Model to be registered as well
if (!this.rowSelectionPlugin) {
this.rowSelectionPlugin = new Slick.RowSelectionModel(this._gridOptions.rowSelectionOptions || {});
this._grid.setSelectionModel(this.rowSelectionPlugin);
}
}
// Row Selection Plugin
if (!this._gridOptions.enableCheckboxSelector && this._gridOptions.enableRowSelection) {
this.rowSelectionPlugin = new Slick.RowSelectionModel(this._gridOptions.rowSelectionOptions || {});
this._grid.setSelectionModel(this.rowSelectionPlugin);
}
// Header Button Plugin
if (this._gridOptions.enableHeaderButton) {
this.headerButtonsPlugin = new Slick.Plugins.HeaderButtons(this._gridOptions.headerButton || {});
this._grid.registerPlugin(this.headerButtonsPlugin);
this.headerButtonsPlugin.onCommand.subscribe((e: Event, args: HeaderButtonOnCommandArgs) => {
if (this._gridOptions.headerButton && typeof this._gridOptions.headerButton.onCommand === 'function') {
this._gridOptions.headerButton.onCommand(e, args);
}
});
}
// Header Menu Plugin
if (this._gridOptions.enableHeaderMenu) {
this.headerMenuPlugin = this.createHeaderMenu(this._grid, this._dataView, this._columnDefinitions);
}
// Cell External Copy Manager Plugin (Excel Like)
if (this._gridOptions.enableExcelCopyBuffer) {
this.createUndoRedoBuffer();
this.hookUndoShortcutKey();
this.createCellExternalCopyManagerPlugin(this._grid);
}
// manually register other plugins
if (this._gridOptions.registerPlugins !== undefined) {
if (Array.isArray(this._gridOptions.registerPlugins)) {
this._gridOptions.registerPlugins.forEach((plugin) => {
this._grid.registerPlugin(plugin);
});
} else {
this._grid.registerPlugin(this._gridOptions.registerPlugins);
}
}
}
/**
* Attach/Create different plugins before the Grid creation.
* For example the multi-select have to be added to the column definition before the grid is created to work properly
* @param columnDefinitions
* @param options
*/
createPluginBeforeGridCreation(columnDefinitions: Column[], options: GridOption) {
if (options.enableCheckboxSelector) {
this.checkboxSelectorPlugin = new Slick.CheckboxSelectColumn(options.checkboxSelector || {});
const selectionColumn: Column = this.checkboxSelectorPlugin.getColumnDefinition();
selectionColumn.excludeFromExport = true;
selectionColumn.excludeFromQuery = true;
selectionColumn.excludeFromHeaderMenu = true;
columnDefinitions.unshift(selectionColumn);
}
}
/** Create the Excel like copy manager */
createCellExternalCopyManagerPlugin(grid: any) {
let newRowIds = 0;
const pluginOptions = {
clipboardCommandHandler: (editCommand) => {
this.undoRedoBuffer.queueAndExecuteCommand.call(this.undoRedoBuffer, editCommand);
},
dataItemColumnValueExtractor: (item, columnDef) => {
// when grid or cell is not editable, we will possibly evaluate the Formatter if it was passed
// to decide if we evaluate the Formatter, we will use the same flag from Export which is "exportWithFormatter"
if (!this._gridOptions.editable || !columnDef.editor) {
const isEvaluatingFormatter = (columnDef.exportWithFormatter !== undefined) ? columnDef.exportWithFormatter : this._gridOptions.exportOptions.exportWithFormatter;
if (columnDef.formatter && isEvaluatingFormatter) {
const formattedOutput = columnDef.formatter(0, 0, item[columnDef.field], columnDef, item, this._grid);
if (columnDef.sanitizeDataExport || (this._gridOptions.exportOptions && this._gridOptions.exportOptions.sanitizeDataExport)) {
return sanitizeHtmlToText(formattedOutput);
}
return formattedOutput;
}
}
// else use the default "dataItemColumnValueExtractor" from the plugin itself
// we can do that by setting back the getter with null
return null;
},
readOnlyMode: false,
includeHeaderWhenCopying: false,
newRowCreator: (count) => {
for (let i = 0; i < count; i++) {
const item = {
id: 'newRow_' + newRowIds++
};
grid.getData().addItem(item);
}
}
};
grid.setSelectionModel(new Slick.CellSelectionModel());
grid.registerPlugin(new Slick.CellExternalCopyManager(pluginOptions));
}
/**
* Create the Column Picker and expose all the available hooks that user can subscribe (onColumnsChanged)
* @param grid
* @param columnDefinitions
* @param gridOptions
*/
createColumnPicker(grid: any, columnDefinitions: Column[]) {
// localization support for the picker
const forceFitTitle = this._gridOptions.enableTranslate ? this.translate.instant('FORCE_FIT_COLUMNS') : 'Force fit columns';
const syncResizeTitle = this._gridOptions.enableTranslate ? this.translate.instant('SYNCHRONOUS_RESIZE') : 'Synchronous resize';
this._gridOptions.columnPicker = this._gridOptions.columnPicker || {};
this._gridOptions.columnPicker.forceFitTitle = this._gridOptions.columnPicker.forceFitTitle || forceFitTitle;
this._gridOptions.columnPicker.syncResizeTitle = this._gridOptions.columnPicker.syncResizeTitle || syncResizeTitle;
this.columnPickerControl = new Slick.Controls.ColumnPicker(columnDefinitions, grid, this._gridOptions);
if (grid && this._gridOptions.enableColumnPicker) {
this.columnPickerControl.onColumnsChanged.subscribe((e: Event, args: CellArgs) => {
if (this._gridOptions.columnPicker && typeof this._gridOptions.columnPicker.onColumnsChanged === 'function') {
this._gridOptions.columnPicker.onColumnsChanged(e, args);
}
});
}
}
/**
* Create (or re-create) Grid Menu and expose all the available hooks that user can subscribe (onCommand, onMenuClose, ...)
* @param grid
* @param columnDefinitions
* @param _gridOptions
*/
createGridMenu(grid: any, columnDefinitions: Column[]) {
this._gridOptions.gridMenu = { ...this.getDefaultGridMenuOptions(), ...this._gridOptions.gridMenu };
this.addGridMenuCustomCommands(grid, this._gridOptions);
const gridMenuControl = new Slick.Controls.GridMenu(columnDefinitions, grid, this._gridOptions);
if (grid && this._gridOptions.gridMenu) {
gridMenuControl.onBeforeMenuShow.subscribe((e: Event, args: CellArgs) => {
if (this._gridOptions.gridMenu && typeof this._gridOptions.gridMenu.onBeforeMenuShow === 'function') {
this._gridOptions.gridMenu.onBeforeMenuShow(e, args);
}
});
gridMenuControl.onColumnsChanged.subscribe((e: Event, args: CellArgs) => {
this.areVisibleColumnDifferent = true;
if (this._gridOptions.gridMenu && typeof this._gridOptions.gridMenu.onColumnsChanged === 'function') {
this._gridOptions.gridMenu.onColumnsChanged(e, args);
}
});
gridMenuControl.onCommand.subscribe((e: Event, args: CellArgs) => {
if (this._gridOptions.gridMenu && typeof this._gridOptions.gridMenu.onCommand === 'function') {
this._gridOptions.gridMenu.onCommand(e, args);
}
});
gridMenuControl.onMenuClose.subscribe((e: Event, args: CellArgs) => {
if (this._gridOptions.gridMenu && typeof this._gridOptions.gridMenu.onMenuClose === 'function') {
this._gridOptions.gridMenu.onMenuClose(e, args);
}
// we also want to resize the columns if the user decided to hide certain column(s)
if (grid && typeof grid.autosizeColumns === 'function') {
// make sure that the grid still exist (by looking if the Grid UID is found in the DOM tree)
const gridUid = grid.getUID();
if (this.areVisibleColumnDifferent && gridUid && $(`.${gridUid}`).length > 0) {
grid.autosizeColumns();
}
}
});
}
return gridMenuControl;
}
/**
* Create the Header Menu and expose all the available hooks that user can subscribe (onCommand, onBeforeMenuShow, ...)
* @param grid
* @param columnDefinitions
* @param options
*/
createHeaderMenu(grid: any, dataView: any, columnDefinitions: Column[]) {
this._gridOptions.headerMenu = { ...this.getDefaultHeaderMenuOptions(), ...this._gridOptions.headerMenu };
if (this._gridOptions.enableHeaderMenu) {
this._gridOptions.headerMenu = this.addHeaderMenuCustomCommands(grid, dataView, this._gridOptions, columnDefinitions);
}
const headerMenuPlugin = new Slick.Plugins.HeaderMenu(this._gridOptions.headerMenu);
grid.registerPlugin(headerMenuPlugin);
headerMenuPlugin.onCommand.subscribe((e: Event, args: HeaderMenuOnCommandArgs) => {
if (this._gridOptions.headerMenu && typeof this._gridOptions.headerMenu.onCommand === 'function') {
this._gridOptions.headerMenu.onCommand(e, args);
}
});
headerMenuPlugin.onCommand.subscribe((e: Event, args: HeaderMenuOnBeforeMenuShowArgs) => {
if (this._gridOptions.headerMenu && typeof this._gridOptions.headerMenu.onBeforeMenuShow === 'function') {
this._gridOptions.headerMenu.onBeforeMenuShow(e, args);
}
});
return headerMenuPlugin;
}
/** Create an undo redo buffer used by the Excel like copy */
createUndoRedoBuffer() {
const commandQueue = [];
let commandCtr = 0;
this.undoRedoBuffer = {
queueAndExecuteCommand: (editCommand) => {
commandQueue[commandCtr] = editCommand;
commandCtr++;
editCommand.execute();
},
undo: () => {
if (commandCtr === 0) { return; }
commandCtr--;
const command = commandQueue[commandCtr];
if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) {
command.undo();
}
},
redo: () => {
if (commandCtr >= commandQueue.length) { return; }
const command = commandQueue[commandCtr];
commandCtr++;
if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) {
command.execute();
}
}
};
}
/** Hide a column from the grid */
hideColumn(column: Column) {
if (this._grid && this.visibleColumns) {
const columnIndex = this._grid.getColumnIndex(column.id);
this.visibleColumns = this.removeColumnByIndex(this.visibleColumns, columnIndex);
this._grid.setColumns(this.visibleColumns);
}
}
/** Attach an undo shortcut key hook that will redo/undo the copy buffer */
hookUndoShortcutKey() {
// undo shortcut
$(document).keydown((e) => {
if (e.which === 90 && (e.ctrlKey || e.metaKey)) { // CTRL + (shift) + Z
if (e.shiftKey) {
this.undoRedoBuffer.redo();
} else {
this.undoRedoBuffer.undo();
}
}
});
}
/** Dispose of all the controls & plugins */
dispose() {
this._grid = null;
this._dataView = null;
this.visibleColumns = [];
if (this.columnPickerControl) {
this.columnPickerControl.destroy();
this.columnPickerControl = null;
}
if (this.gridMenuControl) {
this.gridMenuControl.destroy();
this.gridMenuControl = null;
}
if (this.rowSelectionPlugin) {
this.rowSelectionPlugin.destroy();
this.rowSelectionPlugin = null;
}
if (this.checkboxSelectorPlugin) {
this.checkboxSelectorPlugin.destroy();
this.checkboxSelectorPlugin = null;
}
if (this.autoTooltipPlugin) {
this.autoTooltipPlugin.destroy();
this.autoTooltipPlugin = null;
}
if (this.headerButtonsPlugin) {
this.headerButtonsPlugin.destroy();
this.headerButtonsPlugin = null;
}
if (this.headerMenuPlugin) {
this.headerMenuPlugin.destroy();
this.headerMenuPlugin = null;
}
}
/**
* Create Grid Menu with Custom Commands if user has enabled Filters and/or uses a Backend Service (OData, GraphQL)
* @param grid
* @param options
* @return gridMenu
*/
private addGridMenuCustomCommands(grid: any, options: GridOption) {
const backendApi = options.backendServiceApi || null;
if (options.enableFiltering) {
// show grid menu: clear all filters
if (options && options.gridMenu && options.gridMenu.showClearAllFiltersCommand && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'clear-filter').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconClearAllFiltersCommand || 'fa fa-filter text-danger',
title: options.enableTranslate ? this.translate.instant('CLEAR_ALL_FILTERS') : 'Clear All Filters',
disabled: false,
command: 'clear-filter',
positionOrder: 50
}
);
}
// show grid menu: toggle filter row
if (options && options.gridMenu && options.gridMenu.showToggleFilterCommand && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'toggle-filter').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconToggleFilterCommand || 'fa fa-random',
title: options.enableTranslate ? this.translate.instant('TOGGLE_FILTER_ROW') : 'Toggle Filter Row',
disabled: false,
command: 'toggle-filter',
positionOrder: 52
}
);
}
// show grid menu: refresh dataset
if (options && options.gridMenu && options.gridMenu.showRefreshDatasetCommand && backendApi && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'refresh-dataset').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconRefreshDatasetCommand || 'fa fa-refresh',
title: options.enableTranslate ? this.translate.instant('REFRESH_DATASET') : 'Refresh Dataset',
disabled: false,
command: 'refresh-dataset',
positionOrder: 54
}
);
}
}
if (options.enableSorting) {
// show grid menu: clear all sorting
if (options && options.gridMenu && options.gridMenu.showClearAllSortingCommand && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'clear-sorting').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconClearAllSortingCommand || 'fa fa-unsorted text-danger',
title: options.enableTranslate ? this.translate.instant('CLEAR_ALL_SORTING') : 'Clear All Sorting',
disabled: false,
command: 'clear-sorting',
positionOrder: 51
}
);
}
}
// show grid menu: export to file
if (options && options.enableExport && options.gridMenu && options.gridMenu.showExportCsvCommand && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'export-csv').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconExportCsvCommand || 'fa fa-download',
title: options.enableTranslate ? this.translate.instant('EXPORT_TO_CSV') : 'Export in CSV format',
disabled: false,
command: 'export-csv',
positionOrder: 53
}
);
}
// show grid menu: export to text file as tab delimited
if (options && options.enableExport && options.gridMenu && options.gridMenu.showExportTextDelimitedCommand && options.gridMenu.customItems && options.gridMenu.customItems.filter((item: CustomGridMenu) => item.command === 'export-text-delimited').length === 0) {
options.gridMenu.customItems.push(
{
iconCssClass: options.gridMenu.iconExportTextDelimitedCommand || 'fa fa-download',
title: options.enableTranslate ? this.translate.instant('EXPORT_TO_TAB_DELIMITED') : 'Export in Text format (Tab delimited)',
disabled: false,
command: 'export-text-delimited',
positionOrder: 54
}
);
}
// Command callback, what will be executed after command is clicked
if (options.gridMenu && options.gridMenu.customItems.length > 0) {
options.gridMenu.onCommand = (e, args) => {
if (args && args.command) {
switch (args.command) {
case 'clear-filter':
this.filterService.clearFilters();
this._dataView.refresh();
break;
case 'clear-sorting':
this.sortService.clearSorting();
this._dataView.refresh();
break;
case 'export-csv':
this.exportService.exportToFile({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true
});
break;
case 'export-text-delimited':
this.exportService.exportToFile({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true
});
break;
case 'toggle-filter':
grid.setHeaderRowVisibility(!grid.getOptions().showHeaderRow);
break;
case 'toggle-toppanel':
grid.setTopPanelVisibility(!grid.getOptions().showTopPanel);
break;
case 'refresh-dataset':
this.refreshBackendDataset();
break;
default:
alert('Command: ' + args.command);
break;
}
}
};
}
// add the custom "Commands" title if there are any commands
if (options && options.gridMenu && options.gridMenu.customItems && options.gridMenu.customItems.length > 0) {
const customTitle = options.enableTranslate ? this.translate.instant('COMMANDS') : 'Commands';
options.gridMenu.customTitle = options.gridMenu.customTitle || customTitle;
// sort the custom items by their position in the list
options.gridMenu.customItems.sort((itemA, itemB) => {
if (itemA && itemB && itemA.hasOwnProperty('positionOrder') && itemB.hasOwnProperty('positionOrder')) {
return itemA.positionOrder - itemB.positionOrder;
}
return 0;
});
}
}
/**
* Create Header Menu with Custom Commands if user has enabled Header Menu
* @param grid
* @param dataView
* @param options
* @param columnDefinitions
* @return header menu
*/
private addHeaderMenuCustomCommands(grid: any, dataView: any, options: GridOption, columnDefinitions: Column[]): HeaderMenu {
const headerMenuOptions = options.headerMenu;
if (columnDefinitions && Array.isArray(columnDefinitions) && options.enableHeaderMenu) {
columnDefinitions.forEach((columnDef: Column) => {
if (columnDef && !columnDef.excludeFromHeaderMenu) {
if (!columnDef.header || !columnDef.header.menu) {
columnDef.header = {
menu: {
items: []
}
};
}
const columnHeaderMenuItems: HeaderMenuItem[] = columnDef.header.menu.items || [];
// Sorting Commands
if (options.enableSorting && columnDef.sortable && headerMenuOptions.showSortCommands) {
if (columnHeaderMenuItems.filter((item: HeaderMenuItem) => item.command === 'sort-asc').length === 0) {
columnHeaderMenuItems.push({
iconCssClass: headerMenuOptions.iconSortAscCommand || 'fa fa-sort-asc',
title: options.enableTranslate ? this.translate.instant('SORT_ASCENDING') : 'Sort Ascending',
command: 'sort-asc'
});
}
if (columnHeaderMenuItems.filter((item: HeaderMenuItem) => item.command === 'sort-desc').length === 0) {
columnHeaderMenuItems.push({
iconCssClass: headerMenuOptions.iconSortDescCommand || 'fa fa-sort-desc',
title: options.enableTranslate ? this.translate.instant('SORT_DESCENDING') : 'Sort Descending',
command: 'sort-desc'
});
}
}
// Hide Column Command
if (headerMenuOptions.showColumnHideCommand && columnHeaderMenuItems.filter((item: HeaderMenuItem) => item.command === 'hide').length === 0) {
columnHeaderMenuItems.push({
iconCssClass: headerMenuOptions.iconColumnHideCommand || 'fa fa-times',
title: options.enableTranslate ? this.translate.instant('HIDE_COLUMN') : 'Hide Column',
command: 'hide'
});
}
}
});
// Command callback, what will be executed after command is clicked
if (headerMenuOptions) {
headerMenuOptions.onCommand = (e, args) => {
if (args && args.command) {
switch (args.command) {
case 'hide':
this.hideColumn(args.column);
this.autoResizeColumns();
break;
case 'sort-asc':
case 'sort-desc':
// get previously sorted columns
const cols: ColumnSort[] = this.sortService.getPreviousColumnSorts(args.column.id + '');
// add to the column array, the column sorted by the header menu
cols.push({ sortCol: args.column, sortAsc: (args.command === 'sort-asc') });
if (options.backendServiceApi) {
this.sortService.onBackendSortChanged(e, { multiColumnSort: true, sortCols: cols, grid });
} else {
this.sortService.onLocalSortChanged(grid, dataView, cols);
}
// update the this.gridObj sortColumns array which will at the same add the visual sort icon(s) on the UI
const newSortColumns: ColumnSort[] = cols.map((col) => {
return { columnId: col.sortCol.id, sortAsc: col.sortAsc };
});
grid.setSortColumns(newSortColumns); // add sort icon in UI
break;
default:
alert('Command: ' + args.command);
break;
}
}
};
}
}
return headerMenuOptions;
}
/** Refresh the dataset through the Backend Service */
refreshBackendDataset() {
let query;
const backendApi = this._gridOptions.backendServiceApi;
if (!backendApi || !backendApi.service || !backendApi.process) {
throw new Error(`BackendServiceApi requires at least a "process" function and a "service" defined`);
}
if (backendApi.service) {
query = backendApi.service.buildQuery();
}
if (query && query !== '') {
if (backendApi.preProcess) {
backendApi.preProcess();
}
// the process could be an Observable (like HttpClient) or a Promise
// in any case, we need to have a Promise so that we can await on it (if an Observable, convert it to Promise)
const observableOrPromise = backendApi.process(query);
castToPromise(observableOrPromise).then((processResult: GraphqlResult | any) => {
// from the result, call our internal post process to update the Dataset and Pagination info
if (processResult && backendApi.internalPostProcess) {
backendApi.internalPostProcess(processResult);
}
// send the response process to the postProcess callback
if (backendApi.postProcess) {
backendApi.postProcess(processResult);
}
});
}
}
/** Remove a column from the grid by it's index in the grid */
removeColumnByIndex(array: any[], index: number) {
return array.filter((el: any, i: number) => {
return index !== i;
});
}
/**
* Translate the Column Picker and it's last 2 checkboxes
* Note that the only way that seems to work is to destroy and re-create the Column Picker
* Changing only the columnPicker.columnTitle with i18n translate was not enough.
*/
translateColumnPicker() {
// destroy and re-create the Column Picker which seems to be the only way to translate properly
if (this.columnPickerControl) {
this.columnPickerControl.destroy();
this.columnPickerControl = null;
}
this._gridOptions.columnPicker = undefined;
this.createColumnPicker(this._grid, this.visibleColumns);
}
/**
* Translate the Grid Menu ColumnTitle and CustomTitle.
* Note that the only way that seems to work is to destroy and re-create the Grid Menu
* Changing only the gridMenu.columnTitle with i18n translate was not enough.
*/
translateGridMenu() {
// destroy and re-create the Grid Menu which seems to be the only way to translate properly
this.gridMenuControl.destroy();
// reset all Grid Menu options that have translation text & then re-create the Grid Menu and also the custom items array
if (this._gridOptions && this._gridOptions.gridMenu) {
this._gridOptions.gridMenu = this.resetGridMenuTranslations(this._gridOptions.gridMenu);
}
this.createGridMenu(this._grid, this.visibleColumns);
}
/**
* Translate the Header Menu titles, we need to loop through all column definition to re-translate them
*/
translateHeaderMenu() {
// reset all Grid Menu options that have translation text & then re-create the Grid Menu and also the custom items array
if (this._gridOptions && this._gridOptions.headerMenu) {
this.resetHeaderMenuTranslations(this.visibleColumns);
}
}
/**
* Translate manually the header titles.
* We could optionally pass a locale (that will change currently loaded locale), else it will use current locale
* @param locale locale to use
*/
translateColumnHeaders(locale?: string) {
if (locale) {
this.translate.use(locale);
}
for (const column of this._columnDefinitions) {
if (column.headerKey) {
column.name = this.translate.instant(column.headerKey);
}
}
// re-render the column headers
this.renderColumnHeaders();
}
/**
* Render (or re-render) the column headers from column definitions.
* calling setColumns() will trigger a grid re-render
*/
renderColumnHeaders(newColumnDefinitions?: Column[]) {
const collection = newColumnDefinitions || this._columnDefinitions;
if (Array.isArray(collection) && this._grid && this._grid.setColumns) {
this._grid.setColumns(collection);
}
}
/**
* @return default Grid Menu options
*/
private getDefaultGridMenuOptions(): GridMenu {
return {
columnTitle: this.translate.instant('COLUMNS') || 'Columns',
forceFitTitle: this.translate.instant('FORCE_FIT_COLUMNS') || 'Force fit columns',
syncResizeTitle: this.translate.instant('SYNCHRONOUS_RESIZE') || 'Synchronous resize',
iconCssClass: 'fa fa-bars',
menuWidth: 18,
customTitle: undefined,
customItems: [],
showClearAllFiltersCommand: true,
showRefreshDatasetCommand: true,
showToggleFilterCommand: true
};
}
/**
* @return default Header Menu options
*/
private getDefaultHeaderMenuOptions(): HeaderMenu {
return {
autoAlignOffset: 12,
minWidth: 140,
showColumnHideCommand: true,
showSortCommands: true
};
}
/**
* Reset all the Grid Menu options which have text to translate
* @param grid menu object
*/
private resetGridMenuTranslations(gridMenu: GridMenu): GridMenu {
// we will reset the custom items array since the commands title have to be translated too (no worries, we will re-create it later)
gridMenu.customItems = [];
delete gridMenu.customTitle;
gridMenu.columnTitle = this.translate.instant('COLUMNS') || 'Columns';
gridMenu.forceFitTitle = this.translate.instant('FORCE_FIT_COLUMNS') || 'Force fit columns';
gridMenu.syncResizeTitle = this.translate.instant('SYNCHRONOUS_RESIZE') || 'Synchronous resize';
return gridMenu;
}
/**
* Reset all the Grid Menu options which have text to translate
* @param grid menu object
*/
private resetHeaderMenuTranslations(columnDefinitions: Column[]) {
columnDefinitions.forEach((columnDef: Column) => {
if (columnDef && columnDef.header && columnDef.header && columnDef.header.menu && columnDef.header.menu.items) {
if (!columnDef.excludeFromHeaderMenu) {
const columnHeaderMenuItems: HeaderMenuItem[] = columnDef.header.menu.items || [];
columnHeaderMenuItems.forEach((item) => {
switch (item.command) {
case 'sort-asc':
item.title = this.translate.instant('SORT_ASCENDING') || 'Sort Ascending';
break;
case 'sort-desc':
item.title = this.translate.instant('SORT_DESCENDING') || 'Sort Ascending';
break;
case 'hide':
item.title = this.translate.instant('HIDE_COLUMN') || 'Sort Ascending';
break;
}
});
}
}
});
}
}