@@ -21,6 +21,7 @@ import {
2121 ChangeDetectorRef ,
2222 SkipSelf ,
2323 Inject ,
24+ AfterContentInit ,
2425} from '@angular/core' ;
2526import { DOCUMENT } from '@angular/common' ;
2627import { Directionality } from '@angular/cdk/bidi' ;
@@ -31,6 +32,9 @@ import {CDK_DROP_LIST_CONTAINER, CdkDropListContainer} from '../drop-list-contai
3132import { CdkDropListGroup } from './drop-list-group' ;
3233import { DropListRef } from '../drop-list-ref' ;
3334import { DragRef } from '../drag-ref' ;
35+ import { DragDrop } from '../drag-drop' ;
36+ import { Subject } from 'rxjs' ;
37+ import { startWith , takeUntil } from 'rxjs/operators' ;
3438
3539/** Counter used to generate unique ids for drop zones. */
3640let _uniqueIdCounter = 0 ;
@@ -62,15 +66,22 @@ export interface CdkDropListInternal extends CdkDropList {}
6266 '[class.cdk-drop-list-receiving]' : '_dropListRef.isReceiving()' ,
6367 }
6468} )
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+
6673 /** Keeps track of the drop lists that are currently on the page. */
6774 private static _dropLists : CdkDropList [ ] = [ ] ;
6875
6976 /** Reference to the underlying drop list instance. */
7077 _dropListRef : DropListRef < CdkDropList < T > > ;
7178
7279 /** 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 > ;
7485
7586 /**
7687 * 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 {
134145 sorted : EventEmitter < CdkDragSortEvent < T > > = new EventEmitter < CdkDragSortEvent < T > > ( ) ;
135146
136147 constructor (
148+ /** Element that the drop list is attached to. */
137149 public element : ElementRef < HTMLElement > ,
138150 dragDropRegistry : DragDropRegistry < DragRef , DropListRef > ,
139151 private _changeDetectorRef : ChangeDetectorRef ,
140- @Optional ( ) dir ?: Directionality ,
152+ @Optional ( ) private _dir ?: Directionality ,
141153 @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+ }
145169
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 > ) => {
151172 return this . enterPredicate ( drag . data , drop . data ) ;
152173 } ;
153- this . _syncInputs ( ref ) ;
154- this . _handleEvents ( ref ) ;
174+
175+ this . _syncInputs ( this . _dropListRef ) ;
176+ this . _handleEvents ( this . _dropListRef ) ;
155177 CdkDropList . _dropLists . push ( this ) ;
156178
157179 if ( _group ) {
158180 _group . _items . add ( this ) ;
159181 }
160182 }
161183
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+
162192 ngOnDestroy ( ) {
163193 const index = CdkDropList . _dropLists . indexOf ( this ) ;
164- this . _dropListRef . dispose ( ) ;
165194
166195 if ( index > - 1 ) {
167196 CdkDropList . _dropLists . splice ( index , 1 ) ;
@@ -170,6 +199,10 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
170199 if ( this . _group ) {
171200 this . _group . _items . delete ( this ) ;
172201 }
202+
203+ this . _dropListRef . dispose ( ) ;
204+ this . _destroyed . next ( ) ;
205+ this . _destroyed . complete ( ) ;
173206 }
174207
175208 /** Starts dragging an item. */
@@ -253,6 +286,12 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
253286
254287 /** Syncs the inputs of the CdkDropList with the options of the underlying DropListRef. */
255288 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+
256295 ref . beforeStarted . subscribe ( ( ) => {
257296 const siblings = coerceArray ( this . connectedTo ) . map ( drop => {
258297 return typeof drop === 'string' ?
@@ -270,8 +309,7 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
270309 ref . lockAxis = this . lockAxis ;
271310 ref
272311 . 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 ) ;
275313 } ) ;
276314 }
277315
0 commit comments