@@ -10,8 +10,6 @@ import {Injectable, NgZone, OnDestroy, Inject} from '@angular/core';
10
10
import { DOCUMENT } from '@angular/common' ;
11
11
import { supportsPassiveEventListeners } from '@angular/cdk/platform' ;
12
12
import { Subject } from 'rxjs' ;
13
- import { CdkDrop } from './drop' ;
14
- import { CdkDrag } from './drag' ;
15
13
16
14
/** Event options that can be used to bind an active event. */
17
15
const activeEventOptions = supportsPassiveEventListeners ( ) ? { passive : false } : false ;
@@ -20,35 +18,38 @@ const activeEventOptions = supportsPassiveEventListeners() ? {passive: false} :
20
18
type PointerEventHandler = ( event : TouchEvent | MouseEvent ) => void ;
21
19
22
20
/**
23
- * Service that keeps track of all the `CdkDrag` and `CdkDrop` instances, and
24
- * manages global event listeners on the `document`.
21
+ * Service that keeps track of all the drag item and drop container
22
+ * instances, and manages global event listeners on the `document`.
25
23
* @docs -private
26
24
*/
25
+ // Note: this class is generic, rather than referencing CdkDrag and CdkDrop directly, in order to
26
+ // avoid circular imports. If we were to reference them here, importing the registry into the
27
+ // classes that are registering themselves will introduce a circular import.
27
28
@Injectable ( { providedIn : 'root' } )
28
- export class CdkDragDropRegistry implements OnDestroy {
29
+ export class DragDropRegistry < I , C extends { id : string } > implements OnDestroy {
29
30
private _document : Document ;
30
31
31
- /** Registered `CdkDrop` instances. */
32
- private _dropInstances = new Set < CdkDrop > ( ) ;
32
+ /** Registered drop container instances. */
33
+ private _dropInstances = new Set < C > ( ) ;
33
34
34
- /** Registered `CdkDrag` instances. */
35
- private _dragInstances = new Set < CdkDrag > ( ) ;
35
+ /** Registered drag item instances. */
36
+ private _dragInstances = new Set < I > ( ) ;
36
37
37
- /** `CdkDrag` instances that are currently being dragged. */
38
- private _activeDragInstances = new Set < CdkDrag > ( ) ;
38
+ /** Drag item instances that are currently being dragged. */
39
+ private _activeDragInstances = new Set < I > ( ) ;
39
40
40
41
/** Keeps track of the event listeners that we've bound to the `document`. */
41
42
private _globalListeners = new Map < string , { handler : PointerEventHandler , options ?: any } > ( ) ;
42
43
43
44
/**
44
45
* Emits the `touchmove` or `mousemove` events that are dispatched
45
- * while the user is dragging a `CdkDrag` instance.
46
+ * while the user is dragging a drag item instance.
46
47
*/
47
48
readonly pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
48
49
49
50
/**
50
51
* Emits the `touchend` or `mouseup` events that are dispatched
51
- * while the user is dragging a `CdkDrag` instance.
52
+ * while the user is dragging a drag item instance.
52
53
*/
53
54
readonly pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
54
55
@@ -58,52 +59,44 @@ export class CdkDragDropRegistry implements OnDestroy {
58
59
this . _document = _document ;
59
60
}
60
61
61
- /** Adds a `CdkDrop` instance to the registry. */
62
- register ( drop : CdkDrop ) ;
63
-
64
- /** Adds a `CdkDrag` instance to the registry. */
65
- register ( drag : CdkDrag ) ;
66
-
67
- register ( instance : CdkDrop | CdkDrag ) {
68
- if ( instance instanceof CdkDrop ) {
69
- if ( ! this . _dropInstances . has ( instance ) ) {
70
- if ( this . getDropContainer ( instance . id ) ) {
71
- throw Error ( `Drop instance with id "${ instance . id } " has already been registered.` ) ;
72
- }
73
-
74
- this . _dropInstances . add ( instance ) ;
75
- }
76
- } else {
77
- this . _dragInstances . add ( instance ) ;
78
-
79
- if ( this . _dragInstances . size === 1 ) {
80
- this . _ngZone . runOutsideAngular ( ( ) => {
81
- // The event handler has to be explicitly active, because
82
- // newer browsers make it passive by default.
83
- this . _document . addEventListener ( 'touchmove' , this . _preventScrollListener ,
84
- activeEventOptions ) ;
85
- } ) ;
62
+ /** Adds a drop container to the registry. */
63
+ registerDropContainer ( drop : C ) {
64
+ if ( ! this . _dropInstances . has ( drop ) ) {
65
+ if ( this . getDropContainer ( drop . id ) ) {
66
+ throw Error ( `Drop instance with id "${ drop . id } " has already been registered.` ) ;
86
67
}
68
+
69
+ this . _dropInstances . add ( drop ) ;
87
70
}
88
71
}
89
72
90
- /** Removes a `CdkDrop` instance from the registry. */
91
- remove ( drop : CdkDrop ) ;
73
+ /** Adds a drag item instance to the registry. */
74
+ registerDragItem ( drag : I ) {
75
+ this . _dragInstances . add ( drag ) ;
76
+
77
+ if ( this . _dragInstances . size === 1 ) {
78
+ this . _ngZone . runOutsideAngular ( ( ) => {
79
+ // The event handler has to be explicitly active, because
80
+ // newer browsers make it passive by default.
81
+ this . _document . addEventListener ( 'touchmove' , this . _preventScrollListener ,
82
+ activeEventOptions ) ;
83
+ } ) ;
84
+ }
85
+ }
92
86
93
- /** Removes a `CdkDrag` instance from the registry. */
94
- remove ( drag : CdkDrag ) ;
87
+ /** Removes a drop container from the registry. */
88
+ removeDropContainer ( drop : C ) {
89
+ this . _dropInstances . delete ( drop ) ;
90
+ }
95
91
96
- remove ( instance : CdkDrop | CdkDrag ) {
97
- if ( instance instanceof CdkDrop ) {
98
- this . _dropInstances . delete ( instance ) ;
99
- } else {
100
- this . _dragInstances . delete ( instance ) ;
101
- this . stopDragging ( instance ) ;
92
+ /** Removes a drag item instance from the registry. */
93
+ removeDragItem ( drag : I ) {
94
+ this . _dragInstances . delete ( drag ) ;
95
+ this . stopDragging ( drag ) ;
102
96
103
- if ( this . _dragInstances . size === 0 ) {
104
- this . _document . removeEventListener ( 'touchmove' , this . _preventScrollListener ,
105
- activeEventOptions as any ) ;
106
- }
97
+ if ( this . _dragInstances . size === 0 ) {
98
+ this . _document . removeEventListener ( 'touchmove' , this . _preventScrollListener ,
99
+ activeEventOptions as any ) ;
107
100
}
108
101
}
109
102
@@ -112,7 +105,7 @@ export class CdkDragDropRegistry implements OnDestroy {
112
105
* @param drag Drag instance which is being dragged.
113
106
* @param event Event that initiated the dragging.
114
107
*/
115
- startDragging ( drag : CdkDrag , event : TouchEvent | MouseEvent ) {
108
+ startDragging ( drag : I , event : TouchEvent | MouseEvent ) {
116
109
this . _activeDragInstances . add ( drag ) ;
117
110
118
111
if ( this . _activeDragInstances . size === 1 ) {
@@ -134,28 +127,28 @@ export class CdkDragDropRegistry implements OnDestroy {
134
127
}
135
128
}
136
129
137
- /** Stops dragging a `CdkDrag` instance. */
138
- stopDragging ( drag : CdkDrag ) {
130
+ /** Stops dragging a drag item instance. */
131
+ stopDragging ( drag : I ) {
139
132
this . _activeDragInstances . delete ( drag ) ;
140
133
141
134
if ( this . _activeDragInstances . size === 0 ) {
142
135
this . _clearGlobalListeners ( ) ;
143
136
}
144
137
}
145
138
146
- /** Gets whether a `CdkDrag` instance is currently being dragged. */
147
- isDragging ( drag : CdkDrag ) {
139
+ /** Gets whether a drag item instance is currently being dragged. */
140
+ isDragging ( drag : I ) {
148
141
return this . _activeDragInstances . has ( drag ) ;
149
142
}
150
143
151
- /** Gets a `CdkDrop` instance by its id. */
152
- getDropContainer < T = any > ( id : string ) : CdkDrop < T > | undefined {
144
+ /** Gets a drop container by its id. */
145
+ getDropContainer ( id : string ) : C | undefined {
153
146
return Array . from ( this . _dropInstances ) . find ( instance => instance . id === id ) ;
154
147
}
155
148
156
149
ngOnDestroy ( ) {
157
- this . _dragInstances . forEach ( instance => this . remove ( instance ) ) ;
158
- this . _dropInstances . forEach ( instance => this . remove ( instance ) ) ;
150
+ this . _dragInstances . forEach ( instance => this . removeDragItem ( instance ) ) ;
151
+ this . _dropInstances . forEach ( instance => this . removeDropContainer ( instance ) ) ;
159
152
this . _clearGlobalListeners ( ) ;
160
153
this . pointerMove . complete ( ) ;
161
154
this . pointerUp . complete ( ) ;
0 commit comments