blob: 21b2f4e4c695289a15ace607acb9b6101f1f1a7f [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 };
85 });
86 </file>
87 <file name="index.html">
88 <div ng-controller="SampleCtrl as vm">
89 <xos-field ng-model="vm.field1.model" name="vm.field1.name" field="vm.field1.field"></xos-field>
90 <xos-field ng-model="vm.field2.model" name="vm.field2.name" field="vm.field2.field"></xos-field>
91 <xos-field ng-model="vm.field3.model" name="vm.field3.name" field="vm.field3.field"></xos-field>
92 <xos-field ng-model="vm.field4.model" name="vm.field4.name" field="vm.field4.field"></xos-field>
93 </div>
94 </file>
95 </example>
96
97 # This element is recursive
98
99 <example module="sampleField3">
100 <file name="script.js">
101 angular.module('sampleField3', ['xos.uiComponents'])
102 .factory('_', function($window){
103 return $window._;
104 })
105 .controller('SampleCtrl', function(){
106 this.name1 = 'input-name';
107 this.field1 = {label: 'My Object Field:', type: 'object'};
108 this.model1 = {
109 name: 'Jhon',
110 age: '25',
111 email: 'jhon@thewall.ru',
112 active: true
113 };
114
115 this.name2 = 'another-name';
116 this.field2 = {
117 label: 'Empty Object Field',
118 type: 'object',
119 properties: {
120 foo: {
121 label: 'FooLabel:',
122 type: 'string',
123 validators: {
124 required: true
125 }
126 },
127 bar: {
128 type: 'number'
129 }
130 }
131 }
132 });
133 </file>
134 <file name="index.html">
135 <div ng-controller="SampleCtrl as vm">
136 <h4>Autogenerated object field</h4>
137 <xos-field ng-model="vm.model1" name="vm.name1" field="vm.field1"></xos-field>
138
139 <h4>Configured object field</h4>
140 <xos-field ng-model="vm.model2" name="vm.name2" field="vm.field2"></xos-field>
141 </div>
142 </file>
143 </example>
144 */
145 .directive('xosField', function(RecursionHelper){
146 return {
147 restrict: 'E',
148 scope: {
149 name: '=',
150 field: '=',
151 ngModel: '='
152 },
153 template: `
154 <label ng-if="vm.field.type !== 'object'">{{vm.field.label}}</label>
155 <input
156 xos-custom-validator custom-validator="vm.field.validators.custom || null"
157 ng-if="vm.field.type !== 'boolean' && vm.field.type !== 'object' && vm.field.type !== 'select'"
158 type="{{vm.field.type}}"
159 name="{{vm.name}}"
160 class="form-control"
161 ng-model="vm.ngModel"
162 ng-minlength="vm.field.validators.minlength || 0"
163 ng-maxlength="vm.field.validators.maxlength || 2000"
164 ng-required="vm.field.validators.required || false" />
165 <select class="form-control" ng-if ="vm.field.type === 'select'"
166 name = "{{vm.name}}"
167 ng-options="item.id as item.label for item in vm.field.options"
168 ng-model="vm.ngModel"
169 ng-required="vm.field.validators.required || false">
170 </select>
171 <span class="boolean-field" ng-if="vm.field.type === 'boolean'">
172 <a href="#"
173 class="btn btn-success"
174 ng-show="vm.ngModel"
175 ng-click="vm.ngModel = false">
176 <i class="glyphicon glyphicon-ok"></i>
177 </a>
178 <a href="#"
179 class="btn btn-danger"
180 ng-show="!vm.ngModel"
181 ng-click="vm.ngModel = true">
182 <i class="glyphicon glyphicon-remove"></i>
183 </a>
184 </span>
185 <div
186 class="panel panel-default object-field"
187 ng-if="vm.field.type == 'object' && (!vm.isEmptyObject(vm.ngModel) || !vm.isEmptyObject(vm.field.properties))"
188 >
189 <div class="panel-heading">{{vm.field.label}}</div>
190 <div class="panel-body">
191 <div ng-if="!vm.field.properties" ng-repeat="(k, v) in vm.ngModel">
192 <xos-field
193 name="k"
194 field="{label: vm.formatLabel(k), type: vm.getType(v)}"
195 ng-model="v">
196 </xos-field>
197 </div>
198 <div ng-if="vm.field.properties" ng-repeat="(k, v) in vm.field.properties">
199 <xos-field
200 name="k"
201 field="{
202 label: v.label || vm.formatLabel(k),
203 type: v.type,
204 validators: v.validators
205 }"
206 ng-model="vm.ngModel[k]">
207 </xos-field>
208 </div>
209 </div>
210 </div>
211 `,
212 bindToController: true,
213 controllerAs: 'vm',
214 // the compile cicle is needed to support recursion
215 compile: function (element) {
216 return RecursionHelper.compile(element);
217 },
218 controller: function($attrs, XosFormHelpers, LabelFormatter){
219
220 if(!this.name){
221 throw new Error('[xosField] Please provide a field name');
222 }
223 if(!this.field){
224 throw new Error('[xosField] Please provide a field definition');
225 }
226 if(!this.field.type){
227 throw new Error('[xosField] Please provide a type in the field definition');
228 }
229 if(!$attrs.ngModel){
230 throw new Error('[xosField] Please provide an ng-model');
231 }
232 this.getType = XosFormHelpers._getFieldFormat;
233 this.formatLabel = LabelFormatter.format;
234
235 this.isEmptyObject = o => o ? Object.keys(o).length === 0 : true;
236 }
237 }
238 })
239
240/**
241 * @ngdoc directive
242 * @name xos.uiComponents.directive:xosCustomValidator
243 * @restrict A
244 * @description The xosCustomValidator directive.
245 * This component apply a custom validation function
246 * @param {function} customValidator The function that execute the validation.
247 *
248 * You should do your validation here and return true | false,
249 * or alternatively you can return an array [errorName, true|false]
250 */
251 .directive('xosCustomValidator', function(){
252 return {
253 restrict: 'A',
254 scope: {
255 fn: '=customValidator'
256 },
257 require: 'ngModel',
258 link: function(scope, element, attr, ctrl){
259 if(!angular.isFunction(scope.fn)){
260 return;
261 }
262
263 function customValidatorWrapper(ngModelValue) {
264 const valid = scope.fn(ngModelValue);
265 if(angular.isArray(valid)){
266 // ES6 spread rocks over fn.apply()
267 ctrl.$setValidity(...valid);
268 }
269 else{
270 ctrl.$setValidity('custom', valid);
271 }
272 return ngModelValue;
273 }
274
275 ctrl.$parsers.push(customValidatorWrapper);
276 }
277 };
278 });
279})();