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/:msg/: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/1/' + 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, $location, $timeout) {
151
	$scope.orgId = $routeParams.id;
152
	$scope.org = {};
153
	$scope.vocabularies = {};
154
	
155
	if ($routeParams.msg == 1) {
156
		$scope.message = 'New organization registered !!!';
157
	} else if ($routeParams.msg == 2) {
158
		$scope.message = 'Organization updated !!!';
159
	} else {
160
		$scope.message = '';
161
	}	
162
	
163
	$timeout(function() { $scope.message = ''; }, 3000)
164
		
165
	$http.get('/api/vocabularies').then(function successCallback(res) {
166
		$scope.vocabularies = res.data;
167

    
168
		$http.get('/api/organizations/get?id=' + $routeParams.id).then(function successCallback(res) {
169
			$scope.org = res.data;
170
		}, function errorCallback(res) {
171
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
172
		});
173
	}, function errorCallback(res) {
174
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
175
	});
176
	
177
	$scope.save = function() {
178
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
179
		$http.post('/api/organizations/save', $scope.org).then(function successCallback(res) {
180
			if ($routeParams.msg == 2) { $route.reload(); }
181
			else                       { $location.url('/metadata/2/' + res.data[0]); }
182
		}, function errorCallback(res) {
183
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
184
		});
185
	}
186
	
187
});
188

    
189

    
190
orgsModule.controller('showRelationsCtrl', function ($scope, $http, $routeParams) {
191
	$scope.orgId = $routeParams.id;
192
	$scope.rels = [];
193
	$scope.vocabularies = {};
194
	
195
	$scope.searchValue = '';
196
	$scope.searchOrgs = [];
197
	$scope.newRelType='';
198
	$scope.newRelation = {};
199
	
200
	$http.get('/api/vocabularies').then(function successCallback(res) {
201
		$scope.vocabularies = res.data;
202

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

    
252
orgsModule.controller('showSimilaritiesCtrl', function ($scope, $http, $routeParams) {
253
	$scope.orgId = $routeParams.id;
254
	$scope.similarities = [];
255
	$scope.vocabularies = {};
256
	
257
	$http.get('/api/vocabularies').then(function successCallback(res) {
258
		$scope.vocabularies = res.data;
259

    
260
		$http.get('/api/organizations/similarities?id=' + $routeParams.id).then(function successCallback(res) {
261
			$scope.similarities = res.data;
262
		}, function errorCallback(res) {
263
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
264
		});
265
	}, function errorCallback(res) {
266
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
267
	});
268
	
269
	
270
	$scope.saveSimilarities = function() {
271
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
272
		$http.post('/api/organizations/similarities', $scope.similarities).then(function successCallback(res) {
273
			alert('Similarities updated !!!');
274
			$scope.similarities = res.data;
275
		}, function errorCallback(res) {
276
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
277
		});
278
	}
279
});
280

    
(9-9/11)