Skip to content

Commit b341159

Browse files
committed
feat(popover-edit): Adds support for spanning multiple columns and setting width of the popup based on the size of the cell(s)
1 parent 2f009d0 commit b341159

8 files changed

+331
-28
lines changed

src/cdk-experimental/popover-edit/popover-edit-position-strategy-factory.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ElementRef, Injectable} from '@angular/core';
10-
import {Overlay, PositionStrategy} from '@angular/cdk/overlay';
9+
import {Directionality} from '@angular/cdk/bidi';
10+
import {Overlay, OverlaySizeConfig, PositionStrategy} from '@angular/cdk/overlay';
11+
import {Injectable} from '@angular/core';
1112

1213
/**
13-
* Overridable factory responsible for configuring how cdkPopoverEdit popovers are positioned.
14+
* Overridable factory responsible for configuring how cdkPopoverEdit popovers are positioned
15+
* and sized.
1416
*/
1517
@Injectable()
1618
export abstract class PopoverEditPositionStrategyFactory {
17-
abstract forElementRef(elementRef: ElementRef): PositionStrategy;
19+
/**
20+
* Creates a PositionStrategy based on the specified table cells.
21+
* The cells will be provided in DOM order.
22+
*/
23+
abstract positionStrategyForCells(cells: HTMLElement[]): PositionStrategy;
24+
25+
/**
26+
* Creates an OverlaySizeConfig based on the specified table cells.
27+
* The cells will be provided in DOM order.
28+
*/
29+
abstract sizeConfigForCells(cells: HTMLElement[]): OverlaySizeConfig;
1830
}
1931

2032
/**
@@ -24,19 +36,39 @@ export abstract class PopoverEditPositionStrategyFactory {
2436
*/
2537
@Injectable()
2638
export class DefaultPopoverEditPositionStrategyFactory extends PopoverEditPositionStrategyFactory {
27-
constructor(protected readonly overlay: Overlay) {
39+
constructor(protected readonly direction: Directionality, protected readonly overlay: Overlay) {
2840
super();
2941
}
3042

31-
forElementRef(elementRef: ElementRef): PositionStrategy {
32-
return this.overlay.position().flexibleConnectedTo(elementRef)
43+
positionStrategyForCells(cells: HTMLElement[]): PositionStrategy {
44+
// We assume that the cells are in DOM order.
45+
return this.overlay.position()
46+
.flexibleConnectedTo(cells[0])
3347
.withGrowAfterOpen()
3448
.withPush()
49+
.withViewportMargin(16)
3550
.withPositions([{
3651
originX: 'start',
3752
originY: 'top',
3853
overlayX: 'start',
3954
overlayY: 'top',
4055
}]);
4156
}
57+
58+
sizeConfigForCells(cells: HTMLElement[]): OverlaySizeConfig {
59+
if (cells.length === 1) {
60+
return {width: cells[0].getBoundingClientRect().width};
61+
}
62+
63+
let firstCell, lastCell;
64+
if (this.direction.value === 'ltr') {
65+
firstCell = cells[0];
66+
lastCell = cells[cells.length - 1];
67+
} else {
68+
lastCell = cells[0];
69+
firstCell = cells[cells.length - 1];
70+
}
71+
72+
return {width: lastCell.getBoundingClientRect().right - firstCell.getBoundingClientRect().left};
73+
}
4274
}

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {BehaviorSubject} from 'rxjs';
2-
import {Component, ElementRef, Type, ViewChild} from '@angular/core';
3-
import {CommonModule} from '@angular/common';
4-
import {FormsModule, NgForm} from '@angular/forms';
5-
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
61
import {DataSource} from '@angular/cdk/collections';
72
import {CdkTableModule} from '@angular/cdk/table';
8-
import {CdkPopoverEditModule, PopoverEditClickOutBehavior} from './index';
3+
import {CommonModule} from '@angular/common';
4+
import {Component, ElementRef, Type, ViewChild} from '@angular/core';
5+
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
6+
import {FormsModule, NgForm} from '@angular/forms';
7+
import {BehaviorSubject} from 'rxjs';
8+
9+
import {CdkPopoverEditColspan, CdkPopoverEditModule, PopoverEditClickOutBehavior} from './index';
910

1011
const EDIT_TEMPLATE = `
1112
<div style="background-color: white;">
@@ -33,6 +34,8 @@ const CELL_TEMPLATE = `
3334
</span>
3435
`;
3536

37+
const POPOVER_EDIT_DIRECTIVE = `[cdkPopoverEdit]="nameEdit" [cdkPopoverEditColspan]="colspan"`;
38+
3639
interface PeriodicElement {
3740
name: string;
3841
weight: number;
@@ -45,6 +48,7 @@ abstract class BaseTestComponent {
4548

4649
ignoreSubmitUnlessValid = true;
4750
clickOutBehavior: PopoverEditClickOutBehavior = 'close';
51+
colspan: CdkPopoverEditColspan = {};
4852

4953
onSubmit(element: PeriodicElement, form: NgForm) {
5054
if (!form.valid) { return; }
@@ -62,7 +66,7 @@ abstract class BaseTestComponent {
6266

6367
getEditCell(rowIndex = 0) {
6468
const row = getRows(this.table.nativeElement)[rowIndex];
65-
return getCells(row)[0];
69+
return getCells(row)[1];
6670
}
6771

6872
focusEditCell(rowIndex = 0) {
@@ -84,6 +88,10 @@ abstract class BaseTestComponent {
8488
flush();
8589
}
8690

91+
getEditPane() {
92+
return document.querySelector('.cdk-edit-pane');
93+
}
94+
8795
getInput() {
8896
return document.querySelector('input') as HTMLInputElement|null;
8997
}
@@ -125,7 +133,10 @@ abstract class BaseTestComponent {
125133
</ng-template>
126134
127135
<tr *ngFor="let element of elements">
128-
<td [cdkPopoverEdit]="nameEdit" [cdkPopoverEditContext]="element">
136+
<td> just a cell </td>
137+
138+
<td ${POPOVER_EDIT_DIRECTIVE}
139+
[cdkPopoverEditContext]="element">
129140
${CELL_TEMPLATE}
130141
</td>
131142
@@ -142,7 +153,9 @@ class VanillaTableOutOfCell extends BaseTestComponent {
142153
template: `
143154
<table #table editable>
144155
<tr *ngFor="let element of elements">
145-
<td [cdkPopoverEdit]="nameEdit">
156+
<td> just a cell </td>
157+
158+
<td ${POPOVER_EDIT_DIRECTIVE}>
146159
${CELL_TEMPLATE}
147160
148161
<ng-template #nameEdit>
@@ -175,9 +188,15 @@ class ElementDataSource extends DataSource<PeriodicElement> {
175188
template: `
176189
<div #table>
177190
<cdk-table cdk-table editable [dataSource]="dataSource">
191+
<ng-container cdkColumnDef="before">
192+
<cdk-cell *cdkCellDef="let element">
193+
just a cell
194+
</cdk-cell>
195+
</ng-container>
196+
178197
<ng-container cdkColumnDef="name">
179198
<cdk-cell *cdkCellDef="let element"
180-
[cdkPopoverEdit]="nameEdit">
199+
${POPOVER_EDIT_DIRECTIVE}>
181200
${CELL_TEMPLATE}
182201
183202
<ng-template #nameEdit>
@@ -202,17 +221,23 @@ class ElementDataSource extends DataSource<PeriodicElement> {
202221
`
203222
})
204223
class CdkFlexTableInCell extends BaseTestComponent {
205-
displayedColumns = ['name', 'weight'];
224+
displayedColumns = ['before', 'name', 'weight'];
206225
dataSource = new ElementDataSource();
207226
}
208227

209228
@Component({
210229
template: `
211230
<div #table>
212231
<table cdk-table editable [dataSource]="dataSource">
232+
<ng-container cdkColumnDef="before">
233+
<td cdk-cell *cdkCellDef="let element">
234+
just a cell
235+
</td>
236+
</ng-container>
237+
213238
<ng-container cdkColumnDef="name">
214239
<td cdk-cell *cdkCellDef="let element"
215-
[cdkPopoverEdit]="nameEdit">
240+
${POPOVER_EDIT_DIRECTIVE}>
216241
${CELL_TEMPLATE}
217242
218243
<ng-template #nameEdit>
@@ -237,7 +262,7 @@ class CdkFlexTableInCell extends BaseTestComponent {
237262
`
238263
})
239264
class CdkTableInCell extends BaseTestComponent {
240-
displayedColumns = ['name', 'weight'];
265+
displayedColumns = ['before', 'name', 'weight'];
241266
dataSource = new ElementDataSource();
242267
}
243268

@@ -316,6 +341,49 @@ describe('CDK Popover Edit', () => {
316341
expect(component.getInput()!.value).toBe('Hydrogen');
317342
}));
318343

344+
it('positions the lens at the top left corner and spans the full width of the cell',
345+
fakeAsync(() => {
346+
component.openLens();
347+
348+
const paneRect = component.getEditPane()!.getBoundingClientRect();
349+
const cellRect = component.getEditCell().getBoundingClientRect();
350+
351+
expect(paneRect.width).toEqual(cellRect.width);
352+
expect(paneRect.left).toEqual(cellRect.left);
353+
expect(paneRect.top).toEqual(cellRect.top);
354+
}));
355+
356+
it('adjusts the positioning of the lens based on colspan', fakeAsync(() => {
357+
const cellRects = getCells(getRows(component.table.nativeElement)[0])
358+
.map(cell => cell.getBoundingClientRect());
359+
360+
component.colspan = {before: 1};
361+
fixture.detectChanges();
362+
363+
component.openLens();
364+
365+
let paneRect = component.getEditPane()!.getBoundingClientRect();
366+
expect(paneRect.top).toEqual(cellRects[0].top);
367+
expect(paneRect.left).toEqual(cellRects[0].left);
368+
expect(paneRect.right).toEqual(cellRects[1].right);
369+
370+
component.colspan = {after: 1};
371+
fixture.detectChanges();
372+
373+
paneRect = component.getEditPane()!.getBoundingClientRect();
374+
expect(paneRect.top).toEqual(cellRects[1].top);
375+
expect(paneRect.left).toEqual(cellRects[1].left);
376+
expect(paneRect.right).toEqual(cellRects[2].right);
377+
378+
component.colspan = {before: 1, after: 1};
379+
fixture.detectChanges();
380+
381+
paneRect = component.getEditPane()!.getBoundingClientRect();
382+
expect(paneRect.top).toEqual(cellRects[0].top);
383+
expect(paneRect.left).toEqual(cellRects[0].left);
384+
expect(paneRect.right).toEqual(cellRects[2].right);
385+
}));
386+
319387
it('updates the form and submits, closing the lens', fakeAsync(() => {
320388
component.openLens();
321389

src/cdk-experimental/popover-edit/table-directives.ts

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {FocusTrap, FocusTrapFactory} from '@angular/cdk/a11y';
9-
import {Overlay, OverlayRef} from '@angular/cdk/overlay';
9+
import {Overlay, OverlayRef, PositionStrategy} from '@angular/cdk/overlay';
1010
import {TemplatePortal} from '@angular/cdk/portal';
11+
import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
1112
import {
1213
AfterViewInit,
1314
Directive,
@@ -19,14 +20,19 @@ import {
1920
TemplateRef,
2021
ViewContainerRef
2122
} from '@angular/core';
22-
import {fromEvent, ReplaySubject} from 'rxjs';
23-
import {debounceTime, filter, map, mapTo, takeUntil} from 'rxjs/operators';
23+
import {fromEvent, merge, ReplaySubject} from 'rxjs';
24+
import {debounceTime, filter, map, mapTo, startWith, takeUntil} from 'rxjs/operators';
2425

2526
import {CELL_SELECTOR, EDIT_PANE_CLASS, EDIT_PANE_SELECTOR, ROW_SELECTOR} from './constants';
2627
import {EditEventDispatcher} from './edit-event-dispatcher';
2728
import {closest} from './polyfill';
2829
import {PopoverEditPositionStrategyFactory} from './popover-edit-position-strategy-factory';
2930

31+
export interface CdkPopoverEditColspan {
32+
before?: number;
33+
after?: number;
34+
}
35+
3036
/**
3137
* The delay between the mouse entering a row and the mouse stopping its movement before
3238
* showing on-hover content.
@@ -111,6 +117,28 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
111117
*/
112118
@Input('cdkPopoverEditContext') context?: C;
113119

120+
/**
121+
* Specifies that the popup should cover additional table cells before and/or after
122+
* this one.
123+
*/
124+
@Input('cdkPopoverEditColspan')
125+
get colspan(): CdkPopoverEditColspan {
126+
return this._colspan;
127+
}
128+
set colspan(value: CdkPopoverEditColspan) {
129+
this._colspan = value;
130+
131+
// Recompute positioning when the colspan changes.
132+
if (this.overlayRef) {
133+
this.overlayRef.updatePositionStrategy(this._getPositionStrategy());
134+
135+
if (this.overlayRef.hasAttached()) {
136+
this._updateOverlaySize();
137+
}
138+
}
139+
}
140+
private _colspan: CdkPopoverEditColspan = {};
141+
114142
protected focusTrap?: FocusTrap;
115143
protected overlayRef?: OverlayRef;
116144
protected readonly destroyed = new ReplaySubject<void>();
@@ -122,7 +150,9 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
122150
protected readonly ngZone: NgZone,
123151
protected readonly overlay: Overlay,
124152
protected readonly positionFactory: PopoverEditPositionStrategyFactory,
125-
protected readonly viewContainerRef: ViewContainerRef) {}
153+
protected readonly scrollDispatcher: ScrollDispatcher,
154+
protected readonly viewContainerRef: ViewContainerRef,
155+
protected readonly viewportRuler: ViewportRuler) {}
126156

127157
ngAfterViewInit(): void {
128158
this._startListeningToEditEvents();
@@ -161,8 +191,8 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
161191
this.overlayRef = this.overlay.create({
162192
disposeOnNavigation: true,
163193
panelClass: EDIT_PANE_CLASS,
164-
positionStrategy: this.positionFactory.forElementRef(this.elementRef),
165-
scrollStrategy: this.overlay.scrollStrategies.reposition({autoClose: true}),
194+
positionStrategy: this._getPositionStrategy(),
195+
scrollStrategy: this.overlay.scrollStrategies.reposition(),
166196
});
167197

168198
this.focusTrap = this.focusTrapFactory.create(this.overlayRef.overlayElement);
@@ -179,6 +209,40 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
179209
this.viewContainerRef,
180210
{$implicit: this.context}));
181211
this.focusTrap!.focusInitialElementWhenReady();
212+
213+
// Update the size of the popup initially and on subsequent changes to
214+
// scroll position and viewport size.
215+
merge(this.scrollDispatcher.scrolled(), this.viewportRuler.change())
216+
.pipe(
217+
startWith(null),
218+
takeUntil(this.overlayRef!.detachments()),
219+
)
220+
.subscribe(() => {
221+
this._updateOverlaySize();
222+
});
223+
}
224+
225+
private _getOverlayCells(): HTMLElement[] {
226+
const cell = closest(this.elementRef.nativeElement!, CELL_SELECTOR) as HTMLElement;
227+
228+
if (!this._colspan.before && !this._colspan.after) {
229+
return [cell];
230+
}
231+
232+
const row = closest(this.elementRef.nativeElement!, ROW_SELECTOR)!;
233+
const rowCells = Array.from(row.querySelectorAll(CELL_SELECTOR)) as HTMLElement[];
234+
const ownIndex = rowCells.indexOf(cell);
235+
236+
return rowCells.slice(
237+
ownIndex - (this._colspan.before || 0), ownIndex + (this._colspan.after || 0) + 1);
238+
}
239+
240+
private _getPositionStrategy(): PositionStrategy {
241+
return this.positionFactory.positionStrategyForCells(this._getOverlayCells());
242+
}
243+
244+
private _updateOverlaySize(): void {
245+
this.overlayRef!.updateSize(this.positionFactory.sizeConfigForCells(this._getOverlayCells()));
182246
}
183247

184248
private _maybeReturnFocusToCell(): void {

0 commit comments

Comments
 (0)