Skip to content

Commit 8621d5c

Browse files
committed
Checkboxes type
Schema with type "array" and a enum defaults to a list of checkboxes. Any array can also be overiden in from definition to show a list of checkboxes.
1 parent 9b819ae commit 8621d5c

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

src/bootstrap-example.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ <h3>Model</h3>
9090
}
9191
}
9292
},
93+
"things": {
94+
"type": "array",
95+
"title": "I like...",
96+
"items": {
97+
"type": "string",
98+
"enum": [
99+
"clowns","compiling","sleeping"
100+
]
101+
}
102+
},
93103
"soul": {
94104
"title": "Terms Of Service",
95105
"description": "I agree to sell my undying soul",

src/directives/decorators/bootstrap/bootstrap-decorator.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function($parse, $compile, $http, $templateCache){
1919
if (form.type === 'checkbox') {
2020
return 'directives/decorators/bootstrap/checkbox.html';
2121
}
22+
if (form.type === 'checkboxes') {
23+
return 'directives/decorators/bootstrap/checkboxes.html';
24+
}
2225
if (form.type === 'number') {
2326
return 'directives/decorators/bootstrap/default.html';
2427
}
@@ -46,7 +49,7 @@ function($parse, $compile, $http, $templateCache){
4649
//We do this manually since we need to bind ng-model properly and also
4750
//for fieldsets to recurse properly.
4851
$http.get(templateUrl(form),{ cache: $templateCache }).then(function(res){
49-
var template = res.data.replace('$$value$$','model.'+form.key);
52+
var template = res.data.replace(/\$\$value\$\$/g,'model.'+form.key);
5053
$compile(template)(scope,function(clone){
5154
element.replaceWith(clone);
5255
});
@@ -58,6 +61,16 @@ function($parse, $compile, $http, $templateCache){
5861
scope.showTitle = function() {
5962
return scope.form && scope.form.notitle !== true && scope.form.title;
6063
};
64+
65+
scope.checkboxValuesToList = function(values){
66+
var lst = [];
67+
angular.forEach(values,function(v,k){
68+
if (v) {
69+
lst.push(k);
70+
}
71+
});
72+
return lst;
73+
};
6174
}
6275
};
6376
}]);

src/directives/decorators/bootstrap/checkbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="checkbox" ng-class="{'has-error': hasError()}">
2-
<label ng-show="showTitle()">
2+
<label>
33
<input type="checkbox" ng-model="$$value$$" schema-validate="form.schema" ng-required="form.required">
44
{{form.title}}
55
</label>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="form-group" ng-class="{'has-error': hasError()}" ng-init="checkboxValues = {}">
2+
<label ng-show="showTitle()">{{form.title}}</label>
3+
<div class="checkbox" ng-repeat="(value,name) in form.titleMap" >
4+
<label>
5+
<input type="checkbox" ng-model="checkboxValues[value]" ng-change="$$value$$ = checkboxValuesToList(checkboxValues)" >
6+
{{name}}
7+
</label>
8+
</div>
9+
<span class="help-block" ng-show="form.description">{{form.description}}</span>
10+
</div>

src/services/schema-form.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ angular.module('schemaForm').factory('schemaForm',[function(){
113113
}
114114
};
115115

116+
var checkboxes = function(name,schema,options) {
117+
if (schema.type === 'array' && schema.items && schema.items.enum) {
118+
var f = stdFormObj(schema,options);
119+
f.key = options.path;
120+
f.type = 'checkboxes';
121+
f.titleMap = {};
122+
schema.items.enum.forEach(function(name){
123+
f.titleMap[name] = name;
124+
});
125+
options.lookup[options.path] = f;
126+
return f;
127+
}
128+
};
129+
116130
var fieldset = function(name,schema,options){
117131

118132
if (schema.type === "object") {
@@ -152,7 +166,8 @@ angular.module('schemaForm').factory('schemaForm',[function(){
152166
fieldset,
153167
number,
154168
integer,
155-
bool
169+
bool,
170+
checkboxes
156171
];
157172

158173

test/schema-form-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,44 @@ describe('Schema form',function(){
469469
});
470470

471471

472+
it('should generate checkboxes for arrays with enums',function(){
473+
474+
inject(function($compile,$rootScope){
475+
var scope = $rootScope.$new();
476+
scope.person = {};
477+
478+
scope.schema = {
479+
"type": "object",
480+
"properties": {
481+
"names": {
482+
"type": "array",
483+
"items": {
484+
"type": "string",
485+
"enum": ["foo","bar"]
486+
}
487+
},
488+
"foobars": {
489+
"type": "array"
490+
}
491+
}
492+
};
493+
494+
scope.form = [
495+
"names",
496+
{ key: "foobars", type: "checkboxes", titleMap:{ 'foo':'Foo','bar':'Bar'}}
497+
];
498+
499+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
500+
501+
$compile(tmpl)(scope);
502+
$rootScope.$apply();
503+
504+
//TODO: more asserts
505+
tmpl.children().length.should.be.equal(2);
506+
tmpl.children().eq(0).find('input[type=checkbox]').length.should.be.eq(2);
507+
tmpl.children().eq(1).find('input[type=checkbox]').length.should.be.eq(2);
508+
});
509+
});
472510
});
473511

474512

0 commit comments

Comments
 (0)