@@ -170,28 +170,62 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
170
170
}
171
171
}
172
172
} ,
173
+ // condition: function(args) {
174
+ // // Do we have a condition? Then we slap on an ng-if on all children,
175
+ // // but be nice to existing ng-if.
176
+ // if (args.form.condition) {
177
+ // var evalExpr = 'evalExpr(' + args.path +
178
+ // '.condition, { model: model, "arrayIndex": $index})';
179
+ // if (args.form.key) {
180
+ // var strKey = sfPathProvider.stringify(args.form.key);
181
+ // evalExpr = 'evalExpr(' + args.path + '.condition,{ model: model, "arrayIndex": $index, ' +
182
+ // '"modelValue": model' + (strKey[0] === '[' ? '' : '.') + strKey + '})';
183
+ // }
184
+ //
185
+ // var children = args.fieldFrag.children || args.fieldFrag.childNodes;
186
+ // for (var i = 0; i < children.length; i++) {
187
+ // var child = children[i];
188
+ // var ngIf = child.getAttribute('ng-if');
189
+ // child.setAttribute(
190
+ // 'ng-if',
191
+ // ngIf ?
192
+ // '(' + ngIf +
193
+ // ') || (' + evalExpr + ')'
194
+ // : evalExpr
195
+ // );
196
+ // }
197
+ // }
198
+ // },
199
+
173
200
condition : function ( args ) {
174
201
// Do we have a condition? Then we slap on an ng-if on all children,
175
202
// but be nice to existing ng-if.
176
203
if ( args . form . condition ) {
177
204
var evalExpr = 'evalExpr(' + args . path +
178
- '.condition, { model: model, "arrayIndex": $index})' ;
205
+ '.condition, { model: model, "arrayIndex": $index})' ;
179
206
if ( args . form . key ) {
180
207
var strKey = sfPathProvider . stringify ( args . form . key ) ;
208
+ var arrayDepth = args . form . key . filter ( function ( e ) { return e === '' } ) . length ;
209
+ var arrayIndices = '$index' ;
210
+ for ( var i = 1 ; i < arrayDepth ; i ++ ) {
211
+ arrayIndices = Array ( i + 1 ) . join ( '$parent.$parent.' ) + '$index,' + arrayIndices ;
212
+ }
213
+
181
214
evalExpr = 'evalExpr(' + args . path + '.condition,{ model: model, "arrayIndex": $index, ' +
182
- '"modelValue": model' + ( strKey [ 0 ] === '[' ? '' : '.' ) + strKey + '})' ;
215
+ '"arrayIndices": [' + arrayIndices + '],' +
216
+ '"modelValue": model' + ( strKey [ 0 ] === '[' ? '' : '.' ) + strKey + '})' ;
183
217
}
184
218
185
219
var children = args . fieldFrag . children || args . fieldFrag . childNodes ;
186
220
for ( var i = 0 ; i < children . length ; i ++ ) {
187
221
var child = children [ i ] ;
188
222
var ngIf = child . getAttribute ( 'ng-if' ) ;
189
223
child . setAttribute (
190
- 'ng-if' ,
191
- ngIf ?
192
- '(' + ngIf +
193
- ') || (' + evalExpr + ')'
194
- : evalExpr
224
+ 'ng-if' ,
225
+ ngIf ?
226
+ '(' + ngIf +
227
+ ') || (' + evalExpr + ')'
228
+ : evalExpr
195
229
) ;
196
230
}
197
231
}
@@ -424,15 +458,30 @@ angular.module('schemaForm').provider('schemaFormDecorators',
424
458
return lst ;
425
459
} ;
426
460
461
+ // scope.buttonClick = function($event, form) {
462
+ // if (angular.isFunction(form.onClick)) {
463
+ // form.onClick($event, form);
464
+ // } else if (angular.isString(form.onClick)) {
465
+ // if (sfSchema) {
466
+ // //evaluating in scope outside of sfSchemas isolated scope
467
+ // sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form});
468
+ // } else {
469
+ // scope.$eval(form.onClick, {'$event': $event, form: form});
470
+ // }
471
+ // }
472
+ // };
473
+
427
474
scope . buttonClick = function ( $event , form ) {
475
+ var arrayIndices = form . key . filter ( function ( e ) { return e === parseInt ( e , 10 ) } ) ;
476
+
428
477
if ( angular . isFunction ( form . onClick ) ) {
429
- form . onClick ( $event , form ) ;
478
+ form . onClick ( $event , form , arrayIndices [ arrayIndices . length - 1 ] , arrayIndices ) ;
430
479
} else if ( angular . isString ( form . onClick ) ) {
431
480
if ( sfSchema ) {
432
481
//evaluating in scope outside of sfSchemas isolated scope
433
- sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form } ) ;
482
+ sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form , arrayIndex : arrayIndices [ arrayIndices . length - 1 ] , arrayIndices : arrayIndices } ) ;
434
483
} else {
435
- scope . $eval ( form . onClick , { '$event' : $event , form : form } ) ;
484
+ scope . $eval ( form . onClick , { '$event' : $event , form : form , arrayIndex : arrayIndices [ arrayIndices . length - 1 ] , arrayIndices : arrayIndices } ) ;
436
485
}
437
486
}
438
487
} ;
@@ -558,7 +607,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
558
607
559
608
var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})' ;
560
609
if ( form . key ) {
561
- evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath . stringify ( form . key ) + '})' ;
610
+ evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "arrayIndices": [' + form . key . filter ( function ( e ) { return e === parseInt ( e , 10 ) } ) . toString ( ) + '], " modelValue": model' + sfPath . stringify ( form . key ) + '})' ;
562
611
}
563
612
564
613
angular . forEach ( element . children ( ) , function ( child ) {
@@ -1971,15 +2020,36 @@ angular.module('schemaForm').directive('sfField',
1971
2020
return lst ;
1972
2021
} ;
1973
2022
2023
+ // scope.buttonClick = function($event, form) {
2024
+ // if (angular.isFunction(form.onClick)) {
2025
+ // form.onClick($event, form);
2026
+ // } else if (angular.isString(form.onClick)) {
2027
+ // if (sfSchema) {
2028
+ // //evaluating in scope outside of sfSchemas isolated scope
2029
+ // sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form});
2030
+ // } else {
2031
+ // scope.$eval(form.onClick, {'$event': $event, form: form});
2032
+ // }
2033
+ // }
2034
+ // };
1974
2035
scope . buttonClick = function ( $event , form ) {
2036
+ var arrayDepth = form . key . filter ( function ( e ) { return e === '' } ) . length ;
2037
+ var arrayIndices = ( arrayDepth > 1 ? Array ( arrayDepth - 1 ) . join ( '$parent.$parent.$parent.' ) + '$parent.$parent.$index,' : '' ) ;
2038
+ for ( var i = arrayDepth ; i > 2 ; i -- ) {
2039
+ arrayIndices += Array ( i - 1 ) . join ( '$parent.$parent.$parent.' ) + '$index,' ;
2040
+ }
2041
+ arrayIndices += '$index' ;
2042
+ arrayIndices = scope . $eval ( '[' + arrayIndices + ']' ) ;
2043
+
1975
2044
if ( angular . isFunction ( form . onClick ) ) {
1976
- form . onClick ( $event , form ) ;
2045
+
2046
+ form . onClick ( $event , form , arrayIndices [ arrayDepth - 1 ] , arrayIndices ) ;
1977
2047
} else if ( angular . isString ( form . onClick ) ) {
1978
2048
if ( sfSchema ) {
1979
2049
//evaluating in scope outside of sfSchemas isolated scope
1980
- sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form } ) ;
2050
+ sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form , arrayIndex : arrayIndices [ arrayDepth - 1 ] , arrayIndices : arrayIndices } ) ;
1981
2051
} else {
1982
- scope . $eval ( form . onClick , { '$event' : $event , form : form } ) ;
2052
+ scope . $eval ( form . onClick , { '$event' : $event , form : form , arrayIndex : '$index' , arrayIndices : '[' + arrayIndices + ']' } ) ;
1983
2053
}
1984
2054
}
1985
2055
} ;
0 commit comments