Skip to content

Commit cee0383

Browse files
committed
Merge branch 'pre-existing-html-layout' of https://github.com/ebrehault/angular-schema-form into ebrehault-pre-existing-html-layout
Conflicts: dist/schema-form.min.js test/schema-form-test.js
2 parents e702834 + 26466ca commit cee0383

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

dist/schema-form.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,17 +1276,26 @@ angular.module('schemaForm')
12761276
//make the form available to decorators
12771277
scope.schemaForm = {form: merged, schema: schema};
12781278

1279-
//Create directives from the form definition
1280-
angular.forEach(merged, function(obj, i) {
1281-
var n = document.createElement(attrs.sfDecoratorName ||
1282-
snakeCase(schemaFormDecorators.defaultDecorator, '-'));
1283-
n.setAttribute('form', 'schemaForm.form[' + i + ']');
1284-
frag.appendChild(n);
1285-
});
1286-
12871279
//clean all but pre existing html.
12881280
element.children(':not(.schema-form-ignore)').remove();
12891281

1282+
//Create directives from the form definition
1283+
angular.forEach(merged,function(obj,i){
1284+
var n = document.createElement(attrs.sfDecorator || snakeCase(schemaFormDecorators.defaultDecorator,'-'));
1285+
n.setAttribute('form','schemaForm.form['+i+']');
1286+
var slot;
1287+
try {
1288+
slot = element[0].querySelector('*[sf-insert-field="' + obj.key + '"]');
1289+
} catch(err) {
1290+
slot = null;
1291+
}
1292+
if(slot) {
1293+
slot.appendChild(n);
1294+
} else {
1295+
frag.appendChild(n);
1296+
}
1297+
});
1298+
12901299
element[0].appendChild(frag);
12911300

12921301
//compile only children

src/directives/schema-form.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,27 @@ angular.module('schemaForm')
8080
//make the form available to decorators
8181
scope.schemaForm = {form: merged, schema: schema};
8282

83-
//Create directives from the form definition
84-
angular.forEach(merged, function(obj, i) {
85-
var n = document.createElement(attrs.sfDecoratorName ||
86-
snakeCase(schemaFormDecorators.defaultDecorator, '-'));
87-
n.setAttribute('form', 'schemaForm.form[' + i + ']');
88-
frag.appendChild(n);
89-
});
90-
9183
//clean all but pre existing html.
9284
element.children(':not(.schema-form-ignore)').remove();
9385

86+
//Create directives from the form definition
87+
angular.forEach(merged,function(obj,i){
88+
var n = document.createElement(attrs.sfDecorator || snakeCase(schemaFormDecorators.defaultDecorator,'-'));
89+
n.setAttribute('form','schemaForm.form['+i+']');
90+
var slot;
91+
try {
92+
slot = element[0].querySelector('*[sf-insert-field="' + obj.key + '"]');
93+
} catch(err) {
94+
// field insertion not supported for complex keys
95+
slot = null;
96+
}
97+
if(slot) {
98+
slot.appendChild(n);
99+
} else {
100+
frag.appendChild(n);
101+
}
102+
});
103+
94104
element[0].appendChild(frag);
95105

96106
//compile only children

test/directives/schema-form-test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ describe('directive',function(){
173173
});
174174
});
175175

176+
it('should preserve existing html and insert fields in matching slots',function(){
177+
178+
inject(function($compile,$rootScope){
179+
var scope = $rootScope.$new();
180+
scope.person = {};
181+
182+
scope.schema = exampleSchema;
183+
184+
scope.form = ["*"];
185+
186+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"><ul><li sf-insert-field="name"></li></ul></form>');
187+
188+
$compile(tmpl)(scope);
189+
$rootScope.$apply();
190+
191+
tmpl.children().eq(0).is('ul').should.be.true;
192+
tmpl.children().eq(0).find('input').attr('ng-model').should.be.equal('model[\'name\']');
193+
});
194+
});
195+
196+
176197
it('should handle submit buttons',function(){
177198

178199
inject(function($compile,$rootScope){
@@ -1599,4 +1620,4 @@ describe('directive',function(){
15991620
});
16001621
});
16011622

1602-
});
1623+
});

0 commit comments

Comments
 (0)