Project

General

Profile

1
var module = angular.module('addApiUI', ['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('ngSelectDsField', function($http) {
38
	return {
39
		restrict: 'E',
40
		scope: {
41
            'label'     : '@',
42
            'typology'  : '=',
43
            'selection' : '='
44
		},
45
		templateUrl: '/html/datasources/ngSelectDsField.html',
46
		link: function(scope, element, attrs, ctrl) {
47
			scope.datasources = [];
48
			scope.current = '';
49
			scope.$watch('typology', function() {
50
				scope.datasources = [];
51
				if (scope.typology) {
52
					showSpinner();
53
					$http.get('/ajax/newApi/datasources/' + scope.typology).success(function(data) {
54
						scope.datasources = 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: '/html/datasources/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: '/html/datasources/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: '/html/datasources/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: '/html/datasources/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: '/html/datasources/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: '/html/datasources/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('addApiCtrl', function ($scope, $http, $sce, $location) {
241
	
242
	initSpinner();
243
	
244
	$scope.values = {
245
		'compliances'         : getCompliances(),
246
		'types'               : getTypes(),
247
		'contentDescriptions' : getContentDescriptions(),
248
		'protocols'           : getProtocols()
249
	}
250
		
251
	$scope.$watch('ds', function() {
252
		if ($scope.ds) {
253
			$scope.api = {
254
				'typology'     : $scope.ds.typology,
255
				'accessParams' : {},
256
				'extraFields'  : {}
257
			};
258
		}
259
	});
260
	
261
	$scope.resetForm = function() {
262
		$scope.currentParams = [];
263
		$scope.selectedTypology = '';
264
		$scope.ds = {};
265
		$scope.api = {};
266
		$scope.validValues = [];
267
	}
268
	
269
	$scope.registerApi = function() {
270
		if (confirm('Add new api to datasource\n\n' + $scope.ds.name + '?')) {
271
			showSpinner();
272
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
273
			$http.post('dsApi.new', $.param({
274
				'dsId' : $scope.ds.origId,
275
				'iface'  : JSON.stringify($scope.api)
276
			})).success(function(res) {
277
	            	if(res) {
278
	            		show_notification("info", 'The API has been registered');
279
	            		hideSpinner();
280
	            		$scope.done = 1;
281
	            		//TODO once we'll get rid of pending/valid ds state, we'll ba able to redirect to the ds detail page
282
	            		//location.href = 'isManager.do#/profile/' + $scope.ds.id;
283
	            	} else {
284
	                	hideSpinner();
285
	            		show_notification("error", 'Registration failed');
286
	            	}
287
			}).error(function(message) {
288
				hideSpinner();
289
				show_notification("error", 'Registration failed: ' + message);
290
			});
291
		}
292
	}
293
	
294
	$scope.listValidValuesForParam = function(param) {
295
		if (!param) {
296
			show_notification("error", "Invalid param");
297
			return;
298
		}
299
		
300
		if (!$scope.api.accessProtocol) {
301
			show_notification("error", "Access Protocol is missing");
302
			return;	
303
		}
304
		if (!$scope.api.baseUrl) {
305
			show_notification("error", "BaseUrl is missing");
306
			return;
307
		}
308
		
309
		var key = $scope.api.baseUrl + '@@@' + param;
310
		$scope.validValues[key] = [];
311

    
312
		showSpinner();
313
		$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
314
		$http.post('listValidValuesForParam.do', $.param({
315
			'protocol' : $scope.api.accessProtocol,
316
			'param'    : param,
317
			'baseUrl'  : $scope.api.baseUrl,
318
		})).success(function(data) {
319
			hideSpinner();
320
			$scope.validValues[key] = data;
321
		}).error(function(message) {
322
			hideSpinner();
323
			show_notification("error", 'Error obtaining values: ' + message);
324
		});
325
	}
326
	
327
	$scope.resetForm();
328
});
(1-1/4)