Skip to content

Commit 22a0575

Browse files
committed
Merge branch 'ebrehault-pre-existing-html-layout' into development
2 parents 18f4226 + 59ce414 commit 22a0575

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

dist/schema-form.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,17 +1276,28 @@ 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+
// field insertion not supported for complex keys
1291+
slot = null;
1292+
}
1293+
if(slot) {
1294+
slot.innerHTML = "";
1295+
slot.appendChild(n);
1296+
} else {
1297+
frag.appendChild(n);
1298+
}
1299+
});
1300+
12901301
element[0].appendChild(frag);
12911302

12921303
//compile only children

dist/schema-form.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/bootstrap-example.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ <h1>Schema Form Example</h1>
3737
<div class="row">
3838
<div class="col-sm-4">
3939
<h3>The Generated Form</h3>
40-
<form name="ngform" sf-model="modelData" sf-form="form" sf-schema="schema" ng-submit="submitForm(ngform,modelData)"></form>
40+
<form name="ngform"
41+
sf-model="modelData"
42+
sf-form="form"
43+
sf-schema="schema"
44+
ng-submit="submitForm(ngform,modelData)">
45+
<em>before</em><div sf-insert-field="name"></div><em>after</em>
46+
</form>
4147
<h3>Model</h3>
4248
<pre ng-cloak>{{pretty()}}</pre>
4349
</div>

src/directives/schema-form.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,28 @@ 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.innerHTML = "";
99+
slot.appendChild(n);
100+
} else {
101+
frag.appendChild(n);
102+
}
103+
});
104+
94105
element[0].appendChild(frag);
95106

96107
//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)