Skip to content

Commit 74ab9a1

Browse files
committed
Checkboxes!
Basically the exact same implementation as before.
1 parent 0e3b0e1 commit 74ab9a1

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/bootstrap-decorator.js

+53-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* TODO: starting form in tabarray, and arrays.
3-
*/
41
angular.module('schemaForm').config(['schemaFormDecoratorsProvider', 'sfBuilderProvider', 'sfPathProvider',
52
function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
63
var base = 'decorators/bootstrap/';
@@ -66,7 +63,7 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
6663
actions: {template: base + 'actions.html', builder: defaults},
6764
select: {template: base + 'select.html', builder: defaults},
6865
checkbox: {template: base + 'checkbox.html', builder: defaults},
69-
checkboxes: {template: base + 'checkboxes.html', replace: false},
66+
checkboxes: {template: base + 'checkboxes.html', builder: [sfField, ngModelOptions, ngModel, array]},
7067
number: {template: base + 'default.html', builder: defaults},
7168
password: {template: base + 'default.html', builder: defaults},
7269
submit: {template: base + 'submit.html', builder: defaults},
@@ -140,6 +137,58 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
140137
});
141138
}
142139

140+
// Title Map handling
141+
// If form has a titleMap configured we'd like to enable looping over
142+
// titleMap instead of modelArray, this is used for intance in
143+
// checkboxes. So instead of variable number of things we like to create
144+
// a array value from a subset of values in the titleMap.
145+
// The problem here is that ng-model on a checkbox doesn't really map to
146+
// a list of values. This is here to fix that.
147+
if (form.titleMap && form.titleMap.length > 0) {
148+
scope.titleMapValues = [];
149+
150+
// We watch the model for changes and the titleMapValues to reflect
151+
// the modelArray
152+
var updateTitleMapValues = function(arr) {
153+
scope.titleMapValues = [];
154+
arr = arr || [];
155+
156+
form.titleMap.forEach(function(item) {
157+
scope.titleMapValues.push(arr.indexOf(item.value) !== -1);
158+
});
159+
};
160+
//Catch default values
161+
updateTitleMapValues(scope.modelArray);
162+
163+
// TODO: Refactor and see if we can get rid of this watch by piggy backing on the
164+
// validation watch.
165+
scope.$watchCollection('modelArray', updateTitleMapValues);
166+
167+
//To get two way binding we also watch our titleMapValues
168+
scope.$watchCollection('titleMapValues', function(vals, old) {
169+
if (vals && vals !== old) {
170+
var arr = scope.modelArray;
171+
172+
// Apparently the fastest way to clear an array, readable too.
173+
// http://jsperf.com/array-destroy/32
174+
while (arr.length > 0) {
175+
arr.pop();
176+
}
177+
form.titleMap.forEach(function(item, index) {
178+
if (vals[index]) {
179+
arr.push(item.value);
180+
}
181+
});
182+
183+
// Time to validate the rebuilt array.
184+
// validateField method is exported by schema-validate
185+
if (scope.validateField) {
186+
scope.validateField();
187+
}
188+
}
189+
});
190+
}
191+
143192
once();
144193
});
145194

src/checkboxes.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<div sf-array="form" sf-field-model
1+
<div sf-field-model="sf-new-array"
2+
sf-new-array
23
class="form-group schema-form-checkboxes {{form.htmlClass}}"
34
ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess()}">
45
<label class="control-label {{form.labelHtmlClass}}" ng-show="showTitle()">{{form.title}}</label>

0 commit comments

Comments
 (0)