Project

General

Profile

1
var module = angular.module('addRepoUI', []);
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('ngSelectVocabularyField', function() {
38
	return {
39
		restrict: 'E',
40
		scope: {
41
            'label'            : '@',
42
            'vocabulary'       : '=',
43
            'selection'        : '=',
44
            'contextualParams' : '='
45
		},
46
		templateUrl: '../resources/html/addRepoApi/ngSelectVocabularyField.html',
47
		link: function(scope, elem, attrs) {
48
			scope.required = true;
49
			scope.selectId = 'select_' + scope.label.replace( /\s/g, "_").toLowerCase();
50
			
51
			scope.$watch('selection', function() {
52
				scope.contextualParams = [];
53
				angular.forEach(scope.vocabulary, function(term){
54
					if (term.name == scope.selection) {
55
						scope.contextualParams = term.params;
56
					} 					
57
				});				
58
			});
59
		}
60
	}
61
});
62

    
63
module.filter('vocabularyTerm', function() {
64
	return function(term) {
65
		if(term.desc) {
66
			return term.name + ' (' + term.desc + ')';
67
	    } else {
68
	    	return term.name;
69
	    }
70
	};
71
});
72

    
73
module.directive('ngSimpleEditField', function() {
74
	return {
75
		restrict: 'E',
76
		scope: {
77
            'label'         : '@',
78
            'regex'         : '@',
79
            'optional'      : '@',
80
            'type'          : '@',
81
            'selection'     : '=',
82
		},
83
		templateUrl: '../resources/html/addRepoApi/ngSimpleEditField.html',
84
		link: function(scope, element, attrs) {
85
			scope.required = (scope.optional != 'true');
86
			if      (scope.regex)             { scope.mypattern = new RegExp(scope.regex); }
87
			else if (scope.type == 'NUMBER')  { scope.mypattern = new RegExp("^[-+]?[0-9]+(\.[0-9]+)?$"); }
88
			else if (scope.type == 'BOOLEAN') { scope.mypattern = new RegExp("^(true|false)$"); }
89
			else                              { scope.mypattern = new RegExp(".+"); }
90
		}
91
	}
92
});
93

    
94
module.controller('addRepoCtrl', function ($scope, $http, $sce, $location) {
95
	
96
	common_init($scope, $http, $sce, $location)
97
	
98
	$scope.validTypes = getTypes();
99
	$scope.validCountries = getCountries();
100
	
101
	$scope.resetForm = function() {
102
		$scope.repo = {
103
			latitude : '0.0',
104
			longitude : '0.0',
105
			timezone : '0.0',
106
		};
107
		
108
		$scope.org = {};
109
	}
110
	
111
	$scope.registerRepo = function() {
112
		if (confirm('Add new datasource \n\n' + $scope.repo.officialname + '?')) {
113
			$scope.showSpinner();
114
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
115
			
116
			$scope.org.id = 'openaire____::' + $scope.repo.id;
117
			$scope.repo.organizations = [$scope.org];
118
			
119
			$http.post('repo.new', $.param({
120
				'repo'  : JSON.stringify($scope.repo)
121
			})).success(function(res) {
122
	            	if(res) {
123
	            		$scope.showNotification('The Datasource has been registered');
124
	            		$scope.hideSpinner();
125
	            		$scope.done = 1;
126
	            	} else {
127
	                	$scope.hideSpinner();
128
	            		$scope.showError('Registration failed');
129
	            	}
130
			}).error(function(message) {
131
				$scope.hideSpinner();
132
				$scope.showError('Registration failed: ' + message);
133
			});
134
		}
135
	}
136
	
137
	$scope.resetForm();
138
});
(1-1/7)