55
66import { ConnectionPositionPair , Overlay , OverlayRef } from '@angular/cdk/overlay' ;
77import { TemplatePortal } from '@angular/cdk/portal' ;
8- import { Injectable } from '@angular/core' ;
9- import { fromEvent , merge , Subscription } from 'rxjs' ;
8+ import { Injectable , NgZone } from '@angular/core' ;
9+ import { fromEvent , Subscription } from 'rxjs' ;
1010import { filter , take } from 'rxjs/operators' ;
1111
1212import { NzContextMenuServiceModule } from './context-menu.service.module' ;
@@ -26,7 +26,7 @@ export class NzContextMenuService {
2626 private overlayRef : OverlayRef | null = null ;
2727 private closeSubscription = Subscription . EMPTY ;
2828
29- constructor ( private overlay : Overlay ) { }
29+ constructor ( private ngZone : NgZone , private overlay : Overlay ) { }
3030
3131 create ( $event : MouseEvent | { x : number ; y : number } , nzDropdownMenuComponent : NzDropdownMenuComponent ) : void {
3232 this . close ( true ) ;
@@ -44,17 +44,24 @@ export class NzContextMenuService {
4444 disposeOnNavigation : true ,
4545 scrollStrategy : this . overlay . scrollStrategies . close ( )
4646 } ) ;
47- this . closeSubscription = merge (
48- nzDropdownMenuComponent . descendantMenuItemClick$ ,
49- fromEvent < MouseEvent > ( document , 'click' ) . pipe (
50- filter ( event => ! ! this . overlayRef && ! this . overlayRef . overlayElement . contains ( event . target as HTMLElement ) ) ,
51- /** handle firefox contextmenu event **/
52- filter ( event => event . button !== 2 ) ,
53- take ( 1 )
47+
48+ this . closeSubscription = new Subscription ( ) ;
49+
50+ this . closeSubscription . add ( nzDropdownMenuComponent . descendantMenuItemClick$ . subscribe ( ( ) => this . close ( ) ) ) ;
51+
52+ this . closeSubscription . add (
53+ this . ngZone . runOutsideAngular ( ( ) =>
54+ fromEvent < MouseEvent > ( document , 'click' )
55+ . pipe (
56+ filter ( event => ! ! this . overlayRef && ! this . overlayRef . overlayElement . contains ( event . target as HTMLElement ) ) ,
57+ /** handle firefox contextmenu event **/
58+ filter ( event => event . button !== 2 ) ,
59+ take ( 1 )
60+ )
61+ . subscribe ( ( ) => this . ngZone . run ( ( ) => this . close ( ) ) )
5462 )
55- ) . subscribe ( ( ) => {
56- this . close ( ) ;
57- } ) ;
63+ ) ;
64+
5865 this . overlayRef . attach (
5966 new TemplatePortal ( nzDropdownMenuComponent . templateRef , nzDropdownMenuComponent . viewContainerRef )
6067 ) ;
0 commit comments