-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCustomFields.js
executable file
·92 lines (87 loc) · 3.73 KB
/
CustomFields.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(function(){
'use strict';
CustomFields.$inject = [];
function CustomFields(){
return{
restrict: 'E',
scope: {
fields: '=',
useLabels: '?='
},
bindToController: true,
controller: ['$scope','$element','$attrs', '$http', '$compile','$filter', function($scope, $element, $attrs, $http, $compile, $filter) {
let ctrl = this;
ctrl.open = function() {
ctrl.isDatePickerOpen = !ctrl.isDatePickerOpen
}
setTimeout(function() {
if (!ctrl.fields) throw 'O componente gumgaCustomFields requer o escopo populado com os fields para geração do template.'
angular.forEach(ctrl.fields.gumgaCustomFields, (v) => {
if (
angular.isString(v.field.options) &&
v.field.type == 'SELECTION' &&
v.field.options.charAt(0) == '['
) {
v.field.selection = JSON.parse(v.field.options)
} else if (
angular.isString(v.field.options) &&
v.field.type == 'SELECTION' &&
v.field.options.charAt(0) != '['
) {
$http.get(v.field.options).then((response) => {
v.field.selection = response.data[v.field.optionsCollection]
v.field.selection.forEach(
b => b[v.field.optionValueField] = b[v.field.optionValueField].toString()
)
}, (error) => {
console.error(error);
});
}
if (v.field.type == 'DATE') {
v.dateValue = new Date(v.dateValue)
$scope.$apply()
}
})
}, 500)
let template = `
<div class="row" ng-if="f.field.active" ng-repeat="f in ctrl.fields.gumgaCustomFields">
<div class="col-md-12">
<label ng-bind="f.field.name" ng-if="!ctrl.useLabels" gumga-translate-tag="f.field.translateKey"></label>
<label ng-if="ctrl.useLabels">{{f.field.translateKey}}</label>
<div ng-switch="f.field.type" class="form-group">
<div ng-switch-when="TEXT">
<input type="text" ng-model="f.textValue" class="form-control" />
</div>
<div ng-switch-when="NUMBER">
<input type="number" ng-model="f.numberValue" class="form-control" />
</div>
<div ng-switch-when="DATE">
<p class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="dd/MM/yyyy" ng-model="f.dateValue" is-open="ctrl.isDatePickerOpen"/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="ctrl.open()">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</p>
</div>
<div ng-switch-when="SELECTION">
<select ng-options="opt[f.field.optionValueField] as opt[f.field.optionLabelField] for opt in f.field.selection" ng-model="f.textValue" class="form-control"></select>
</div>
<div ng-switch-when="LOGIC">
<button type="button" class="btn" ng-class="{'btn-success': f.logicValue, 'btn-default': !f.logicValue}" ng-model="f.logicValue" uib-btn-checkbox btn-checkbox-true="true" btn-checkbox-false="false">
{{(f.logicValue) ? "On" : "Off" }}
</button>
</div>
</div>
</div>
</div>
`;
$element.append($compile(template)($scope));
}],
controllerAs: 'ctrl',
}
}
angular.module('gumga.directives.customfields', [])
.directive('gumgaCustomFields', CustomFields);
})();