@@ -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,51 @@ 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 . _proxyEvents ( ref ) ;
174
+ this . _syncInputs ( this . _dropListRef ) ;
175
+ this . _proxyEvents ( this . _dropListRef ) ;
155
176
CdkDropList . _dropLists . push ( this ) ;
156
177
157
178
if ( _group ) {
158
179
_group . _items . add ( this ) ;
159
180
}
160
181
}
161
182
183
+ ngAfterContentInit ( ) {
184
+ this . _draggables . changes
185
+ . pipe ( startWith ( this . _draggables ) , takeUntil ( this . _destroyed ) )
186
+ . subscribe ( ( items : QueryList < CdkDrag > ) => {
187
+ this . _dropListRef . withItems ( items . map ( drag => drag . _dragRef ) ) ;
188
+ } ) ;
189
+ }
190
+
162
191
ngOnDestroy ( ) {
163
192
const index = CdkDropList . _dropLists . indexOf ( this ) ;
164
- this . _dropListRef . dispose ( ) ;
165
193
166
194
if ( index > - 1 ) {
167
195
CdkDropList . _dropLists . splice ( index , 1 ) ;
@@ -170,6 +198,10 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
170
198
if ( this . _group ) {
171
199
this . _group . _items . delete ( this ) ;
172
200
}
201
+
202
+ this . _dropListRef . dispose ( ) ;
203
+ this . _destroyed . next ( ) ;
204
+ this . _destroyed . complete ( ) ;
173
205
}
174
206
175
207
/** Starts dragging an item. */
@@ -253,6 +285,12 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
253
285
254
286
/** Syncs the inputs of the CdkDropList with the options of the underlying DropListRef. */
255
287
private _syncInputs ( ref : DropListRef < CdkDropList > ) {
288
+ if ( this . _dir ) {
289
+ this . _dir . change
290
+ . pipe ( startWith ( this . _dir . value ) , takeUntil ( this . _destroyed ) )
291
+ . subscribe ( value => ref . withDirection ( value ) ) ;
292
+ }
293
+
256
294
ref . beforeStarted . subscribe ( ( ) => {
257
295
const siblings = coerceArray ( this . connectedTo ) . map ( drop => {
258
296
return typeof drop === 'string' ?
@@ -270,8 +308,7 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
270
308
ref . lockAxis = this . lockAxis ;
271
309
ref
272
310
. connectedTo ( siblings . filter ( drop => drop && drop !== this ) . map ( list => list . _dropListRef ) )
273
- . withOrientation ( this . orientation )
274
- . withItems ( this . _draggables . map ( drag => drag . _dragRef ) ) ;
311
+ . withOrientation ( this . orientation ) ;
275
312
} ) ;
276
313
}
277
314
0 commit comments