1
|
var module = angular.module('addRepoApiUI', ['localytics.directives']);
|
2
|
|
3
|
module.directive('bsHasError', function() {
|
4
|
return {
|
5
|
restrict: "A",
|
6
|
link: function(scope, element, attrs, ctrl) {
|
7
|
element.toggleClass('has-feedback', true);
|
8
|
var input = element.find('input[ng-model], select[ng-model]');
|
9
|
if (input) {
|
10
|
scope.$watch(function() {
|
11
|
if (input.hasClass('ng-invalid')) {
|
12
|
return 0;
|
13
|
} else if (input.hasClass('empty')) {
|
14
|
return 1;
|
15
|
} else {
|
16
|
return 2;
|
17
|
}
|
18
|
}, function(code) {
|
19
|
if (code < 0) return;
|
20
|
|
21
|
element.toggleClass('has-error', (code == 0));
|
22
|
element.toggleClass('has-warning', (code == 1));
|
23
|
element.toggleClass('has-success', (code == 2));
|
24
|
|
25
|
var feedback = element.find('.form-control-feedback');
|
26
|
if (feedback) {
|
27
|
feedback.toggleClass('glyphicon-remove', (code == 0));
|
28
|
feedback.toggleClass('glyphicon-warning-sign', (code == 1));
|
29
|
feedback.toggleClass('glyphicon-ok', (code == 2));
|
30
|
}
|
31
|
});
|
32
|
}
|
33
|
}
|
34
|
};
|
35
|
});
|
36
|
|
37
|
module.directive('ngSelectRepoField', function($http) {
|
38
|
return {
|
39
|
restrict: 'E',
|
40
|
scope: {
|
41
|
'label' : '@',
|
42
|
'typology' : '=',
|
43
|
'selection' : '='
|
44
|
},
|
45
|
templateUrl: '../resources/html/addRepoApi/ngSelectRepoField.html',
|
46
|
link: function(scope, element, attrs, ctrl) {
|
47
|
scope.repositories = [];
|
48
|
scope.current = '';
|
49
|
scope.$watch('typology', function() {
|
50
|
scope.repositories = [];
|
51
|
if (scope.typology) {
|
52
|
showSpinner();
|
53
|
$http.get('listRepositories.json?type=' + scope.typology).success(function(data) {
|
54
|
scope.repositories = data;
|
55
|
hideSpinner();
|
56
|
}).error(function() {
|
57
|
show_notification("error", "An error occurred while fetching datasource of type " + scope.typology);
|
58
|
hideSpinner();
|
59
|
});
|
60
|
}
|
61
|
});
|
62
|
}
|
63
|
}
|
64
|
});
|
65
|
|
66
|
module.directive('ngFixedValueField', function() {
|
67
|
return {
|
68
|
restrict: 'E',
|
69
|
scope: {
|
70
|
'label' : '@',
|
71
|
'value' : '='
|
72
|
},
|
73
|
templateUrl: '../resources/html/addRepoApi/ngFixedValueField.html'
|
74
|
}
|
75
|
});
|
76
|
|
77
|
module.directive('ngApiIdValidation', function() {
|
78
|
return {
|
79
|
restrict: 'A',
|
80
|
scope: {
|
81
|
'valid' : '='
|
82
|
},
|
83
|
require: 'ng-model',
|
84
|
link: function(scope, element, attrs, ctrl) {
|
85
|
scope.$watch('valid', function() {
|
86
|
ctrl.$setValidity('wrongId', scope.valid);
|
87
|
});
|
88
|
}
|
89
|
}
|
90
|
});
|
91
|
|
92
|
module.directive('ngApiIdField', function() {
|
93
|
return {
|
94
|
restrict: 'E',
|
95
|
scope: {
|
96
|
'label' : '@',
|
97
|
'prefix' : '@',
|
98
|
'selection' : '=',
|
99
|
'exclusion' : '='
|
100
|
},
|
101
|
templateUrl: '../resources/html/addRepoApi/ngApiIdField.html',
|
102
|
link: function(scope, element, attrs) {
|
103
|
scope.suffix = '';
|
104
|
scope.alert = '';
|
105
|
scope.valid = false;
|
106
|
scope.required = true;
|
107
|
scope.mypattern = new RegExp(scope.regex);
|
108
|
|
109
|
scope.validate = function(b, newValue, message) {
|
110
|
scope.valid = b;
|
111
|
scope.selection = newValue;
|
112
|
scope.alert = message;
|
113
|
element.toggleClass('has-error', !b);
|
114
|
element.toggleClass('has-success', b);
|
115
|
var feedback = element.find('.form-control-feedback');
|
116
|
if (feedback) {
|
117
|
feedback.toggleClass('glyphicon-remove', !b);
|
118
|
feedback.toggleClass('glyphicon-ok', b);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
scope.$watch('suffix', function() {
|
123
|
var tmpId = scope.prefix + scope.suffix;
|
124
|
|
125
|
if (!scope.suffix) {
|
126
|
scope.validate(false, '', 'ID is empty');
|
127
|
} else if (!scope.suffix.match(/^[A-Za-z0-9_]*$/g)) {
|
128
|
scope.validate(false, '', 'Invalid format, valid chars are: A-Za-z0-9_');
|
129
|
} else if($.inArray(tmpId, scope.exclusion) > -1) {
|
130
|
scope.validate(false, '', 'The ID already exists');
|
131
|
} else {
|
132
|
scope.selection = tmpId;
|
133
|
scope.validate(true, tmpId, '');
|
134
|
}
|
135
|
});
|
136
|
}
|
137
|
}
|
138
|
});
|
139
|
|
140
|
module.directive('ngSelectVocabularyField', function() {
|
141
|
return {
|
142
|
restrict: 'E',
|
143
|
scope: {
|
144
|
'label' : '@',
|
145
|
'vocabulary' : '=',
|
146
|
'selection' : '=',
|
147
|
'contextualParams' : '='
|
148
|
},
|
149
|
templateUrl: '../resources/html/addRepoApi/ngSelectVocabularyField.html',
|
150
|
link: function(scope, elem, attrs) {
|
151
|
scope.required = true;
|
152
|
scope.selectId = 'select_' + scope.label.replace( /\s/g, "_").toLowerCase();
|
153
|
|
154
|
scope.$watch('selection', function() {
|
155
|
scope.contextualParams = [];
|
156
|
angular.forEach(scope.vocabulary, function(term){
|
157
|
if (term.name == scope.selection) {
|
158
|
scope.contextualParams = term.params;
|
159
|
}
|
160
|
});
|
161
|
});
|
162
|
}
|
163
|
}
|
164
|
});
|
165
|
|
166
|
module.filter('vocabularyTerm', function() {
|
167
|
return function(term) {
|
168
|
if(term.desc) {
|
169
|
return term.name + ' (' + term.desc + ')';
|
170
|
} else {
|
171
|
return term.name;
|
172
|
}
|
173
|
};
|
174
|
});
|
175
|
|
176
|
module.directive('ngSimpleEditField', function() {
|
177
|
return {
|
178
|
restrict: 'E',
|
179
|
scope: {
|
180
|
'label' : '@',
|
181
|
'regex' : '@',
|
182
|
'optional' : '@',
|
183
|
'type' : '@',
|
184
|
'selection' : '=',
|
185
|
},
|
186
|
templateUrl: '../resources/html/addRepoApi/ngSimpleEditField.html',
|
187
|
link: function(scope, element, attrs) {
|
188
|
scope.required = (scope.optional != 'true');
|
189
|
if (scope.regex) { scope.mypattern = new RegExp(scope.regex); }
|
190
|
else if (scope.type == 'NUMBER') { scope.mypattern = new RegExp("^[-+]?[0-9]+(\.[0-9]+)?$"); }
|
191
|
else if (scope.type == 'BOOLEAN') { scope.mypattern = new RegExp("^(true|false)$"); }
|
192
|
else { scope.mypattern = new RegExp(".+"); }
|
193
|
}
|
194
|
}
|
195
|
});
|
196
|
|
197
|
module.directive('ngSimpleSelectField', function() {
|
198
|
return {
|
199
|
restrict: 'E',
|
200
|
scope: {
|
201
|
'label' : '@',
|
202
|
'optional' : '@',
|
203
|
'refreshFunction' : '&',
|
204
|
'validValues' : '=',
|
205
|
'selection' : '='
|
206
|
},
|
207
|
templateUrl: '../resources/html/addRepoApi/ngSimpleSelectField.html',
|
208
|
link: function(scope, element, attrs) {
|
209
|
scope.required = (scope.optional != 'true');
|
210
|
scope.populateList = function() {
|
211
|
scope.validValues = scope.functionList();
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
});
|
216
|
|
217
|
|
218
|
module.directive('ngMultiSelectField', function() {
|
219
|
return {
|
220
|
restrict: 'E',
|
221
|
scope: {
|
222
|
'label' : '@',
|
223
|
'optional' : '@',
|
224
|
'refreshFunction' : '&',
|
225
|
'validValues' : '=',
|
226
|
'selection' : '='
|
227
|
},
|
228
|
templateUrl: '../resources/html/addRepoApi/ngMultiSelectField.html',
|
229
|
link: function(scope, element, attrs) {
|
230
|
scope.arraySelection = [];
|
231
|
scope.required = (scope.optional != 'true');
|
232
|
scope.$watch('arraySelection', function() {
|
233
|
scope.selection = scope.arraySelection.join();
|
234
|
});
|
235
|
}
|
236
|
}
|
237
|
});
|
238
|
|
239
|
|
240
|
module.controller('addRepoApiCtrl', function ($scope, $http, $sce, $location) {
|
241
|
|
242
|
common_init($scope, $http, $sce, $location)
|
243
|
|
244
|
$scope.values = {
|
245
|
'compliances' : getCompliances(),
|
246
|
'types' : getTypes(),
|
247
|
'contentDescriptions' : getContentDescriptions(),
|
248
|
'protocols' : getProtocols()
|
249
|
}
|
250
|
|
251
|
$scope.$watch('repo', function() {
|
252
|
if ($scope.repo) {
|
253
|
$scope.apiParams = {};
|
254
|
$scope.api = {
|
255
|
'typology' : $scope.repo.typology,
|
256
|
'apiParams' : []
|
257
|
};
|
258
|
}
|
259
|
});
|
260
|
|
261
|
$scope.resetForm = function() {
|
262
|
$scope.currentParams = [];
|
263
|
$scope.selectedTypology = '';
|
264
|
$scope.repo = {};
|
265
|
$scope.api = {};
|
266
|
$scope.apiParams = {};
|
267
|
$scope.validValues = [];
|
268
|
}
|
269
|
|
270
|
$scope.registerApi = function() {
|
271
|
if (confirm('Add new api to repository \n\n' + $scope.repo.name + '?')) {
|
272
|
|
273
|
$scope.api.apiParams = [];
|
274
|
angular.forEach($scope.apiParams, function(value, param) {
|
275
|
$scope.api.apiParams.push({
|
276
|
'param' : param,
|
277
|
'value' : value
|
278
|
});
|
279
|
});
|
280
|
|
281
|
$scope.showSpinner();
|
282
|
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
|
283
|
$http.post('repoApi.new', $.param({
|
284
|
'repoId' : $scope.repo.origId,
|
285
|
'iface' : JSON.stringify($scope.api)
|
286
|
})).success(function(res) {
|
287
|
if(res) {
|
288
|
$scope.showNotification('The API has been registered');
|
289
|
$scope.hideSpinner();
|
290
|
$scope.done = 1;
|
291
|
//TODO once we'll get rid of pending/valid repo state, we'll ba able to redirect to the repo detail page
|
292
|
//location.href = 'isManager.do#/profile/' + $scope.repo.id;
|
293
|
} else {
|
294
|
$scope.hideSpinner();
|
295
|
$scope.showError('Registration failed');
|
296
|
}
|
297
|
}).error(function(err) {
|
298
|
$scope.hideSpinner();
|
299
|
$scope.showError('Registration failed: ' + err.message);
|
300
|
});
|
301
|
}
|
302
|
}
|
303
|
|
304
|
$scope.listValidValuesForParam = function(param) {
|
305
|
if (!param) {
|
306
|
$scope.showError("Invalid param");
|
307
|
return;
|
308
|
}
|
309
|
|
310
|
if (!$scope.api.protocol) {
|
311
|
$scope.showError("Access Protocol is missing");
|
312
|
return;
|
313
|
}
|
314
|
if (!$scope.api.baseurl) {
|
315
|
$scope.showError("BaseUrl is missing");
|
316
|
return;
|
317
|
}
|
318
|
|
319
|
var key = $scope.api.baseurl + '@@@' + param;
|
320
|
$scope.validValues[key] = [];
|
321
|
|
322
|
$scope.showSpinner();
|
323
|
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
|
324
|
$http.post('listValidValuesForParam.do', $.param({
|
325
|
'protocol' : $scope.api.protocol,
|
326
|
'param' : param,
|
327
|
'baseUrl' : $scope.api.baseurl,
|
328
|
})).success(function(data) {
|
329
|
$scope.hideSpinner();
|
330
|
$scope.validValues[key] = data;
|
331
|
}).error(function(message) {
|
332
|
$scope.hideSpinner();
|
333
|
$scope.showError('Error obtaining values: ' + message);
|
334
|
});
|
335
|
}
|
336
|
|
337
|
$scope.resetForm();
|
338
|
});
|