Skip to content

Commit 198911f

Browse files
authored
feat(cdk-experimental/table): experimental column resize (#16114)
1 parent 555037a commit 198911f

File tree

58 files changed

+2895
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2895
-1
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191

9292
# Material experimental package
9393
/src/material-experimental/* @jelbourn
94+
/src/material-experimental/column-resize/** @kseamon @andrewseguin
9495
/src/material-experimental/mdc-autocomplete/** @crisbeto
9596
/src/material-experimental/mdc-button/** @andrewseguin
9697
/src/material-experimental/mdc-card/** @mmalerba
@@ -118,6 +119,7 @@
118119

119120
# CDK experimental package
120121
/src/cdk-experimental/* @jelbourn
122+
/src/cdk-experimental/column-resize/** @kseamon @andrewseguin
121123
/src/cdk-experimental/dialog/** @jelbourn @crisbeto
122124
/src/cdk-experimental/popover-edit/** @kseamon @andrewseguin
123125
/src/cdk-experimental/scrolling/** @mmalerba
@@ -140,6 +142,7 @@
140142
/src/dev-app/card/** @jelbourn
141143
/src/dev-app/checkbox/** @jelbourn @devversion
142144
/src/dev-app/chips/** @jelbourn
145+
/src/dev-app/column-resize/** @kseamon @andrewseguin
143146
/src/dev-app/connected-overlay/** @jelbourn @crisbeto
144147
/src/dev-app/dataset/** @andrewseguin
145148
/src/dev-app/datepicker/** @mmalerba
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_module")
4+
5+
ng_module(
6+
name = "column-resize",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/cdk-experimental/column-resize",
12+
deps = [
13+
"//src/cdk-experimental/popover-edit",
14+
"//src/cdk/bidi",
15+
"//src/cdk/coercion",
16+
"//src/cdk/keycodes",
17+
"//src/cdk/overlay",
18+
"//src/cdk/portal",
19+
"//src/cdk/table",
20+
"@npm//@angular/common",
21+
"@npm//@angular/core",
22+
"@npm//rxjs",
23+
],
24+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import {Directive, ElementRef, NgZone} from '@angular/core';
10+
import {Directionality} from '@angular/cdk/bidi';
11+
12+
import {ColumnResize} from '../column-resize';
13+
import {ColumnResizeNotifier, ColumnResizeNotifierSource} from '../column-resize-notifier';
14+
import {HeaderRowEventDispatcher} from '../event-dispatcher';
15+
import {HOST_BINDINGS, FLEX_PROVIDERS} from './constants';
16+
17+
/**
18+
* Explicitly enables column resizing for a flexbox-based cdk-table.
19+
* Individual columns must be annotated specifically.
20+
*/
21+
@Directive({
22+
selector: 'cdk-table[columnResize]',
23+
host: HOST_BINDINGS,
24+
providers: [
25+
...FLEX_PROVIDERS,
26+
{provide: ColumnResize, useExisting: CdkColumnResizeFlex},
27+
],
28+
})
29+
export class CdkColumnResizeFlex extends ColumnResize {
30+
constructor(
31+
readonly columnResizeNotifier: ColumnResizeNotifier,
32+
readonly directionality: Directionality,
33+
protected readonly elementRef: ElementRef,
34+
protected readonly eventDispatcher: HeaderRowEventDispatcher,
35+
protected readonly ngZone: NgZone,
36+
protected readonly notifier: ColumnResizeNotifierSource) {
37+
super();
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import {Directive, ElementRef, NgZone} from '@angular/core';
10+
import {Directionality} from '@angular/cdk/bidi';
11+
12+
import {ColumnResize} from '../column-resize';
13+
import {ColumnResizeNotifier, ColumnResizeNotifierSource} from '../column-resize-notifier';
14+
import {HeaderRowEventDispatcher} from '../event-dispatcher';
15+
import {HOST_BINDINGS, TABLE_PROVIDERS} from './constants';
16+
17+
/**
18+
* Explicitly enables column resizing for a table-based cdk-table.
19+
* Individual columns must be annotated specifically.
20+
*/
21+
@Directive({
22+
selector: 'table[cdk-table][columnResize]',
23+
host: HOST_BINDINGS,
24+
providers: [
25+
...TABLE_PROVIDERS,
26+
{provide: ColumnResize, useExisting: CdkColumnResize},
27+
],
28+
})
29+
export class CdkColumnResize extends ColumnResize {
30+
constructor(
31+
readonly columnResizeNotifier: ColumnResizeNotifier,
32+
readonly directionality: Directionality,
33+
protected readonly elementRef: ElementRef,
34+
protected readonly eventDispatcher: HeaderRowEventDispatcher,
35+
protected readonly ngZone: NgZone,
36+
protected readonly notifier: ColumnResizeNotifierSource) {
37+
super();
38+
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import {Provider} from '@angular/core';
10+
import {ColumnResizeNotifier, ColumnResizeNotifierSource} from '../column-resize-notifier';
11+
import {HeaderRowEventDispatcher} from '../event-dispatcher';
12+
import {
13+
TABLE_LAYOUT_FIXED_RESIZE_STRATEGY_PROVIDER,
14+
FLEX_RESIZE_STRATEGY_PROVIDER,
15+
} from '../resize-strategy';
16+
17+
const PROVIDERS: Provider[] = [
18+
ColumnResizeNotifier,
19+
HeaderRowEventDispatcher,
20+
ColumnResizeNotifierSource,
21+
];
22+
23+
export const TABLE_PROVIDERS: Provider[] = [
24+
...PROVIDERS,
25+
TABLE_LAYOUT_FIXED_RESIZE_STRATEGY_PROVIDER,
26+
];
27+
export const FLEX_PROVIDERS: Provider[] = [...PROVIDERS, FLEX_RESIZE_STRATEGY_PROVIDER];
28+
export const HOST_BINDINGS = {
29+
'[class.cdk-column-resize-rtl]': 'directionality.value === "rtl"',
30+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import {Directive, ElementRef, NgZone} from '@angular/core';
10+
import {Directionality} from '@angular/cdk/bidi';
11+
12+
import {ColumnResize} from '../column-resize';
13+
import {ColumnResizeNotifier, ColumnResizeNotifierSource} from '../column-resize-notifier';
14+
import {HeaderRowEventDispatcher} from '../event-dispatcher';
15+
import {HOST_BINDINGS, FLEX_PROVIDERS} from './constants';
16+
17+
/**
18+
* Implicitly enables column resizing for a flex cdk-table.
19+
* Individual columns will be resizable unless opted out.
20+
*/
21+
@Directive({
22+
selector: 'cdk-table',
23+
host: HOST_BINDINGS,
24+
providers: [
25+
...FLEX_PROVIDERS,
26+
{provide: ColumnResize, useExisting: CdkDefaultEnabledColumnResizeFlex},
27+
],
28+
})
29+
export class CdkDefaultEnabledColumnResizeFlex extends ColumnResize {
30+
constructor(
31+
readonly columnResizeNotifier: ColumnResizeNotifier,
32+
readonly directionality: Directionality,
33+
protected readonly elementRef: ElementRef,
34+
protected readonly eventDispatcher: HeaderRowEventDispatcher,
35+
protected readonly ngZone: NgZone,
36+
protected readonly notifier: ColumnResizeNotifierSource) {
37+
super();
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import {Directive, ElementRef, NgZone} from '@angular/core';
10+
import {Directionality} from '@angular/cdk/bidi';
11+
12+
import {ColumnResize} from '../column-resize';
13+
import {ColumnResizeNotifier, ColumnResizeNotifierSource} from '../column-resize-notifier';
14+
import {HeaderRowEventDispatcher} from '../event-dispatcher';
15+
import {HOST_BINDINGS, TABLE_PROVIDERS} from './constants';
16+
17+
/**
18+
* Implicitly enables column resizing for a table-based cdk-table.
19+
* Individual columns will be resizable unless opted out.
20+
*/
21+
@Directive({
22+
selector: 'table[cdk-table]',
23+
host: HOST_BINDINGS,
24+
providers: [
25+
...TABLE_PROVIDERS,
26+
{provide: ColumnResize, useExisting: CdkDefaultEnabledColumnResize},
27+
],
28+
})
29+
export class CdkDefaultEnabledColumnResize extends ColumnResize {
30+
constructor(
31+
readonly columnResizeNotifier: ColumnResizeNotifier,
32+
readonly directionality: Directionality,
33+
protected readonly elementRef: ElementRef,
34+
protected readonly eventDispatcher: HeaderRowEventDispatcher,
35+
protected readonly ngZone: NgZone,
36+
protected readonly notifier: ColumnResizeNotifierSource) {
37+
super();
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import {NgModule} from '@angular/core';
10+
11+
import {CdkColumnResize} from './column-resize-directives/column-resize';
12+
import {CdkColumnResizeFlex} from './column-resize-directives/column-resize-flex';
13+
import {
14+
CdkDefaultEnabledColumnResize
15+
} from './column-resize-directives/default-enabled-column-resize';
16+
import {
17+
CdkDefaultEnabledColumnResizeFlex
18+
} from './column-resize-directives/default-enabled-column-resize-flex';
19+
20+
/**
21+
* One of two NgModules for use with CdkColumnResize.
22+
* When using this module, columns are resizable by default.
23+
*/
24+
@NgModule({
25+
declarations: [CdkDefaultEnabledColumnResize, CdkDefaultEnabledColumnResizeFlex],
26+
exports: [CdkDefaultEnabledColumnResize, CdkDefaultEnabledColumnResizeFlex],
27+
})
28+
export class CdkColumnResizeDefaultEnabledModule {}
29+
30+
/**
31+
* One of two NgModules for use with CdkColumnResize.
32+
* When using this module, columns are not resizable by default.
33+
*/
34+
@NgModule({
35+
declarations: [CdkColumnResize, CdkColumnResizeFlex],
36+
exports: [CdkColumnResize, CdkColumnResizeFlex],
37+
})
38+
export class CdkColumnResizeModule {}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import {Injectable} from '@angular/core';
10+
import {Observable, Subject} from 'rxjs';
11+
12+
/** Indicates the width of a column. */
13+
export interface ColumnSize {
14+
/** The ID/name of the column, as defined in CdkColumnDef. */
15+
readonly columnId: string;
16+
17+
/** The width in pixels of the column. */
18+
readonly size: number;
19+
}
20+
21+
/** Interface describing column size changes. */
22+
export interface ColumnSizeAction extends ColumnSize {
23+
/**
24+
* Whether the resize action should be applied instantaneously. False for events triggered during
25+
* a UI-triggered resize (such as with the mouse) until the mouse button is released. True
26+
* for all programatically triggered resizes.
27+
*/
28+
readonly completeImmediately?: boolean;
29+
}
30+
31+
/** Originating source of column resize events within a table. */
32+
@Injectable()
33+
export class ColumnResizeNotifierSource {
34+
/** Emits when an in-progress resize is canceled. */
35+
readonly resizeCanceled = new Subject<ColumnSizeAction>();
36+
37+
/** Emits when a resize is applied. */
38+
readonly resizeCompleted = new Subject<ColumnSize>();
39+
40+
/** Triggers a resize action. */
41+
readonly triggerResize = new Subject<ColumnSizeAction>();
42+
}
43+
44+
/** Service for triggering column resizes imperatively or being notified of them. */
45+
@Injectable()
46+
export class ColumnResizeNotifier {
47+
/** Emits whenever a column is resized. */
48+
readonly resizeCompleted: Observable<ColumnSize> = this._source.resizeCompleted.asObservable();
49+
50+
constructor(private readonly _source: ColumnResizeNotifierSource) {}
51+
52+
/** Instantly resizes the specified column. */
53+
resize(columnId: string, size: number): void {
54+
this._source.triggerResize.next({columnId, size, completeImmediately: true});
55+
}
56+
}

0 commit comments

Comments
 (0)