1 | import { Directive, Component, ViewEncapsulation, ChangeDetectionStrategy, Input, NgModule } from '@angular/core';
|
---|
2 | import { CdkTable, CDK_TABLE_TEMPLATE, CDK_TABLE, _COALESCED_STYLE_SCHEDULER, _CoalescedStyleScheduler, STICKY_POSITIONING_LISTENER, CdkCellDef, CdkHeaderCellDef, CdkFooterCellDef, CdkColumnDef, CdkHeaderCell, CdkFooterCell, CdkCell, CdkHeaderRowDef, CdkFooterRowDef, CdkRowDef, CdkHeaderRow, CDK_ROW_TEMPLATE, CdkFooterRow, CdkRow, CdkNoDataRow, CdkTextColumn, CdkTableModule, DataSource } from '@angular/cdk/table';
|
---|
3 | import { _VIEW_REPEATER_STRATEGY, _RecycleViewRepeaterStrategy, _DisposeViewRepeaterStrategy } from '@angular/cdk/collections';
|
---|
4 | import { MatCommonModule } from '@angular/material/core';
|
---|
5 | import { _isNumberValue } from '@angular/cdk/coercion';
|
---|
6 | import { BehaviorSubject, Subject, merge, of, combineLatest } from 'rxjs';
|
---|
7 | import { map } from 'rxjs/operators';
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * @license
|
---|
11 | * Copyright Google LLC All Rights Reserved.
|
---|
12 | *
|
---|
13 | * Use of this source code is governed by an MIT-style license that can be
|
---|
14 | * found in the LICENSE file at https://angular.io/license
|
---|
15 | */
|
---|
16 | /**
|
---|
17 | * Enables the recycle view repeater strategy, which reduces rendering latency. Not compatible with
|
---|
18 | * tables that animate rows.
|
---|
19 | */
|
---|
20 | class MatRecycleRows {
|
---|
21 | }
|
---|
22 | MatRecycleRows.decorators = [
|
---|
23 | { type: Directive, args: [{
|
---|
24 | selector: 'mat-table[recycleRows], table[mat-table][recycleRows]',
|
---|
25 | providers: [
|
---|
26 | { provide: _VIEW_REPEATER_STRATEGY, useClass: _RecycleViewRepeaterStrategy },
|
---|
27 | ],
|
---|
28 | },] }
|
---|
29 | ];
|
---|
30 | /**
|
---|
31 | * Wrapper for the CdkTable with Material design styles.
|
---|
32 | */
|
---|
33 | class MatTable extends CdkTable {
|
---|
34 | constructor() {
|
---|
35 | super(...arguments);
|
---|
36 | /** Overrides the sticky CSS class set by the `CdkTable`. */
|
---|
37 | this.stickyCssClass = 'mat-table-sticky';
|
---|
38 | /** Overrides the need to add position: sticky on every sticky cell element in `CdkTable`. */
|
---|
39 | this.needsPositionStickyOnElement = false;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | MatTable.decorators = [
|
---|
43 | { type: Component, args: [{
|
---|
44 | selector: 'mat-table, table[mat-table]',
|
---|
45 | exportAs: 'matTable',
|
---|
46 | template: CDK_TABLE_TEMPLATE,
|
---|
47 | host: {
|
---|
48 | 'class': 'mat-table',
|
---|
49 | '[class.mat-table-fixed-layout]': 'fixedLayout',
|
---|
50 | },
|
---|
51 | providers: [
|
---|
52 | // TODO(michaeljamesparsons) Abstract the view repeater strategy to a directive API so this code
|
---|
53 | // is only included in the build if used.
|
---|
54 | { provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy },
|
---|
55 | { provide: CdkTable, useExisting: MatTable },
|
---|
56 | { provide: CDK_TABLE, useExisting: MatTable },
|
---|
57 | { provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler },
|
---|
58 | // Prevent nested tables from seeing this table's StickyPositioningListener.
|
---|
59 | { provide: STICKY_POSITIONING_LISTENER, useValue: null },
|
---|
60 | ],
|
---|
61 | encapsulation: ViewEncapsulation.None,
|
---|
62 | // See note on CdkTable for explanation on why this uses the default change detection strategy.
|
---|
63 | // tslint:disable-next-line:validate-decorators
|
---|
64 | changeDetection: ChangeDetectionStrategy.Default,
|
---|
65 | styles: ["mat-table{display:block}mat-header-row{min-height:56px}mat-row,mat-footer-row{min-height:48px}mat-row,mat-header-row,mat-footer-row{display:flex;border-width:0;border-bottom-width:1px;border-style:solid;align-items:center;box-sizing:border-box}mat-row::after,mat-header-row::after,mat-footer-row::after{display:inline-block;min-height:inherit;content:\"\"}mat-cell:first-of-type,mat-header-cell:first-of-type,mat-footer-cell:first-of-type{padding-left:24px}[dir=rtl] mat-cell:first-of-type:not(:only-of-type),[dir=rtl] mat-header-cell:first-of-type:not(:only-of-type),[dir=rtl] mat-footer-cell:first-of-type:not(:only-of-type){padding-left:0;padding-right:24px}mat-cell:last-of-type,mat-header-cell:last-of-type,mat-footer-cell:last-of-type{padding-right:24px}[dir=rtl] mat-cell:last-of-type:not(:only-of-type),[dir=rtl] mat-header-cell:last-of-type:not(:only-of-type),[dir=rtl] mat-footer-cell:last-of-type:not(:only-of-type){padding-right:0;padding-left:24px}mat-cell,mat-header-cell,mat-footer-cell{flex:1;display:flex;align-items:center;overflow:hidden;word-wrap:break-word;min-height:inherit}table.mat-table{border-spacing:0}tr.mat-header-row{height:56px}tr.mat-row,tr.mat-footer-row{height:48px}th.mat-header-cell{text-align:left}[dir=rtl] th.mat-header-cell{text-align:right}th.mat-header-cell,td.mat-cell,td.mat-footer-cell{padding:0;border-bottom-width:1px;border-bottom-style:solid}th.mat-header-cell:first-of-type,td.mat-cell:first-of-type,td.mat-footer-cell:first-of-type{padding-left:24px}[dir=rtl] th.mat-header-cell:first-of-type:not(:only-of-type),[dir=rtl] td.mat-cell:first-of-type:not(:only-of-type),[dir=rtl] td.mat-footer-cell:first-of-type:not(:only-of-type){padding-left:0;padding-right:24px}th.mat-header-cell:last-of-type,td.mat-cell:last-of-type,td.mat-footer-cell:last-of-type{padding-right:24px}[dir=rtl] th.mat-header-cell:last-of-type:not(:only-of-type),[dir=rtl] td.mat-cell:last-of-type:not(:only-of-type),[dir=rtl] td.mat-footer-cell:last-of-type:not(:only-of-type){padding-right:0;padding-left:24px}.mat-table-sticky{position:-webkit-sticky !important;position:sticky !important}.mat-table-fixed-layout{table-layout:fixed}\n"]
|
---|
66 | },] }
|
---|
67 | ];
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @license
|
---|
71 | * Copyright Google LLC All Rights Reserved.
|
---|
72 | *
|
---|
73 | * Use of this source code is governed by an MIT-style license that can be
|
---|
74 | * found in the LICENSE file at https://angular.io/license
|
---|
75 | */
|
---|
76 | /**
|
---|
77 | * Cell definition for the mat-table.
|
---|
78 | * Captures the template of a column's data row cell as well as cell-specific properties.
|
---|
79 | */
|
---|
80 | class MatCellDef extends CdkCellDef {
|
---|
81 | }
|
---|
82 | MatCellDef.decorators = [
|
---|
83 | { type: Directive, args: [{
|
---|
84 | selector: '[matCellDef]',
|
---|
85 | providers: [{ provide: CdkCellDef, useExisting: MatCellDef }]
|
---|
86 | },] }
|
---|
87 | ];
|
---|
88 | /**
|
---|
89 | * Header cell definition for the mat-table.
|
---|
90 | * Captures the template of a column's header cell and as well as cell-specific properties.
|
---|
91 | */
|
---|
92 | class MatHeaderCellDef extends CdkHeaderCellDef {
|
---|
93 | }
|
---|
94 | MatHeaderCellDef.decorators = [
|
---|
95 | { type: Directive, args: [{
|
---|
96 | selector: '[matHeaderCellDef]',
|
---|
97 | providers: [{ provide: CdkHeaderCellDef, useExisting: MatHeaderCellDef }]
|
---|
98 | },] }
|
---|
99 | ];
|
---|
100 | /**
|
---|
101 | * Footer cell definition for the mat-table.
|
---|
102 | * Captures the template of a column's footer cell and as well as cell-specific properties.
|
---|
103 | */
|
---|
104 | class MatFooterCellDef extends CdkFooterCellDef {
|
---|
105 | }
|
---|
106 | MatFooterCellDef.decorators = [
|
---|
107 | { type: Directive, args: [{
|
---|
108 | selector: '[matFooterCellDef]',
|
---|
109 | providers: [{ provide: CdkFooterCellDef, useExisting: MatFooterCellDef }]
|
---|
110 | },] }
|
---|
111 | ];
|
---|
112 | /**
|
---|
113 | * Column definition for the mat-table.
|
---|
114 | * Defines a set of cells available for a table column.
|
---|
115 | */
|
---|
116 | class MatColumnDef extends CdkColumnDef {
|
---|
117 | /** Unique name for this column. */
|
---|
118 | get name() { return this._name; }
|
---|
119 | set name(name) { this._setNameInput(name); }
|
---|
120 | /**
|
---|
121 | * Add "mat-column-" prefix in addition to "cdk-column-" prefix.
|
---|
122 | * In the future, this will only add "mat-column-" and columnCssClassName
|
---|
123 | * will change from type string[] to string.
|
---|
124 | * @docs-private
|
---|
125 | */
|
---|
126 | _updateColumnCssClassName() {
|
---|
127 | super._updateColumnCssClassName();
|
---|
128 | this._columnCssClassName.push(`mat-column-${this.cssClassFriendlyName}`);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | MatColumnDef.decorators = [
|
---|
132 | { type: Directive, args: [{
|
---|
133 | selector: '[matColumnDef]',
|
---|
134 | inputs: ['sticky'],
|
---|
135 | providers: [
|
---|
136 | { provide: CdkColumnDef, useExisting: MatColumnDef },
|
---|
137 | { provide: 'MAT_SORT_HEADER_COLUMN_DEF', useExisting: MatColumnDef }
|
---|
138 | ],
|
---|
139 | },] }
|
---|
140 | ];
|
---|
141 | MatColumnDef.propDecorators = {
|
---|
142 | name: [{ type: Input, args: ['matColumnDef',] }]
|
---|
143 | };
|
---|
144 | /** Header cell template container that adds the right classes and role. */
|
---|
145 | class MatHeaderCell extends CdkHeaderCell {
|
---|
146 | }
|
---|
147 | MatHeaderCell.decorators = [
|
---|
148 | { type: Directive, args: [{
|
---|
149 | selector: 'mat-header-cell, th[mat-header-cell]',
|
---|
150 | host: {
|
---|
151 | 'class': 'mat-header-cell',
|
---|
152 | 'role': 'columnheader',
|
---|
153 | },
|
---|
154 | },] }
|
---|
155 | ];
|
---|
156 | /** Footer cell template container that adds the right classes and role. */
|
---|
157 | class MatFooterCell extends CdkFooterCell {
|
---|
158 | }
|
---|
159 | MatFooterCell.decorators = [
|
---|
160 | { type: Directive, args: [{
|
---|
161 | selector: 'mat-footer-cell, td[mat-footer-cell]',
|
---|
162 | host: {
|
---|
163 | 'class': 'mat-footer-cell',
|
---|
164 | 'role': 'gridcell',
|
---|
165 | },
|
---|
166 | },] }
|
---|
167 | ];
|
---|
168 | /** Cell template container that adds the right classes and role. */
|
---|
169 | class MatCell extends CdkCell {
|
---|
170 | }
|
---|
171 | MatCell.decorators = [
|
---|
172 | { type: Directive, args: [{
|
---|
173 | selector: 'mat-cell, td[mat-cell]',
|
---|
174 | host: {
|
---|
175 | 'class': 'mat-cell',
|
---|
176 | 'role': 'gridcell',
|
---|
177 | },
|
---|
178 | },] }
|
---|
179 | ];
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * @license
|
---|
183 | * Copyright Google LLC All Rights Reserved.
|
---|
184 | *
|
---|
185 | * Use of this source code is governed by an MIT-style license that can be
|
---|
186 | * found in the LICENSE file at https://angular.io/license
|
---|
187 | */
|
---|
188 | /**
|
---|
189 | * Header row definition for the mat-table.
|
---|
190 | * Captures the header row's template and other header properties such as the columns to display.
|
---|
191 | */
|
---|
192 | class MatHeaderRowDef extends CdkHeaderRowDef {
|
---|
193 | }
|
---|
194 | MatHeaderRowDef.decorators = [
|
---|
195 | { type: Directive, args: [{
|
---|
196 | selector: '[matHeaderRowDef]',
|
---|
197 | providers: [{ provide: CdkHeaderRowDef, useExisting: MatHeaderRowDef }],
|
---|
198 | inputs: ['columns: matHeaderRowDef', 'sticky: matHeaderRowDefSticky'],
|
---|
199 | },] }
|
---|
200 | ];
|
---|
201 | /**
|
---|
202 | * Footer row definition for the mat-table.
|
---|
203 | * Captures the footer row's template and other footer properties such as the columns to display.
|
---|
204 | */
|
---|
205 | class MatFooterRowDef extends CdkFooterRowDef {
|
---|
206 | }
|
---|
207 | MatFooterRowDef.decorators = [
|
---|
208 | { type: Directive, args: [{
|
---|
209 | selector: '[matFooterRowDef]',
|
---|
210 | providers: [{ provide: CdkFooterRowDef, useExisting: MatFooterRowDef }],
|
---|
211 | inputs: ['columns: matFooterRowDef', 'sticky: matFooterRowDefSticky'],
|
---|
212 | },] }
|
---|
213 | ];
|
---|
214 | /**
|
---|
215 | * Data row definition for the mat-table.
|
---|
216 | * Captures the data row's template and other properties such as the columns to display and
|
---|
217 | * a when predicate that describes when this row should be used.
|
---|
218 | */
|
---|
219 | class MatRowDef extends CdkRowDef {
|
---|
220 | }
|
---|
221 | MatRowDef.decorators = [
|
---|
222 | { type: Directive, args: [{
|
---|
223 | selector: '[matRowDef]',
|
---|
224 | providers: [{ provide: CdkRowDef, useExisting: MatRowDef }],
|
---|
225 | inputs: ['columns: matRowDefColumns', 'when: matRowDefWhen'],
|
---|
226 | },] }
|
---|
227 | ];
|
---|
228 | /** Header template container that contains the cell outlet. Adds the right class and role. */
|
---|
229 | class MatHeaderRow extends CdkHeaderRow {
|
---|
230 | }
|
---|
231 | MatHeaderRow.decorators = [
|
---|
232 | { type: Component, args: [{
|
---|
233 | selector: 'mat-header-row, tr[mat-header-row]',
|
---|
234 | template: CDK_ROW_TEMPLATE,
|
---|
235 | host: {
|
---|
236 | 'class': 'mat-header-row',
|
---|
237 | 'role': 'row',
|
---|
238 | },
|
---|
239 | // See note on CdkTable for explanation on why this uses the default change detection strategy.
|
---|
240 | // tslint:disable-next-line:validate-decorators
|
---|
241 | changeDetection: ChangeDetectionStrategy.Default,
|
---|
242 | encapsulation: ViewEncapsulation.None,
|
---|
243 | exportAs: 'matHeaderRow',
|
---|
244 | providers: [{ provide: CdkHeaderRow, useExisting: MatHeaderRow }]
|
---|
245 | },] }
|
---|
246 | ];
|
---|
247 | /** Footer template container that contains the cell outlet. Adds the right class and role. */
|
---|
248 | class MatFooterRow extends CdkFooterRow {
|
---|
249 | }
|
---|
250 | MatFooterRow.decorators = [
|
---|
251 | { type: Component, args: [{
|
---|
252 | selector: 'mat-footer-row, tr[mat-footer-row]',
|
---|
253 | template: CDK_ROW_TEMPLATE,
|
---|
254 | host: {
|
---|
255 | 'class': 'mat-footer-row',
|
---|
256 | 'role': 'row',
|
---|
257 | },
|
---|
258 | // See note on CdkTable for explanation on why this uses the default change detection strategy.
|
---|
259 | // tslint:disable-next-line:validate-decorators
|
---|
260 | changeDetection: ChangeDetectionStrategy.Default,
|
---|
261 | encapsulation: ViewEncapsulation.None,
|
---|
262 | exportAs: 'matFooterRow',
|
---|
263 | providers: [{ provide: CdkFooterRow, useExisting: MatFooterRow }]
|
---|
264 | },] }
|
---|
265 | ];
|
---|
266 | /** Data row template container that contains the cell outlet. Adds the right class and role. */
|
---|
267 | class MatRow extends CdkRow {
|
---|
268 | }
|
---|
269 | MatRow.decorators = [
|
---|
270 | { type: Component, args: [{
|
---|
271 | selector: 'mat-row, tr[mat-row]',
|
---|
272 | template: CDK_ROW_TEMPLATE,
|
---|
273 | host: {
|
---|
274 | 'class': 'mat-row',
|
---|
275 | 'role': 'row',
|
---|
276 | },
|
---|
277 | // See note on CdkTable for explanation on why this uses the default change detection strategy.
|
---|
278 | // tslint:disable-next-line:validate-decorators
|
---|
279 | changeDetection: ChangeDetectionStrategy.Default,
|
---|
280 | encapsulation: ViewEncapsulation.None,
|
---|
281 | exportAs: 'matRow',
|
---|
282 | providers: [{ provide: CdkRow, useExisting: MatRow }]
|
---|
283 | },] }
|
---|
284 | ];
|
---|
285 | /** Row that can be used to display a message when no data is shown in the table. */
|
---|
286 | class MatNoDataRow extends CdkNoDataRow {
|
---|
287 | }
|
---|
288 | MatNoDataRow.decorators = [
|
---|
289 | { type: Directive, args: [{
|
---|
290 | selector: 'ng-template[matNoDataRow]',
|
---|
291 | providers: [{ provide: CdkNoDataRow, useExisting: MatNoDataRow }],
|
---|
292 | },] }
|
---|
293 | ];
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * @license
|
---|
297 | * Copyright Google LLC All Rights Reserved.
|
---|
298 | *
|
---|
299 | * Use of this source code is governed by an MIT-style license that can be
|
---|
300 | * found in the LICENSE file at https://angular.io/license
|
---|
301 | */
|
---|
302 | /**
|
---|
303 | * Column that simply shows text content for the header and row cells. Assumes that the table
|
---|
304 | * is using the native table implementation (`<table>`).
|
---|
305 | *
|
---|
306 | * By default, the name of this column will be the header text and data property accessor.
|
---|
307 | * The header text can be overridden with the `headerText` input. Cell values can be overridden with
|
---|
308 | * the `dataAccessor` input. Change the text justification to the start or end using the `justify`
|
---|
309 | * input.
|
---|
310 | */
|
---|
311 | class MatTextColumn extends CdkTextColumn {
|
---|
312 | }
|
---|
313 | MatTextColumn.decorators = [
|
---|
314 | { type: Component, args: [{
|
---|
315 | selector: 'mat-text-column',
|
---|
316 | template: `
|
---|
317 | <ng-container matColumnDef>
|
---|
318 | <th mat-header-cell *matHeaderCellDef [style.text-align]="justify">
|
---|
319 | {{headerText}}
|
---|
320 | </th>
|
---|
321 | <td mat-cell *matCellDef="let data" [style.text-align]="justify">
|
---|
322 | {{dataAccessor(data, name)}}
|
---|
323 | </td>
|
---|
324 | </ng-container>
|
---|
325 | `,
|
---|
326 | encapsulation: ViewEncapsulation.None,
|
---|
327 | // Change detection is intentionally not set to OnPush. This component's template will be provided
|
---|
328 | // to the table to be inserted into its view. This is problematic when change detection runs since
|
---|
329 | // the bindings in this template will be evaluated _after_ the table's view is evaluated, which
|
---|
330 | // mean's the template in the table's view will not have the updated value (and in fact will cause
|
---|
331 | // an ExpressionChangedAfterItHasBeenCheckedError).
|
---|
332 | // tslint:disable-next-line:validate-decorators
|
---|
333 | changeDetection: ChangeDetectionStrategy.Default
|
---|
334 | },] }
|
---|
335 | ];
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * @license
|
---|
339 | * Copyright Google LLC All Rights Reserved.
|
---|
340 | *
|
---|
341 | * Use of this source code is governed by an MIT-style license that can be
|
---|
342 | * found in the LICENSE file at https://angular.io/license
|
---|
343 | */
|
---|
344 | const EXPORTED_DECLARATIONS = [
|
---|
345 | // Table
|
---|
346 | MatTable,
|
---|
347 | MatRecycleRows,
|
---|
348 | // Template defs
|
---|
349 | MatHeaderCellDef,
|
---|
350 | MatHeaderRowDef,
|
---|
351 | MatColumnDef,
|
---|
352 | MatCellDef,
|
---|
353 | MatRowDef,
|
---|
354 | MatFooterCellDef,
|
---|
355 | MatFooterRowDef,
|
---|
356 | // Cell directives
|
---|
357 | MatHeaderCell,
|
---|
358 | MatCell,
|
---|
359 | MatFooterCell,
|
---|
360 | // Row directives
|
---|
361 | MatHeaderRow,
|
---|
362 | MatRow,
|
---|
363 | MatFooterRow,
|
---|
364 | MatNoDataRow,
|
---|
365 | MatTextColumn,
|
---|
366 | ];
|
---|
367 | class MatTableModule {
|
---|
368 | }
|
---|
369 | MatTableModule.decorators = [
|
---|
370 | { type: NgModule, args: [{
|
---|
371 | imports: [
|
---|
372 | CdkTableModule,
|
---|
373 | MatCommonModule,
|
---|
374 | ],
|
---|
375 | exports: [MatCommonModule, EXPORTED_DECLARATIONS],
|
---|
376 | declarations: EXPORTED_DECLARATIONS,
|
---|
377 | },] }
|
---|
378 | ];
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * @license
|
---|
382 | * Copyright Google LLC All Rights Reserved.
|
---|
383 | *
|
---|
384 | * Use of this source code is governed by an MIT-style license that can be
|
---|
385 | * found in the LICENSE file at https://angular.io/license
|
---|
386 | */
|
---|
387 | /**
|
---|
388 | * Corresponds to `Number.MAX_SAFE_INTEGER`. Moved out into a variable here due to
|
---|
389 | * flaky browser support and the value not being defined in Closure's typings.
|
---|
390 | */
|
---|
391 | const MAX_SAFE_INTEGER = 9007199254740991;
|
---|
392 | /** Shared base class with MDC-based implementation. */
|
---|
393 | class _MatTableDataSource extends DataSource {
|
---|
394 | constructor(initialData = []) {
|
---|
395 | super();
|
---|
396 | /** Stream emitting render data to the table (depends on ordered data changes). */
|
---|
397 | this._renderData = new BehaviorSubject([]);
|
---|
398 | /** Stream that emits when a new filter string is set on the data source. */
|
---|
399 | this._filter = new BehaviorSubject('');
|
---|
400 | /** Used to react to internal changes of the paginator that are made by the data source itself. */
|
---|
401 | this._internalPageChanges = new Subject();
|
---|
402 | /**
|
---|
403 | * Subscription to the changes that should trigger an update to the table's rendered rows, such
|
---|
404 | * as filtering, sorting, pagination, or base data changes.
|
---|
405 | */
|
---|
406 | this._renderChangesSubscription = null;
|
---|
407 | /**
|
---|
408 | * Data accessor function that is used for accessing data properties for sorting through
|
---|
409 | * the default sortData function.
|
---|
410 | * This default function assumes that the sort header IDs (which defaults to the column name)
|
---|
411 | * matches the data's properties (e.g. column Xyz represents data['Xyz']).
|
---|
412 | * May be set to a custom function for different behavior.
|
---|
413 | * @param data Data object that is being accessed.
|
---|
414 | * @param sortHeaderId The name of the column that represents the data.
|
---|
415 | */
|
---|
416 | this.sortingDataAccessor = (data, sortHeaderId) => {
|
---|
417 | const value = data[sortHeaderId];
|
---|
418 | if (_isNumberValue(value)) {
|
---|
419 | const numberValue = Number(value);
|
---|
420 | // Numbers beyond `MAX_SAFE_INTEGER` can't be compared reliably so we
|
---|
421 | // leave them as strings. For more info: https://goo.gl/y5vbSg
|
---|
422 | return numberValue < MAX_SAFE_INTEGER ? numberValue : value;
|
---|
423 | }
|
---|
424 | return value;
|
---|
425 | };
|
---|
426 | /**
|
---|
427 | * Gets a sorted copy of the data array based on the state of the MatSort. Called
|
---|
428 | * after changes are made to the filtered data or when sort changes are emitted from MatSort.
|
---|
429 | * By default, the function retrieves the active sort and its direction and compares data
|
---|
430 | * by retrieving data using the sortingDataAccessor. May be overridden for a custom implementation
|
---|
431 | * of data ordering.
|
---|
432 | * @param data The array of data that should be sorted.
|
---|
433 | * @param sort The connected MatSort that holds the current sort state.
|
---|
434 | */
|
---|
435 | this.sortData = (data, sort) => {
|
---|
436 | const active = sort.active;
|
---|
437 | const direction = sort.direction;
|
---|
438 | if (!active || direction == '') {
|
---|
439 | return data;
|
---|
440 | }
|
---|
441 | return data.sort((a, b) => {
|
---|
442 | let valueA = this.sortingDataAccessor(a, active);
|
---|
443 | let valueB = this.sortingDataAccessor(b, active);
|
---|
444 | // If there are data in the column that can be converted to a number,
|
---|
445 | // it must be ensured that the rest of the data
|
---|
446 | // is of the same type so as not to order incorrectly.
|
---|
447 | const valueAType = typeof valueA;
|
---|
448 | const valueBType = typeof valueB;
|
---|
449 | if (valueAType !== valueBType) {
|
---|
450 | if (valueAType === 'number') {
|
---|
451 | valueA += '';
|
---|
452 | }
|
---|
453 | if (valueBType === 'number') {
|
---|
454 | valueB += '';
|
---|
455 | }
|
---|
456 | }
|
---|
457 | // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if
|
---|
458 | // one value exists while the other doesn't. In this case, existing value should come last.
|
---|
459 | // This avoids inconsistent results when comparing values to undefined/null.
|
---|
460 | // If neither value exists, return 0 (equal).
|
---|
461 | let comparatorResult = 0;
|
---|
462 | if (valueA != null && valueB != null) {
|
---|
463 | // Check if one value is greater than the other; if equal, comparatorResult should remain 0.
|
---|
464 | if (valueA > valueB) {
|
---|
465 | comparatorResult = 1;
|
---|
466 | }
|
---|
467 | else if (valueA < valueB) {
|
---|
468 | comparatorResult = -1;
|
---|
469 | }
|
---|
470 | }
|
---|
471 | else if (valueA != null) {
|
---|
472 | comparatorResult = 1;
|
---|
473 | }
|
---|
474 | else if (valueB != null) {
|
---|
475 | comparatorResult = -1;
|
---|
476 | }
|
---|
477 | return comparatorResult * (direction == 'asc' ? 1 : -1);
|
---|
478 | });
|
---|
479 | };
|
---|
480 | /**
|
---|
481 | * Checks if a data object matches the data source's filter string. By default, each data object
|
---|
482 | * is converted to a string of its properties and returns true if the filter has
|
---|
483 | * at least one occurrence in that string. By default, the filter string has its whitespace
|
---|
484 | * trimmed and the match is case-insensitive. May be overridden for a custom implementation of
|
---|
485 | * filter matching.
|
---|
486 | * @param data Data object used to check against the filter.
|
---|
487 | * @param filter Filter string that has been set on the data source.
|
---|
488 | * @returns Whether the filter matches against the data
|
---|
489 | */
|
---|
490 | this.filterPredicate = (data, filter) => {
|
---|
491 | // Transform the data into a lowercase string of all property values.
|
---|
492 | const dataStr = Object.keys(data).reduce((currentTerm, key) => {
|
---|
493 | // Use an obscure Unicode character to delimit the words in the concatenated string.
|
---|
494 | // This avoids matches where the values of two columns combined will match the user's query
|
---|
495 | // (e.g. `Flute` and `Stop` will match `Test`). The character is intended to be something
|
---|
496 | // that has a very low chance of being typed in by somebody in a text field. This one in
|
---|
497 | // particular is "White up-pointing triangle with dot" from
|
---|
498 | // https://en.wikipedia.org/wiki/List_of_Unicode_characters
|
---|
499 | return currentTerm + data[key] + '◬';
|
---|
500 | }, '').toLowerCase();
|
---|
501 | // Transform the filter by converting it to lowercase and removing whitespace.
|
---|
502 | const transformedFilter = filter.trim().toLowerCase();
|
---|
503 | return dataStr.indexOf(transformedFilter) != -1;
|
---|
504 | };
|
---|
505 | this._data = new BehaviorSubject(initialData);
|
---|
506 | this._updateChangeSubscription();
|
---|
507 | }
|
---|
508 | /** Array of data that should be rendered by the table, where each object represents one row. */
|
---|
509 | get data() { return this._data.value; }
|
---|
510 | set data(data) {
|
---|
511 | this._data.next(data);
|
---|
512 | // Normally the `filteredData` is updated by the re-render
|
---|
513 | // subscription, but that won't happen if it's inactive.
|
---|
514 | if (!this._renderChangesSubscription) {
|
---|
515 | this._filterData(data);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | /**
|
---|
519 | * Filter term that should be used to filter out objects from the data array. To override how
|
---|
520 | * data objects match to this filter string, provide a custom function for filterPredicate.
|
---|
521 | */
|
---|
522 | get filter() { return this._filter.value; }
|
---|
523 | set filter(filter) {
|
---|
524 | this._filter.next(filter);
|
---|
525 | // Normally the `filteredData` is updated by the re-render
|
---|
526 | // subscription, but that won't happen if it's inactive.
|
---|
527 | if (!this._renderChangesSubscription) {
|
---|
528 | this._filterData(this.data);
|
---|
529 | }
|
---|
530 | }
|
---|
531 | /**
|
---|
532 | * Instance of the MatSort directive used by the table to control its sorting. Sort changes
|
---|
533 | * emitted by the MatSort will trigger an update to the table's rendered data.
|
---|
534 | */
|
---|
535 | get sort() { return this._sort; }
|
---|
536 | set sort(sort) {
|
---|
537 | this._sort = sort;
|
---|
538 | this._updateChangeSubscription();
|
---|
539 | }
|
---|
540 | /**
|
---|
541 | * Instance of the MatPaginator component used by the table to control what page of the data is
|
---|
542 | * displayed. Page changes emitted by the MatPaginator will trigger an update to the
|
---|
543 | * table's rendered data.
|
---|
544 | *
|
---|
545 | * Note that the data source uses the paginator's properties to calculate which page of data
|
---|
546 | * should be displayed. If the paginator receives its properties as template inputs,
|
---|
547 | * e.g. `[pageLength]=100` or `[pageIndex]=1`, then be sure that the paginator's view has been
|
---|
548 | * initialized before assigning it to this data source.
|
---|
549 | */
|
---|
550 | get paginator() { return this._paginator; }
|
---|
551 | set paginator(paginator) {
|
---|
552 | this._paginator = paginator;
|
---|
553 | this._updateChangeSubscription();
|
---|
554 | }
|
---|
555 | /**
|
---|
556 | * Subscribe to changes that should trigger an update to the table's rendered rows. When the
|
---|
557 | * changes occur, process the current state of the filter, sort, and pagination along with
|
---|
558 | * the provided base data and send it to the table for rendering.
|
---|
559 | */
|
---|
560 | _updateChangeSubscription() {
|
---|
561 | var _a;
|
---|
562 | // Sorting and/or pagination should be watched if MatSort and/or MatPaginator are provided.
|
---|
563 | // The events should emit whenever the component emits a change or initializes, or if no
|
---|
564 | // component is provided, a stream with just a null event should be provided.
|
---|
565 | // The `sortChange` and `pageChange` acts as a signal to the combineLatests below so that the
|
---|
566 | // pipeline can progress to the next step. Note that the value from these streams are not used,
|
---|
567 | // they purely act as a signal to progress in the pipeline.
|
---|
568 | const sortChange = this._sort ?
|
---|
569 | merge(this._sort.sortChange, this._sort.initialized) :
|
---|
570 | of(null);
|
---|
571 | const pageChange = this._paginator ?
|
---|
572 | merge(this._paginator.page, this._internalPageChanges, this._paginator.initialized) :
|
---|
573 | of(null);
|
---|
574 | const dataStream = this._data;
|
---|
575 | // Watch for base data or filter changes to provide a filtered set of data.
|
---|
576 | const filteredData = combineLatest([dataStream, this._filter])
|
---|
577 | .pipe(map(([data]) => this._filterData(data)));
|
---|
578 | // Watch for filtered data or sort changes to provide an ordered set of data.
|
---|
579 | const orderedData = combineLatest([filteredData, sortChange])
|
---|
580 | .pipe(map(([data]) => this._orderData(data)));
|
---|
581 | // Watch for ordered data or page changes to provide a paged set of data.
|
---|
582 | const paginatedData = combineLatest([orderedData, pageChange])
|
---|
583 | .pipe(map(([data]) => this._pageData(data)));
|
---|
584 | // Watched for paged data changes and send the result to the table to render.
|
---|
585 | (_a = this._renderChangesSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
---|
586 | this._renderChangesSubscription = paginatedData.subscribe(data => this._renderData.next(data));
|
---|
587 | }
|
---|
588 | /**
|
---|
589 | * Returns a filtered data array where each filter object contains the filter string within
|
---|
590 | * the result of the filterTermAccessor function. If no filter is set, returns the data array
|
---|
591 | * as provided.
|
---|
592 | */
|
---|
593 | _filterData(data) {
|
---|
594 | // If there is a filter string, filter out data that does not contain it.
|
---|
595 | // Each data object is converted to a string using the function defined by filterTermAccessor.
|
---|
596 | // May be overridden for customization.
|
---|
597 | this.filteredData = (this.filter == null || this.filter === '') ? data :
|
---|
598 | data.filter(obj => this.filterPredicate(obj, this.filter));
|
---|
599 | if (this.paginator) {
|
---|
600 | this._updatePaginator(this.filteredData.length);
|
---|
601 | }
|
---|
602 | return this.filteredData;
|
---|
603 | }
|
---|
604 | /**
|
---|
605 | * Returns a sorted copy of the data if MatSort has a sort applied, otherwise just returns the
|
---|
606 | * data array as provided. Uses the default data accessor for data lookup, unless a
|
---|
607 | * sortDataAccessor function is defined.
|
---|
608 | */
|
---|
609 | _orderData(data) {
|
---|
610 | // If there is no active sort or direction, return the data without trying to sort.
|
---|
611 | if (!this.sort) {
|
---|
612 | return data;
|
---|
613 | }
|
---|
614 | return this.sortData(data.slice(), this.sort);
|
---|
615 | }
|
---|
616 | /**
|
---|
617 | * Returns a paged slice of the provided data array according to the provided MatPaginator's page
|
---|
618 | * index and length. If there is no paginator provided, returns the data array as provided.
|
---|
619 | */
|
---|
620 | _pageData(data) {
|
---|
621 | if (!this.paginator) {
|
---|
622 | return data;
|
---|
623 | }
|
---|
624 | const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
---|
625 | return data.slice(startIndex, startIndex + this.paginator.pageSize);
|
---|
626 | }
|
---|
627 | /**
|
---|
628 | * Updates the paginator to reflect the length of the filtered data, and makes sure that the page
|
---|
629 | * index does not exceed the paginator's last page. Values are changed in a resolved promise to
|
---|
630 | * guard against making property changes within a round of change detection.
|
---|
631 | */
|
---|
632 | _updatePaginator(filteredDataLength) {
|
---|
633 | Promise.resolve().then(() => {
|
---|
634 | const paginator = this.paginator;
|
---|
635 | if (!paginator) {
|
---|
636 | return;
|
---|
637 | }
|
---|
638 | paginator.length = filteredDataLength;
|
---|
639 | // If the page index is set beyond the page, reduce it to the last page.
|
---|
640 | if (paginator.pageIndex > 0) {
|
---|
641 | const lastPageIndex = Math.ceil(paginator.length / paginator.pageSize) - 1 || 0;
|
---|
642 | const newPageIndex = Math.min(paginator.pageIndex, lastPageIndex);
|
---|
643 | if (newPageIndex !== paginator.pageIndex) {
|
---|
644 | paginator.pageIndex = newPageIndex;
|
---|
645 | // Since the paginator only emits after user-generated changes,
|
---|
646 | // we need our own stream so we know to should re-render the data.
|
---|
647 | this._internalPageChanges.next();
|
---|
648 | }
|
---|
649 | }
|
---|
650 | });
|
---|
651 | }
|
---|
652 | /**
|
---|
653 | * Used by the MatTable. Called when it connects to the data source.
|
---|
654 | * @docs-private
|
---|
655 | */
|
---|
656 | connect() {
|
---|
657 | if (!this._renderChangesSubscription) {
|
---|
658 | this._updateChangeSubscription();
|
---|
659 | }
|
---|
660 | return this._renderData;
|
---|
661 | }
|
---|
662 | /**
|
---|
663 | * Used by the MatTable. Called when it disconnects from the data source.
|
---|
664 | * @docs-private
|
---|
665 | */
|
---|
666 | disconnect() {
|
---|
667 | var _a;
|
---|
668 | (_a = this._renderChangesSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
---|
669 | this._renderChangesSubscription = null;
|
---|
670 | }
|
---|
671 | }
|
---|
672 | /**
|
---|
673 | * Data source that accepts a client-side data array and includes native support of filtering,
|
---|
674 | * sorting (using MatSort), and pagination (using MatPaginator).
|
---|
675 | *
|
---|
676 | * Allows for sort customization by overriding sortingDataAccessor, which defines how data
|
---|
677 | * properties are accessed. Also allows for filter customization by overriding filterTermAccessor,
|
---|
678 | * which defines how row data is converted to a string for filter matching.
|
---|
679 | *
|
---|
680 | * **Note:** This class is meant to be a simple data source to help you get started. As such
|
---|
681 | * it isn't equipped to handle some more advanced cases like robust i18n support or server-side
|
---|
682 | * interactions. If your app needs to support more advanced use cases, consider implementing your
|
---|
683 | * own `DataSource`.
|
---|
684 | */
|
---|
685 | class MatTableDataSource extends _MatTableDataSource {
|
---|
686 | }
|
---|
687 |
|
---|
688 | /**
|
---|
689 | * @license
|
---|
690 | * Copyright Google LLC All Rights Reserved.
|
---|
691 | *
|
---|
692 | * Use of this source code is governed by an MIT-style license that can be
|
---|
693 | * found in the LICENSE file at https://angular.io/license
|
---|
694 | */
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Generated bundle index. Do not edit.
|
---|
698 | */
|
---|
699 |
|
---|
700 | export { MatCell, MatCellDef, MatColumnDef, MatFooterCell, MatFooterCellDef, MatFooterRow, MatFooterRowDef, MatHeaderCell, MatHeaderCellDef, MatHeaderRow, MatHeaderRowDef, MatNoDataRow, MatRecycleRows, MatRow, MatRowDef, MatTable, MatTableDataSource, MatTableModule, MatTextColumn, _MatTableDataSource };
|
---|
701 | //# sourceMappingURL=table.js.map
|
---|