Skip to content

Commit 685e636

Browse files
committed
move logic from baserow to table
1 parent 15e1641 commit 685e636

File tree

4 files changed

+65
-66
lines changed

4 files changed

+65
-66
lines changed

src/cdk/table/row.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {
1010
ChangeDetectionStrategy,
1111
Component,
12-
Directive,
12+
Directive, IterableChanges,
1313
IterableDiffer,
1414
IterableDiffers,
1515
SimpleChanges,
@@ -33,35 +33,26 @@ export abstract class BaseRowDef {
3333
/** The columns to be displayed on this row. */
3434
columns: string[];
3535

36-
/** Event stream that emits when changes are made to the columns. */
37-
columnsChange: Subject<void> = new Subject<void>();
38-
3936
/** Differ used to check if any changes were made to the columns. */
40-
protected _columnsDiffer: IterableDiffer<any>;
41-
42-
private viewInitialized = false;
37+
columnsDiffer: IterableDiffer<any>;
4338

4439
constructor(public template: TemplateRef<any>,
4540
protected _differs: IterableDiffers) { }
4641

47-
ngAfterViewInit() {
48-
this.viewInitialized = true;
49-
}
50-
5142
ngOnChanges(changes: SimpleChanges): void {
5243
// Create a new columns differ if one does not yet exist. Initialize it based on initial value
5344
// of the columns property.
54-
if (!this._columnsDiffer && changes['columns'].currentValue) {
55-
this._columnsDiffer = this._differs.find(changes['columns'].currentValue).create();
45+
if (!this.columnsDiffer && changes['columns'].currentValue) {
46+
this.columnsDiffer = this._differs.find(changes['columns'].currentValue).create();
5647
}
5748
}
5849

59-
ngDoCheck(): void {
60-
if (!this.viewInitialized || !this._columnsDiffer || !this.columns) { return; }
61-
62-
// Notify the table if there are any changes to the columns.
63-
const changes = this._columnsDiffer.diff(this.columns);
64-
if (changes) { this.columnsChange.next(); }
50+
/**
51+
* Returns the difference between the current columns and the collumns from the last diff, or null
52+
* if there is no difference.
53+
*/
54+
getColumnsDiff(): IterableChanges<any> | null {
55+
return this.columnsDiffer.diff(this.columns);
6556
}
6657
}
6758

src/cdk/table/table.ts

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
} from '@angular/core';
3232
import {CollectionViewer, DataSource} from './data-source';
3333
import {CdkCellOutlet, CdkCellOutletRowContext, CdkHeaderRowDef, CdkRowDef} from './row';
34-
import {merge} from 'rxjs/observable/merge';
3534
import {takeUntil} from 'rxjs/operator/takeUntil';
3635
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
3736
import {Subscription} from 'rxjs/Subscription';
@@ -174,6 +173,27 @@ export class CdkTable<T> implements CollectionViewer {
174173
}
175174
}
176175

176+
ngOnInit() {
177+
// TODO(andrewseguin): Setup a listener for scrolling, emit the calculated view to viewChange
178+
this._dataDiffer = this._differs.find([]).create(this._trackByFn);
179+
}
180+
181+
ngAfterContentInit() {
182+
this._updateColumnDefinitions();
183+
this._columnDefinitions.changes.subscribe(() => this._updateColumnDefinitions());
184+
}
185+
186+
ngAfterContentChecked() {
187+
this._checkColumnsChange();
188+
}
189+
190+
ngAfterViewInit() {
191+
this._isViewInitialized = true;
192+
if (this.dataSource && !this._renderChangeSubscription) {
193+
this._observeRenderChanges();
194+
}
195+
}
196+
177197
ngOnDestroy() {
178198
this._onDestroy.next();
179199
this._onDestroy.complete();
@@ -182,52 +202,34 @@ export class CdkTable<T> implements CollectionViewer {
182202
}
183203
}
184204

185-
ngOnInit() {
186-
// TODO(andrewseguin): Setup a listener for scroll events
187-
// and emit the calculated view to this.viewChange
188-
}
189-
190-
ngAfterContentInit() {
205+
/** Update the map containing the content's column definitions. */
206+
private _updateColumnDefinitions() {
191207
// TODO(andrewseguin): Throw an error if two columns share the same name
192208
this._columnDefinitions.forEach(columnDef => {
193209
this._columnDefinitionsByName.set(columnDef.name, columnDef);
194210
});
211+
}
195212

196-
this._columnDefinitions.changes.subscribe(() => {
197-
console.log('Column def change');
198-
});
199-
200-
// Re-render the rows if any of their columns change.
201-
// TODO(andrewseguin): Determine how to only re-render the rows that have their columns changed.
202-
const columnChangeEvents = this._rowDefinitions.map(rowDef => rowDef.columnsChange);
213+
/**
214+
* Check if the header or rows have changed what columns they want to display. If there is a diff,
215+
* then re-render that section.
216+
*/
217+
private _checkColumnsChange() {
218+
// Re-render the rows when the row definition columns change.
219+
this._rowDefinitions.forEach(def => {
220+
if (!!def.getColumnsDiff()) {
221+
// Reset the data to an empty array so that renderRowChanges will re-render all new rows.
222+
this._dataDiffer.diff([]);
203223

204-
takeUntil.call(merge(...columnChangeEvents), this._onDestroy).subscribe(() => {
205-
// Reset the data to an empty array so that renderRowChanges will re-render all new rows.
206-
this._rowPlaceholder.viewContainer.clear();
207-
this._dataDiffer.diff([]);
208-
this._renderRowChanges();
224+
this._rowPlaceholder.viewContainer.clear();
225+
this._renderRowChanges();
226+
}
209227
});
210228

211-
// Re-render the header row if the columns change
212-
takeUntil.call(this._headerDefinition.columnsChange, this._onDestroy).subscribe(() => {
213-
console.log('Header columns changed');
229+
// Re-render the header row if there is a difference in its columns.
230+
if (this._headerDefinition.getColumnsDiff()) {
214231
this._headerRowPlaceholder.viewContainer.clear();
215232
this._renderHeaderRow();
216-
});
217-
}
218-
219-
ngAfterViewInit() {
220-
// Find and construct an iterable differ that can be used to find the diff in an array.
221-
this._dataDiffer = this._differs.find([]).create(this._trackByFn);
222-
this._isViewInitialized = true;
223-
}
224-
225-
ngDoCheck() {
226-
if (this._isViewInitialized && this.dataSource && !this._renderChangeSubscription) {
227-
this._renderHeaderRow();
228-
if (this.dataSource && !this._renderChangeSubscription) {
229-
this._observeRenderChanges();
230-
}
231233
}
232234
}
233235

src/demo-app/table/table-demo.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
</div>
2828

2929
<div>
30-
<button md-raised-button (click)="addDynamicColumnDef()">Add Dynamic Column Def</button>
30+
<button md-raised-button (click)="addDynamicColumnDef()" [disabled]="dynamicColumnIds.length >= 4">
31+
Add Dynamic Column Def
32+
</button>
3133
</div>
3234
</div>
3335

3436
<h3>CdkTable With Dynamic Column Def</h3>
3537

3638
<cdk-table [dataSource]="dataSource">
37-
<ng-container cdkColumnDef="column.id" *ngFor="let column of dynamicColumns">
39+
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
3840
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
3941
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
4042
</ng-container>

src/demo-app/table/table-demo.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import {Component, ViewChild} from '@angular/core';
1+
import {
2+
ChangeDetectorRef, Component, ContentChildren, QueryList, ViewChild,
3+
ViewChildren
4+
} from '@angular/core';
25
import {PeopleDatabase, UserData} from './people-database';
36
import {PersonDataSource} from './person-data-source';
47
import {MdPaginator} from '@angular/material';
58
import {MdSort} from '@angular/material';
9+
import {CdkColumnDef} from '@angular/cdk';
610

711
export type UserProperties = 'userId' | 'userName' | 'progress' | 'color' | undefined;
812

913
export type TrackByStrategy = 'id' | 'reference' | 'index';
1014

15+
const properties = ['id', 'name', 'progress', 'color'];
16+
1117
@Component({
1218
moduleId: module.id,
1319
selector: 'table-demo',
@@ -21,7 +27,7 @@ export class TableDemo {
2127
changeReferences = false;
2228
highlights = new Set<string>();
2329

24-
dynamicColumns: any[] = [];
30+
dynamicColumnDefs: any[] = [];
2531
dynamicColumnIds: string[] = [];
2632

2733
@ViewChild(MdPaginator) _paginator: MdPaginator;
@@ -35,15 +41,13 @@ export class TableDemo {
3541
}
3642

3743
addDynamicColumnDef() {
38-
const properties = ['userId', 'userName', 'progress', 'color'];
39-
const nextProperty = properties[this.dynamicColumns.length];
40-
this.dynamicColumns.push({
41-
id: nextProperty,
44+
const nextProperty = properties[this.dynamicColumnDefs.length];
45+
this.dynamicColumnDefs.push({
46+
id: nextProperty.toUpperCase(),
4247
property: nextProperty,
4348
headerText: nextProperty
4449
});
45-
46-
this.dynamicColumnIds.push(nextProperty);
50+
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
4751
}
4852

4953
connect() {

0 commit comments

Comments
 (0)