-
Notifications
You must be signed in to change notification settings - Fork 78
/
cell.ts
307 lines (276 loc) · 8.49 KB
/
cell.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
/**
* @license
* Copyright 2019 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { coerceNumberProperty } from '@angular/cdk/coercion';
import { CdkCellDef, CdkColumnDef } from '@angular/cdk/table';
import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
Directive,
ElementRef,
Input,
OnChanges,
OnDestroy,
Optional,
QueryList,
Renderer2,
SkipSelf,
ViewEncapsulation,
} from '@angular/core';
import { Subject, Subscription, merge } from 'rxjs';
import { filter, startWith, switchMap, takeUntil } from 'rxjs/operators';
import {
DtIndicator,
addCssClass,
isDefined,
parseCssValue,
removeCssClass,
} from '@dynatrace/barista-components/core';
import { DtRow } from './row';
import { DtSort, DtSortEvent } from './sort/sort';
/** Custom Types for Cell alignments */
export type DtTableColumnAlign = 'left' | 'right' | 'center';
export type DtTableColumnTypedAlign =
| 'text'
| 'id'
| 'icon'
| 'control'
| 'number'
| 'date'
| 'ip';
/**
* Cell definition for the dt-table.
* Captures the template of a column's data row cell as well as cell-specific properties.
*/
@Directive({
selector: '[dtCellDef]',
exportAs: 'dtCellDef',
providers: [{ provide: CdkCellDef, useExisting: DtCellDef }],
})
export class DtCellDef extends CdkCellDef {}
/**
* Column definition for the dt-table.
* Defines a set of cells available for a table column.
*/
@Directive({
selector: '[dtColumnDef]',
exportAs: 'dtColumnDef',
providers: [{ provide: CdkColumnDef, useExisting: DtColumnDef }],
})
export class DtColumnDef extends CdkColumnDef implements OnChanges {
/** Unique name for this column. */
// tslint:disable-next-line:no-input-rename
@Input('dtColumnDef') name: string;
/** The alignment of the colums */
// tslint:disable-next-line:no-input-rename
@Input('dtColumnAlign') align: DtTableColumnTypedAlign | DtTableColumnAlign =
'left';
/** The proportion of the column compared to the others. */
// tslint:disable-next-line:no-input-rename
@Input('dtColumnProportion') proportion: number;
/** The min width of the column. */
// TODO: Consider switching to ngStyle syntax in the future - value.unit
// tslint:disable-next-line:no-input-rename
@Input('dtColumnMinWidth') minWidth: string | number;
/** @internal Alignment subject which fires with changes to the alignment input. */
_stateChanges = new Subject<void>();
ngOnChanges(): void {
this._stateChanges.next();
}
}
type IndicatorType = 'error' | 'warning';
/** Cell template container that adds the right classes and role. */
@Component({
selector: 'dt-cell',
template: '<ng-content></ng-content>',
styleUrls: ['./cell.scss'],
host: {
class: 'dt-cell',
role: 'gridcell',
'[class.dt-cell-sorted]': '_isSorted',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.Emulated,
exportAs: 'dtCell',
})
export class DtCell implements AfterContentInit, OnDestroy {
/** @internal A list of references of the applied indicator. */
@ContentChildren(DtIndicator, { descendants: true })
_indicators: QueryList<DtIndicator>;
/** Whether the cell has an error */
get hasError(): boolean {
return this._hasIndicator('error');
}
/** Whether the cell has a warning */
get hasWarning(): boolean {
return this._hasIndicator('warning');
}
/**
* @internal
* Emits whenever the indicators change or one of the inputs on the indicators changes
*/
_stateChanges = new Subject<void>();
/**
* @internal
* The parent row
*/
_row: DtRow;
/**
* @internal
* indicates if a cell is sorted, used for displaying a bold value
*/
_isSorted = false;
private _sortChangeSubscription: Subscription = Subscription.EMPTY;
private _destroy = new Subject<void>();
constructor(
public _columnDef: DtColumnDef,
public _changeDetectorRef: ChangeDetectorRef,
renderer: Renderer2,
elem: ElementRef,
@Optional() @SkipSelf() dtSortable?: DtSort,
) {
if (dtSortable) {
this._isSorted = dtSortable.active === this._columnDef.name;
dtSortable.sortChange
.pipe(takeUntil(this._destroy))
.subscribe((sort: DtSortEvent) => {
// If event is void, it is being unregisterd.
this._isSorted = sort.active === this._columnDef.name;
this._changeDetectorRef.detectChanges();
});
}
this._columnDef._stateChanges
.pipe(
startWith(null),
takeUntil(this._destroy),
)
.subscribe(() => {
_updateDtColumnStyles(this._columnDef, elem, renderer);
});
if (DtRow.mostRecentRow) {
this._row = DtRow.mostRecentRow;
this._row._registerCell(this);
}
}
ngAfterContentInit(): void {
this._indicators.changes.pipe(takeUntil(this._destroy)).subscribe(() => {
this._stateChanges.next();
});
// Emits whenever one of the indicator's inputs changes
this._indicators.changes
.pipe(
startWith(null),
takeUntil(this._destroy),
filter(() => !!this._indicators.length),
switchMap(() =>
merge(...this._indicators.map(indicator => indicator._stateChanges)),
),
)
.subscribe(() => {
this._stateChanges.next();
});
Promise.resolve().then(() => {
this._stateChanges.next();
});
}
ngOnDestroy(): void {
this._stateChanges.complete();
this._sortChangeSubscription.unsubscribe();
this._destroy.next();
this._destroy.complete();
if (this._row) {
this._row._unregisterCell(this);
}
}
private _hasIndicator(indicatorType: IndicatorType): boolean {
return (
this._indicators &&
isDefined(
this._indicators.find(
indicator => indicator.active && indicator.color === indicatorType,
),
)
);
}
}
const ALIGNMENT_CAST_MAP = new Map<DtTableColumnTypedAlign, DtTableColumnAlign>(
[
['icon', 'center'],
['control', 'center'],
['number', 'right'],
['date', 'right'],
['ip', 'right'],
],
);
/**
* Maps the provided alignment to a css align provided by the cast map, if there's no coincidence
* return the provided one. In the latter case will be handle with the default left-aligned SCSS style.
*
* This will be also 'type checked' with the Template Compiler feature from @Angular6.
*/
function coerceAlignment(
value: DtTableColumnAlign | DtTableColumnTypedAlign,
): DtTableColumnAlign {
return (
ALIGNMENT_CAST_MAP.get(value as DtTableColumnTypedAlign) ||
(value as DtTableColumnAlign)
);
}
/** @internal Sets the css classes on a DtColumn */
export function _setDtColumnCssClasses(
columnDef: DtColumnDef,
elementRef: ElementRef,
): void {
const { align, cssClassFriendlyName } = columnDef;
const cssAlignmentClass = coerceAlignment(align);
removeCssClass(elementRef.nativeElement, 'dt-table-column-align-left');
removeCssClass(elementRef.nativeElement, 'dt-table-column-align-center');
removeCssClass(elementRef.nativeElement, 'dt-table-column-align-right');
addCssClass(
elementRef.nativeElement,
`dt-table-column-align-${cssAlignmentClass}`,
);
addCssClass(
elementRef.nativeElement,
`dt-table-column-${cssClassFriendlyName}`,
);
}
/** @internal Set classes name and styles props for columns. */
export function _updateDtColumnStyles(
columnDef: DtColumnDef,
elementRef: ElementRef,
renderer: Renderer2,
): void {
_setDtColumnCssClasses(columnDef, elementRef);
const { proportion, minWidth } = columnDef;
const setProportion = coerceNumberProperty(proportion);
if (setProportion > 0) {
renderer.setStyle(elementRef.nativeElement, 'flex-grow', setProportion);
renderer.setStyle(elementRef.nativeElement, 'flex-shrink', setProportion);
}
const valueAndUnit = parseCssValue(minWidth);
if (valueAndUnit !== null) {
renderer.setStyle(
elementRef.nativeElement,
'min-width',
`${valueAndUnit.value}${valueAndUnit.unit}`,
);
} else {
renderer.removeStyle(elementRef.nativeElement, 'min-width');
}
}