@@ -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,35 @@ 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
*/
27
25
@Injectable ( { providedIn : 'root' } )
28
- export class CdkDragDropRegistry implements OnDestroy {
26
+ export class CdkDragDropRegistry < I , C extends { id : string } > implements OnDestroy {
29
27
private _document : Document ;
30
28
31
- /** Registered `CdkDrop` instances. */
32
- private _dropInstances = new Set < CdkDrop > ( ) ;
29
+ /** Registered drop container instances. */
30
+ private _dropInstances = new Set < C > ( ) ;
33
31
34
- /** Registered `CdkDrag` instances. */
35
- private _dragInstances = new Set < CdkDrag > ( ) ;
32
+ /** Registered drag item instances. */
33
+ private _dragInstances = new Set < I > ( ) ;
36
34
37
- /** `CdkDrag` instances that are currently being dragged. */
38
- private _activeDragInstances = new Set < CdkDrag > ( ) ;
35
+ /** Drag item instances that are currently being dragged. */
36
+ private _activeDragInstances = new Set < I > ( ) ;
39
37
40
38
/** Keeps track of the event listeners that we've bound to the `document`. */
41
39
private _globalListeners = new Map < string , { handler : PointerEventHandler , options ?: any } > ( ) ;
42
40
43
41
/**
44
42
* Emits the `touchmove` or `mousemove` events that are dispatched
45
- * while the user is dragging a `CdkDrag` instance.
43
+ * while the user is dragging a drag item instance.
46
44
*/
47
45
readonly pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
48
46
49
47
/**
50
48
* Emits the `touchend` or `mouseup` events that are dispatched
51
- * while the user is dragging a `CdkDrag` instance.
49
+ * while the user is dragging a drag item instance.
52
50
*/
53
51
readonly pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
54
52
@@ -58,52 +56,44 @@ export class CdkDragDropRegistry implements OnDestroy {
58
56
this . _document = _document ;
59
57
}
60
58
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
- } ) ;
59
+ /** Adds a drop container to the registry. */
60
+ registerDropContainer ( drop : C ) {
61
+ if ( ! this . _dropInstances . has ( drop ) ) {
62
+ if ( this . getDropContainer ( drop . id ) ) {
63
+ throw Error ( `Drop instance with id "${ drop . id } " has already been registered.` ) ;
86
64
}
65
+
66
+ this . _dropInstances . add ( drop ) ;
87
67
}
88
68
}
89
69
90
- /** Removes a `CdkDrop` instance from the registry. */
91
- remove ( drop : CdkDrop ) ;
70
+ /** Adds a drag item instance to the registry. */
71
+ registerDragItem ( drag : I ) {
72
+ this . _dragInstances . add ( drag ) ;
73
+
74
+ if ( this . _dragInstances . size === 1 ) {
75
+ this . _ngZone . runOutsideAngular ( ( ) => {
76
+ // The event handler has to be explicitly active, because
77
+ // newer browsers make it passive by default.
78
+ this . _document . addEventListener ( 'touchmove' , this . _preventScrollListener ,
79
+ activeEventOptions ) ;
80
+ } ) ;
81
+ }
82
+ }
92
83
93
- /** Removes a `CdkDrag` instance from the registry. */
94
- remove ( drag : CdkDrag ) ;
84
+ /** Removes a drop container from the registry. */
85
+ removeDropContainer ( drop : C ) {
86
+ this . _dropInstances . delete ( drop ) ;
87
+ }
95
88
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 ) ;
89
+ /** Removes a drag item instance from the registry. */
90
+ removeDragItem ( drag : I ) {
91
+ this . _dragInstances . delete ( drag ) ;
92
+ this . stopDragging ( drag ) ;
102
93
103
- if ( this . _dragInstances . size === 0 ) {
104
- this . _document . removeEventListener ( 'touchmove' , this . _preventScrollListener ,
105
- activeEventOptions as any ) ;
106
- }
94
+ if ( this . _dragInstances . size === 0 ) {
95
+ this . _document . removeEventListener ( 'touchmove' , this . _preventScrollListener ,
96
+ activeEventOptions as any ) ;
107
97
}
108
98
}
109
99
@@ -112,7 +102,7 @@ export class CdkDragDropRegistry implements OnDestroy {
112
102
* @param drag Drag instance which is being dragged.
113
103
* @param event Event that initiated the dragging.
114
104
*/
115
- startDragging ( drag : CdkDrag , event : TouchEvent | MouseEvent ) {
105
+ startDragging ( drag : I , event : TouchEvent | MouseEvent ) {
116
106
this . _activeDragInstances . add ( drag ) ;
117
107
118
108
if ( this . _activeDragInstances . size === 1 ) {
@@ -134,28 +124,28 @@ export class CdkDragDropRegistry implements OnDestroy {
134
124
}
135
125
}
136
126
137
- /** Stops dragging a `CdkDrag` instance. */
138
- stopDragging ( drag : CdkDrag ) {
127
+ /** Stops dragging a drag item instance. */
128
+ stopDragging ( drag : I ) {
139
129
this . _activeDragInstances . delete ( drag ) ;
140
130
141
131
if ( this . _activeDragInstances . size === 0 ) {
142
132
this . _clearGlobalListeners ( ) ;
143
133
}
144
134
}
145
135
146
- /** Gets whether a `CdkDrag` instance is currently being dragged. */
147
- isDragging ( drag : CdkDrag ) {
136
+ /** Gets whether a drag item instance is currently being dragged. */
137
+ isDragging ( drag : I ) {
148
138
return this . _activeDragInstances . has ( drag ) ;
149
139
}
150
140
151
- /** Gets a `CdkDrop` instance by its id. */
152
- getDropContainer < T = any > ( id : string ) : CdkDrop < T > | undefined {
141
+ /** Gets a drop container by its id. */
142
+ getDropContainer ( id : string ) : C | undefined {
153
143
return Array . from ( this . _dropInstances ) . find ( instance => instance . id === id ) ;
154
144
}
155
145
156
146
ngOnDestroy ( ) {
157
- this . _dragInstances . forEach ( instance => this . remove ( instance ) ) ;
158
- this . _dropInstances . forEach ( instance => this . remove ( instance ) ) ;
147
+ this . _dragInstances . forEach ( instance => this . removeDragItem ( instance ) ) ;
148
+ this . _dropInstances . forEach ( instance => this . removeDropContainer ( instance ) ) ;
159
149
this . _clearGlobalListeners ( ) ;
160
150
this . pointerMove . complete ( ) ;
161
151
this . pointerUp . complete ( ) ;
0 commit comments