@@ -28,6 +28,7 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
28
28
@Injectable ( { providedIn : 'root' } )
29
29
export class DragDropRegistry < I , C > implements OnDestroy {
30
30
private _document : Document ;
31
+ private _window : Window | null ;
31
32
32
33
/** Registered drop container instances. */
33
34
private _dropInstances = new Set < C > ( ) ;
@@ -41,28 +42,36 @@ export class DragDropRegistry<I, C> implements OnDestroy {
41
42
/** Keeps track of the event listeners that we've bound to the `document`. */
42
43
private _globalListeners = new Map < string , {
43
44
handler : ( event : Event ) => void ,
45
+ // The target needs to be `| null` because we bind either to `window` or `document` which
46
+ // aren't available during SSR. There's an injection token for the document, but not one for
47
+ // window so we fall back to not binding events to it.
48
+ target : EventTarget | null ,
44
49
options ?: AddEventListenerOptions | boolean
45
50
} > ( ) ;
46
51
47
52
/**
48
53
* Emits the `touchmove` or `mousemove` events that are dispatched
49
54
* while the user is dragging a drag item instance.
50
55
*/
51
- readonly pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
56
+ pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
52
57
53
58
/**
54
59
* Emits the `touchend` or `mouseup` events that are dispatched
55
60
* while the user is dragging a drag item instance.
56
61
*/
57
- readonly pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
62
+ pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
58
63
59
64
/** Emits when the viewport has been scrolled while the user is dragging an item. */
60
- readonly scroll : Subject < Event > = new Subject < Event > ( ) ;
65
+ scroll : Subject < Event > = new Subject < Event > ( ) ;
66
+
67
+ /** Emits when the page has been blurred while the user is dragging an item. */
68
+ pageBlurred : Subject < void > = new Subject < void > ( ) ;
61
69
62
70
constructor (
63
71
private _ngZone : NgZone ,
64
72
@Inject ( DOCUMENT ) _document : any ) {
65
73
this . _document = _document ;
74
+ this . _window = ( typeof window !== 'undefined' && window . addEventListener ) ? window : null ;
66
75
}
67
76
68
77
/** Adds a drop container to the registry. */
@@ -129,30 +138,40 @@ export class DragDropRegistry<I, C> implements OnDestroy {
129
138
this . _globalListeners
130
139
. set ( moveEvent , {
131
140
handler : ( e : Event ) => this . pointerMove . next ( e as TouchEvent | MouseEvent ) ,
132
- options : activeCapturingEventOptions
141
+ options : activeCapturingEventOptions ,
142
+ target : this . _document
133
143
} )
134
144
. set ( upEvent , {
135
145
handler : ( e : Event ) => this . pointerUp . next ( e as TouchEvent | MouseEvent ) ,
136
- options : true
146
+ options : true ,
147
+ target : this . _document
137
148
} )
138
149
. set ( 'scroll' , {
139
150
handler : ( e : Event ) => this . scroll . next ( e ) ,
140
151
// Use capturing so that we pick up scroll changes in any scrollable nodes that aren't
141
152
// the document. See https://github.com/angular/components/issues/17144.
142
- options : true
153
+ options : true ,
154
+ target : this . _document
143
155
} )
144
156
// Preventing the default action on `mousemove` isn't enough to disable text selection
145
157
// on Safari so we need to prevent the selection event as well. Alternatively this can
146
158
// be done by setting `user-select: none` on the `body`, however it has causes a style
147
159
// recalculation which can be expensive on pages with a lot of elements.
148
160
. set ( 'selectstart' , {
149
161
handler : this . _preventDefaultWhileDragging ,
150
- options : activeCapturingEventOptions
162
+ options : activeCapturingEventOptions ,
163
+ target : this . _document
164
+ } )
165
+ . set ( 'blur' , {
166
+ handler : ( ) => this . pageBlurred . next ( ) ,
167
+ target : this . _window // Note that this event can only be bound on the window, not document
151
168
} ) ;
152
169
153
170
this . _ngZone . runOutsideAngular ( ( ) => {
154
171
this . _globalListeners . forEach ( ( config , name ) => {
155
- this . _document . addEventListener ( name , config . handler , config . options ) ;
172
+ if ( config . target ) {
173
+ config . target . addEventListener ( name , config . handler , config . options ) ;
174
+ }
156
175
} ) ;
157
176
} ) ;
158
177
}
@@ -178,6 +197,7 @@ export class DragDropRegistry<I, C> implements OnDestroy {
178
197
this . _clearGlobalListeners ( ) ;
179
198
this . pointerMove . complete ( ) ;
180
199
this . pointerUp . complete ( ) ;
200
+ this . pageBlurred . complete ( ) ;
181
201
}
182
202
183
203
/**
@@ -193,7 +213,9 @@ export class DragDropRegistry<I, C> implements OnDestroy {
193
213
/** Clears out the global event listeners from the `document`. */
194
214
private _clearGlobalListeners ( ) {
195
215
this . _globalListeners . forEach ( ( config , name ) => {
196
- this . _document . removeEventListener ( name , config . handler , config . options ) ;
216
+ if ( config . target ) {
217
+ config . target . removeEventListener ( name , config . handler , config . options ) ;
218
+ }
197
219
} ) ;
198
220
199
221
this . _globalListeners . clear ( ) ;
0 commit comments