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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*!
* 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-field>
*
* eg <div piwik-field uicontrol="select"
* title="{{ 'SitesManager_Timezone'|translate }}"
* value="site.timezone"
* options="timezones"
* inline-help="test"
* description=""
* introduction=""
* name=""
* placeholder=""
* rows="3"
* autocomplete="off"
* data-disabled="true"
* full-width="true"
* templateFile=""></div>
*
* templateFile allows to render a custom template
* We do not use type= attribute here as it would match some CSS from input type=radio etc
*/
(function () {
angular.module('piwikApp').directive('piwikField', piwikField);
piwikField.$inject = ['piwik', '$compile'];
function piwikField(piwik, $compile){
return {
restrict: 'A',
require: '?ngModel',
scope: {
uicontrol: '@',
name: '@',
value: '@',
default: '@',
options: '=',
description: '@',
introduction: '@',
title: '@',
inlineHelp: '@',
disabled: '=',
uiControlAttributes: '=',
autocomplete: '@',
condition: '@',
varType: '@',
autofocus: '@',
tabindex: '@',
templateFile: '@',
fullWidth: '@',
maxlength: '@',
required: '@',
placeholder: '@',
rows: '@',
min: '@',
max: '@'
},
template: '<div piwik-form-field="field"></div>',
link: function(scope, elm, attrs, ctrl) {
if (!ctrl) {
return;
}
// load init value
if (scope.field.value !== undefined && scope.field.value !== null) {
ctrl.$setViewValue(scope.field.value);
} else if (ctrl.$viewValue) {
scope.field.value = ctrl.$viewValue;
}
// view -> model
scope.$watch('field.value', function (val, oldVal) {
if (val !== oldVal && val !== ctrl.$viewValue) {
ctrl.$setViewValue(val);
}
});
// model -> view
ctrl.$render = function() {
scope.field.value = ctrl.$viewValue;
};
},
controller: function ($scope) {
var field = {};
field.uiControl = $scope.uicontrol;
if ($scope.varType) {
field.type = $scope.varType;
} else if (field.uiControl === 'multiselect') {
field.type = 'array';
} else if (field.uiControl === 'checkbox') {
field.type = 'boolean';
} else if (field.uiControl === 'site') {
field.type = 'object';
} else if (field.uiControl === 'number') {
field.type = 'integer';
} else {
field.type = 'string';
}
field.name = $scope.name;
field.value = $scope.value;
field.defaultValue = $scope.default;
field.availableValues = $scope.options;
field.description = $scope.description;
field.introduction = $scope.introduction;
field.inlineHelp = $scope.inlineHelp;
field.templateFile = $scope.templateFile;
field.title = $scope.title;
field.uiControlAttributes = $scope.uiControlAttributes || {};
field.fullWidth = !!$scope.fullWidth;
if (field.type === 'array' && angular.isString(field.value) && field.value) {
field.value = JSON.parse(field.value);
}
var i = 0, attribute;
var attributes = ['disabled', 'autocomplete', 'tabindex', 'autofocus', 'rows', 'required', 'maxlength', 'placeholder', 'min', 'max'];
for (i; i < attributes.length; i++) {
attribute = attributes[i];
if (!!$scope[attribute]) {
field.uiControlAttributes[attribute] = $scope[attribute];
}
}
$scope.field = field;
$scope.$watch('options', function (val, oldVal) {
if (val !== oldVal) {
$scope.field.availableValues = val;
}
});
$scope.$watch('title', function (val, oldVal) {
if (val !== oldVal) {
$scope.field.title = val;
}
});
$scope.$watch('inlineHelp', function (val, oldVal) {
if (val !== oldVal) {
$scope.field.inlineHelp = val;
}
});
if ('undefined' !== typeof $scope.placeholder && $scope.placeholder !== null) {
$scope.$watch('placeholder', function (val, oldVal) {
if (val !== oldVal) {
$scope.field.uiControlAttributes.placeholder = val;
}
});
}
$scope.$watch('disabled', function (val, oldVal) {
if (val !== oldVal) {
$scope.field.uiControlAttributes.disabled = val;
}
});
}
};
}
})();
|