-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathdrag.ts
619 lines (535 loc) · 21.2 KB
/
drag.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* @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.dev/license
*/
import {Directionality} from '../../bidi';
import {
Directive,
ElementRef,
EventEmitter,
Input,
NgZone,
OnDestroy,
Output,
ViewContainerRef,
OnChanges,
SimpleChanges,
ChangeDetectorRef,
InjectionToken,
booleanAttribute,
afterNextRender,
AfterViewInit,
inject,
Injector,
numberAttribute,
} from '@angular/core';
import {coerceElement, coerceNumberProperty} from '../../coercion';
import {BehaviorSubject, Observable, Observer, Subject, merge} from 'rxjs';
import {startWith, take, map, takeUntil, switchMap, tap} from 'rxjs/operators';
import type {
CdkDragDrop,
CdkDragEnd,
CdkDragEnter,
CdkDragExit,
CdkDragMove,
CdkDragStart,
CdkDragRelease,
} from '../drag-events';
import {CDK_DRAG_HANDLE, CdkDragHandle} from './drag-handle';
import {CdkDragPlaceholder} from './drag-placeholder';
import {CdkDragPreview} from './drag-preview';
import {CDK_DRAG_PARENT} from '../drag-parent';
import {DragRef, Point, PreviewContainer, DragConstrainPosition} from '../drag-ref';
import type {CdkDropList} from './drop-list';
import {DragDrop} from '../drag-drop';
import {CDK_DRAG_CONFIG, DragDropConfig, DragStartDelay, DragAxis} from './config';
import {assertElementNode} from './assertions';
import {DragDropRegistry} from '../drag-drop-registry';
/**
* Injection token that can be used to reference instances of `CdkDropList`. It serves as
* alternative token to the actual `CdkDropList` class which could cause unnecessary
* retention of the class and its directive metadata.
*/
export const CDK_DROP_LIST = new InjectionToken<CdkDropList>('CdkDropList');
/** Element that can be moved inside a CdkDropList container. */
@Directive({
selector: '[cdkDrag]',
exportAs: 'cdkDrag',
host: {
'class': 'cdk-drag',
'[class.cdk-drag-disabled]': 'disabled',
'[class.cdk-drag-dragging]': '_dragRef.isDragging()',
},
providers: [{provide: CDK_DRAG_PARENT, useExisting: CdkDrag}],
})
export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
element = inject<ElementRef<HTMLElement>>(ElementRef);
dropContainer = inject<CdkDropList>(CDK_DROP_LIST, {optional: true, skipSelf: true})!;
private _ngZone = inject(NgZone);
private _viewContainerRef = inject(ViewContainerRef);
private _dir = inject(Directionality, {optional: true});
private _changeDetectorRef = inject(ChangeDetectorRef);
private _selfHandle = inject<CdkDragHandle>(CDK_DRAG_HANDLE, {optional: true, self: true});
private _parentDrag = inject<CdkDrag>(CDK_DRAG_PARENT, {optional: true, skipSelf: true});
private _dragDropRegistry = inject(DragDropRegistry);
private readonly _destroyed = new Subject<void>();
private _handles = new BehaviorSubject<CdkDragHandle[]>([]);
private _previewTemplate: CdkDragPreview | null;
private _placeholderTemplate: CdkDragPlaceholder | null;
/** Reference to the underlying drag instance. */
_dragRef: DragRef<CdkDrag<T>>;
/** Arbitrary data to attach to this drag instance. */
@Input('cdkDragData') data: T;
/** Locks the position of the dragged element along the specified axis. */
@Input('cdkDragLockAxis') lockAxis: DragAxis;
/**
* Selector that will be used to determine the root draggable element, starting from
* the `cdkDrag` element and going up the DOM. Passing an alternate root element is useful
* when trying to enable dragging on an element that you might not have access to.
*/
@Input('cdkDragRootElement') rootElementSelector: string;
/**
* Node or selector that will be used to determine the element to which the draggable's
* position will be constrained. If a string is passed in, it'll be used as a selector that
* will be matched starting from the element's parent and going up the DOM until a match
* has been found.
*/
@Input('cdkDragBoundary') boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
/**
* Amount of milliseconds to wait after the user has put their
* pointer down before starting to drag the element.
*/
@Input('cdkDragStartDelay') dragStartDelay: DragStartDelay;
/**
* Sets the position of a `CdkDrag` that is outside of a drop container.
* Can be used to restore the element's position for a returning user.
*/
@Input('cdkDragFreeDragPosition') freeDragPosition: Point;
/** Whether starting to drag this element is disabled. */
@Input({alias: 'cdkDragDisabled', transform: booleanAttribute})
get disabled(): boolean {
return this._disabled || !!(this.dropContainer && this.dropContainer.disabled);
}
set disabled(value: boolean) {
this._disabled = value;
this._dragRef.disabled = this._disabled;
}
private _disabled: boolean;
/**
* Function that can be used to customize the logic of how the position of the drag item
* is limited while it's being dragged. Gets called with a point containing the current position
* of the user's pointer on the page, a reference to the item being dragged and its dimensions.
* Should return a point describing where the item should be rendered.
*/
@Input('cdkDragConstrainPosition') constrainPosition?: DragConstrainPosition;
/** Class to be added to the preview element. */
@Input('cdkDragPreviewClass') previewClass: string | string[];
/**
* Configures the place into which the preview of the item will be inserted. Can be configured
* globally through `CDK_DROP_LIST`. Possible values:
* - `global` - Preview will be inserted at the bottom of the `<body>`. The advantage is that
* you don't have to worry about `overflow: hidden` or `z-index`, but the item won't retain
* its inherited styles.
* - `parent` - Preview will be inserted into the parent of the drag item. The advantage is that
* inherited styles will be preserved, but it may be clipped by `overflow: hidden` or not be
* visible due to `z-index`. Furthermore, the preview is going to have an effect over selectors
* like `:nth-child` and some flexbox configurations.
* - `ElementRef<HTMLElement> | HTMLElement` - Preview will be inserted into a specific element.
* Same advantages and disadvantages as `parent`.
*/
@Input('cdkDragPreviewContainer') previewContainer: PreviewContainer;
/**
* If the parent of the dragged element has a `scale` transform, it can throw off the
* positioning when the user starts dragging. Use this input to notify the CDK of the scale.
*/
@Input({alias: 'cdkDragScale', transform: numberAttribute})
scale: number = 1;
/** Emits when the user starts dragging the item. */
@Output('cdkDragStarted') readonly started: EventEmitter<CdkDragStart> =
new EventEmitter<CdkDragStart>();
/** Emits when the user has released a drag item, before any animations have started. */
@Output('cdkDragReleased') readonly released: EventEmitter<CdkDragRelease> =
new EventEmitter<CdkDragRelease>();
/** Emits when the user stops dragging an item in the container. */
@Output('cdkDragEnded') readonly ended: EventEmitter<CdkDragEnd> = new EventEmitter<CdkDragEnd>();
/** Emits when the user has moved the item into a new container. */
@Output('cdkDragEntered') readonly entered: EventEmitter<CdkDragEnter<any>> = new EventEmitter<
CdkDragEnter<any>
>();
/** Emits when the user removes the item its container by dragging it into another container. */
@Output('cdkDragExited') readonly exited: EventEmitter<CdkDragExit<any>> = new EventEmitter<
CdkDragExit<any>
>();
/** Emits when the user drops the item inside a container. */
@Output('cdkDragDropped') readonly dropped: EventEmitter<CdkDragDrop<any>> = new EventEmitter<
CdkDragDrop<any>
>();
/**
* Emits as the user is dragging the item. Use with caution,
* because this event will fire for every pixel that the user has dragged.
*/
@Output('cdkDragMoved')
readonly moved: Observable<CdkDragMove<T>> = new Observable(
(observer: Observer<CdkDragMove<T>>) => {
const subscription = this._dragRef.moved
.pipe(
map(movedEvent => ({
source: this,
pointerPosition: movedEvent.pointerPosition,
event: movedEvent.event,
delta: movedEvent.delta,
distance: movedEvent.distance,
})),
)
.subscribe(observer);
return () => {
subscription.unsubscribe();
};
},
);
private _injector = inject(Injector);
constructor(...args: unknown[]);
constructor() {
const dropContainer = this.dropContainer;
const config = inject<DragDropConfig>(CDK_DRAG_CONFIG, {optional: true});
const dragDrop = inject(DragDrop);
this._dragRef = dragDrop.createDrag(this.element, {
dragStartThreshold:
config && config.dragStartThreshold != null ? config.dragStartThreshold : 5,
pointerDirectionChangeThreshold:
config && config.pointerDirectionChangeThreshold != null
? config.pointerDirectionChangeThreshold
: 5,
zIndex: config?.zIndex,
});
this._dragRef.data = this;
this._dragDropRegistry.registerDirectiveNode(this.element.nativeElement, this);
if (config) {
this._assignDefaults(config);
}
// Note that usually the container is assigned when the drop list is picks up the item, but in
// some cases (mainly transplanted views with OnPush, see #18341) we may end up in a situation
// where there are no items on the first change detection pass, but the items get picked up as
// soon as the user triggers another pass by dragging. This is a problem, because the item would
// have to switch from standalone mode to drag mode in the middle of the dragging sequence which
// is too late since the two modes save different kinds of information. We work around it by
// assigning the drop container both from here and the list.
if (dropContainer) {
this._dragRef._withDropContainer(dropContainer._dropListRef);
dropContainer.addItem(this);
// The drop container reads this so we need to sync it here.
dropContainer._dropListRef.beforeStarted.pipe(takeUntil(this._destroyed)).subscribe(() => {
this._dragRef.scale = this.scale;
});
}
this._syncInputs(this._dragRef);
this._handleEvents(this._dragRef);
}
/**
* Returns the element that is being used as a placeholder
* while the current element is being dragged.
*/
getPlaceholderElement(): HTMLElement {
return this._dragRef.getPlaceholderElement();
}
/** Returns the root draggable element. */
getRootElement(): HTMLElement {
return this._dragRef.getRootElement();
}
/** Resets a standalone drag item to its initial position. */
reset(): void {
this._dragRef.reset();
}
/**
* Gets the pixel coordinates of the draggable outside of a drop container.
*/
getFreeDragPosition(): Readonly<Point> {
return this._dragRef.getFreeDragPosition();
}
/**
* Sets the current position in pixels the draggable outside of a drop container.
* @param value New position to be set.
*/
setFreeDragPosition(value: Point): void {
this._dragRef.setFreeDragPosition(value);
}
ngAfterViewInit() {
// We need to wait until after render, in order for the reference
// element to be in the proper place in the DOM. This is mostly relevant
// for draggable elements inside portals since they get stamped out in
// their original DOM position, and then they get transferred to the portal.
afterNextRender(
() => {
this._updateRootElement();
this._setupHandlesListener();
this._dragRef.scale = this.scale;
if (this.freeDragPosition) {
this._dragRef.setFreeDragPosition(this.freeDragPosition);
}
},
{injector: this._injector},
);
}
ngOnChanges(changes: SimpleChanges) {
const rootSelectorChange = changes['rootElementSelector'];
const positionChange = changes['freeDragPosition'];
// We don't have to react to the first change since it's being
// handled in the `afterNextRender` queued up in the constructor.
if (rootSelectorChange && !rootSelectorChange.firstChange) {
this._updateRootElement();
}
// Scale affects the free drag position so we need to sync it up here.
this._dragRef.scale = this.scale;
// Skip the first change since it's being handled in the `afterNextRender` queued up in the
// constructor.
if (positionChange && !positionChange.firstChange && this.freeDragPosition) {
this._dragRef.setFreeDragPosition(this.freeDragPosition);
}
}
ngOnDestroy() {
if (this.dropContainer) {
this.dropContainer.removeItem(this);
}
this._dragDropRegistry.removeDirectiveNode(this.element.nativeElement);
// Unnecessary in most cases, but used to avoid extra change detections with `zone-paths-rxjs`.
this._ngZone.runOutsideAngular(() => {
this._handles.complete();
this._destroyed.next();
this._destroyed.complete();
this._dragRef.dispose();
});
}
_addHandle(handle: CdkDragHandle) {
const handles = this._handles.getValue();
handles.push(handle);
this._handles.next(handles);
}
_removeHandle(handle: CdkDragHandle) {
const handles = this._handles.getValue();
const index = handles.indexOf(handle);
if (index > -1) {
handles.splice(index, 1);
this._handles.next(handles);
}
}
_setPreviewTemplate(preview: CdkDragPreview) {
this._previewTemplate = preview;
}
_resetPreviewTemplate(preview: CdkDragPreview) {
if (preview === this._previewTemplate) {
this._previewTemplate = null;
}
}
_setPlaceholderTemplate(placeholder: CdkDragPlaceholder) {
this._placeholderTemplate = placeholder;
}
_resetPlaceholderTemplate(placeholder: CdkDragPlaceholder) {
if (placeholder === this._placeholderTemplate) {
this._placeholderTemplate = null;
}
}
/** Syncs the root element with the `DragRef`. */
private _updateRootElement() {
const element = this.element.nativeElement as HTMLElement;
let rootElement = element;
if (this.rootElementSelector) {
rootElement =
element.closest !== undefined
? (element.closest(this.rootElementSelector) as HTMLElement)
: // Comment tag doesn't have closest method, so use parent's one.
(element.parentElement?.closest(this.rootElementSelector) as HTMLElement);
}
if (rootElement && (typeof ngDevMode === 'undefined' || ngDevMode)) {
assertElementNode(rootElement, 'cdkDrag');
}
this._dragRef.withRootElement(rootElement || element);
}
/** Gets the boundary element, based on the `boundaryElement` value. */
private _getBoundaryElement() {
const boundary = this.boundaryElement;
if (!boundary) {
return null;
}
if (typeof boundary === 'string') {
return this.element.nativeElement.closest<HTMLElement>(boundary);
}
return coerceElement(boundary);
}
/** Syncs the inputs of the CdkDrag with the options of the underlying DragRef. */
private _syncInputs(ref: DragRef<CdkDrag<T>>) {
ref.beforeStarted.subscribe(() => {
if (!ref.isDragging()) {
const dir = this._dir;
const dragStartDelay = this.dragStartDelay;
const placeholder = this._placeholderTemplate
? {
template: this._placeholderTemplate.templateRef,
context: this._placeholderTemplate.data,
viewContainer: this._viewContainerRef,
}
: null;
const preview = this._previewTemplate
? {
template: this._previewTemplate.templateRef,
context: this._previewTemplate.data,
matchSize: this._previewTemplate.matchSize,
snapToCursor: this._previewTemplate.snapToCursor,
viewContainer: this._viewContainerRef,
}
: null;
ref.disabled = this.disabled;
ref.lockAxis = this.lockAxis;
ref.scale = this.scale;
ref.dragStartDelay =
typeof dragStartDelay === 'object' && dragStartDelay
? dragStartDelay
: coerceNumberProperty(dragStartDelay);
ref.constrainPosition = this.constrainPosition;
ref.previewClass = this.previewClass;
ref
.withBoundaryElement(this._getBoundaryElement())
.withPlaceholderTemplate(placeholder)
.withPreviewTemplate(preview)
.withPreviewContainer(this.previewContainer || 'global');
if (dir) {
ref.withDirection(dir.value);
}
}
});
// This only needs to be resolved once.
ref.beforeStarted.pipe(take(1)).subscribe(() => {
// If we managed to resolve a parent through DI, use it.
if (this._parentDrag) {
ref.withParent(this._parentDrag._dragRef);
return;
}
// Otherwise fall back to resolving the parent by looking up the DOM. This can happen if
// the item was projected into another item by something like `ngTemplateOutlet`.
let parent = this.element.nativeElement.parentElement;
while (parent) {
const parentDrag = this._dragDropRegistry.getDragDirectiveForNode(parent);
if (parentDrag) {
ref.withParent(parentDrag._dragRef);
break;
}
parent = parent.parentElement;
}
});
}
/** Handles the events from the underlying `DragRef`. */
private _handleEvents(ref: DragRef<CdkDrag<T>>) {
ref.started.subscribe(startEvent => {
this.started.emit({source: this, event: startEvent.event});
// Since all of these events run outside of change detection,
// we need to ensure that everything is marked correctly.
this._changeDetectorRef.markForCheck();
});
ref.released.subscribe(releaseEvent => {
this.released.emit({source: this, event: releaseEvent.event});
});
ref.ended.subscribe(endEvent => {
this.ended.emit({
source: this,
distance: endEvent.distance,
dropPoint: endEvent.dropPoint,
event: endEvent.event,
});
// Since all of these events run outside of change detection,
// we need to ensure that everything is marked correctly.
this._changeDetectorRef.markForCheck();
});
ref.entered.subscribe(enterEvent => {
this.entered.emit({
container: enterEvent.container.data,
item: this,
currentIndex: enterEvent.currentIndex,
});
});
ref.exited.subscribe(exitEvent => {
this.exited.emit({
container: exitEvent.container.data,
item: this,
});
});
ref.dropped.subscribe(dropEvent => {
this.dropped.emit({
previousIndex: dropEvent.previousIndex,
currentIndex: dropEvent.currentIndex,
previousContainer: dropEvent.previousContainer.data,
container: dropEvent.container.data,
isPointerOverContainer: dropEvent.isPointerOverContainer,
item: this,
distance: dropEvent.distance,
dropPoint: dropEvent.dropPoint,
event: dropEvent.event,
});
});
}
/** Assigns the default input values based on a provided config object. */
private _assignDefaults(config: DragDropConfig) {
const {
lockAxis,
dragStartDelay,
constrainPosition,
previewClass,
boundaryElement,
draggingDisabled,
rootElementSelector,
previewContainer,
} = config;
this.disabled = draggingDisabled == null ? false : draggingDisabled;
this.dragStartDelay = dragStartDelay || 0;
if (lockAxis) {
this.lockAxis = lockAxis;
}
if (constrainPosition) {
this.constrainPosition = constrainPosition;
}
if (previewClass) {
this.previewClass = previewClass;
}
if (boundaryElement) {
this.boundaryElement = boundaryElement;
}
if (rootElementSelector) {
this.rootElementSelector = rootElementSelector;
}
if (previewContainer) {
this.previewContainer = previewContainer;
}
}
/** Sets up the listener that syncs the handles with the drag ref. */
private _setupHandlesListener() {
// Listen for any newly-added handles.
this._handles
.pipe(
// Sync the new handles with the DragRef.
tap(handles => {
const handleElements = handles.map(handle => handle.element);
// Usually handles are only allowed to be a descendant of the drag element, but if
// the consumer defined a different drag root, we should allow the drag element
// itself to be a handle too.
if (this._selfHandle && this.rootElementSelector) {
handleElements.push(this.element);
}
this._dragRef.withHandles(handleElements);
}),
// Listen if the state of any of the handles changes.
switchMap((handles: CdkDragHandle[]) => {
return merge(
...handles.map(item => item._stateChanges.pipe(startWith(item))),
) as Observable<CdkDragHandle>;
}),
takeUntil(this._destroyed),
)
.subscribe(handleInstance => {
// Enabled/disable the handle that changed in the DragRef.
const dragRef = this._dragRef;
const handle = handleInstance.element.nativeElement;
handleInstance.disabled ? dragRef.disableHandle(handle) : dragRef.enableHandle(handle);
});
}
}