@@ -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 ;
@@ -61,15 +65,22 @@ export interface CdkDropListInternal extends CdkDropList {}
61
65
'[class.cdk-drop-list-dragging]' : '_dropListRef.isDragging()'
62
66
}
63
67
} )
64
- export class CdkDropList < T = any > implements CdkDropListContainer , OnDestroy {
68
+ export class CdkDropList < T = any > implements CdkDropListContainer , AfterContentInit , OnDestroy {
69
+ /** Emits when the list has been destroyed. */
70
+ private _destroyed = new Subject < void > ( ) ;
71
+
65
72
/** Keeps track of the drop lists that are currently on the page. */
66
73
private static _dropLists : CdkDropList [ ] = [ ] ;
67
74
68
75
/** Reference to the underlying drop list instance. */
69
76
_dropListRef : DropListRef < CdkDropList < T > > ;
70
77
71
78
/** Draggable items in the container. */
72
- @ContentChildren ( forwardRef ( ( ) => CdkDrag ) ) _draggables : QueryList < CdkDrag > ;
79
+ @ContentChildren ( forwardRef ( ( ) => CdkDrag ) , {
80
+ // Explicitly set to false since some of the logic below makes assumptions about it.
81
+ // The `.withItems` call below should be updated if we ever need to switch this to `true`.
82
+ descendants : false
83
+ } ) _draggables : QueryList < CdkDrag > ;
73
84
74
85
/**
75
86
* Other draggable containers that this container is connected to and into which the
@@ -130,34 +141,51 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
130
141
sorted : EventEmitter < CdkDragSortEvent < T > > = new EventEmitter < CdkDragSortEvent < T > > ( ) ;
131
142
132
143
constructor (
144
+ /** Element that the drop list is attached to. */
133
145
public element : ElementRef < HTMLElement > ,
134
146
dragDropRegistry : DragDropRegistry < DragRef , DropListRef > ,
135
147
private _changeDetectorRef : ChangeDetectorRef ,
136
- @Optional ( ) dir ?: Directionality ,
148
+ @Optional ( ) private _dir ?: Directionality ,
137
149
@Optional ( ) @SkipSelf ( ) private _group ?: CdkDropListGroup < CdkDropList > ,
138
- // @breaking -change 8.0.0 `_document` parameter to be made required.
139
- @Optional ( ) @Inject ( DOCUMENT ) _document ?: any ) {
140
-
150
+ @Optional ( ) @Inject ( DOCUMENT ) _document ?: any ,
151
+
152
+ /**
153
+ * @deprecated `dragDropRegistry` and `_document` parameters to be removed.
154
+ * Also `dragDrop` parameter to be made required.
155
+ * @breaking -change 8.0.0.
156
+ */
157
+ dragDrop ?: DragDrop ) {
158
+
159
+ // @breaking -change 8.0.0 Remove null check once `dragDrop` parameter is made required.
160
+ if ( dragDrop ) {
161
+ this . _dropListRef = dragDrop . createDropList ( element ) ;
162
+ } else {
163
+ this . _dropListRef = new DropListRef ( element , dragDropRegistry , _document || document ) ;
164
+ }
141
165
142
- // @breaking -change 8.0.0 Remove || once `_document` parameter is required.
143
- const ref = this . _dropListRef = new DropListRef ( element , dragDropRegistry ,
144
- _document || document , dir ) ;
145
- ref . data = this ;
146
- ref . enterPredicate = ( drag : DragRef < CdkDrag > , drop : DropListRef < CdkDropList > ) => {
166
+ this . _dropListRef . data = this ;
167
+ this . _dropListRef . enterPredicate = ( drag : DragRef < CdkDrag > , drop : DropListRef < CdkDropList > ) => {
147
168
return this . enterPredicate ( drag . data , drop . data ) ;
148
169
} ;
149
- this . _syncInputs ( ref ) ;
150
- this . _proxyEvents ( ref ) ;
170
+ this . _syncInputs ( this . _dropListRef ) ;
171
+ this . _proxyEvents ( this . _dropListRef ) ;
151
172
CdkDropList . _dropLists . push ( this ) ;
152
173
153
174
if ( _group ) {
154
175
_group . _items . add ( this ) ;
155
176
}
156
177
}
157
178
179
+ ngAfterContentInit ( ) {
180
+ this . _draggables . changes
181
+ . pipe ( startWith ( this . _draggables ) , takeUntil ( this . _destroyed ) )
182
+ . subscribe ( ( items : QueryList < CdkDrag > ) => {
183
+ this . _dropListRef . withItems ( items . map ( drag => drag . _dragRef ) ) ;
184
+ } ) ;
185
+ }
186
+
158
187
ngOnDestroy ( ) {
159
188
const index = CdkDropList . _dropLists . indexOf ( this ) ;
160
- this . _dropListRef . dispose ( ) ;
161
189
162
190
if ( index > - 1 ) {
163
191
CdkDropList . _dropLists . splice ( index , 1 ) ;
@@ -166,6 +194,10 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
166
194
if ( this . _group ) {
167
195
this . _group . _items . delete ( this ) ;
168
196
}
197
+
198
+ this . _dropListRef . dispose ( ) ;
199
+ this . _destroyed . next ( ) ;
200
+ this . _destroyed . complete ( ) ;
169
201
}
170
202
171
203
/** Starts dragging an item. */
@@ -249,6 +281,12 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
249
281
250
282
/** Syncs the inputs of the CdkDropList with the options of the underlying DropListRef. */
251
283
private _syncInputs ( ref : DropListRef < CdkDropList > ) {
284
+ if ( this . _dir ) {
285
+ this . _dir . change
286
+ . pipe ( startWith ( this . _dir . value ) , takeUntil ( this . _destroyed ) )
287
+ . subscribe ( value => ref . withDirection ( value ) ) ;
288
+ }
289
+
252
290
ref . beforeStarted . subscribe ( ( ) => {
253
291
const siblings = coerceArray ( this . connectedTo ) . map ( drop => {
254
292
return typeof drop === 'string' ?
@@ -266,8 +304,7 @@ export class CdkDropList<T = any> implements CdkDropListContainer, OnDestroy {
266
304
ref . lockAxis = this . lockAxis ;
267
305
ref
268
306
. connectedTo ( siblings . filter ( drop => drop && drop !== this ) . map ( list => list . _dropListRef ) )
269
- . withOrientation ( this . orientation )
270
- . withItems ( this . _draggables . map ( drag => drag . _dragRef ) ) ;
307
+ . withOrientation ( this . orientation ) ;
271
308
} ) ;
272
309
}
273
310
0 commit comments