blob: c2aa62ddbde249173578c366caf5b66d6b996d45 [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
29 */
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070030 .directive('xosField', function(RecursionHelper){
Matteo Scandolo974c0e42016-05-25 16:02:16 -070031 return {
32 restrict: 'E',
33 scope: {
34 name: '=',
35 field: '=',
36 ngModel: '='
37 },
38 template: `
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070039 <label ng-if="vm.field.type !== 'object'">{{vm.field.label}}</label>
Matteo Scandolo974c0e42016-05-25 16:02:16 -070040 <input
41 ng-if="vm.field.type !== 'boolean' && vm.field.type !== 'object'"
42 type="{{vm.field.type}}"
43 name="{{vm.name}}"
44 class="form-control"
45 ng-model="vm.ngModel"
46 ng-minlength="vm.field.validators.minlength || 0"
47 ng-maxlength="vm.field.validators.maxlength || 2000"
48 ng-required="vm.field.validators.required || false" />
49 <span class="boolean-field" ng-if="vm.field.type === 'boolean'">
50 <button
51 class="btn btn-success"
52 ng-show="vm.ngModel"
53 ng-click="vm.ngModel = false">
54 <i class="glyphicon glyphicon-ok"></i>
55 </button>
56 <button
57 class="btn btn-danger"
58 ng-show="!vm.ngModel"
59 ng-click="vm.ngModel = true">
60 <i class="glyphicon glyphicon-remove"></i>
61 </button>
62 </span>
63 <div
64 class="panel panel-default object-field"
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070065 ng-if="vm.field.type == 'object' && !vm.isEmptyObject(vm.ngModel)"
Matteo Scandolo974c0e42016-05-25 16:02:16 -070066 >
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070067 <div class="panel-heading">{{vm.field.label}}</div>
Matteo Scandolo974c0e42016-05-25 16:02:16 -070068 <div class="panel-body">
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070069 <div ng-repeat="(k, v) in vm.ngModel">
70 <xos-field
71 name="k"
72 field="{label: k, type: vm.getType(v)}"
73 ng-model="v">
74 </xos-field>
75 </div>
Matteo Scandolo974c0e42016-05-25 16:02:16 -070076 </div>
77 </div>
78 `,
79 bindToController: true,
80 controllerAs: 'vm',
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070081 // the compile cicle is needed to support recursion
82 compile: function (element) {
83 return RecursionHelper.compile(element);
84 },
85 controller: function(XosFormHelpers){
86 // console.log('Field: ', this.name, this.field, this.ngModel);
Matteo Scandolo974c0e42016-05-25 16:02:16 -070087 if(!this.name){
88 throw new Error('[xosField] Please provide a field name');
89 }
90 if(!this.field){
91 throw new Error('[xosField] Please provide a field definition');
92 }
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070093 if(!angular.isDefined(this.ngModel)){
Matteo Scandolo974c0e42016-05-25 16:02:16 -070094 throw new Error('[xosField] Please provide an ng-model');
95 }
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070096
97 this.getType = XosFormHelpers._getFieldFormat;
98
99 this.isEmptyObject = o => Object.keys(o).length === 0;
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700100 }
101 }
102 });
103})();