Project

General

Profile

1
var orgsModule = angular.module('orgs', ['ngRoute']);
2

    
3
orgsModule.config(function($routeProvider) {
4
	$routeProvider
5
		.when('/new',                              { templateUrl: 'html/edit.html',         controller: 'newOrgCtrl' })
6
		.when('/search',                           { templateUrl: 'html/search.html',       controller: 'searchCtrl' })
7
		.when('/searchResults/:page/:size/:text*', { templateUrl: 'html/list.html',         controller: 'searchResultsCtrl' })
8
		.when('/countries',                        { templateUrl: 'html/browse.html',       controller: 'countriesCtrl' })
9
		.when('/byCountry/:page/:size/:code*',     { templateUrl: 'html/list.html',         controller: 'byCountryCtrl' })
10
		.when('/types',                            { templateUrl: 'html/browse.html',       controller: 'typesCtrl' })
11
		.when('/byType/:page/:size/:type*',        { templateUrl: 'html/list.html',         controller: 'byTypeCtrl' })
12
		.when('/metadata/:id',                     { templateUrl: 'html/edit.html',         controller: 'showMetadataCtrl' })
13
		.when('/relations/:id',                    { templateUrl: 'html/relations.html',    controller: 'showRelationsCtrl' })
14
		.when('/similarities/:id',                 { templateUrl: 'html/similarities.html', controller: 'showSimilaritiesCtrl' })
15
		.otherwise({ redirectTo: '/search' });
16
});
17

    
18
orgsModule.filter('escape', function() {
19
	return function(input) {
20
		return encodeURIComponent(encodeURIComponent(input));
21
	}; 
22
});
23

    
24
orgsModule.filter('unescape', function() {
25
	return function(input) {
26
		return decodeURIComponent(input);
27
	};
28
});
29

    
30
orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $location) {
31
	$scope.org = {
32
			"id": "",
33
			"name": "",
34
			"lat": 0.0,
35
			"lng": 0.0,
36
			"city": "",
37
			"country": "",
38
			"source": "user",
39
			"otherIdentifiers": [],
40
			"otherNames": [],
41
			"relations": [],
42
			"acronyms": [],
43
			"types": [],
44
			"urls": []
45
		};
46
	$scope.vocabularies = {};
47
	
48
	$http.get('/api/vocabularies').then(function successCallback(res) {
49
		$scope.vocabularies = res.data;
50
	}, function errorCallback(res) {
51
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
52
	});
53
	
54
	$scope.save = function() {
55
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
56
		$http.post('/api/organizations/save', $scope.org).then(function successCallback(res) {
57
			alert('Organization saved !!!');
58
			$location.url('/metadata/' + res.data[0]);
59
		}, function errorCallback(res) {
60
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
61
		});
62
	}
63
	
64
});
65

    
66
orgsModule.controller('searchCtrl', function ($scope, $http, $routeParams) {
67
	$scope.textSearch = '';
68
});
69

    
70

    
71
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams) {
72
	$scope.basepath = '/searchResults';
73
	$scope.fieldValue = decodeURIComponent($routeParams.text);
74
	
75
	$scope.orgs = [];
76
	
77
	$http.get('/api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.fieldValue).then(function successCallback(res) {
78
		$scope.orgs = res.data;
79
	}, function errorCallback(res) {
80
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
81
	});
82
});
83

    
84
orgsModule.controller('countriesCtrl', function ($scope, $http, $routeParams) {
85
	$scope.field = 'Country';
86
	$scope.resultsBasePath = '/byCountry'
87
	$scope.entries = [];
88
	
89
	$http.get('/api/organizations/browse/countries').then(function successCallback(res) {
90
		$scope.entries = res.data;
91
	}, function errorCallback(res) {
92
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
93
	});
94
	
95
});
96

    
97
orgsModule.controller('byCountryCtrl', function ($scope, $http, $routeParams) {
98
	$scope.basepath = '/byCountry';
99
	$scope.fieldValue = $routeParams.code;
100
	
101
	$scope.orgs = [];
102
	
103
	$http.get('/api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
104
		$scope.orgs = res.data;
105
	}, function errorCallback(res) {
106
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
107
	});
108
});
109

    
110
orgsModule.controller('typesCtrl', function ($scope, $http, $routeParams) {
111
	$scope.field = 'Organization type';
112
	$scope.resultsBasePath = '/byType'
113
	$scope.entries = [];
114
	
115
	$http.get('/api/organizations/browse/types').then(function successCallback(res) {
116
		$scope.entries = res.data;
117
	}, function errorCallback(res) {
118
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
119
	});
120
	
121
});
122

    
123
orgsModule.controller('byTypeCtrl', function ($scope, $http, $routeParams) {
124
	$scope.basepath = '/byType';
125
	$scope.fieldValue = $routeParams.type;
126
	
127
	$scope.orgs = [];
128
	
129
	$http.get('/api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
130
		$scope.orgs = res.data;
131
	}, function errorCallback(res) {
132
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
133
	});
134
});
135

    
136

    
137
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams, $route) {
138
	$scope.orgId = $routeParams.id;
139
	$scope.org = {};
140
	$scope.vocabularies = {};
141
	
142
	$http.get('/api/vocabularies').then(function successCallback(res) {
143
		$scope.vocabularies = res.data;
144

    
145
		$http.get('/api/organizations/get?id=' + $routeParams.id).then(function successCallback(res) {
146
			$scope.org = res.data;
147
		}, function errorCallback(res) {
148
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
149
		});
150
	}, function errorCallback(res) {
151
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
152
	});
153
	
154
	$scope.save = function() {
155
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
156
		$http.post('/api/organizations/save', $scope.org).then(function successCallback(res) {
157
			alert('Organization updated !!!');
158
			$route.reload();
159
		}, function errorCallback(res) {
160
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
161
		});
162
	}
163
	
164
});
165

    
166

    
167
orgsModule.controller('showRelationsCtrl', function ($scope, $http, $routeParams) {
168
	$scope.orgId = $routeParams.id;
169
	$scope.rels = [];
170
	$scope.vocabularies = {};
171
	
172
	$http.get('/api/vocabularies').then(function successCallback(res) {
173
		$scope.vocabularies = res.data;
174

    
175
		$http.get('/api/organizations/relations?id=' + $routeParams.id).then(function successCallback(res) {
176
			$scope.rels = res.data;
177
		}, function errorCallback(res) {
178
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
179
		});
180
	}, function errorCallback(res) {
181
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
182
	});
183
	
184
	$scope.addRelation = function(to, type) {
185
		$http.put('/api/organizations/relations', null, { 
186
			'params': { 'from': $scope.orgId, 'to': to, 'type': type }
187
		}).then(function successCallback(res) {
188
			$scope.rels = res.data;
189
			alert("Relation added !!!")
190
		}, function errorCallback(res) {
191
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
192
		});
193
	}
194
	
195
	$scope.deleteRelation = function(to, type) {
196
		$http.delete('/api/organizations/relations', { 
197
			'params': { 'from': $scope.orgId, 'to': to, 'type': type }
198
		}).then(function successCallback(res) {
199
			$scope.rels = res.data;
200
			alert("Relation deleted !!!")
201
		}, function errorCallback(res) {
202
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
203
		});
204
	}
205
	
206
});
207

    
208
orgsModule.controller('showSimilaritiesCtrl', function ($scope, $http, $routeParams) {
209
	$scope.orgId = $routeParams.id;
210
	$scope.similarities = [];
211
	$scope.vocabularies = {};
212
	
213
	$http.get('/api/vocabularies').then(function successCallback(res) {
214
		$scope.vocabularies = res.data;
215

    
216
		$http.get('/api/organizations/similarities?id=' + $routeParams.id).then(function successCallback(res) {
217
			$scope.similarities = res.data;
218
		}, function errorCallback(res) {
219
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
220
		});
221
	}, function errorCallback(res) {
222
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
223
	});
224
	
225
});
226

    
(9-9/11)