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
		.when('/users',                            { templateUrl: 'resources/html/users.html',        controller: 'usersCtrl' })
16
		.otherwise({ redirectTo: '/search' });
17
});
18

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

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

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

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

    
71

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

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

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

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

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

    
137

    
138
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams, $route, $location, $timeout) {
139
	$scope.orgId = $routeParams.id;
140
	$scope.org = {};
141
	$scope.vocabularies = {};
142
	
143
	if ($routeParams.msg == 1) {
144
		$scope.message = 'New organization registered !!!';
145
	} else if ($routeParams.msg == 2) {
146
		$scope.message = 'Organization updated !!!';
147
	} else {
148
		$scope.message = '';
149
	}	
150
	
151
	$timeout(function() { $scope.message = ''; }, 3000)
152
		
153
	$http.get('api/vocabularies').then(function successCallback(res) {
154
		$scope.vocabularies = res.data;
155

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

    
177

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

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

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

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

    
269

    
270

    
271
orgsModule.controller('usersCtrl', function ($scope, $http) {
272
	$scope.users = [];
273
	$scope.vocabularies = {};
274
	
275
	$http.get('api/vocabularies').then(function successCallback(res) {
276
		$scope.vocabularies = res.data;
277

    
278
		$http.get('api/users').then(function successCallback(res) {
279
			$scope.users = res.data;
280
		}, function errorCallback(res) {
281
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
282
		});
283
	}, function errorCallback(res) {
284
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
285
	});
286
	
287
});
(9-9/11)