@@ -21,6 +21,7 @@ import {
21
21
ChangeDetectorRef ,
22
22
SkipSelf ,
23
23
Inject ,
24
+ AfterContentInit ,
24
25
} from '@angular/core' ;
25
26
import { DOCUMENT } from '@angular/common' ;
26
27
import { Directionality } from '@angular/cdk/bidi' ;
@@ -31,6 +32,9 @@ import {CDK_DROP_LIST_CONTAINER, CdkDropListContainer} from '../drop-list-contai
31
32
import { CdkDropListGroup } from './drop-list-group' ;
32
33
import { DropListRef } from '../drop-list-ref' ;
33
34
import { DragRef } from '../drag-ref' ;
35
+ import { DragDrop } from '../drag-drop' ;
36
+ import { Subject } from 'rxjs' ;
37
+ import { startWith , takeUntil } from 'rxjs/operators' ;
34
38
35
39
/** Counter used to generate unique ids for drop zones. */
36
40
let _uniqueIdCounter = 0 ;
@@ -62,15 +66,22 @@ export interface CdkDropListInternal extends CdkDropList {}
62
66
'[class.cdk-drop-list-receiving]' : '_dropListRef.isReceiving()' ,
63
67
}
64
68
} )
65
- export class CdkDropList < T = any > implements CdkDropListContainer , OnDestroy {
69
+ export class CdkDropList < T = any > implements CdkDropListContainer , AfterContentInit , OnDestroy {
70
+ /** Emits when the list has been destroyed. */
71
+ private _destroyed = new Subject < void > ( ) ;
72
+
66
73
/** Keeps track of the drop lists that are currently on the page. */
67
74
private static _dropLists : CdkDropList [ ] = [ ] ;
68
75
69
76
/** Reference to the underlying drop list instance. */
70
77
_dropListRef : DropListRef < CdkDropList < T > > ;
71
78
72
79
/** Draggable items in the container. */
73
- @ContentChildren ( forwardRef ( ( ) => CdkDrag ) ) _draggables : QueryList < CdkDrag > ;
80
+ @ContentChildren ( forwardRef ( ( ) => CdkDrag ) , {
81
+ // Explicitly set to false since some of the logic below makes assumptions about it.
82
+ // The `.withItems` call below should be updated if we ever need to switch this to `true`.
83
+ descendants : false
84
+ } ) _draggables : QueryList < CdkDrag > ;
74
85
75
86
/**
76
87
* Other draggable containers that this container is connected to and into which the
@@ -134,34 +145,52 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
134
145
sorted : EventEmitter < CdkDragSortEvent < T > > = new EventEmitter < CdkDragSortEvent < T > > ( ) ;
135
146
136
147
constructor (
148
+ /** Element that the drop list is attached to. */
137
149
public element : ElementRef < HTMLElement > ,
138
150
dragDropRegistry : DragDropRegistry < DragRef , DropListRef > ,
139
151
private _changeDetectorRef : ChangeDetectorRef ,
140
- @Optional ( ) dir ?: Directionality ,
152
+ @Optional ( ) private _dir ?: Directionality ,
141
153
@Optional ( ) @SkipSelf ( ) private _group ?: CdkDropListGroup < CdkDropList > ,
142
- // @breaking -change 8.0.0 `_document` parameter to be made required.
143
- @Optional ( ) @Inject ( DOCUMENT ) _document ?: any ) {
144
-
154
+ @Optional ( ) @Inject ( DOCUMENT ) _document ?: any ,
155
+
156
+ /**
157
+ * @deprecated `dragDropRegistry` and `_document` parameters to be removed.
158
+ * Also `dragDrop` parameter to be made required.
159
+ * @breaking -change 8.0.0.
160
+ */
161
+ dragDrop ?: DragDrop ) {
162
+
163
+ // @breaking -change 8.0.0 Remove null check once `dragDrop` parameter is made required.
164
+ if ( dragDrop ) {
165
+ this . _dropListRef = dragDrop . createDropList ( element ) ;
166
+ } else {
167
+ this . _dropListRef = new DropListRef ( element , dragDropRegistry , _document || document ) ;
168
+ }
145
169
146
- // @breaking -change 8.0.0 Remove || once `_document` parameter is required.
147
- const ref = this . _dropListRef = new DropListRef ( element , dragDropRegistry ,
148
- _document || document , dir ) ;
149
- ref . data = this ;
150
- ref . enterPredicate = ( drag : DragRef < CdkDrag > , drop : DropListRef < CdkDropList > ) => {
170
+ this . _dropListRef . data = this ;
171
+ this . _dropListRef . enterPredicate = ( drag : DragRef < CdkDrag > , drop : DropListRef < CdkDropList > ) => {
151
172
return this . enterPredicate ( drag . data , drop . data ) ;
152
173
} ;
153
- this . _syncInputs ( ref ) ;
154
- this . _handleEvents ( ref ) ;
174
+
175
+ this . _syncInputs ( this . _dropListRef ) ;
176
+ this . _handleEvents ( this . _dropListRef ) ;
155
177
CdkDropList . _dropLists . push ( this ) ;
156
178
157
179
if ( _group ) {
158
180
_group . _items . add ( this ) ;
159
181
}
160
182
}
161
183
184
+ ngAfterContentInit ( ) {
185
+ this . _draggables . changes
186
+ . pipe ( startWith ( this . _draggables ) , takeUntil ( this . _destroyed ) )
187
+ . subscribe ( ( items : QueryList < CdkDrag > ) => {
188
+ this . _dropListRef . withItems ( items . map ( drag => drag . _dragRef ) ) ;
189
+ } ) ;
190
+ }
191
+
162
192
ngOnDestroy ( ) {
163
193
const index = CdkDropList . _dropLists . indexOf ( this ) ;
164
- this . _dropListRef . dispose ( ) ;
165
194
166
195
if ( index > - 1 ) {
167
196
CdkDropList . _dropLists . splice ( index , 1 ) ;
@@ -170,6 +199,10 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
170
199
if ( this . _group ) {
171
200
this . _group . _items . delete ( this ) ;
172
201
}
202
+
203
+ this . _dropListRef . dispose ( ) ;
204
+ this . _destroyed . next ( ) ;
205
+ this . _destroyed . complete ( ) ;
173
206
}
174
207
175
208
/** Starts dragging an item. */
@@ -253,6 +286,12 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
253
286
254
287
/** Syncs the inputs of the CdkDropList with the options of the underlying DropListRef. */
255
288
private _syncInputs ( ref : DropListRef < CdkDropList > ) {
289
+ if ( this . _dir ) {
290
+ this . _dir . change
291
+ . pipe ( startWith ( this . _dir . value ) , takeUntil ( this . _destroyed ) )
292
+ . subscribe ( value => ref . withDirection ( value ) ) ;
293
+ }
294
+
256
295
ref . beforeStarted . subscribe ( ( ) => {
257
296
const siblings = coerceArray ( this . connectedTo ) . map ( drop => {
258
297
return typeof drop === 'string' ?
@@ -270,8 +309,7 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
270
309
ref . lockAxis = this . lockAxis ;
271
310
ref
272
311
. connectedTo ( siblings . filter ( drop => drop && drop !== this ) . map ( list => list . _dropListRef ) )
273
- . withOrientation ( this . orientation )
274
- . withItems ( this . _draggables . map ( drag => drag . _dragRef ) ) ;
312
+ . withOrientation ( this . orientation ) ;
275
313
} ) ;
276
314
}
277
315
0 commit comments