blob: fa02dbb624e739c05cacf4af41fbb2711052e209 [file] [log] [blame]
Matteo Scandolo974c0e42016-05-25 16:02:16 -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
Matteo Scandolob9fb6252016-05-26 15:09:55 -070029 *
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(){
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700106 this.name1 = 'input-name';
107 this.field1 = {label: 'My Object Field:', type: 'object'};
108 this.model1 = {
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700109 name: 'Jhon',
110 age: '25',
111 email: 'jhon@thewall.ru',
112 active: true
113 };
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700114
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 }
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700132 });
133 </file>
134 <file name="index.html">
135 <div ng-controller="SampleCtrl as vm">
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700136 <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>
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700141 </div>
142 </file>
143 </example>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700144 */
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700145 .directive('xosField', function(RecursionHelper){
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700146 return {
147 restrict: 'E',
148 scope: {
149 name: '=',
150 field: '=',
151 ngModel: '='
152 },
153 template: `
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700154 <label ng-if="vm.field.type !== 'object'">{{vm.field.label}}</label>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700155 <input
arpiagariu47cd6892016-06-03 16:41:39 -0700156 ng-if="vm.field.type !== 'boolean' && vm.field.type !== 'object' && vm.field.type !== 'select'"
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700157 type="{{vm.field.type}}"
158 name="{{vm.name}}"
159 class="form-control"
160 ng-model="vm.ngModel"
161 ng-minlength="vm.field.validators.minlength || 0"
162 ng-maxlength="vm.field.validators.maxlength || 2000"
163 ng-required="vm.field.validators.required || false" />
arpiagariu47cd6892016-06-03 16:41:39 -0700164 <select class="form-control" ng-if ="vm.field.type === 'select'"
165 name = "{{vm.name}}"
arpiagariu4629a6d2016-06-07 15:53:33 -0700166 ng-options="item.id as item.label for item in vm.field.options"
arpiagariu47cd6892016-06-03 16:41:39 -0700167 ng-model="vm.ngModel"
168 ng-required="vm.field.validators.required || false">
169 </select>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700170 <span class="boolean-field" ng-if="vm.field.type === 'boolean'">
arpiagariu10bd9cb2016-06-10 13:28:34 -0700171 <a href="#"
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700172 class="btn btn-success"
173 ng-show="vm.ngModel"
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700174 ng-click="vm.ngModel = false">
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700175 <i class="glyphicon glyphicon-ok"></i>
arpiagariud4f6db12016-06-06 15:25:28 -0700176 </a>
arpiagariu10bd9cb2016-06-10 13:28:34 -0700177 <a href="#"
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700178 class="btn btn-danger"
179 ng-show="!vm.ngModel"
Matteo Scandolo3dfbced2016-06-15 16:42:12 -0700180 ng-click="vm.ngModel = true">
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700181 <i class="glyphicon glyphicon-remove"></i>
arpiagariud4f6db12016-06-06 15:25:28 -0700182 </a>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700183 </span>
184 <div
185 class="panel panel-default object-field"
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700186 ng-if="vm.field.type == 'object' && (!vm.isEmptyObject(vm.ngModel) || !vm.isEmptyObject(vm.field.properties))"
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700187 >
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700188 <div class="panel-heading">{{vm.field.label}}</div>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700189 <div class="panel-body">
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700190 <div ng-if="!vm.field.properties" ng-repeat="(k, v) in vm.ngModel">
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700191 <xos-field
192 name="k"
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700193 field="{label: vm.formatLabel(k), type: vm.getType(v)}"
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700194 ng-model="v">
195 </xos-field>
196 </div>
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700197 <div ng-if="vm.field.properties" ng-repeat="(k, v) in vm.field.properties">
198 <xos-field
199 name="k"
200 field="{
201 label: v.label || vm.formatLabel(k),
202 type: v.type,
203 validators: v.validators
204 }"
205 ng-model="vm.ngModel[k]">
206 </xos-field>
207 </div>
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700208 </div>
209 </div>
210 `,
211 bindToController: true,
212 controllerAs: 'vm',
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700213 // the compile cicle is needed to support recursion
214 compile: function (element) {
215 return RecursionHelper.compile(element);
216 },
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700217 controller: function($attrs, XosFormHelpers, LabelFormatter){
Matteo Scandolodee14362016-06-08 15:11:39 -0700218
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700219 if(!this.name){
220 throw new Error('[xosField] Please provide a field name');
221 }
222 if(!this.field){
223 throw new Error('[xosField] Please provide a field definition');
224 }
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700225 if(!this.field.type){
226 throw new Error('[xosField] Please provide a type in the field definition');
227 }
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700228 if(!$attrs.ngModel){
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700229 throw new Error('[xosField] Please provide an ng-model');
230 }
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700231 this.getType = XosFormHelpers._getFieldFormat;
Matteo Scandolob9fb6252016-05-26 15:09:55 -0700232 this.formatLabel = LabelFormatter.format;
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -0700233
Matteo Scandolobd4057e2016-06-08 14:27:30 -0700234 this.isEmptyObject = o => o ? Object.keys(o).length === 0 : true;
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700235 }
236 }
237 });
238})();