Project

General

Profile

1
var module = angular.module('vocabulariesUI', ['uniqueChecker', 'modalTerm', 'modalInfo', 'modalSynonym', 'modalRelation']);
2

    
3
module.directive('vocabularyList', function(){
4
	return {
5
		restrict: 'E',
6
		templateUrl: '../resources/html/vocabularies/vocabulary-list.html'
7
	};
8
});
9

    
10
module.directive('termList', function(){
11
	return {
12
		restrict: 'E',
13
		templateUrl: '../resources/html/vocabularies/term-list.html'
14
	};
15
});
16

    
17
module.controller('vocabulariesCtrl', function ($scope, $http) {
18
	$scope.entries = [];
19
	$scope.modalSynonym = {};
20
	$scope.modalRel = {};
21
	$scope.modalTerm = {};
22
	$scope.modalInfo = {};
23
	
24
	$scope.validRelations = getValidRelations();
25

    
26
	$scope.autocommit = false;
27
	
28
	$scope.allTermsOpened = false;
29
	
30
	initSpinner();
31
	showSpinner();
32
	
33
	$scope.loadVocabularies = function() {
34
		$http.get('vocabularies.json')
35
		.success(
36
			function(data) {
37
				$scope.vocabularies = data;
38
				hideSpinner();
39
			})
40
		.error(
41
			function() {
42
				show_notification("error", "An error occurred while loading registered vocabulary profiles..");
43
				hideSpinner();
44
			});
45
	};
46
	
47
	$scope.loadVocabularies();
48
	
49
	
50
	$scope.openCloseAllTerms = function() {
51
	
52
		$scope.allTermsOpened = !$scope.allTermsOpened;
53

    
54
		if ($scope.allTermsOpened) {
55
			jQuery('#TermsAccordion .collapse').collapse('show');
56
		} else {
57
			jQuery('#TermsAccordion .collapse').collapse('hide');
58
		}
59
	
60
	};
61
	
62
	
63
	/*---------LOAD TERMS---------*/
64
	$scope.loadTerms = function(vocabulary) {
65
		//		// check if same vocabulary
66
		//		if ($scope.selectedVocabularyIndex != null && vocabulary.id == $scope.vocabularies[$scope.selectedVocabularyIndex].id) {
67
		//			return;
68
		//		}
69
		// prompt for uncommitted changes
70
		if (probePermanotice() == true) {
71
			var r=confirm("Switching vocabulary means losing uncommitted changes.. Are you fine with that?");
72
			if (r == false) {
73
				return;
74
			}
75
		}
76
		//actually load terms
77
		showSpinner();
78
		$http.get('terms.json?vocabularyId=' + vocabulary.id)
79
			.success(
80
				function(data) {
81
					$scope.selectedVocabularyIndex = $scope.vocabularies.indexOf(vocabulary);
82
					$scope.entries = data;
83
					$scope.termFilter = "";
84
					hidePermanotice();
85
					hideSpinner();
86
				})
87
			.error(
88
				function() {
89
					show_notification("error","An error occurred while fetching terms of vocabulary d"+vocabulary.name);
90
					hideSpinner();
91
				});
92
	};
93
	
94
	$scope.reloadTerms = function() {
95
		$scope.loadTerms($scope.vocabularies[$scope.selectedVocabularyIndex]);
96
	};
97

    
98
	/*---------DELETE---------*/
99
	$scope.deleteTerm = function(term) {
100
		if ($scope.autocommit) {
101
			var retVal = confirm("Do you want to continue ?");
102
		}
103
		if (!$scope.autocommit || retVal == true) {
104
			var index = $scope.entries.indexOf(term);
105
			$scope.entries.splice(index, 1);
106
			if ($scope.autocommit) {
107
				$scope.commit();
108
			} else {
109
				showPermanotice("Uncommitted changes!");
110
			}
111
		}
112
	};
113

    
114
	$scope.deleteSynonym = function(term, synonym) {
115
		if ($scope.autocommit) {
116
			var retVal = confirm("Do you want to continue ?");
117
		}
118
		if (!$scope.autocommit || retVal == true) {
119
			var termIndex = $scope.entries.indexOf(term);
120
			var synonymIndex = $scope.entries[termIndex].synonyms.indexOf(synonym);
121
			$scope.entries[termIndex].synonyms.splice(synonymIndex, 1);
122
			if ($scope.autocommit) {
123
				$scope.commit();
124
			} else {
125
				showPermanotice("Uncommitted changes!");
126
			}
127
		}
128
	};
129
	
130
	$scope.deleteRel = function(term, rel) {
131
		if ($scope.autocommit) {
132
			var retVal = confirm("Do you want to continue ?");
133
		}
134
		if (!$scope.autocommit || retVal == true) {
135
			var termIndex = $scope.entries.indexOf(term);
136
			var relIndex = $scope.entries[termIndex].relations.indexOf(rel);
137
			$scope.entries[termIndex].relations.splice(relIndex, 1);
138
			if ($scope.autocommit) {
139
				$scope.commit();
140
			} else {
141
				showPermanotice("Uncommitted changes!");
142
			}
143
		}
144
	};
145

    
146
	$scope.dropVocabulary = function() {
147
		var retVal = confirm("Do you want to continue ?");
148
		if (retVal == true) {
149
			showSpinner();
150
			$http.get('dropVocabulary?vocabularyId=' + $scope.vocabularies[$scope.selectedVocabularyIndex].id)
151
				.success(
152
					function () {
153
						$scope.vocabularies.splice($scope.selectedVocabularyIndex, 1);
154
						$scope.selectedVocabularyIndex = null;
155
						show_notification("success", "Vocabulary deleted successfully!");
156
						hideSpinner();
157
					})
158
				.error(
159
					function () {
160
						hideSpinner();
161
						show_notification("error", "An error occurred while dropping vocabulary " + $scope.vocabularies[$scope.selectedVocabularyIndex].name);
162
					});
163
		}
164
	};
165

    
166
	/*---------COMMIT---------*/
167
	$scope.commit = function() {
168
		showSpinner();
169
		$http.post('commitVocabulary?vocabularyId=' + $scope.vocabularies[$scope.selectedVocabularyIndex].id, $scope.entries)
170
			.success(
171
				function() {
172
					show_notification("success", "Vocabulary successfully committed!");
173
					hideSpinner();
174
					hidePermanotice();
175
					if (!$scope.autocommit) {
176
						$scope.reloadTerms();
177
					}
178
				})
179
			.error(
180
				function() {
181
					show_notification("error", "An error occurred while saving the modifications..");
182
					showPermanotice("Uncommitted changes!");
183
					hideSpinner();
184
				});
185
	}
186
});
(6-6/6)