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
	initSpinner();
29
	showSpinner();
30
	
31
	$scope.loadVocabularies = function() {
32
		$http.get('vocabularies.json')
33
		.success(
34
			function(data) {
35
				$scope.vocabularies = data;
36
				hideSpinner();
37
			})
38
		.error(
39
			function() {
40
				show_notification("error", "An error occurred while loading registered vocabulary profiles..");
41
				hideSpinner();
42
			});
43
	};
44
	
45
	$scope.loadVocabularies();
46
	
47
	/*---------LOAD TERMS---------*/
48
	$scope.loadTerms = function(vocabulary) {
49
		//		// check if same vocabulary
50
		//		if ($scope.selectedVocabularyIndex != null && vocabulary.id == $scope.vocabularies[$scope.selectedVocabularyIndex].id) {
51
		//			return;
52
		//		}
53
		// prompt for uncommitted changes
54
		if (probePermanotice() == true) {
55
			var r=confirm("Switching vocabulary means losing uncommitted changes.. Are you fine with that?");
56
			if (r == false) {
57
				return;
58
			}
59
		}
60
		//actually load terms
61
		showSpinner();
62
		$http.get('terms.json?vocabularyId=' + vocabulary.id)
63
			.success(
64
				function(data) {
65
					$scope.selectedVocabularyIndex = $scope.vocabularies.indexOf(vocabulary);
66
					$scope.entries = data;
67
					$scope.termFilter = "";
68
					hidePermanotice();
69
					hideSpinner();
70
				})
71
			.error(
72
				function() {
73
					show_notification("error","An error occurred while fetching terms of vocabulary d"+vocabulary.name);
74
					hideSpinner();
75
				});
76
	};
77
	
78
	$scope.reloadTerms = function() {
79
		$scope.loadTerms($scope.vocabularies[$scope.selectedVocabularyIndex]);
80
	};
81

    
82
	/*---------DELETE---------*/
83
	$scope.deleteTerm = function(term) {
84
		if ($scope.autocommit) {
85
			var retVal = confirm("Do you want to continue ?");
86
		}
87
		if (!$scope.autocommit || retVal == true) {
88
			var index = $scope.entries.indexOf(term);
89
			$scope.entries.splice(index, 1);
90
			if ($scope.autocommit) {
91
				$scope.commit();
92
			} else {
93
				showPermanotice("Uncommitted changes!");
94
			}
95
		}
96
	};
97

    
98
	$scope.deleteSynonym = function(term, synonym) {
99
		if ($scope.autocommit) {
100
			var retVal = confirm("Do you want to continue ?");
101
		}
102
		if (!$scope.autocommit || retVal == true) {
103
			var termIndex = $scope.entries.indexOf(term);
104
			var synonymIndex = $scope.entries[termIndex].synonyms.indexOf(synonym);
105
			$scope.entries[termIndex].synonyms.splice(synonymIndex, 1);
106
			if ($scope.autocommit) {
107
				$scope.commit();
108
			} else {
109
				showPermanotice("Uncommitted changes!");
110
			}
111
		}
112
	};
113
	
114
	$scope.deleteRel = function(term, rel) {
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 relIndex = $scope.entries[termIndex].relations.indexOf(rel);
121
			$scope.entries[termIndex].relations.splice(relIndex, 1);
122
			if ($scope.autocommit) {
123
				$scope.commit();
124
			} else {
125
				showPermanotice("Uncommitted changes!");
126
			}
127
		}
128
	};
129

    
130
	$scope.dropVocabulary = function() {
131
		var retVal = confirm("Do you want to continue ?");
132
		if (retVal == true) {
133
			showSpinner();
134
			$http.get('dropVocabulary?vocabularyId=' + $scope.vocabularies[$scope.selectedVocabularyIndex].id)
135
				.success(
136
					function () {
137
						$scope.vocabularies.splice($scope.selectedVocabularyIndex, 1);
138
						$scope.selectedVocabularyIndex = null;
139
						show_notification("success", "Vocabulary deleted successfully!");
140
						hideSpinner();
141
					})
142
				.error(
143
					function () {
144
						hideSpinner();
145
						show_notification("error", "An error occurred while dropping vocabulary " + $scope.vocabularies[$scope.selectedVocabularyIndex].name);
146
					});
147
		}
148
	};
149

    
150
	/*---------COMMIT---------*/
151
	$scope.commit = function() {
152
		showSpinner();
153
		$http.post('commitVocabulary?vocabularyId=' + $scope.vocabularies[$scope.selectedVocabularyIndex].id, $scope.entries)
154
			.success(
155
				function() {
156
					show_notification("success", "Vocabulary successfully committed!");
157
					hideSpinner();
158
					hidePermanotice();
159
					if (!$scope.autocommit) {
160
						$scope.reloadTerms();
161
					}
162
				})
163
			.error(
164
				function() {
165
					show_notification("error", "An error occurred while saving the modifications..");
166
					showPermanotice("Uncommitted changes!");
167
					hideSpinner();
168
				});
169
	}
170
});
(34-34/34)