Skip to content

Commit b0028fc

Browse files
committed
Compatability fixes for add-ons in new builder.
Add-ons not transitioned to the new builder broke if they where in an array built by the new builder. This fixes it.
1 parent ce5534b commit b0028fc

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/directives/newArray.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Directive that handles the model arrays
33
*/
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) {
56
return {
67
scope: false,
78
link: function(scope, element, attrs) {
@@ -24,10 +25,10 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
2425

2526
var onChangeFn = function() {
2627
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);
2930
} else {
30-
scope.evalExpr(form.onChange, {'modelValue': ctrl.$modelValue, form: form});
31+
scope.evalExpr(scope.form.onChange, {'modelValue': scope.modelArray, form: scope.form});
3132
}
3233
}
3334
};
@@ -162,6 +163,49 @@ angular.module('schemaForm').directive('sfNewArray', ['sfSelect', 'sfPath', func
162163
}
163164
return model;
164165
};
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+
165209
}
166210
};
167211
}]);

src/services/builder.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
146146
state.modelName = 'item';
147147
}
148148

149+
// Flag to the builder that where in an array.
150+
// This is needed for compatabiliy if a "old" add-on is used that
151+
// hasn't been transitioned to the new builder.
152+
state.arrayCompatFlag = true;
153+
149154
var childFrag = args.build(args.form.items, args.path + '.items', state);
150155
items.appendChild(childFrag);
151156
}
@@ -186,12 +191,20 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
186191
if (!field.replace) {
187192
// Backwards compatability build
188193
var n = document.createElement(snakeCase(decorator.__name, '-'));
189-
n.setAttribute('form', path + '[' + index + ']');
194+
if (state.arrayCompatFlag) {
195+
n.setAttribute('form','copyWithIndex($index)');
196+
} else {
197+
n.setAttribute('form', path + '[' + index + ']');
198+
}
199+
190200
(checkForSlot(f, slots) || frag).appendChild(n);
191201

192202
} else {
193203
var tmpl;
194204

205+
// Reset arrayCompatFlag, it's only valid for direct children of the array.
206+
state.arrayCompatFlag = false;
207+
195208
// TODO: Create a couple fo testcases, small and large and
196209
// measure optmization. A good start is probably a cache of DOM nodes for a particular
197210
// template that can be cloned instead of using innerHTML

0 commit comments

Comments
 (0)