69
69
validateOnType : false
70
70
} ;
71
71
72
+ var isActiveFormEnabled = false ;
73
+
72
74
var methods = {
73
75
init : function ( options ) {
74
76
var settings = $ . extend ( true , { } , defaultOptions , options || { } ) ,
93
95
addInput ( $ ( this ) ) ;
94
96
} ) ;
95
97
98
+ var i = 0 ,
99
+ event = $ . Event ( events . afterInit ) ;
100
+
96
101
var intervalID = setInterval ( function ( ) {
97
102
if ( typeof form . data ( 'yiiActiveForm' ) === 'object' ) {
98
- var attribute = form . yiiActiveForm ( 'find' , id ) ;
99
- var attributeDefaults = [ ] ;
103
+ var attribute = form . yiiActiveForm ( 'find' , id ) ,
104
+ attributeDefaults = [ ] ;
105
+
100
106
if ( typeof attribute === 'object' ) {
101
107
$ . each ( attribute , function ( key , value ) {
102
108
if ( [ 'id' , 'input' , 'container' ] . indexOf ( key ) == - 1 ) {
103
109
attributeDefaults [ key ] = value ;
104
110
}
105
111
} ) ;
112
+
106
113
form . yiiActiveForm ( 'remove' , id ) ;
107
114
}
108
115
109
116
var attributeOptions = $ . extend ( { } , defaultAttributeOptions , settings . attributeOptions ) ;
117
+
110
118
$ . each ( attributeOptions , function ( key , value ) {
111
119
if ( typeof attributeDefaults [ key ] === 'undefined' ) {
112
120
attributeDefaults [ key ] = value ;
119
127
addAttribute ( $ ( this ) ) ;
120
128
} ) ;
121
129
122
- $wrapper . data ( 'multipleInput' ) . currentIndex = $wrapper
123
- . find ( '.multiple-input-list__item.' + settings . uniqueHash )
124
- . length ;
130
+ $wrapper . data ( 'multipleInput' ) . currentIndex = getCurrentIndex ( $wrapper ) ;
131
+ isActiveFormEnabled = true ;
125
132
126
133
clearInterval ( intervalID ) ;
134
+ $wrapper . trigger ( event ) ;
135
+ } else {
136
+ i ++ ;
137
+ }
138
+
139
+ // wait for initialization of ActiveForm a second
140
+ // If after a second system could not detect ActiveForm it means
141
+ // that widget is used without ActiveForm and we should just complete initialization of the widget
142
+ if ( i > 10 ) {
143
+ $wrapper . data ( 'multipleInput' ) . currentIndex = getCurrentIndex ( $wrapper ) ;
144
+ isActiveFormEnabled = false ;
127
145
128
- var event = $ . Event ( events . afterInit ) ;
146
+ clearInterval ( intervalID ) ;
129
147
$wrapper . trigger ( event ) ;
130
148
}
131
149
} , 100 ) ;
132
150
} ,
151
+
133
152
add : function ( values ) {
134
153
addInput ( $ ( this ) , values ) ;
135
154
} ,
155
+
136
156
remove : function ( index ) {
137
157
var row = null ;
138
158
if ( index ) {
139
159
row = $ ( this ) . find ( '.js-input-remove:eq(' + index + ')' ) ;
140
160
} else {
141
161
row = $ ( this ) . find ( '.js-input-remove' ) . last ( ) ;
142
162
}
163
+
143
164
removeInput ( row ) ;
144
165
} ,
166
+
145
167
clear : function ( ) {
146
168
$ ( '.js-input-remove' ) . each ( function ( ) {
147
169
removeInput ( $ ( this ) ) ;
211
233
}
212
234
}
213
235
214
- addAttribute ( that ) ;
236
+ if ( isActiveFormEnabled ) {
237
+ addAttribute ( that ) ;
238
+ }
215
239
216
240
index ++ ;
217
241
} ) ;
232
256
if ( count > settings . min ) {
233
257
var event = $ . Event ( events . beforeDeleteRow ) ;
234
258
$wrapper . trigger ( event , [ $toDelete ] ) ;
259
+
235
260
if ( event . result === false ) {
236
261
return ;
237
262
}
238
263
239
- $toDelete . find ( 'input, select, textarea' ) . each ( function ( ) {
240
- removeAttribute ( $ ( this ) ) ;
241
- } ) ;
264
+ if ( isActiveFormEnabled ) {
265
+ $toDelete . find ( 'input, select, textarea' ) . each ( function ( ) {
266
+ removeAttribute ( $ ( this ) ) ;
267
+ } ) ;
268
+ }
242
269
243
270
$toDelete . fadeOut ( 300 , function ( ) {
244
271
$ ( this ) . remove ( ) ;
249
276
}
250
277
} ;
251
278
279
+ /**
280
+ * Add an attribute to ActiveForm.
281
+ *
282
+ * @param input
283
+ */
252
284
var addAttribute = function ( input ) {
253
285
var id = getInputId ( input ) ;
254
286
281
313
} ) ) ;
282
314
} ;
283
315
316
+ /**
317
+ * Removes an attribute from ActiveForm.
318
+ */
284
319
var removeAttribute = function ( ) {
285
320
var id = getInputId ( $ ( this ) ) ;
286
321
309
344
return id ;
310
345
} ;
311
346
347
+ var getCurrentIndex = function ( $wrapper ) {
348
+ if ( typeof $wrapper . data ( 'multipleInput' ) !== 'object' ) {
349
+ return 0 ;
350
+ }
351
+
352
+ return $wrapper
353
+ . find ( '.multiple-input-list__item.' + $wrapper . data ( 'multipleInput' ) . settings . uniqueHash )
354
+ . length ;
355
+ } ;
356
+
312
357
String . prototype . replaceAll = function ( search , replace ) {
313
358
return this . split ( search ) . join ( replace ) ;
314
359
} ;
315
- } ) ( window . jQuery ) ;
360
+ } ) ( window . jQuery ) ;
0 commit comments