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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
/**
* Usage:
* <div piwik-form-field="{...}">
*/
(function () {
angular.module('piwikApp').directive('piwikFormField', piwikFormField);
piwikFormField.$inject = ['piwik'];
function piwikFormField(piwik){
return {
restrict: 'A',
scope: {
piwikFormField: '=',
allSettings: '='
},
templateUrl: 'plugins/CorePluginsAdmin/angularjs/form-field/form-field.directive.html?cb=' + piwik.cacheBuster,
compile: function (element, attrs) {
function evaluateConditionalExpression(scope, field)
{
if (!field.condition) {
return;
}
var values = {};
angular.forEach(scope.allSettings, function (setting) {
if (setting.value === '0') {
values[setting.name] = 0;
} else {
values[setting.name] = setting.value;
}
});
field.showField = scope.$eval(field.condition, values);
}
function hasUiControl(field, uiControlType)
{
return field.uiControl === uiControlType;
}
function isSelectControl(field)
{
return hasUiControl(field, 'select') || hasUiControl(field, 'multiselect');
}
function hasGroupedValues(availableValues)
{
if (!angular.isObject(availableValues)
|| angular.isArray(availableValues)) {
return false;
}
var key;
for (key in availableValues) {
if (Object.prototype.hasOwnProperty.call(availableValues, key)) {
if (angular.isObject(availableValues[key])) {
return true;
} else {
return false;
}
}
}
return false;
}
return function (scope, element, attrs) {
var field = scope.piwikFormField;
if (angular.isArray(field.defaultValue)) {
field.defaultValue = field.defaultValue.join(',');
}
if (isSelectControl(field) && field.availableValues) {
var availableValues = field.availableValues;
if (!hasGroupedValues(availableValues)) {
availableValues = {'': availableValues};
}
var flatValues = [];
angular.forEach(availableValues, function (values, group) {
angular.forEach(values, function (value, key) {
if (field.type === 'integer' && angular.isString(key)) {
key = parseInt(key, 10);
}
flatValues.push({group: group, key: key, value: value});
});
});
field.availableValues = flatValues;
}
field.showField = true;
if (field.condition && scope.allSettings) {
evaluateConditionalExpression(scope, field);
for (var key in scope.allSettings) {
if(scope.allSettings.hasOwnProperty(key)) {
scope.$watchCollection('allSettings[' + key + '].value', function (val, oldVal) {
if (val !== oldVal) {
evaluateConditionalExpression(scope, field);
}
});
}
}
}
scope.formField = field;
};
}
};
}
})();
|