Skip to content

Commit d4f266d

Browse files
committed
Array indices for nested conditionals (see PR json-schema-form#742)
1 parent 767c7f3 commit d4f266d

File tree

1 file changed

+84
-14
lines changed

1 file changed

+84
-14
lines changed

dist/schema-form.js

+84-14
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,62 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
170170
}
171171
}
172172
},
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+
173200
condition: function(args) {
174201
// Do we have a condition? Then we slap on an ng-if on all children,
175202
// but be nice to existing ng-if.
176203
if (args.form.condition) {
177204
var evalExpr = 'evalExpr(' + args.path +
178-
'.condition, { model: model, "arrayIndex": $index})';
205+
'.condition, { model: model, "arrayIndex": $index})';
179206
if (args.form.key) {
180207
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+
181214
evalExpr = 'evalExpr(' + args.path + '.condition,{ model: model, "arrayIndex": $index, ' +
182-
'"modelValue": model' + (strKey[0] === '[' ? '' : '.') + strKey + '})';
215+
'"arrayIndices": [' + arrayIndices + '],' +
216+
'"modelValue": model' + (strKey[0] === '[' ? '' : '.') + strKey + '})';
183217
}
184218

185219
var children = args.fieldFrag.children || args.fieldFrag.childNodes;
186220
for (var i = 0; i < children.length; i++) {
187221
var child = children[i];
188222
var ngIf = child.getAttribute('ng-if');
189223
child.setAttribute(
190-
'ng-if',
191-
ngIf ?
192-
'(' + ngIf +
193-
') || (' + evalExpr + ')'
194-
: evalExpr
224+
'ng-if',
225+
ngIf ?
226+
'(' + ngIf +
227+
') || (' + evalExpr + ')'
228+
: evalExpr
195229
);
196230
}
197231
}
@@ -424,15 +458,30 @@ angular.module('schemaForm').provider('schemaFormDecorators',
424458
return lst;
425459
};
426460

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+
427474
scope.buttonClick = function($event, form) {
475+
var arrayIndices = form.key.filter(function(e) { return e === parseInt(e, 10)});
476+
428477
if (angular.isFunction(form.onClick)) {
429-
form.onClick($event, form);
478+
form.onClick($event, form, arrayIndices[arrayIndices.length-1], arrayIndices);
430479
} else if (angular.isString(form.onClick)) {
431480
if (sfSchema) {
432481
//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});
434483
} 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});
436485
}
437486
}
438487
};
@@ -558,7 +607,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
558607

559608
var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})';
560609
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) + '})';
562611
}
563612

564613
angular.forEach(element.children(), function(child) {
@@ -1971,15 +2020,36 @@ angular.module('schemaForm').directive('sfField',
19712020
return lst;
19722021
};
19732022

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+
// };
19742035
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+
19752044
if (angular.isFunction(form.onClick)) {
1976-
form.onClick($event, form);
2045+
2046+
form.onClick($event, form, arrayIndices[arrayDepth-1], arrayIndices);
19772047
} else if (angular.isString(form.onClick)) {
19782048
if (sfSchema) {
19792049
//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});
19812051
} else {
1982-
scope.$eval(form.onClick, {'$event': $event, form: form});
2052+
scope.$eval(form.onClick, {'$event': $event, form: form, arrayIndex: '$index', arrayIndices: '[' + arrayIndices + ']'});
19832053
}
19842054
}
19852055
};

0 commit comments

Comments
 (0)