|
1 |
| -/** |
2 |
| - * TODO: starting form in tabarray, and arrays. |
3 |
| - */ |
4 | 1 | angular.module('schemaForm').config(['schemaFormDecoratorsProvider', 'sfBuilderProvider', 'sfPathProvider',
|
5 | 2 | function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
|
6 | 3 | var base = 'decorators/bootstrap/';
|
@@ -66,7 +63,7 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
|
66 | 63 | actions: {template: base + 'actions.html', builder: defaults},
|
67 | 64 | select: {template: base + 'select.html', builder: defaults},
|
68 | 65 | 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]}, |
70 | 67 | number: {template: base + 'default.html', builder: defaults},
|
71 | 68 | password: {template: base + 'default.html', builder: defaults},
|
72 | 69 | submit: {template: base + 'submit.html', builder: defaults},
|
@@ -140,6 +137,58 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
|
140 | 137 | });
|
141 | 138 | }
|
142 | 139 |
|
| 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 | + |
143 | 192 | once();
|
144 | 193 | });
|
145 | 194 |
|
|
0 commit comments