Skip to content

Commit a41e82c

Browse files
committed
fix(material-expeirmental/table): export missing symbols
1 parent 40b602f commit a41e82c

4 files changed

Lines changed: 387 additions & 0 deletions

File tree

src/material-experimental/mdc-table/module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
MatRowDef,
2929
MatNoDataRow
3030
} from './row';
31+
import {MatTextColumn} from './text-column';
3132

3233
const EXPORTED_DECLARATIONS = [
3334
// Table
@@ -52,6 +53,8 @@ const EXPORTED_DECLARATIONS = [
5253
MatRow,
5354
MatFooterRow,
5455
MatNoDataRow,
56+
57+
MatTextColumn,
5558
];
5659

5760
@NgModule({

src/material-experimental/mdc-table/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export * from './table';
1010
export * from './module';
1111
export * from './cell';
1212
export * from './row';
13+
export * from './table-data-source';
14+
export * from './text-column';
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// NOTE: This file is a direct copy of src/material/table/table-data-source.ts, but it imports
10+
// `MatPaginator` from `@angular/material-experimental/mdc-paginator` rather than
11+
// `@angular/material/paginator`.
12+
13+
import {_isNumberValue} from '@angular/cdk/coercion';
14+
import {DataSource} from '@angular/cdk/table';
15+
import {
16+
BehaviorSubject,
17+
combineLatest,
18+
merge,
19+
Observable,
20+
of as observableOf,
21+
Subscription,
22+
Subject,
23+
} from 'rxjs';
24+
import {MatPaginator, PageEvent} from '@angular/material-experimental/mdc-paginator';
25+
import {MatSort, Sort} from '@angular/material/sort';
26+
import {map} from 'rxjs/operators';
27+
28+
/**
29+
* Corresponds to `Number.MAX_SAFE_INTEGER`. Moved out into a variable here due to
30+
* flaky browser support and the value not being defined in Closure's typings.
31+
*/
32+
const MAX_SAFE_INTEGER = 9007199254740991;
33+
34+
/**
35+
* Data source that accepts a client-side data array and includes native support of filtering,
36+
* sorting (using MatSort), and pagination (using MatPaginator).
37+
*
38+
* Allows for sort customization by overriding sortingDataAccessor, which defines how data
39+
* properties are accessed. Also allows for filter customization by overriding filterTermAccessor,
40+
* which defines how row data is converted to a string for filter matching.
41+
*
42+
* **Note:** This class is meant to be a simple data source to help you get started. As such
43+
* it isn't equipped to handle some more advanced cases like robust i18n support or server-side
44+
* interactions. If your app needs to support more advanced use cases, consider implementing your
45+
* own `DataSource`.
46+
*/
47+
export class MatTableDataSource<T> extends DataSource<T> {
48+
/** Stream that emits when a new data array is set on the data source. */
49+
private readonly _data: BehaviorSubject<T[]>;
50+
51+
/** Stream emitting render data to the table (depends on ordered data changes). */
52+
private readonly _renderData = new BehaviorSubject<T[]>([]);
53+
54+
/** Stream that emits when a new filter string is set on the data source. */
55+
private readonly _filter = new BehaviorSubject<string>('');
56+
57+
/** Used to react to internal changes of the paginator that are made by the data source itself. */
58+
private readonly _internalPageChanges = new Subject<void>();
59+
60+
/**
61+
* Subscription to the changes that should trigger an update to the table's rendered rows, such
62+
* as filtering, sorting, pagination, or base data changes.
63+
*/
64+
_renderChangesSubscription = Subscription.EMPTY;
65+
66+
/**
67+
* The filtered set of data that has been matched by the filter string, or all the data if there
68+
* is no filter. Useful for knowing the set of data the table represents.
69+
* For example, a 'selectAll()' function would likely want to select the set of filtered data
70+
* shown to the user rather than all the data.
71+
*/
72+
filteredData: T[];
73+
74+
/** Array of data that should be rendered by the table, where each object represents one row. */
75+
get data() { return this._data.value; }
76+
set data(data: T[]) { this._data.next(data); }
77+
78+
/**
79+
* Filter term that should be used to filter out objects from the data array. To override how
80+
* data objects match to this filter string, provide a custom function for filterPredicate.
81+
*/
82+
get filter(): string { return this._filter.value; }
83+
set filter(filter: string) { this._filter.next(filter); }
84+
85+
/**
86+
* Instance of the MatSort directive used by the table to control its sorting. Sort changes
87+
* emitted by the MatSort will trigger an update to the table's rendered data.
88+
*/
89+
get sort(): MatSort | null { return this._sort; }
90+
set sort(sort: MatSort|null) {
91+
this._sort = sort;
92+
this._updateChangeSubscription();
93+
}
94+
private _sort: MatSort|null;
95+
96+
/**
97+
* Instance of the MatPaginator component used by the table to control what page of the data is
98+
* displayed. Page changes emitted by the MatPaginator will trigger an update to the
99+
* table's rendered data.
100+
*
101+
* Note that the data source uses the paginator's properties to calculate which page of data
102+
* should be displayed. If the paginator receives its properties as template inputs,
103+
* e.g. `[pageLength]=100` or `[pageIndex]=1`, then be sure that the paginator's view has been
104+
* initialized before assigning it to this data source.
105+
*/
106+
get paginator(): MatPaginator | null { return this._paginator; }
107+
set paginator(paginator: MatPaginator|null) {
108+
this._paginator = paginator;
109+
this._updateChangeSubscription();
110+
}
111+
private _paginator: MatPaginator|null;
112+
113+
/**
114+
* Data accessor function that is used for accessing data properties for sorting through
115+
* the default sortData function.
116+
* This default function assumes that the sort header IDs (which defaults to the column name)
117+
* matches the data's properties (e.g. column Xyz represents data['Xyz']).
118+
* May be set to a custom function for different behavior.
119+
* @param data Data object that is being accessed.
120+
* @param sortHeaderId The name of the column that represents the data.
121+
*/
122+
sortingDataAccessor: ((data: T, sortHeaderId: string) => string|number) =
123+
(data: T, sortHeaderId: string): string|number => {
124+
const value = (data as {[key: string]: any})[sortHeaderId];
125+
126+
if (_isNumberValue(value)) {
127+
const numberValue = Number(value);
128+
129+
// Numbers beyond `MAX_SAFE_INTEGER` can't be compared reliably so we
130+
// leave them as strings. For more info: https://goo.gl/y5vbSg
131+
return numberValue < MAX_SAFE_INTEGER ? numberValue : value;
132+
}
133+
134+
return value;
135+
}
136+
137+
/**
138+
* Gets a sorted copy of the data array based on the state of the MatSort. Called
139+
* after changes are made to the filtered data or when sort changes are emitted from MatSort.
140+
* By default, the function retrieves the active sort and its direction and compares data
141+
* by retrieving data using the sortingDataAccessor. May be overridden for a custom implementation
142+
* of data ordering.
143+
* @param data The array of data that should be sorted.
144+
* @param sort The connected MatSort that holds the current sort state.
145+
*/
146+
sortData: ((data: T[], sort: MatSort) => T[]) = (data: T[], sort: MatSort): T[] => {
147+
const active = sort.active;
148+
const direction = sort.direction;
149+
if (!active || direction == '') { return data; }
150+
151+
return data.sort((a, b) => {
152+
let valueA = this.sortingDataAccessor(a, active);
153+
let valueB = this.sortingDataAccessor(b, active);
154+
155+
// If there are data in the column that can be converted to a number,
156+
// it must be ensured that the rest of the data
157+
// is of the same type so as not to order incorrectly.
158+
const valueAType = typeof valueA;
159+
const valueBType = typeof valueB;
160+
161+
if (valueAType !== valueBType) {
162+
if (valueAType === 'number') { valueA += ''; }
163+
if (valueBType === 'number') { valueB += ''; }
164+
}
165+
166+
// If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if
167+
// one value exists while the other doesn't. In this case, existing value should come last.
168+
// This avoids inconsistent results when comparing values to undefined/null.
169+
// If neither value exists, return 0 (equal).
170+
let comparatorResult = 0;
171+
if (valueA != null && valueB != null) {
172+
// Check if one value is greater than the other; if equal, comparatorResult should remain 0.
173+
if (valueA > valueB) {
174+
comparatorResult = 1;
175+
} else if (valueA < valueB) {
176+
comparatorResult = -1;
177+
}
178+
} else if (valueA != null) {
179+
comparatorResult = 1;
180+
} else if (valueB != null) {
181+
comparatorResult = -1;
182+
}
183+
184+
return comparatorResult * (direction == 'asc' ? 1 : -1);
185+
});
186+
}
187+
188+
/**
189+
* Checks if a data object matches the data source's filter string. By default, each data object
190+
* is converted to a string of its properties and returns true if the filter has
191+
* at least one occurrence in that string. By default, the filter string has its whitespace
192+
* trimmed and the match is case-insensitive. May be overridden for a custom implementation of
193+
* filter matching.
194+
* @param data Data object used to check against the filter.
195+
* @param filter Filter string that has been set on the data source.
196+
* @returns Whether the filter matches against the data
197+
*/
198+
filterPredicate: ((data: T, filter: string) => boolean) = (data: T, filter: string): boolean => {
199+
// Transform the data into a lowercase string of all property values.
200+
const dataStr = Object.keys(data).reduce((currentTerm: string, key: string) => {
201+
// Use an obscure Unicode character to delimit the words in the concatenated string.
202+
// This avoids matches where the values of two columns combined will match the user's query
203+
// (e.g. `Flute` and `Stop` will match `Test`). The character is intended to be something
204+
// that has a very low chance of being typed in by somebody in a text field. This one in
205+
// particular is "White up-pointing triangle with dot" from
206+
// https://en.wikipedia.org/wiki/List_of_Unicode_characters
207+
return currentTerm + (data as {[key: string]: any})[key] + '◬';
208+
}, '').toLowerCase();
209+
210+
// Transform the filter by converting it to lowercase and removing whitespace.
211+
const transformedFilter = filter.trim().toLowerCase();
212+
213+
return dataStr.indexOf(transformedFilter) != -1;
214+
}
215+
216+
constructor(initialData: T[] = []) {
217+
super();
218+
this._data = new BehaviorSubject<T[]>(initialData);
219+
this._updateChangeSubscription();
220+
}
221+
222+
/**
223+
* Subscribe to changes that should trigger an update to the table's rendered rows. When the
224+
* changes occur, process the current state of the filter, sort, and pagination along with
225+
* the provided base data and send it to the table for rendering.
226+
*/
227+
_updateChangeSubscription() {
228+
// Sorting and/or pagination should be watched if MatSort and/or MatPaginator are provided.
229+
// The events should emit whenever the component emits a change or initializes, or if no
230+
// component is provided, a stream with just a null event should be provided.
231+
// The `sortChange` and `pageChange` acts as a signal to the combineLatests below so that the
232+
// pipeline can progress to the next step. Note that the value from these streams are not used,
233+
// they purely act as a signal to progress in the pipeline.
234+
const sortChange: Observable<Sort|null|void> = this._sort ?
235+
merge(this._sort.sortChange, this._sort.initialized) as Observable<Sort|void> :
236+
observableOf(null);
237+
const pageChange: Observable<PageEvent|null|void> = this._paginator ?
238+
merge(
239+
this._paginator.page,
240+
this._internalPageChanges,
241+
this._paginator.initialized
242+
) as Observable<PageEvent|void> :
243+
observableOf(null);
244+
const dataStream = this._data;
245+
// Watch for base data or filter changes to provide a filtered set of data.
246+
const filteredData = combineLatest([dataStream, this._filter])
247+
.pipe(map(([data]) => this._filterData(data)));
248+
// Watch for filtered data or sort changes to provide an ordered set of data.
249+
const orderedData = combineLatest([filteredData, sortChange])
250+
.pipe(map(([data]) => this._orderData(data)));
251+
// Watch for ordered data or page changes to provide a paged set of data.
252+
const paginatedData = combineLatest([orderedData, pageChange])
253+
.pipe(map(([data]) => this._pageData(data)));
254+
// Watched for paged data changes and send the result to the table to render.
255+
this._renderChangesSubscription.unsubscribe();
256+
this._renderChangesSubscription = paginatedData.subscribe(data => this._renderData.next(data));
257+
}
258+
259+
/**
260+
* Returns a filtered data array where each filter object contains the filter string within
261+
* the result of the filterTermAccessor function. If no filter is set, returns the data array
262+
* as provided.
263+
*/
264+
_filterData(data: T[]) {
265+
// If there is a filter string, filter out data that does not contain it.
266+
// Each data object is converted to a string using the function defined by filterTermAccessor.
267+
// May be overridden for customization.
268+
this.filteredData =
269+
!this.filter ? data : data.filter(obj => this.filterPredicate(obj, this.filter));
270+
271+
if (this.paginator) { this._updatePaginator(this.filteredData.length); }
272+
273+
return this.filteredData;
274+
}
275+
276+
/**
277+
* Returns a sorted copy of the data if MatSort has a sort applied, otherwise just returns the
278+
* data array as provided. Uses the default data accessor for data lookup, unless a
279+
* sortDataAccessor function is defined.
280+
*/
281+
_orderData(data: T[]): T[] {
282+
// If there is no active sort or direction, return the data without trying to sort.
283+
if (!this.sort) { return data; }
284+
285+
return this.sortData(data.slice(), this.sort);
286+
}
287+
288+
/**
289+
* Returns a paged slice of the provided data array according to the provided MatPaginator's page
290+
* index and length. If there is no paginator provided, returns the data array as provided.
291+
*/
292+
_pageData(data: T[]): T[] {
293+
if (!this.paginator) { return data; }
294+
295+
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
296+
return data.slice(startIndex, startIndex + this.paginator.pageSize);
297+
}
298+
299+
/**
300+
* Updates the paginator to reflect the length of the filtered data, and makes sure that the page
301+
* index does not exceed the paginator's last page. Values are changed in a resolved promise to
302+
* guard against making property changes within a round of change detection.
303+
*/
304+
_updatePaginator(filteredDataLength: number) {
305+
Promise.resolve().then(() => {
306+
const paginator = this.paginator;
307+
308+
if (!paginator) { return; }
309+
310+
paginator.length = filteredDataLength;
311+
312+
// If the page index is set beyond the page, reduce it to the last page.
313+
if (paginator.pageIndex > 0) {
314+
const lastPageIndex = Math.ceil(paginator.length / paginator.pageSize) - 1 || 0;
315+
const newPageIndex = Math.min(paginator.pageIndex, lastPageIndex);
316+
317+
if (newPageIndex !== paginator.pageIndex) {
318+
paginator.pageIndex = newPageIndex;
319+
320+
// Since the paginator only emits after user-generated changes,
321+
// we need our own stream so we know to should re-render the data.
322+
this._internalPageChanges.next();
323+
}
324+
}
325+
});
326+
}
327+
328+
/**
329+
* Used by the MatTable. Called when it connects to the data source.
330+
* @docs-private
331+
*/
332+
connect() { return this._renderData; }
333+
334+
/**
335+
* Used by the MatTable. Called when it is destroyed. No-op.
336+
* @docs-private
337+
*/
338+
disconnect() { }
339+
}

0 commit comments

Comments
 (0)