@@ -34,6 +34,8 @@ export class DynamicOverlayController {
3434 if ( ! this . content ) {
3535 this . content = document . createElement ( 'div' ) ;
3636 }
37+ this . __fakeExtendsEventTarget ( ) ;
38+ this . __delegateEvent = this . __delegateEvent . bind ( this ) ;
3739 }
3840
3941 add ( ctrlToAdd ) {
@@ -71,9 +73,13 @@ export class DynamicOverlayController {
7173 if ( this . isShown === true ) {
7274 throw new Error ( 'You can not switch overlays while being shown' ) ;
7375 }
76+ const prevActive = this . active ;
77+
7478 this . active . switchOut ( ) ;
7579 ctrlToSwitchTo . switchIn ( ) ;
7680 this . __active = ctrlToSwitchTo ;
81+
82+ this . _delegateEvents ( this . __active , prevActive ) ;
7783 }
7884
7985 async show ( ) {
@@ -99,4 +105,24 @@ export class DynamicOverlayController {
99105 get invokerNode ( ) {
100106 return this . active . invokerNode ;
101107 }
108+
109+ _delegateEvents ( active , prevActive ) {
110+ [ 'show' , 'hide' ] . forEach ( event => {
111+ active . addEventListener ( event , this . __delegateEvent ) ;
112+ prevActive . removeEventListener ( event , this . __delegateEvent ) ;
113+ } ) ;
114+ }
115+
116+ __delegateEvent ( ev ) {
117+ ev . stopPropagation ( ) ;
118+ this . dispatchEvent ( new Event ( ev . type ) ) ;
119+ }
120+
121+ // TODO: this method has to be removed when EventTarget polyfill is available on IE11
122+ __fakeExtendsEventTarget ( ) {
123+ const delegate = document . createDocumentFragment ( ) ;
124+ [ 'addEventListener' , 'dispatchEvent' , 'removeEventListener' ] . forEach ( funcName => {
125+ this [ funcName ] = ( ...args ) => delegate [ funcName ] ( ...args ) ;
126+ } ) ;
127+ }
102128}
0 commit comments