Skip to content

Commit c2b359c

Browse files
committed
Condition on every form type
Works exactly the same as the condition form type (which now becomes deprecated).
1 parent 4901c5b commit c2b359c

File tree

3 files changed

+81
-27
lines changed

3 files changed

+81
-27
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<div class="schema-form-section {{form.htmlClass}}"
2-
ng-if="!form.condition || evalExpr(form.condition,{ model: model, 'arrayIndex': arrayIndex })">
1+
<div class="schema-form-section {{form.htmlClass}}">
32
<sf-decorator ng-repeat="item in form.items" form="item"></sf-decorator>
43
</div>

src/services/decorators.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,6 @@ angular.module('schemaForm').provider('schemaFormDecorators',
4040
scope: true,
4141
require: '?^sfSchema',
4242
link: function(scope, element, attrs, sfSchema) {
43-
//rebind our part of the form to the scope.
44-
var once = scope.$watch(attrs.form, function(form) {
45-
46-
if (form) {
47-
scope.form = form;
48-
49-
//ok let's replace that template!
50-
//We do this manually since we need to bind ng-model properly and also
51-
//for fieldsets to recurse properly.
52-
var url = templateUrl(name, form);
53-
$http.get(url, {cache: $templateCache}).then(function(res) {
54-
var key = form.key ?
55-
sfPathProvider.stringify(form.key).replace(/"/g, '&quot;') : '';
56-
var template = res.data.replace(
57-
/\$\$value\$\$/g,
58-
'model' + (key[0] !== '[' ? '.' : '') + key
59-
);
60-
element.html(template);
61-
$compile(element.contents())(scope);
62-
});
63-
once();
64-
}
65-
});
6643

6744
//Keep error prone logic from the template
6845
scope.showTitle = function() {
@@ -158,8 +135,47 @@ angular.module('schemaForm').provider('schemaFormDecorators',
158135

159136
//Otherwise we only have input number not being a number
160137
return 'Not a number';
161-
162138
};
139+
140+
// Rebind our part of the form to the scope.
141+
var once = scope.$watch(attrs.form, function(form) {
142+
if (form) {
143+
scope.form = form;
144+
145+
//ok let's replace that template!
146+
//We do this manually since we need to bind ng-model properly and also
147+
//for fieldsets to recurse properly.
148+
var url = templateUrl(name, form);
149+
$http.get(url, {cache: $templateCache}).then(function(res) {
150+
var key = form.key ?
151+
sfPathProvider.stringify(form.key).replace(/"/g, '&quot;') : '';
152+
var template = res.data.replace(
153+
/\$\$value\$\$/g,
154+
'model' + (key[0] !== '[' ? '.' : '') + key
155+
);
156+
element.html(template);
157+
158+
// Do we have a condition? Then we slap on an ng-if on all children,
159+
// but be nice to existing ng-if.
160+
if (form.condition) {
161+
element.children().each(function() {
162+
var ngIf = this.getAttribute('ng-if');
163+
this.setAttribute(
164+
'ng-if',
165+
ngIf ?
166+
'(' + ngIf +
167+
') || (evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex }))'
168+
: 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex })'
169+
);
170+
});
171+
}
172+
173+
$compile(element.contents())(scope);
174+
});
175+
176+
once();
177+
}
178+
});
163179
}
164180
};
165181
}

test/directives/schema-form-test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ describe('directive',function(){
454454
});
455455
});
456456

457-
457+
458458
it('should honor defaults in schema',function(){
459459

460460
inject(function($compile,$rootScope){
@@ -1678,5 +1678,44 @@ describe('directive',function(){
16781678
});
16791679
});
16801680

1681+
it('should remove or add fields depending on condition',function(done) {
1682+
1683+
inject(function($compile, $rootScope){
1684+
var scope = $rootScope.$new();
1685+
scope.person = {
1686+
flag: true
1687+
};
1688+
1689+
scope.schema = {
1690+
type: 'object',
1691+
properties: {
1692+
name: {type: 'string'}
1693+
}
1694+
};
1695+
1696+
scope.form = [
1697+
{
1698+
key:'name',
1699+
condition: 'person.flag'
1700+
}
1701+
];
1702+
1703+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
1704+
1705+
$compile(tmpl)(scope);
1706+
$rootScope.$apply();
1707+
1708+
tmpl.children().find('.schema-form-text').length.should.be.equal(1);
1709+
1710+
setTimeout(function() {
1711+
scope.person.flag = false;
1712+
$rootScope.$apply();
1713+
tmpl.children().find('.schema-form-text').length.should.be.equal(0);
1714+
done();
1715+
}, 0);
1716+
1717+
});
1718+
});
1719+
16811720

16821721
});

0 commit comments

Comments
 (0)