-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathoverlay-directives.ts
478 lines (403 loc) · 15.3 KB
/
overlay-directives.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Direction, Directionality} from '@angular/cdk/bidi';
import {ESCAPE, hasModifierKey} from '@angular/cdk/keycodes';
import {TemplatePortal} from '@angular/cdk/portal';
import {
Directive,
ElementRef,
EventEmitter,
Inject,
InjectionToken,
Input,
NgZone,
OnChanges,
OnDestroy,
Optional,
Output,
SimpleChanges,
TemplateRef,
ViewContainerRef,
booleanAttribute,
inject,
} from '@angular/core';
import {_getEventTarget} from '@angular/cdk/platform';
import {Subscription} from 'rxjs';
import {takeWhile} from 'rxjs/operators';
import {Overlay} from './overlay';
import {OverlayConfig} from './overlay-config';
import {OverlayRef} from './overlay-ref';
import {ConnectedOverlayPositionChange, ViewportMargin} from './position/connected-position';
import {
ConnectedPosition,
FlexibleConnectedPositionStrategy,
FlexibleConnectedPositionStrategyOrigin,
} from './position/flexible-connected-position-strategy';
import {RepositionScrollStrategy, ScrollStrategy} from './scroll/index';
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
const defaultPositionList: ConnectedPosition[] = [
{
originX: 'start',
originY: 'bottom',
overlayX: 'start',
overlayY: 'top',
},
{
originX: 'start',
originY: 'top',
overlayX: 'start',
overlayY: 'bottom',
},
{
originX: 'end',
originY: 'top',
overlayX: 'end',
overlayY: 'bottom',
},
{
originX: 'end',
originY: 'bottom',
overlayX: 'end',
overlayY: 'top',
},
];
/** Injection token that determines the scroll handling while the connected overlay is open. */
export const CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
'cdk-connected-overlay-scroll-strategy',
{
providedIn: 'root',
factory: () => {
const overlay = inject(Overlay);
return () => overlay.scrollStrategies.reposition();
},
},
);
/**
* Directive applied to an element to make it usable as an origin for an Overlay using a
* ConnectedPositionStrategy.
*/
@Directive({
selector: '[cdk-overlay-origin], [overlay-origin], [cdkOverlayOrigin]',
exportAs: 'cdkOverlayOrigin',
standalone: true,
})
export class CdkOverlayOrigin {
constructor(
/** Reference to the element on which the directive is applied. */
public elementRef: ElementRef,
) {}
}
/**
* Directive to facilitate declarative creation of an
* Overlay using a FlexibleConnectedPositionStrategy.
*/
@Directive({
selector: '[cdk-connected-overlay], [connected-overlay], [cdkConnectedOverlay]',
exportAs: 'cdkConnectedOverlay',
standalone: true,
})
export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _overlayRef: OverlayRef;
private _templatePortal: TemplatePortal;
private _backdropSubscription = Subscription.EMPTY;
private _attachSubscription = Subscription.EMPTY;
private _detachSubscription = Subscription.EMPTY;
private _positionSubscription = Subscription.EMPTY;
private _offsetX: number;
private _offsetY: number;
private _position: FlexibleConnectedPositionStrategy;
private _scrollStrategyFactory: () => ScrollStrategy;
private _disposeOnNavigation = false;
private _ngZone = inject(NgZone);
/** Origin for the connected overlay. */
@Input('cdkConnectedOverlayOrigin')
origin: CdkOverlayOrigin | FlexibleConnectedPositionStrategyOrigin;
/** Registered connected position pairs. */
@Input('cdkConnectedOverlayPositions') positions: ConnectedPosition[];
/**
* This input overrides the positions input if specified. It lets users pass
* in arbitrary positioning strategies.
*/
@Input('cdkConnectedOverlayPositionStrategy') positionStrategy: FlexibleConnectedPositionStrategy;
/** The offset in pixels for the overlay connection point on the x-axis */
@Input('cdkConnectedOverlayOffsetX')
get offsetX(): number {
return this._offsetX;
}
set offsetX(offsetX: number) {
this._offsetX = offsetX;
if (this._position) {
this._updatePositionStrategy(this._position);
}
}
/** The offset in pixels for the overlay connection point on the y-axis */
@Input('cdkConnectedOverlayOffsetY')
get offsetY() {
return this._offsetY;
}
set offsetY(offsetY: number) {
this._offsetY = offsetY;
if (this._position) {
this._updatePositionStrategy(this._position);
}
}
/** The width of the overlay panel. */
@Input('cdkConnectedOverlayWidth') width: number | string;
/** The height of the overlay panel. */
@Input('cdkConnectedOverlayHeight') height: number | string;
/** The min width of the overlay panel. */
@Input('cdkConnectedOverlayMinWidth') minWidth: number | string;
/** The min height of the overlay panel. */
@Input('cdkConnectedOverlayMinHeight') minHeight: number | string;
/** The custom class to be set on the backdrop element. */
@Input('cdkConnectedOverlayBackdropClass') backdropClass: string | string[];
/** The custom class to add to the overlay pane element. */
@Input('cdkConnectedOverlayPanelClass') panelClass: string | string[];
/** Margin between the overlay and the viewport edges. */
@Input('cdkConnectedOverlayViewportMargin') viewportMargin: ViewportMargin = 0;
/** Strategy to be used when handling scroll events while the overlay is open. */
@Input('cdkConnectedOverlayScrollStrategy') scrollStrategy: ScrollStrategy;
/** Whether the overlay is open. */
@Input('cdkConnectedOverlayOpen') open: boolean = false;
/** Whether the overlay can be closed by user interaction. */
@Input('cdkConnectedOverlayDisableClose') disableClose: boolean = false;
/** CSS selector which to set the transform origin. */
@Input('cdkConnectedOverlayTransformOriginOn') transformOriginSelector: string;
/** Whether or not the overlay should attach a backdrop. */
@Input({alias: 'cdkConnectedOverlayHasBackdrop', transform: booleanAttribute})
hasBackdrop: boolean = false;
/** Whether or not the overlay should be locked when scrolling. */
@Input({alias: 'cdkConnectedOverlayLockPosition', transform: booleanAttribute})
lockPosition: boolean = false;
/** Whether the overlay's width and height can be constrained to fit within the viewport. */
@Input({alias: 'cdkConnectedOverlayFlexibleDimensions', transform: booleanAttribute})
flexibleDimensions: boolean = false;
/** Whether the overlay can grow after the initial open when flexible positioning is turned on. */
@Input({alias: 'cdkConnectedOverlayGrowAfterOpen', transform: booleanAttribute})
growAfterOpen: boolean = false;
/** Whether the overlay can be pushed on-screen if none of the provided positions fit. */
@Input({alias: 'cdkConnectedOverlayPush', transform: booleanAttribute}) push: boolean = false;
/** Whether the overlay should be disposed of when the user goes backwards/forwards in history. */
@Input({alias: 'cdkConnectedOverlayDisposeOnNavigation', transform: booleanAttribute})
get disposeOnNavigation(): boolean {
return this._disposeOnNavigation;
}
set disposeOnNavigation(value: boolean) {
this._disposeOnNavigation = value;
}
/** Event emitted when the backdrop is clicked. */
@Output() readonly backdropClick = new EventEmitter<MouseEvent>();
/** Event emitted when the position has changed. */
@Output() readonly positionChange = new EventEmitter<ConnectedOverlayPositionChange>();
/** Event emitted when the overlay has been attached. */
@Output() readonly attach = new EventEmitter<void>();
/** Event emitted when the overlay has been detached. */
@Output() readonly detach = new EventEmitter<void>();
/** Emits when there are keyboard events that are targeted at the overlay. */
@Output() readonly overlayKeydown = new EventEmitter<KeyboardEvent>();
/** Emits when there are mouse outside click events that are targeted at the overlay. */
@Output() readonly overlayOutsideClick = new EventEmitter<MouseEvent>();
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.
constructor(
private _overlay: Overlay,
templateRef: TemplateRef<any>,
viewContainerRef: ViewContainerRef,
@Inject(CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY) scrollStrategyFactory: any,
@Optional() private _dir: Directionality,
) {
this._templatePortal = new TemplatePortal(templateRef, viewContainerRef);
this._scrollStrategyFactory = scrollStrategyFactory;
this.scrollStrategy = this._scrollStrategyFactory();
}
/** The associated overlay reference. */
get overlayRef(): OverlayRef {
return this._overlayRef;
}
/** The element's layout direction. */
get dir(): Direction {
return this._dir ? this._dir.value : 'ltr';
}
ngOnDestroy() {
this._attachSubscription.unsubscribe();
this._detachSubscription.unsubscribe();
this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
if (this._overlayRef) {
this._overlayRef.dispose();
}
}
ngOnChanges(changes: SimpleChanges) {
if (this._position) {
this._updatePositionStrategy(this._position);
this._overlayRef.updateSize({
width: this.width,
minWidth: this.minWidth,
height: this.height,
minHeight: this.minHeight,
});
if (changes['origin'] && this.open) {
this._position.apply();
}
}
if (changes['open']) {
this.open ? this._attachOverlay() : this._detachOverlay();
}
}
/** Creates an overlay */
private _createOverlay() {
if (!this.positions || !this.positions.length) {
this.positions = defaultPositionList;
}
const overlayRef = (this._overlayRef = this._overlay.create(this._buildConfig()));
this._attachSubscription = overlayRef.attachments().subscribe(() => this.attach.emit());
this._detachSubscription = overlayRef.detachments().subscribe(() => this.detach.emit());
overlayRef.keydownEvents().subscribe((event: KeyboardEvent) => {
this.overlayKeydown.next(event);
if (event.keyCode === ESCAPE && !this.disableClose && !hasModifierKey(event)) {
event.preventDefault();
this._detachOverlay();
}
});
this._overlayRef.outsidePointerEvents().subscribe((event: MouseEvent) => {
const origin = this._getOriginElement();
const target = _getEventTarget(event) as Element | null;
if (!origin || (origin !== target && !origin.contains(target))) {
this.overlayOutsideClick.next(event);
}
});
}
/** Builds the overlay config based on the directive's inputs */
private _buildConfig(): OverlayConfig {
const positionStrategy = (this._position =
this.positionStrategy || this._createPositionStrategy());
const overlayConfig = new OverlayConfig({
direction: this._dir,
positionStrategy,
scrollStrategy: this.scrollStrategy,
hasBackdrop: this.hasBackdrop,
disposeOnNavigation: this.disposeOnNavigation,
});
if (this.width || this.width === 0) {
overlayConfig.width = this.width;
}
if (this.height || this.height === 0) {
overlayConfig.height = this.height;
}
if (this.minWidth || this.minWidth === 0) {
overlayConfig.minWidth = this.minWidth;
}
if (this.minHeight || this.minHeight === 0) {
overlayConfig.minHeight = this.minHeight;
}
if (this.backdropClass) {
overlayConfig.backdropClass = this.backdropClass;
}
if (this.panelClass) {
overlayConfig.panelClass = this.panelClass;
}
return overlayConfig;
}
/** Updates the state of a position strategy, based on the values of the directive inputs. */
private _updatePositionStrategy(positionStrategy: FlexibleConnectedPositionStrategy) {
const positions: ConnectedPosition[] = this.positions.map(currentPosition => ({
originX: currentPosition.originX,
originY: currentPosition.originY,
overlayX: currentPosition.overlayX,
overlayY: currentPosition.overlayY,
offsetX: currentPosition.offsetX || this.offsetX,
offsetY: currentPosition.offsetY || this.offsetY,
panelClass: currentPosition.panelClass || undefined,
}));
return positionStrategy
.setOrigin(this._getOrigin())
.withPositions(positions)
.withFlexibleDimensions(this.flexibleDimensions)
.withPush(this.push)
.withGrowAfterOpen(this.growAfterOpen)
.withViewportMargin(this.viewportMargin)
.withLockedPosition(this.lockPosition)
.withTransformOriginOn(this.transformOriginSelector);
}
/** Returns the position strategy of the overlay to be set on the overlay config */
private _createPositionStrategy(): FlexibleConnectedPositionStrategy {
const strategy = this._overlay.position().flexibleConnectedTo(this._getOrigin());
this._updatePositionStrategy(strategy);
return strategy;
}
private _getOrigin(): FlexibleConnectedPositionStrategyOrigin {
if (this.origin instanceof CdkOverlayOrigin) {
return this.origin.elementRef;
} else {
return this.origin;
}
}
private _getOriginElement(): Element | null {
if (this.origin instanceof CdkOverlayOrigin) {
return this.origin.elementRef.nativeElement;
}
if (this.origin instanceof ElementRef) {
return this.origin.nativeElement;
}
if (typeof Element !== 'undefined' && this.origin instanceof Element) {
return this.origin;
}
return null;
}
/** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
private _attachOverlay() {
if (!this._overlayRef) {
this._createOverlay();
} else {
// Update the overlay size, in case the directive's inputs have changed
this._overlayRef.getConfig().hasBackdrop = this.hasBackdrop;
}
if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
}
if (this.hasBackdrop) {
this._backdropSubscription = this._overlayRef.backdropClick().subscribe(event => {
this.backdropClick.emit(event);
});
} else {
this._backdropSubscription.unsubscribe();
}
this._positionSubscription.unsubscribe();
// Only subscribe to `positionChanges` if requested, because putting
// together all the information for it can be expensive.
if (this.positionChange.observers.length > 0) {
this._positionSubscription = this._position.positionChanges
.pipe(takeWhile(() => this.positionChange.observers.length > 0))
.subscribe(position => {
this._ngZone.run(() => this.positionChange.emit(position));
if (this.positionChange.observers.length === 0) {
this._positionSubscription.unsubscribe();
}
});
}
}
/** Detaches the overlay and unsubscribes to backdrop clicks if backdrop exists */
private _detachOverlay() {
if (this._overlayRef) {
this._overlayRef.detach();
}
this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
}
}
/** @docs-private */
export function CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY_PROVIDER_FACTORY(
overlay: Overlay,
): () => RepositionScrollStrategy {
return () => overlay.scrollStrategies.reposition();
}
/** @docs-private */
export const CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY_PROVIDER = {
provide: CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY,
deps: [Overlay],
useFactory: CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY_PROVIDER_FACTORY,
};