blob: 6fc07accb2f769f66541f324875f8c20f3389a05 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 5/25/16.
7 */
8
9(function () {
10 'use strict';
11
12 angular.module('xos.uiComponents')
13 /**
14 * @ngdoc directive
15 * @name xos.uiComponents.directive:xosField
16 * @restrict E
17 * @description The xos-field directive.
18 * This component decide, give a field wich kind of input it need to print.
19 * @param {string} name The field name
20 * @param {object} field The field configuration:
21 * ```
22 * {
23 * label: 'Label',
24 * type: 'number', //typeof field
25 * validators: {} // see xosForm for more details
26 * }
27 * ```
28 * @param {mixed} ngModel The field value
29 *
30 * @example
31
32 # Basic Example
33
34 <example module="sampleField1">
35 <file name="script.js">
36 angular.module('sampleField1', ['xos.uiComponents'])
37 .factory('_', function($window){
38 return $window._;
39 })
40 .controller('SampleCtrl', function(){
41 this.name = 'input-name';
42 this.field = {label: 'My String Value:', type: 'string'};
43 this.model = 'my string';
44 });
45 </file>
46 <file name="index.html">
47 <div ng-controller="SampleCtrl as vm">
48 <xos-field ng-model="vm.model" name="vm.name" field="vm.field"></xos-field>
49 </div>
50 </file>
51 </example>
52
53 # Possible Values
54
55 <example module="sampleField2">
56 <file name="script.js">
57 angular.module('sampleField2', ['xos.uiComponents'])
58 .factory('_', function($window){
59 return $window._;
60 })
61 .controller('SampleCtrl', function(){
62 this.field1 = {
63 name: 'number-field',
64 field: {label: 'My Number Value:', type: 'number'},
65 model: 2
66 };
67
68 this.field2 = {
69 name: 'date-field',
70 field: {label: 'My Date Value:', type: 'date'},
71 model: new Date()
72 };
73
74 this.field3 = {
75 name: 'boolean-field',
76 field: {label: 'My Boolean Value:', type: 'boolean'},
77 model: true
78 };
79
80 this.field4 = {
81 name: 'email-field',
82 field: {label: 'My Email Value:', type: 'email'},
83 model: 'sample@domain.us'
84 };
Matteo Scandolo65116c42016-09-21 17:06:23 -070085
86 this.field5 = {
87 name: 'select',
88 label: 'Select field:',
89 type: 'select',
90 model: 1,
91 options: [
92 {id: 1, label: 'One'},
93 {id: 2, label: 'Two'},
94 {id: 3, label: 'Three'},
95 ]
96 };
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070097 });
98 </file>
99 <file name="index.html">
100 <div ng-controller="SampleCtrl as vm">
101 <xos-field ng-model="vm.field1.model" name="vm.field1.name" field="vm.field1.field"></xos-field>
102 <xos-field ng-model="vm.field2.model" name="vm.field2.name" field="vm.field2.field"></xos-field>
103 <xos-field ng-model="vm.field3.model" name="vm.field3.name" field="vm.field3.field"></xos-field>
104 <xos-field ng-model="vm.field4.model" name="vm.field4.name" field="vm.field4.field"></xos-field>
Matteo Scandolo65116c42016-09-21 17:06:23 -0700105 <xos-field ng-model="vm.field5.model" name="vm.field5.name" field="vm.field5.field"></xos-field>
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700106 </div>
107 </file>
108 </example>
109
110 # This element is recursive
111
112 <example module="sampleField3">
113 <file name="script.js">
114 angular.module('sampleField3', ['xos.uiComponents'])
115 .factory('_', function($window){
116 return $window._;
117 })
118 .controller('SampleCtrl', function(){
119 this.name1 = 'input-name';
120 this.field1 = {label: 'My Object Field:', type: 'object'};
121 this.model1 = {
122 name: 'Jhon',
123 age: '25',
124 email: 'jhon@thewall.ru',
125 active: true
126 };
127
128 this.name2 = 'another-name';
129 this.field2 = {
130 label: 'Empty Object Field',
131 type: 'object',
132 properties: {
133 foo: {
134 label: 'FooLabel:',
135 type: 'string',
136 validators: {
137 required: true
138 }
139 },
140 bar: {
141 type: 'number'
142 }
143 }
144 }
145 });
146 </file>
147 <file name="index.html">
148 <div ng-controller="SampleCtrl as vm">
149 <h4>Autogenerated object field</h4>
150 <xos-field ng-model="vm.model1" name="vm.name1" field="vm.field1"></xos-field>
151
152 <h4>Configured object field</h4>
153 <xos-field ng-model="vm.model2" name="vm.name2" field="vm.field2"></xos-field>
154 </div>
155 </file>
156 </example>
157 */
Arpit Agarwal34b63832016-08-08 11:59:45 -0700158 .component('xosField', {
159 restrict: 'E',
160 bindings: {
161 name: '=',
162 field: '=',
163 ngModel: '='
164 },
165 template: `
Matteo Scandolo65116c42016-09-21 17:06:23 -0700166 <label ng-if="vm.field.type !== 'object' && vm.field.type !== 'array'">{{vm.field.label}}</label>
167 <input
168 xos-custom-validator custom-validator="vm.field.validators.custom || null"
169 ng-if="vm.field.type !== 'boolean' && vm.field.type !== 'object' && vm.field.type !== 'select' && vm.field.type !== 'array'"
170 type="{{vm.field.type}}"
171 name="{{vm.name}}"
172 class="form-control"
173 ng-model="vm.ngModel"
174 ng-minlength="vm.field.validators.minlength || 0"
175 ng-maxlength="vm.field.validators.maxlength || 2000"
176 ng-required="vm.field.validators.required || false" />
177 <select class="form-control" ng-if ="vm.field.type === 'select'"
178 name = "{{vm.name}}"
179 ng-options="item.id as item.label for item in vm.field.options"
180 ng-model="vm.ngModel"
181 ng-required="vm.field.validators.required || false">
182 </select>
183 <span class="boolean-field" ng-if="vm.field.type === 'boolean'">
184 <a href="#"
185 class="btn btn-success"
186 ng-show="vm.ngModel"
187 ng-click="vm.ngModel = false">
188 <i class="glyphicon glyphicon-ok"></i>
189 </a>
190 <a href="#"
191 class="btn btn-danger"
192 ng-show="!vm.ngModel"
193 ng-click="vm.ngModel = true">
194 <i class="glyphicon glyphicon-remove"></i>
195 </a>
196 </span>
197 <div
198 class="panel panel-default object-field"
199 ng-if="vm.field.type == 'object' && (!vm.isEmptyObject(vm.ngModel) || !vm.isEmptyObject(vm.field.properties))"
200 >
201 <div class="panel-heading">{{vm.field.label}}</div>
202 <div class="panel-body">
203 <div ng-if="!vm.field.properties" ng-repeat="(k, v) in vm.ngModel">
204 <xos-field
205 name="k"
206 field="{label: vm.formatLabel(k), type: vm.getType(v)}"
207 ng-model="v">
208 </xos-field>
Arpit Agarwal34b63832016-08-08 11:59:45 -0700209 </div>
Matteo Scandolo65116c42016-09-21 17:06:23 -0700210 <div ng-if="vm.field.properties" ng-repeat="(k, v) in vm.field.properties">
211 <xos-field
212 name="k"
213 field="{
214 label: v.label || vm.formatLabel(k),
215 type: v.type,
216 validators: v.validators
217 }"
218 ng-model="vm.ngModel[k]">
219 </xos-field>
220 </div>
221 </div>
222 </div>
223 <div
224 class="panel panel-default array-field"
225 ng-if="vm.field.type == 'array'">
226 <div class="panel-heading">{{vm.field.label}}</div>
227 <div class="panel-body selected">
228 <ul class="draggable" dnd-list="vm.ngModel">
229 <li
230 class="array-element"
231 ng-repeat="item in vm.ngModel"
232 dnd-draggable="item"
233 dnd-moved="vm.ngModel.splice($index, 1)"
234 dnd-effect-allowed="move"
235 dnd-selected="models.selected = item"
236 >
237 <div class="well well-sm text-center">
238 {{item}}
239 </div>
240 </li>
241 <div class="clearfix"></div>
242 </ul>
243 </div>
244 <div class="panel-body unselected">
245 <ul class="draggable" dnd-list="vm.field.availableOptions">
246 <li
247 class="array-element"
248 ng-repeat="item in vm.field.availableOptions"
249 dnd-draggable="item"
250 dnd-moved="vm.field.availableOptions.splice($index, 1)"
251 dnd-effect-allowed="move"
252 dnd-selected="models.selected = item"
253 >
254 <div class="well well-sm text-center">
255 {{item}}
256 </div>
257 </li>
258 <div class="clearfix"></div>
259 </ul>
260 </div>
261 </div>
Arpit Agarwal34b63832016-08-08 11:59:45 -0700262 `,
263 bindToController: true,
264 controllerAs: 'vm',
Matteo Scandolo65116c42016-09-21 17:06:23 -0700265 controller: function($attrs, $scope, XosFormHelpers, LabelFormatter, _){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700266
Arpit Agarwal34b63832016-08-08 11:59:45 -0700267 if(!this.name){
268 throw new Error('[xosField] Please provide a field name');
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700269 }
Arpit Agarwal34b63832016-08-08 11:59:45 -0700270 if(!this.field){
271 throw new Error('[xosField] Please provide a field definition');
272 }
273 if(!this.field.type){
274 throw new Error('[xosField] Please provide a type in the field definition');
275 }
276 if(!$attrs.ngModel){
277 throw new Error('[xosField] Please provide an ng-model');
278 }
279 this.getType = XosFormHelpers._getFieldFormat;
280 this.formatLabel = LabelFormatter.format;
281
282 this.isEmptyObject = o => o ? Object.keys(o).length === 0 : true;
Matteo Scandolo65116c42016-09-21 17:06:23 -0700283
284 if(this.field.type === 'array'){
285 $scope.$watch(() => this.ngModel.length, () => {
286 this.field.availableOptions = _.difference(this.field.options, this.ngModel);
287 });
288 }
289
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700290 }
291 })
292
293/**
294 * @ngdoc directive
295 * @name xos.uiComponents.directive:xosCustomValidator
296 * @restrict A
297 * @description The xosCustomValidator directive.
298 * This component apply a custom validation function
299 * @param {function} customValidator The function that execute the validation.
300 *
301 * You should do your validation here and return true | false,
302 * or alternatively you can return an array [errorName, true|false]
303 */
304 .directive('xosCustomValidator', function(){
305 return {
306 restrict: 'A',
307 scope: {
308 fn: '=customValidator'
309 },
310 require: 'ngModel',
311 link: function(scope, element, attr, ctrl){
312 if(!angular.isFunction(scope.fn)){
313 return;
314 }
315
316 function customValidatorWrapper(ngModelValue) {
317 const valid = scope.fn(ngModelValue);
318 if(angular.isArray(valid)){
319 // ES6 spread rocks over fn.apply()
320 ctrl.$setValidity(...valid);
321 }
322 else{
323 ctrl.$setValidity('custom', valid);
324 }
325 return ngModelValue;
326 }
327
328 ctrl.$parsers.push(customValidatorWrapper);
329 }
330 };
331 });
332})();