Project

General

Profile

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

    
3
orgsModule.config(function($routeProvider) {
4
	$routeProvider
5
		.when('/new',                              { templateUrl: 'resources/html/edit.html',         controller: 'newOrgCtrl' })
6
		.when('/search',                           { templateUrl: 'resources/html/search.html',       controller: 'searchCtrl' })
7
		.when('/searchResults/:page/:size/:text*', { templateUrl: 'resources/html/list.html',         controller: 'searchResultsCtrl' })
8
		.when('/countries',                        { templateUrl: 'resources/html/browse.html',       controller: 'countriesCtrl' })
9
		.when('/byCountry/:page/:size/:code*',     { templateUrl: 'resources/html/list.html',         controller: 'byCountryCtrl' })
10
		.when('/types',                            { templateUrl: 'resources/html/browse.html',       controller: 'typesCtrl' })
11
		.when('/byType/:page/:size/:type*',        { templateUrl: 'resources/html/list.html',         controller: 'byTypeCtrl' })
12
		.when('/metadata/:id',                     { templateUrl: 'resources/html/edit.html',         controller: 'showMetadataCtrl' })
13
		.when('/relations/:id',                    { templateUrl: 'resources/html/relations.html',    controller: 'showRelationsCtrl' })
14
		.when('/similarities/:id',                 { templateUrl: 'resources/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('userCtrl', function ($scope, $http, $location) {
31
	$scope.user = '';
32
	$http.get('/api/user/current').then(function successCallback(res) {
33
		$scope.user = res.data.name;
34
	}, function errorCallback(res) {
35
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
36
	});
37
	
38
	$scope.logout = function() {
39
		location.href= "/logout";
40
	}
41
});
42

    
43
orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $location) {
44
	$scope.org = {
45
			"id": "",
46
			"name": "",
47
			"type": null,
48
			"lat": 0.0,
49
			"lng": 0.0,
50
			"city": "",
51
			"country": "",
52
			"source": "user",
53
			"otherIdentifiers": [],
54
			"otherNames": [],
55
			"relations": [],
56
			"acronyms": [],
57
			"urls": []
58
		};
59
	$scope.vocabularies = {};
60
	
61
	$http.get('/api/vocabularies').then(function successCallback(res) {
62
		$scope.vocabularies = res.data;
63
	}, function errorCallback(res) {
64
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
65
	});
66
	
67
	$scope.save = function() {
68
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
69
		$http.post('/api/organizations/save', $scope.org).then(function successCallback(res) {
70
//			alert('Organization saved !!!');
71
			$location.url('/metadata/' + res.data[0]);
72
		}, function errorCallback(res) {
73
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
74
		});
75
	}
76
	
77
});
78

    
79
orgsModule.controller('searchCtrl', function ($scope, $http, $routeParams) {
80
	$scope.textSearch = '';
81
});
82

    
83

    
84
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams) {
85
	$scope.basepath = '/searchResults';
86
	$scope.fieldValue = decodeURIComponent($routeParams.text);
87
	
88
	$scope.orgs = [];
89
	
90
	$http.get('/api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.fieldValue).then(function successCallback(res) {
91
		$scope.orgs = res.data;
92
	}, function errorCallback(res) {
93
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
94
	});
95
});
96

    
97
orgsModule.controller('countriesCtrl', function ($scope, $http, $routeParams) {
98
	$scope.field = 'Country';
99
	$scope.resultsBasePath = '/byCountry'
100
	$scope.entries = [];
101
	
102
	$http.get('/api/organizations/browse/countries').then(function successCallback(res) {
103
		$scope.entries = res.data;
104
	}, function errorCallback(res) {
105
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
106
	});
107
	
108
});
109

    
110
orgsModule.controller('byCountryCtrl', function ($scope, $http, $routeParams) {
111
	$scope.basepath = '/byCountry';
112
	$scope.fieldValue = $routeParams.code;
113
	
114
	$scope.orgs = [];
115
	
116
	$http.get('/api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
117
		$scope.orgs = res.data;
118
	}, function errorCallback(res) {
119
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
120
	});
121
});
122

    
123
orgsModule.controller('typesCtrl', function ($scope, $http, $routeParams) {
124
	$scope.field = 'Organization type';
125
	$scope.resultsBasePath = '/byType'
126
	$scope.entries = [];
127
	
128
	$http.get('/api/organizations/browse/types').then(function successCallback(res) {
129
		$scope.entries = res.data;
130
	}, function errorCallback(res) {
131
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
132
	});
133
	
134
});
135

    
136
orgsModule.controller('byTypeCtrl', function ($scope, $http, $routeParams) {
137
	$scope.basepath = '/byType';
138
	$scope.fieldValue = $routeParams.type;
139
	
140
	$scope.orgs = [];
141
	
142
	$http.get('/api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
143
		$scope.orgs = res.data;
144
	}, function errorCallback(res) {
145
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
146
	});
147
});
148

    
149

    
150
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams, $route) {
151
	$scope.orgId = $routeParams.id;
152
	$scope.org = {};
153
	$scope.vocabularies = {};
154
	
155
	$http.get('/api/vocabularies').then(function successCallback(res) {
156
		$scope.vocabularies = res.data;
157

    
158
		$http.get('/api/organizations/get?id=' + $routeParams.id).then(function successCallback(res) {
159
			$scope.org = res.data;
160
		}, function errorCallback(res) {
161
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
162
		});
163
	}, function errorCallback(res) {
164
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
165
	});
166
	
167
	$scope.save = function() {
168
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
169
		$http.post('/api/organizations/save', $scope.org).then(function successCallback(res) {
170
//			alert('Organization updated !!!');
171
			$route.reload();
172
		}, function errorCallback(res) {
173
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
174
		});
175
	}
176
	
177
});
178

    
179

    
180
orgsModule.controller('showRelationsCtrl', function ($scope, $http, $routeParams) {
181
	$scope.orgId = $routeParams.id;
182
	$scope.rels = [];
183
	$scope.vocabularies = {};
184
	
185
	$scope.searchValue = '';
186
	$scope.searchOrgs = [];
187
	$scope.newRelType='';
188
	$scope.newRelation = {};
189
	
190
	$http.get('/api/vocabularies').then(function successCallback(res) {
191
		$scope.vocabularies = res.data;
192

    
193
		$http.get('/api/organizations/relations?id=' + $routeParams.id).then(function successCallback(res) {
194
			$scope.rels = res.data;
195
		}, function errorCallback(res) {
196
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
197
		});
198
	}, function errorCallback(res) {
199
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
200
	});
201
	
202
	$scope.prepareNewRelation = function(o) {
203
		$scope.newRelation = o;
204
	}
205
		
206
	$scope.addRelation = function() {
207
		$http.put('/api/organizations/relations', null, { 
208
			'params': { 'from': $scope.orgId, 'to': $scope.newRelation.id, 'type': $scope.newRelType }
209
		}).then(function successCallback(res) {
210
			$scope.rels = res.data;
211
			$scope.newRelType = '';
212
			$scope.newRelation = {};
213
//			alert("Relation added !!!")
214
		}, function errorCallback(res) {
215
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
216
		});
217
	}
218
	
219
	$scope.deleteRelation = function(to, type) {
220
		$http.delete('/api/organizations/relations', { 
221
			'params': { 'from': $scope.orgId, 'to': to, 'type': type }
222
		}).then(function successCallback(res) {
223
			$scope.rels = res.data;
224
//			alert("Relation deleted !!!")
225
		}, function errorCallback(res) {
226
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
227
		});
228
	}
229
	
230
	$scope.search = function(text, page, size) {
231
		$scope.orgs = [];
232
		$http.get('/api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) {
233
			$scope.searchValue = text;
234
			$scope.searchOrgs = res.data;
235
		}, function errorCallback(res) {
236
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
237
		});
238
	}
239
	
240
});
241

    
242
orgsModule.controller('showSimilaritiesCtrl', function ($scope, $http, $routeParams) {
243
	$scope.orgId = $routeParams.id;
244
	$scope.similarities = [];
245
	$scope.vocabularies = {};
246
	
247
	$http.get('/api/vocabularies').then(function successCallback(res) {
248
		$scope.vocabularies = res.data;
249

    
250
		$http.get('/api/organizations/similarities?id=' + $routeParams.id).then(function successCallback(res) {
251
			$scope.similarities = res.data;
252
		}, function errorCallback(res) {
253
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
254
		});
255
	}, function errorCallback(res) {
256
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
257
	});
258
	
259
	
260
	$scope.saveSimilarities = function() {
261
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
262
		$http.post('/api/organizations/similarities', $scope.similarities).then(function successCallback(res) {
263
			alert('Similarities updated !!!');
264
			$scope.similarities = res.data;
265
		}, function errorCallback(res) {
266
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
267
		});
268
	}
269
});
270

    
(9-9/11)