1
- module . exports = require ( './dist/rmodal.cjs.js' ) ;
1
+ var is = function ( obj , type ) { return Object . prototype . toString . call ( obj ) . toLowerCase ( ) === ( "[object " + type + "]" ) ; } ;
2
+
3
+ var addClass = function ( el , cls ) {
4
+ var arr = el . className
5
+ . split ( / \s + / )
6
+ . filter ( function ( c ) { return ! ! c && c == cls ; } ) ;
7
+
8
+ if ( ! arr . length ) {
9
+ el . className += " " + cls ;
10
+ }
11
+ }
12
+
13
+ var removeClass = function ( el , cls ) {
14
+ el . className = el . className
15
+ . split ( / \s + / )
16
+ . filter ( function ( c ) { return ! ! c && c != cls ; } )
17
+ . join ( ' ' ) ;
18
+ }
19
+
20
+ var RModal = function RModal ( el , opts ) {
21
+ var this$1 = this ;
22
+
23
+ this . opened = false ;
24
+
25
+ this . opts = {
26
+ bodyClass : 'modal-open'
27
+ , dialogClass : 'modal-dialog'
28
+ , dialogOpenClass : 'bounceInDown'
29
+ , dialogCloseClass : 'bounceOutUp'
30
+
31
+ , focus : true
32
+ , focusElements : [
33
+ 'a[href]' , 'area[href]' , 'input:not([disabled]):not([type=hidden])'
34
+ , 'button:not([disabled])' , 'select:not([disabled])'
35
+ , 'textarea:not([disabled])' , 'iframe' , 'object' , 'embed'
36
+ , '*[tabindex]' , '*[contenteditable]'
37
+ ]
38
+
39
+ , escapeClose : true
40
+ , content : null
41
+ } ;
42
+
43
+ Object . keys ( opts || { } )
44
+ . forEach ( function ( key ) {
45
+ /* istanbul ignore else */
46
+ if ( opts [ key ] !== undefined ) {
47
+ this$1 . opts [ key ] = opts [ key ] ;
48
+ }
49
+ } ) ;
50
+
51
+ this . overlay = el ;
52
+ this . dialog = el . querySelector ( ( "." + ( this . opts . dialogClass ) ) ) ;
53
+
54
+ if ( this . opts . content ) {
55
+ this . content ( this . opts . content ) ;
56
+ }
57
+ } ;
58
+
59
+ RModal . prototype . open = function open ( content ) {
60
+ var this$1 = this ;
61
+
62
+ this . content ( content ) ;
63
+
64
+ if ( ! is ( this . opts . beforeOpen , 'function' ) ) {
65
+ return this . _doOpen ( ) ;
66
+ }
67
+
68
+ this . opts . beforeOpen ( function ( ) {
69
+ this$1 . _doOpen ( ) ;
70
+ } ) ;
71
+ } ;
72
+
73
+ RModal . prototype . _doOpen = function _doOpen ( ) {
74
+ addClass ( document . body , this . opts . bodyClass ) ;
75
+
76
+ removeClass ( this . dialog , this . opts . dialogCloseClass ) ;
77
+ addClass ( this . dialog , this . opts . dialogOpenClass ) ;
78
+
79
+ this . overlay . style . display = 'block' ;
80
+
81
+ if ( this . opts . focus ) {
82
+ this . focusOutElement = document . activeElement ;
83
+ this . focus ( ) ;
84
+ }
85
+
86
+ if ( is ( this . opts . afterOpen , 'function' ) ) {
87
+ this . opts . afterOpen ( ) ;
88
+ }
89
+ this . opened = true ;
90
+ } ;
91
+
92
+ RModal . prototype . close = function close ( ) {
93
+ var this$1 = this ;
94
+
95
+ if ( ! is ( this . opts . beforeClose , 'function' ) ) {
96
+ return this . _doClose ( ) ;
97
+ }
98
+
99
+ this . opts . beforeClose ( function ( ) {
100
+ this$1 . _doClose ( ) ;
101
+ } ) ;
102
+ } ;
103
+
104
+ RModal . prototype . _doClose = function _doClose ( ) {
105
+ var this$1 = this ;
106
+
107
+ removeClass ( this . dialog , this . opts . dialogOpenClass ) ;
108
+ addClass ( this . dialog , this . opts . dialogCloseClass ) ;
109
+
110
+ removeClass ( document . body , this . opts . bodyClass ) ;
111
+
112
+ if ( this . opts . focus ) {
113
+ this . focus ( this . focusOutElement ) ;
114
+ }
115
+
116
+ if ( is ( this . opts . afterClose , 'function' ) ) {
117
+ this . opts . afterClose ( ) ;
118
+ }
119
+
120
+ this . opened = false ;
121
+ setTimeout ( function ( ) {
122
+ this$1 . overlay . style . display = 'none' ;
123
+ } , 500 ) ;
124
+ } ;
125
+
126
+ RModal . prototype . content = function content ( content ) {
127
+ if ( content === undefined ) {
128
+ return this . dialog . innerHTML ;
129
+ }
130
+
131
+ this . dialog . innerHTML = content ;
132
+ } ;
133
+
134
+ RModal . prototype . elements = function elements ( selector , fallback ) {
135
+ fallback = fallback || window . navigator . appVersion . indexOf ( 'MSIE 9.0' ) > - 1 ;
136
+ selector = is ( selector , 'array' ) ? selector . join ( ',' ) : selector ;
137
+
138
+ return [ ] . filter . call (
139
+ this . dialog . querySelectorAll ( selector )
140
+ , function ( element ) {
141
+ if ( fallback ) {
142
+ var style = window . getComputedStyle ( element ) ;
143
+ return style . display !== 'none' && style . visibility !== 'hidden' ;
144
+ }
145
+
146
+ return element . offsetParent !== null ;
147
+ }
148
+ ) ;
149
+ } ;
150
+
151
+ RModal . prototype . focus = function focus ( el ) {
152
+ el = el || this . elements ( this . opts . focusElements ) [ 0 ] || this . dialog . firstChild ;
153
+
154
+ if ( el && is ( el . focus , 'function' ) ) {
155
+ el . focus ( ) ;
156
+ }
157
+ } ;
158
+
159
+ RModal . prototype . keydown = function keydown ( ev ) {
160
+ if ( this . opts . escapeClose && ev . which == 27 ) {
161
+ this . close ( ) ;
162
+ }
163
+
164
+ function stopEvent ( ) {
165
+ ev . preventDefault ( ) ;
166
+ ev . stopPropagation ( ) ;
167
+ }
168
+
169
+ if ( this . opened && ev . which == 9 && this . dialog . contains ( ev . target ) ) {
170
+ var elements = this . elements ( this . opts . focusElements )
171
+ , first = elements [ 0 ]
172
+ , last = elements [ elements . length - 1 ] ;
173
+
174
+ if ( first == last ) {
175
+ stopEvent ( ) ;
176
+ }
177
+ else if ( ev . target == first && ev . shiftKey ) {
178
+ stopEvent ( ) ;
179
+ last . focus ( ) ;
180
+ }
181
+ else if ( ev . target == last && ! ev . shiftKey ) {
182
+ stopEvent ( ) ;
183
+ first . focus ( ) ;
184
+ }
185
+ }
186
+ } ;
187
+
188
+ RModal . prototype . version = '1.0.20' ;
189
+ RModal . version = '1.0.20' ;
190
+
191
+ module . exports = RModal ;
0 commit comments