@@ -17,6 +17,7 @@ import { DialogService } from "./dialog.service";
1717import { CloseMeta , CloseReasons , DialogConfig } from "./dialog-config.interface" ;
1818import { EventService } from "carbon-components-angular/utils" ;
1919import { Dialog } from "./dialog.component" ;
20+ import { fromEvent , Subscription } from "rxjs" ;
2021
2122/**
2223 * A generic directive that can be inherited from to create dialogs (for example, a tooltip or popover)
@@ -125,6 +126,8 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
125126 */
126127 protected dialogRef : ComponentRef < Dialog > ;
127128
129+ private subscriptions : Subscription [ ] = [ ] ;
130+
128131 /**
129132 * Creates an instance of DialogDirective.
130133 * @param elementRef
@@ -136,6 +139,9 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
136139 protected elementRef : ElementRef ,
137140 protected viewContainerRef : ViewContainerRef ,
138141 protected dialogService : DialogService ,
142+ /**
143+ * Deprecated as of v5
144+ */
139145 protected eventService : EventService
140146 ) { }
141147
@@ -180,49 +186,55 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
180186 // fix for safari hijacking clicks
181187 this . dialogService . singletonClickListen ( ) ;
182188
183- const element = this . elementRef . nativeElement ;
189+ const element : HTMLElement = this . elementRef . nativeElement ;
184190
185- this . eventService . on ( element , "keydown" , ( event : KeyboardEvent ) => {
186- if ( event . target === this . dialogConfig . parentRef . nativeElement &&
187- ( event . key === "Tab" || event . key === "Tab" && event . shiftKey ) ||
188- event . key === "Escape" ) {
189- this . close ( {
190- reason : CloseReasons . interaction ,
191- target : event . target
192- } ) ;
193- }
194- } ) ;
191+ this . subscriptions . push (
192+ fromEvent ( element , "keydown" ) . subscribe ( ( event : KeyboardEvent ) => {
193+ if ( event . target === this . dialogConfig . parentRef . nativeElement &&
194+ ( event . key === "Tab" || event . key === "Tab" && event . shiftKey ) ||
195+ event . key === "Escape" ) {
196+ this . close ( {
197+ reason : CloseReasons . interaction ,
198+ target : event . target
199+ } ) ;
200+ }
201+ } )
202+ ) ;
195203
196204 // bind events for hovering or clicking the host
197205 if ( this . trigger === "hover" || this . trigger === "mouseenter" ) {
198- this . eventService . on ( element , "mouseenter" , this . open . bind ( this ) ) ;
199- this . eventService . on ( element , this . closeTrigger , ( event ) => {
200- this . close ( {
201- reason : CloseReasons . interaction ,
202- target : event . target
203- } ) ;
204- } ) ;
205- this . eventService . on ( element , "focus" , this . open . bind ( this ) ) ;
206- this . eventService . on ( element , "blur" , ( event ) => {
207- this . close ( {
208- reason : CloseReasons . interaction ,
209- target : event . target
210- } ) ;
211- } ) ;
206+ this . subscriptions . push (
207+ fromEvent ( element , "mouseenter" ) . subscribe ( ( ) => this . open ( ) ) ,
208+ fromEvent ( element , this . closeTrigger ) . subscribe ( ( event ) => {
209+ this . close ( {
210+ reason : CloseReasons . interaction ,
211+ target : event . target
212+ } ) ;
213+ } ) ,
214+ fromEvent ( element , "focus" ) . subscribe ( ( ) => this . open ( ) ) ,
215+ fromEvent ( element , "blur" ) . subscribe ( ( event ) => {
216+ this . close ( {
217+ reason : CloseReasons . interaction ,
218+ target : event . target
219+ } ) ;
220+ } )
221+ ) ;
212222 } else {
213- this . eventService . on ( element , "click" , ( event ) => {
214- this . toggle ( {
215- reason : CloseReasons . interaction ,
216- target : event . target
217- } ) ;
218- } ) ;
219- this . eventService . on ( element , "keydown" , ( event : KeyboardEvent ) => {
220- if ( event . key === "Enter" || event . key === " " ) {
221- setTimeout ( ( ) => {
222- this . open ( ) ;
223+ this . subscriptions . push (
224+ fromEvent ( element , "click" ) . subscribe ( ( event ) => {
225+ this . toggle ( {
226+ reason : CloseReasons . interaction ,
227+ target : event . target
223228 } ) ;
224- }
225- } ) ;
229+ } ) ,
230+ fromEvent ( element , "keydown" ) . subscribe ( ( event : KeyboardEvent ) => {
231+ if ( event . key === "Enter" || event . key === " " ) {
232+ setTimeout ( ( ) => {
233+ this . open ( ) ;
234+ } ) ;
235+ }
236+ } )
237+ ) ;
226238 }
227239
228240 DialogDirective . dialogCounter ++ ;
@@ -241,6 +253,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
241253 this . close ( {
242254 reason : CloseReasons . destroyed
243255 } ) ;
256+ this . subscriptions . forEach ( ( subscription ) => subscription . unsubscribe ( ) ) ;
244257 }
245258
246259 /**
@@ -259,7 +272,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
259272
260273 // Handles emitting all the close events to clean everything up
261274 // Also enforce accessibility on close by updating an aria attr on the nativeElement.
262- this . dialogRef . instance . close . subscribe ( ( meta : CloseMeta ) => {
275+ const subscription = this . dialogRef . instance . close . subscribe ( ( meta : CloseMeta ) => {
263276 if ( ! this . dialogRef ) { return ; }
264277 if ( this . dialogConfig . shouldClose && this . dialogConfig . shouldClose ( meta ) ) {
265278 // close the dialog, emit events, and clear out the open states
@@ -268,6 +281,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
268281 this . isOpen = false ;
269282 this . onClose . emit ( ) ;
270283 this . isOpenChange . emit ( false ) ;
284+ subscription . unsubscribe ( ) ;
271285 }
272286 } ) ;
273287
0 commit comments