@@ -17,23 +17,40 @@ function getOptions(
17
17
} ;
18
18
}
19
19
20
- let loaded = false ;
20
+ const getWin = ( el : HTMLElement | Window ) => {
21
+ let win = el as Window | null ;
21
22
22
- function bindEvent ( win : Window , binding : EventBinding , options : EventOptions ) {
23
+ if ( ( el as HTMLElement ) . nodeName === 'IFRAME' ) {
24
+ win = ( el as HTMLIFrameElement ) . contentWindow ;
25
+ }
26
+
27
+ return win ;
28
+ } ;
29
+
30
+ function bindEvent (
31
+ el : HTMLElement | Window ,
32
+ binding : EventBinding ,
33
+ options : EventOptions ,
34
+ ) {
23
35
let timer : number | undefined ;
24
36
25
- if ( ! loaded ) {
26
- // Some browsers require us to defer binding events, i.e. Safari
37
+ if ( ( el as HTMLElement ) . nodeName === 'IFRAME' ) {
27
38
timer = setInterval ( ( ) => {
28
- if ( ( win as Window ) . document . readyState === 'complete' ) {
29
- win . addEventListener ( binding . eventName , binding . fn , options ) ;
30
- loaded = true ;
39
+ const currentWin = getWin ( el ) ;
40
+
41
+ if ( currentWin ?. document . readyState === 'complete' ) {
42
+ currentWin . addEventListener ( binding . eventName , binding . fn , options ) ;
43
+ clearInterval ( timer ) ;
31
44
}
32
45
} , 100 ) ;
33
- } else {
34
- win . addEventListener ( binding . eventName , binding . fn , options ) ;
46
+
47
+ return timer ;
35
48
}
36
49
50
+ const win = getWin ( el ) ;
51
+
52
+ win ?. addEventListener ( binding . eventName , binding . fn , options ) ;
53
+
37
54
return timer ;
38
55
}
39
56
@@ -49,18 +66,19 @@ export default function bindEvents(
49
66
'[data-rfd-iframe]' ,
50
67
) as HTMLIFrameElement [ ] ;
51
68
52
- const windows = [ el , ...iframes . map ( ( iframe ) => iframe . contentWindow ) ] ;
69
+ const els = [ el , ...iframes ] ;
53
70
54
- return windows . map ( ( win ) => {
55
- if ( ! win ) return function unbind ( ) { } ;
71
+ return els . map ( ( currentEl ) => {
72
+ if ( ! currentEl ) return function unbind ( ) { } ;
56
73
57
74
const options = getOptions ( sharedOptions , binding . options ) ;
58
75
59
- const timer = bindEvent ( win as Window , binding , options ) ;
76
+ const timer = bindEvent ( currentEl , binding , options ) ;
60
77
61
78
return function unbind ( ) {
62
79
clearInterval ( timer ) ;
63
- win . removeEventListener ( binding . eventName , binding . fn , options ) ;
80
+ const win = getWin ( currentEl ) ;
81
+ win ?. removeEventListener ( binding . eventName , binding . fn , options ) ;
64
82
} ;
65
83
} ) ;
66
84
} ,
0 commit comments