@@ -48,8 +48,7 @@ import type {CdkDropList} from './drop-list';
48
48
import { DragDrop } from '../drag-drop' ;
49
49
import { CDK_DRAG_CONFIG , DragDropConfig , DragStartDelay , DragAxis } from './config' ;
50
50
import { assertElementNode } from './assertions' ;
51
-
52
- const DRAG_HOST_CLASS = 'cdk-drag' ;
51
+ import { DragDropRegistry } from '../drag-drop-registry' ;
53
52
54
53
/**
55
54
* Injection token that can be used to reference instances of `CdkDropList`. It serves as
@@ -63,7 +62,7 @@ export const CDK_DROP_LIST = new InjectionToken<CdkDropList>('CdkDropList');
63
62
selector : '[cdkDrag]' ,
64
63
exportAs : 'cdkDrag' ,
65
64
host : {
66
- 'class' : DRAG_HOST_CLASS ,
65
+ 'class' : 'cdk-drag' ,
67
66
'[class.cdk-drag-disabled]' : 'disabled' ,
68
67
'[class.cdk-drag-dragging]' : '_dragRef.isDragging()' ,
69
68
} ,
@@ -78,9 +77,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
78
77
private _changeDetectorRef = inject ( ChangeDetectorRef ) ;
79
78
private _selfHandle = inject < CdkDragHandle > ( CDK_DRAG_HANDLE , { optional : true , self : true } ) ;
80
79
private _parentDrag = inject < CdkDrag > ( CDK_DRAG_PARENT , { optional : true , skipSelf : true } ) ;
80
+ private _dragDropRegistry = inject ( DragDropRegistry ) ;
81
81
82
82
private readonly _destroyed = new Subject < void > ( ) ;
83
- private static _dragInstances : CdkDrag [ ] = [ ] ;
84
83
private _handles = new BehaviorSubject < CdkDragHandle [ ] > ( [ ] ) ;
85
84
private _previewTemplate : CdkDragPreview | null ;
86
85
private _placeholderTemplate : CdkDragPlaceholder | null ;
@@ -240,11 +239,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
240
239
zIndex : config ?. zIndex ,
241
240
} ) ;
242
241
this . _dragRef . data = this ;
243
-
244
- // We have to keep track of the drag instances in order to be able to match an element to
245
- // a drag instance. We can't go through the global registry of `DragRef`, because the root
246
- // element could be different.
247
- CdkDrag . _dragInstances . push ( this ) ;
242
+ this . _dragDropRegistry . registerDirectiveNode ( this . element . nativeElement , this ) ;
248
243
249
244
if ( config ) {
250
245
this . _assignDefaults ( config ) ;
@@ -348,10 +343,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
348
343
this . dropContainer . removeItem ( this ) ;
349
344
}
350
345
351
- const index = CdkDrag . _dragInstances . indexOf ( this ) ;
352
- if ( index > - 1 ) {
353
- CdkDrag . _dragInstances . splice ( index , 1 ) ;
354
- }
346
+ this . _dragDropRegistry . removeDirectiveNode ( this . element . nativeElement ) ;
355
347
356
348
// Unnecessary in most cases, but used to avoid extra change detections with `zone-paths-rxjs`.
357
349
this . _ngZone . runOutsideAngular ( ( ) => {
@@ -487,12 +479,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
487
479
// the item was projected into another item by something like `ngTemplateOutlet`.
488
480
let parent = this . element . nativeElement . parentElement ;
489
481
while ( parent ) {
490
- if ( parent . classList . contains ( DRAG_HOST_CLASS ) ) {
491
- ref . withParent (
492
- CdkDrag . _dragInstances . find ( drag => {
493
- return drag . element . nativeElement === parent ;
494
- } ) ?. _dragRef || null ,
495
- ) ;
482
+ const parentDrag = this . _dragDropRegistry . getDragDirectiveForNode ( parent ) ;
483
+ if ( parentDrag ) {
484
+ ref . withParent ( parentDrag . _dragRef ) ;
496
485
break ;
497
486
}
498
487
parent = parent . parentElement ;
0 commit comments