blob: f33c6398c8d0b6eb9ddc6e5f2887981813730467 [file] [log] [blame]
Matteo Scandolo7bc39c42016-04-20 11:38:42 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 4/18/16.
7 */
8
9(function () {
10 'use strict';
11
12 angular.module('xos.uiComponents')
13
14 /**
15 * @ngdoc directive
16 * @name xos.uiComponents.directive:xosForm
17 * @restrict E
18 * @description The xos-form directive
19 * @param {Object} config The configuration object
20 * ```
21 * {
22 * exclude: ['id', 'validators', 'created', 'updated', 'deleted'], //field to be skipped in the form, the provide values are concatenated
23 * actions: [ // define the form buttons with related callback
24 * {
25 label: 'save',
26 icon: 'ok', // refers to bootstraps glyphicon
27 cb: (user) => { // receive the model
28 console.log(user);
29 },
30 class: 'success'
31 }
32 * ]
33 * }
34 * ```
35 * @element ANY
36 * @scope
37 * @example
38 <example module="sampleAlert1">
39 <file name="index.html">
40 <div ng-controller="SampleCtrl1 as vm">
41
42 </div>
43 </file>
44 <file name="script.js">
45 angular.module('sampleAlert1', ['xos.uiComponents'])
46 .controller('SampleCtrl1', function(){
47 this.config1 = {
48 exclude: ['password', 'last_login']
49 };
50 });
51 </file>
52 </example>
53
54 **/
55
56 .directive('xosForm', function(){
57 return {
58 restrict: 'E',
59 scope: {
60 config: '=',
61 ngModel: '='
62 },
63 template: `
64 <ng-form name="vm.config.formName || 'form'">
65 <div class="form-group" ng-repeat="field in vm.formField">
66 <label>{{vm.formatLabel(field.label)}}</label>
67 <input type="text" name="" class="form-control" ng-model="vm.ngModel[field]"/>
68 </div>
69 <div class="form-group" ng-if="vm.config.actions">
70 <button href=""
71 ng-repeat="action in vm.config.actions"
72 ng-click="action.cb(vm.ngModel)"
73 class="btn btn-{{action.class}}"
74 title="{{action.label}}">
75 <i class="glyphicon glyphicon-{{action.icon}}"></i>
76 {{action.label}}
77 </button>
78 </div>
79 </ng-form>
80 `,
81 bindToController: true,
82 controllerAs: 'vm',
83 controller: function($scope, $log, _, LabelFormatter, XosFormHelpers){
84
85 if(!this.config){
86 throw new Error('[xosForm] Please provide a configuration via the "config" attribute');
87 }
88
89 if(!this.config.actions){
90 throw new Error('[xosForm] Please provide an action list in the configuration');
91 }
92
93 this.formatLabel = LabelFormatter.format;
94
95 this.excludedField = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status'];
96 if(this.config && this.config.exclude){
97 this.excludedField = this.excludedField.concat(this.config.exclude);
98 }
99
100
101 this.formField = [];
102 $scope.$watch(() => this.ngModel, (model) => {
103 if(!model){
104 return;
105 }
106 this.formField = XosFormHelpers.buildFormStructure(_.difference(Object.keys(model), this.excludedField));
107 });
108
109 }
110 }
111 })
112 .service('XosFormHelpers', function(_, LabelFormatter){
113
114 this._getFieldFormat = (value) => {
115
116 // check if is date
117 if (_.isDate(value)){
118 return 'date';
119 }
120
121 // check if is boolean
122 // isNaN(false) = false, false is a number (0), true is a number (1)
123 if(typeof value === 'boolean'){
124 return 'boolean';
125 }
126
127 // check if a string is a number
128 if(!isNaN(value)){
129 return 'number';
130 }
131
132 return typeof value;
133 };
134
135 this.buildFormStructure = (modelField, customField, model) => {
136 return _.reduce(Object.keys(modelField), (form, f) => {
137 form[f] = {
138 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
139 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
140 validators: {}
141 };
142 return form;
143 }, {});
144 };
145
146 this.parseModelField = (fields) => {
147 return _.reduce(fields, (form, f) => {
148 form[f] = {};
149 return form;
150 }, {});
151 }
152
153 })
154})();