Project

General

Profile

1
var module = angular.module('modalInfo', []);
2

    
3
module.directive('modalInfo', function(){
4
	return {
5
		restrict: 'E',
6
		templateUrl: '../resources/html/vocabularies/modalInfo.html',
7
		controller: function($scope, $http){
8
			$scope.showInfoModal = function() {
9
				if ($scope.selectedVocabularyIndex != null) {
10
					// show modal for editing info of existing vocabulary
11
					$scope.modalInfo.id = $scope.vocabularies[$scope.selectedVocabularyIndex].id;
12
					$scope.modalInfo.name = $scope.vocabularies[$scope.selectedVocabularyIndex].name;
13
					$scope.modalInfo.code = $scope.vocabularies[$scope.selectedVocabularyIndex].code;
14
					$scope.modalInfo.description = $scope.vocabularies[$scope.selectedVocabularyIndex].description;
15
				} else {
16
					// show modal for new vocabulary
17
					$scope.modalInfo.id = '';
18
					$scope.modalInfo.name = '';
19
					$scope.modalInfo.code = '';
20
					$scope.modalInfo.description = '';
21
					hidePermanotice();
22
				}
23
				$('#infoModal').modal();
24
			}
25
			
26
			$scope.editInfo = function() {
27
				showSpinner();
28
				if ($scope.selectedVocabularyIndex == null) {
29
					// new vocabulary
30
					$http.post('createVocabulary', 
31
								{"name" : $scope.modalInfo.name, 
32
								"code" : $scope.modalInfo.code, 
33
								"description" : $scope.modalInfo.description})
34
						.success(
35
							function(data) {
36
								show_notification("success", "Vocabulary created successfully!");
37
								$scope.vocabularies.push({
38
									"id" : data,
39
									"name" : $scope.modalInfo.name,
40
									"code" : $scope.modalInfo.code,
41
									"description" : $scope.modalInfo.description
42
								});
43
								hideSpinner();
44
								$scope.loadVocabularies();
45
							})
46
						.error(
47
							function() {
48
								show_notification("error", "Cannot edit info..");
49
								hideSpinner();
50
							});
51
				} else {
52
					// edit vocabulary
53
					$http.post('commitVocabularyInfo?vocabularyId=' + $scope.vocabularies[$scope.selectedVocabularyIndex].id, 
54
								{"name" : $scope.modalInfo.name, 
55
								"code" : $scope.modalInfo.code, 
56
								"description" : $scope.modalInfo.description})
57
						.success(
58
							function() {
59
								// update model
60
								$scope.vocabularies[$scope.selectedVocabularyIndex].id = $scope.modalInfo.id;
61
								$scope.vocabularies[$scope.selectedVocabularyIndex].name = $scope.modalInfo.name;
62
								$scope.vocabularies[$scope.selectedVocabularyIndex].code = $scope.modalInfo.code;
63
								$scope.vocabularies[$scope.selectedVocabularyIndex].description = $scope.modalInfo.description;
64
								show_notification("success", "Vocabulary information saved correctly!");
65
								hideSpinner();
66
								$scope.loadVocabularies();
67
							})
68
						.error(
69
							function() {
70
								show_notification("error", "Cannot edit info..");
71
								hideSpinner();
72
							});
73
				}
74
				// dismiss modal
75
				$('.modal').modal('hide');
76
			}
77
		},
78
		controllerAs: 'modalInfo'
79
	};
80
});
(18-18/34)