-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.component.ts
More file actions
383 lines (315 loc) · 10.5 KB
/
table.component.ts
File metadata and controls
383 lines (315 loc) · 10.5 KB
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
import {
BehaviorSubject,
ReplaySubject,
combineLatest,
firstValueFrom,
map,
shareReplay,
} from 'rxjs';
import { Clipboard } from '@angular/cdk/clipboard';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { CommonModule } from '@angular/common';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
DestroyRef,
EventEmitter,
HostBinding,
Input,
Output,
TrackByFunction,
ViewChild,
booleanAttribute,
inject,
numberAttribute,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatPaginator } from '@angular/material/paginator';
import { MatSnackBar } from '@angular/material/snack-bar';
import {
MatSort,
MatSortModule,
Sort,
SortDirection,
} from '@angular/material/sort';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import { MatTooltipModule } from '@angular/material/tooltip';
import { DateTimePipe } from '../pipes';
import {
TableColumn,
TableSortChangeEvent,
TableTemplateReferenceExpandableObject,
} from '../types';
import {
getData as _getData,
getDate as _getDate,
getTypeOf as _getTypeOf,
isNonEmptyObject as _isNonEmptyObject,
isTableTemplateRefExpandableObject as _isTableTemplateRefExpandableObject,
isTableTemplateRefObject as _isTableTemplateRefObject,
copyToClipboard,
isNonEmptyString,
} from '../utilities';
const rT = { required: true };
const rF = { required: false };
const rFB = { required: false, transform: booleanAttribute };
const rFN = { required: false, transform: numberAttribute };
/**
* A custom table component wrapper for Angular Material's table component.
*
* @see https://material.angular.dev/components/table/overview
*/
@Component({
selector: 'pro-table[columns][data]',
standalone: true,
imports: [
CommonModule,
MatTableModule,
MatSortModule,
MatCheckboxModule,
MatTooltipModule,
DateTimePipe,
],
templateUrl: './table.component.html',
styleUrls: ['./table.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableComponent<T extends object & { id: number | string }>
implements AfterViewInit
{
/** ---- INJECTED SERVICE DEPENDENCIES ---- */
private readonly clipboard = inject(Clipboard);
private readonly destroyRef = inject(DestroyRef);
private readonly matSnackBar = inject(MatSnackBar);
/** ---- INPUT BINDINGS (REQUIRED) ---- */
@Input(rT) public set columns(value: ReadonlyArray<TableColumn<T>>) {
this.columnsSubject.next(value);
}
@Input(rT)
public set data(values: readonly T[] | null | undefined) {
if (values === null || values === undefined) {
this.dataSubject.next(null);
return;
}
this.dataSubject.next(
new MatTableDataSource<T, MatPaginator>(Array.from(values)),
);
}
/** ---- INPUT BINDINGS (OPTIONAL) ---- */
@Input(rF) public initialSort: TableSortChangeEvent<T> | null = null;
@Input(rF) public placeholderEmptyData = 'N/A';
@Input(rF) public placeholderEmptyList = 'No items to display.';
@Input(rF) public placeholderLoading = 'Loading...';
@Input(rF) public set selectable(value: BooleanInput) {
this.selectableSubject.next(coerceBooleanProperty(value));
}
@Input(rFB) public stickyHeader = false;
@Input(rFN) public maxSelectableRows: number | null = null;
/** ---- INPUT + HOST BINDINGS ---- */
@HostBinding('class.highlight-odd-rows')
@Input(rF)
public highlightOddRows = false;
@HostBinding('class.clickable')
@Input(rF)
public rowClickEnabled = false;
/** ---- OUTPUT EVENTS ---- */
@Output() public readonly rowClick = new EventEmitter<T>();
@Output() public readonly rowSelectChange = new EventEmitter<readonly T[]>();
@Output() public readonly sortChange = new EventEmitter<
TableSortChangeEvent<T>
>();
/** ---- DATA STREAMS ---- */
private readonly selectableSubject = new BehaviorSubject<boolean>(false);
protected readonly selectableChanges = this.selectableSubject
.asObservable()
.pipe(shareReplay({ bufferSize: 1, refCount: true }));
private readonly columnsSubject = new ReplaySubject<
ReadonlyArray<TableColumn<T>>
>(1);
protected readonly columnsChanges = this.columnsSubject.asObservable();
protected readonly columnKeysChanges = combineLatest([
this.columnsSubject,
this.selectableChanges,
]).pipe(
map(([columns, selectable]) => {
const keys = columns.map(({ key }) => key);
return selectable ? ['select', ...keys] : keys;
}),
takeUntilDestroyed(this.destroyRef),
);
private readonly dataSubject = new ReplaySubject<
MatTableDataSource<T, MatPaginator> | null | undefined
>(1);
protected readonly dataChanges = this.dataSubject
.asObservable()
.pipe(shareReplay({ bufferSize: 1, refCount: true }));
private readonly isAllSelectedSubject = new BehaviorSubject<boolean>(false);
protected readonly isAllSelectedChanges =
this.isAllSelectedSubject.asObservable();
/** ---- UTILITIES ---- */
protected readonly getData = _getData;
protected readonly getDate = _getDate;
protected readonly getTypeOf = _getTypeOf;
protected readonly isNonEmptyObject = _isNonEmptyObject;
protected readonly isTableTemplateRefExpandableObject =
_isTableTemplateRefExpandableObject;
protected readonly isTableTemplateRefObject = _isTableTemplateRefObject;
/** ---- MEMBERS ---- */
@ViewChild(MatSort) private matSort!: MatSort;
protected selectedKeys = new Set<string | number>();
protected set expandableObject(
value: TableTemplateReferenceExpandableObject | null,
) {
if (value !== null && !this.isTableTemplateRefExpandableObject(value)) {
throw new Error(
'The expandable value must be of type TableTemplateReferenceExpandableObject.',
);
}
this.#expandableObject = value;
}
protected get expandableObject(): TableTemplateReferenceExpandableObject | null {
return this.#expandableObject;
}
#expandableObject: TableTemplateReferenceExpandableObject | null = null;
/** ---- FUNCTIONS ---- */
public ngAfterViewInit(): void {
if (!this.matSort) {
return; // `mat-sort` isn't used, return early
}
const init = this.initialSort;
if (init?.key && init.direction) {
this.matSort.sort({
disableClear: false,
id: init.key,
start: init.direction satisfies SortDirection,
});
}
}
public clearSelected(): void {
this.selectedKeys.clear();
this.rowSelectChange.emit([]);
this.updateAllSelectedStatus();
}
@Input()
public trackByFn: TrackByFunction<T> = (_index, item) => item['id'];
protected isRowExpanded(
rowObjectData: T,
expandableObject: TableTemplateReferenceExpandableObject | null,
): boolean {
if (expandableObject === null) return false;
const obj = rowObjectData as Record<string, unknown>;
for (const k of Object.keys(obj)) {
if (obj[k] === expandableObject) return true;
}
return false;
}
protected isSelected(row: T): boolean {
return this.selectedKeys.has(this.trackByFn(0, row));
}
protected async masterToggle(): Promise<void> {
const data = (await firstValueFrom(this.dataChanges))?.data ?? [];
const allSelected = data.every((row) => this.isSelected(row));
if (
allSelected ||
(this.maxSelectableRows !== null &&
this.selectedKeys.size >= this.maxSelectableRows)
) {
for (const row of data) this.selectedKeys.delete(this.trackByFn(0, row));
} else {
for (const row of data) {
if (
this.maxSelectableRows !== null &&
this.selectedKeys.size >= this.maxSelectableRows
) {
this.matSnackBar.open(
`You can only select up to ${this.maxSelectableRows} rows.`,
'Close',
{ duration: 3000 },
);
break;
}
this.selectedKeys.add(this.trackByFn(0, row));
}
}
this.rowSelectChange.emit(await this.getSelectedRows());
this.updateAllSelectedStatus();
}
protected onRowClick(
rowObjectData: T,
event: MouseEvent,
column?: TableColumn<T>,
item?: HTMLTableCellElement,
): void {
event.stopPropagation();
if (column?.copyable && item) {
this.copyToClipboard(item);
return;
}
if (!this.rowClickEnabled) return;
this.rowClick.emit(rowObjectData);
}
protected async onSortChange(sort: Sort): Promise<void> {
const key = sort.active;
const direction = isNonEmptyString(sort.direction) ? sort.direction : null;
const columns = await firstValueFrom(this.columnsSubject);
const column = columns.find((c) => c.key === key);
if (!column?.isSortable) return;
const sortChange: TableSortChangeEvent<T> = {
direction,
key:
direction && isNonEmptyString(key)
? (column.sortKey ?? column.key)
: null,
};
this.sortChange.emit(sortChange);
}
protected async toggleSelection(row: T): Promise<void> {
const key = this.trackByFn(0, row);
if (this.selectedKeys.has(key)) {
this.selectedKeys.delete(key);
} else {
if (
this.maxSelectableRows !== null &&
this.selectedKeys.size >= this.maxSelectableRows
) {
this.matSnackBar.open(
`You can only select up to ${this.maxSelectableRows} rows.`,
'Close',
{ duration: 3000 },
);
return;
}
this.selectedKeys.add(key);
}
this.rowSelectChange.emit(await this.getSelectedRows());
this.updateAllSelectedStatus();
}
private copyToClipboard(item: HTMLTableCellElement): void {
const content = item.innerHTML.replace(/<[^>]*>/g, '').trim();
const copied = copyToClipboard(content, this.clipboard);
if (!copied) {
this.matSnackBar.open(
`Failed to copy "${content}" to clipboard!`,
'Close',
{ duration: 3000 },
);
return;
}
this.matSnackBar.open(`Copied "${content}" to clipboard!`, 'Close', {
duration: 3000,
});
}
private async getSelectedRows(): Promise<T[]> {
const data = (await firstValueFrom(this.dataChanges))?.data ?? [];
return data.filter((row) => this.selectedKeys.has(this.trackByFn(0, row)));
}
private async updateAllSelectedStatus(): Promise<void> {
const data = (await firstValueFrom(this.dataChanges))?.data ?? [];
const allSelected =
data.length > 0 &&
data.every((row) => this.selectedKeys.has(this.trackByFn(0, row)));
this.isAllSelectedSubject.next(allSelected);
}
}