@@ -28,6 +28,7 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
2828@Injectable ( { providedIn : 'root' } )
2929export class DragDropRegistry < I extends { isDragging ( ) : boolean } , C > implements OnDestroy {
3030 private _document : Document ;
31+ private _window : Window | null ;
3132
3233 /** Registered drop container instances. */
3334 private _dropInstances = new Set < C > ( ) ;
@@ -41,6 +42,10 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
4142 /** Keeps track of the event listeners that we've bound to the `document`. */
4243 private _globalListeners = new Map < string , {
4344 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 ,
4449 options ?: AddEventListenerOptions | boolean
4550 } > ( ) ;
4651
@@ -54,21 +59,25 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
5459 * Emits the `touchmove` or `mousemove` events that are dispatched
5560 * while the user is dragging a drag item instance.
5661 */
57- readonly pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
62+ pointerMove : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
5863
5964 /**
6065 * Emits the `touchend` or `mouseup` events that are dispatched
6166 * while the user is dragging a drag item instance.
6267 */
63- readonly pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
68+ pointerUp : Subject < TouchEvent | MouseEvent > = new Subject < TouchEvent | MouseEvent > ( ) ;
6469
6570 /** Emits when the viewport has been scrolled while the user is dragging an item. */
66- readonly scroll : Subject < Event > = new Subject < Event > ( ) ;
71+ scroll : Subject < Event > = new Subject < Event > ( ) ;
72+
73+ /** Emits when the page has been blurred while the user is dragging an item. */
74+ pageBlurred : Subject < void > = new Subject < void > ( ) ;
6775
6876 constructor (
6977 private _ngZone : NgZone ,
7078 @Inject ( DOCUMENT ) _document : any ) {
7179 this . _document = _document ;
80+ this . _window = ( typeof window !== 'undefined' && window . addEventListener ) ? window : null ;
7281 }
7382
7483 /** Adds a drop container to the registry. */
@@ -133,35 +142,45 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
133142 this . _globalListeners
134143 . set ( isTouchEvent ? 'touchend' : 'mouseup' , {
135144 handler : ( e : Event ) => this . pointerUp . next ( e as TouchEvent | MouseEvent ) ,
136- options : true
145+ options : true ,
146+ target : this . _document
137147 } )
138148 . set ( 'scroll' , {
139149 handler : ( e : Event ) => this . scroll . next ( e ) ,
140150 // Use capturing so that we pick up scroll changes in any scrollable nodes that aren't
141151 // the document. See https://github.com/angular/components/issues/17144.
142- options : true
152+ options : true ,
153+ target : this . _document
143154 } )
144155 // Preventing the default action on `mousemove` isn't enough to disable text selection
145156 // on Safari so we need to prevent the selection event as well. Alternatively this can
146157 // be done by setting `user-select: none` on the `body`, however it has causes a style
147158 // recalculation which can be expensive on pages with a lot of elements.
148159 . set ( 'selectstart' , {
149160 handler : this . _preventDefaultWhileDragging ,
150- options : activeCapturingEventOptions
161+ options : activeCapturingEventOptions ,
162+ target : this . _document
163+ } )
164+ . set ( 'blur' , {
165+ handler : ( ) => this . pageBlurred . next ( ) ,
166+ target : this . _window // Note that this event can only be bound on the window, not document
151167 } ) ;
152168
153169 // We don't have to bind a move event for touch drag sequences, because
154170 // we already have a persistent global one bound from `registerDragItem`.
155171 if ( ! isTouchEvent ) {
156172 this . _globalListeners . set ( 'mousemove' , {
157173 handler : ( e : Event ) => this . pointerMove . next ( e as MouseEvent ) ,
158- options : activeCapturingEventOptions
174+ options : activeCapturingEventOptions ,
175+ target : this . _document
159176 } ) ;
160177 }
161178
162179 this . _ngZone . runOutsideAngular ( ( ) => {
163180 this . _globalListeners . forEach ( ( config , name ) => {
164- this . _document . addEventListener ( name , config . handler , config . options ) ;
181+ if ( config . target ) {
182+ config . target . addEventListener ( name , config . handler , config . options ) ;
183+ }
165184 } ) ;
166185 } ) ;
167186 }
@@ -191,6 +210,7 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
191210 this . _clearGlobalListeners ( ) ;
192211 this . pointerMove . complete ( ) ;
193212 this . pointerUp . complete ( ) ;
213+ this . pageBlurred . complete ( ) ;
194214 }
195215
196216 /**
@@ -220,7 +240,9 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
220240 /** Clears out the global event listeners from the `document`. */
221241 private _clearGlobalListeners ( ) {
222242 this . _globalListeners . forEach ( ( config , name ) => {
223- this . _document . removeEventListener ( name , config . handler , config . options ) ;
243+ if ( config . target ) {
244+ config . target . removeEventListener ( name , config . handler , config . options ) ;
245+ }
224246 } ) ;
225247
226248 this . _globalListeners . clear ( ) ;
0 commit comments