1
1
/**
2
2
* Directive that handles the model arrays
3
3
*/
4
- angular . module ( 'schemaForm' ) . directive ( 'sfNewArray' , [ 'sfSelect' , 'sfPath' , function ( sel , sfPath ) {
4
+ angular . module ( 'schemaForm' ) . directive ( 'sfNewArray' , [ 'sfSelect' , 'sfPath' , 'schemaForm' ,
5
+ function ( sel , sfPath , schemaForm ) {
5
6
return {
6
7
scope : false ,
7
8
link : function ( scope , element , attrs ) {
@@ -24,10 +25,10 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
24
25
25
26
var onChangeFn = function ( ) {
26
27
if ( scope . form && scope . form . onChange ) {
27
- if ( angular . isFunction ( form . onChange ) ) {
28
- form . onChange ( ctrl . $modelValue , form ) ;
28
+ if ( angular . isFunction ( scope . form . onChange ) ) {
29
+ scope . form . onChange ( scope . modelArray , scope . form ) ;
29
30
} else {
30
- scope . evalExpr ( form . onChange , { 'modelValue' : ctrl . $modelValue , form : form } ) ;
31
+ scope . evalExpr ( scope . form . onChange , { 'modelValue' : scope . modelArray , form : scope . form } ) ;
31
32
}
32
33
}
33
34
} ;
@@ -162,6 +163,49 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
162
163
}
163
164
return model ;
164
165
} ;
166
+
167
+ // For backwards compatability, i.e. when a bootstrap-decorator tag is used
168
+ // as child to the array.
169
+ var setIndex = function ( index ) {
170
+ return function ( form ) {
171
+ if ( form . key ) {
172
+ form . key [ form . key . indexOf ( '' ) ] = index ;
173
+ }
174
+ } ;
175
+ } ;
176
+ var formDefCache = { } ;
177
+ scope . copyWithIndex = function ( index ) {
178
+ var form = scope . form ;
179
+ if ( ! formDefCache [ index ] ) {
180
+
181
+ // To be more compatible with JSON Form we support an array of items
182
+ // in the form definition of "array" (the schema just a value).
183
+ // for the subforms code to work this means we wrap everything in a
184
+ // section. Unless there is just one.
185
+ var subForm = form . items [ 0 ] ;
186
+ if ( form . items . length > 1 ) {
187
+ subForm = {
188
+ type : 'section' ,
189
+ items : form . items . map ( function ( item ) {
190
+ item . ngModelOptions = form . ngModelOptions ;
191
+ if ( angular . isUndefined ( item . readonly ) ) {
192
+ item . readonly = form . readonly ;
193
+ }
194
+ return item ;
195
+ } )
196
+ } ;
197
+ }
198
+
199
+ if ( subForm ) {
200
+ var copy = angular . copy ( subForm ) ;
201
+ copy . arrayIndex = index ;
202
+ schemaForm . traverseForm ( copy , setIndex ( index ) ) ;
203
+ formDefCache [ index ] = copy ;
204
+ }
205
+ }
206
+ return formDefCache [ index ] ;
207
+ } ;
208
+
165
209
}
166
210
} ;
167
211
} ] ) ;
0 commit comments