Passed
Push — master ( 288f92...809e6b )
by
unknown
13:11 queued 13s
created

myems-admin/app/controllers/settings/energystoragecontainer/energystoragecontainerload.controller.js   A

Complexity

Total Complexity 38
Complexity/F 1.27

Size

Lines of Code 235
Function Count 30

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 38
eloc 166
dl 0
loc 235
rs 9.36
c 0
b 0
f 0
mnd 8
bc 8
fnc 30
bpm 0.2666
cpm 1.2666
noi 0
1
'use strict';
2
3
app.controller('EnergyStorageContainerLoadController', function(
4
	$scope,
5
	$rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	EnergyStorageContainerService,
10
	EnergyStorageContainerLoadService,
11
	PointService,
12
	MeterService,
13
	toaster,
14
	SweetAlert) {
15
      $scope.energystoragecontainers = [];
16
      $scope.energystoragecontainerloads = [];
17
	  $scope.points = [];
18
	  $scope.meters = [];
19
      $scope.currentEnergyStorageContainer = null;
20
	  $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
21
      $scope.getAllEnergyStorageContainers = function() {
22
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
23
  		EnergyStorageContainerService.getAllEnergyStorageContainers(headers, function (response) {
24
  			if (angular.isDefined(response.status) && response.status === 200) {
25
  				$scope.energystoragecontainers = response.data;
26
  			} else {
27
  				$scope.energystoragecontainers = [];
28
  			}
29
  		});
30
  	};
31
32
	$scope.getAllPoints = function() {
33
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
34
		PointService.getAllPoints(headers, function (response) {
35
			if (angular.isDefined(response.status) && response.status === 200) {
36
				$scope.points = response.data;
37
			} else {
38
				$scope.points = [];
39
			}
40
		});
41
	};
42
43
	$scope.getAllMeters = function() {
44
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
45
		MeterService.getAllMeters(headers, function (response) {
46
			if (angular.isDefined(response.status) && response.status === 200) {
47
				$scope.meters = response.data;
48
			} else {
49
				$scope.meters = [];
50
			}
51
		});
52
	};
53
  	$scope.getEnergyStorageContainerLoadsByEnergyStorageContainerID = function(id) {
54
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
55
  		EnergyStorageContainerLoadService.getEnergyStorageContainerLoadsByEnergyStorageContainerID(id, headers, function (response) {
56
			if (angular.isDefined(response.status) && response.status === 200) {
57
				$scope.energystoragecontainerloads = response.data;
58
			} else {
59
          	$scope.energystoragecontainerloads=[];
60
        }
61
			});
62
  	};
63
64
  	$scope.changeEnergyStorageContainer=function(item,model){
65
    	$scope.currentEnergyStorageContainer=item;
66
    	$scope.currentEnergyStorageContainer.selected=model;
67
        $scope.is_show_add_energystoragecontainer_load = true;
68
    	$scope.getEnergyStorageContainerLoadsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
69
  	};
70
71
  	$scope.addEnergyStorageContainerLoad = function() {
72
  		var modalInstance = $uibModal.open({
73
  			templateUrl: 'views/settings/energystoragecontainer/energystoragecontainerload.model.html',
74
  			controller: 'ModalAddEnergyStorageContainerLoadCtrl',
75
  			windowClass: "animated fadeIn",
76
  			resolve: {
77
  				params: function() {
78
  					return {
79
						meters: angular.copy($scope.meters),
80
						points: angular.copy($scope.points),
81
  					};
82
  				}
83
  			}
84
  		});
85
  		modalInstance.result.then(function(energystoragecontainerload) {
86
			energystoragecontainerload.power_point_id = energystoragecontainerload.power_point.id;
87
			energystoragecontainerload.meter_id = energystoragecontainerload.meter.id;
88
89
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
90
  			EnergyStorageContainerLoadService.addEnergyStorageContainerLoad($scope.currentEnergyStorageContainer.id, energystoragecontainerload, headers, function (response) {
91
  				if (angular.isDefined(response.status) && response.status === 201) {
92
  					toaster.pop({
93
  						type: "success",
94
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
95
  						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
96
  						showCloseButton: true,
97
  					});
98
  					$scope.getEnergyStorageContainerLoadsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
99
            		$scope.$emit('handleEmitEnergyStorageContainerLoadChanged');
100
  				} else {
101
  					toaster.pop({
102
  						type: "error",
103
  						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
104
  						body: $translate.instant(response.data.description),
105
  						showCloseButton: true,
106
  					});
107
  				}
108
  			});
109
  		}, function() {
110
111
  		});
112
		$rootScope.modalInstance = modalInstance;
113
  	};
114
115
  	$scope.editEnergyStorageContainerLoad = function(energystoragecontainerload) {
116
  		var modalInstance = $uibModal.open({
117
  			templateUrl: 'views/settings/energystoragecontainer/energystoragecontainerload.model.html',
118
  			controller: 'ModalEditEnergyStorageContainerLoadCtrl',
119
    		windowClass: "animated fadeIn",
120
  			resolve: {
121
  				params: function() {
122
  					return {
123
  						energystoragecontainerload: angular.copy(energystoragecontainerload),
124
						meters: angular.copy($scope.meters),
125
						points: angular.copy($scope.points),
126
  					};
127
  				}
128
  			}
129
  		});
130
131
  		modalInstance.result.then(function(modifiedEnergyStorageContainerLoad) {
132
			modifiedEnergyStorageContainerLoad.power_point_id = modifiedEnergyStorageContainerLoad.power_point.id;
133
			modifiedEnergyStorageContainerLoad.meter_id = modifiedEnergyStorageContainerLoad.meter.id;
134
135
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
136
  			EnergyStorageContainerLoadService.editEnergyStorageContainerLoad($scope.currentEnergyStorageContainer.id, modifiedEnergyStorageContainerLoad, headers, function (response) {
137
  				if (angular.isDefined(response.status) && response.status === 200) {
138
  					toaster.pop({
139
  						type: "success",
140
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
141
  						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
142
  						showCloseButton: true,
143
  					});
144
  					$scope.getEnergyStorageContainerLoadsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
145
            		$scope.$emit('handleEmitEnergyStorageContainerLoadChanged');
146
  				} else {
147
  					toaster.pop({
148
  						type: "error",
149
  						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
150
  						body: $translate.instant(response.data.description),
151
  						showCloseButton: true,
152
  					});
153
  				}
154
  			});
155
  		}, function() {
156
  			//do nothing;
157
  		});
158
		$rootScope.modalInstance = modalInstance;
159
  	};
160
161
  	$scope.deleteEnergyStorageContainerLoad = function(energystoragecontainerload) {
162
  		SweetAlert.swal({
163
  				title: $translate.instant("SWEET.TITLE"),
164
  				text: $translate.instant("SWEET.TEXT"),
165
  				type: "warning",
166
  				showCancelButton: true,
167
  				confirmButtonColor: "#DD6B55",
168
  				confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
169
  				cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
170
  				closeOnConfirm: true,
171
  				closeOnCancel: true
172
  			},
173
  			function(isConfirm) {
174
  				if (isConfirm) {
175
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
176
  					EnergyStorageContainerLoadService.deleteEnergyStorageContainerLoad($scope.currentEnergyStorageContainer.id, energystoragecontainerload.id, headers, function (response) {
177
  						if (angular.isDefined(response.status) && response.status === 204) {
178
							toaster.pop({
179
								type: "success",
180
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
181
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
182
								showCloseButton: true,
183
							});
184
							$scope.getEnergyStorageContainerLoadsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
185
							$scope.$emit('handleEmitEnergyStorageContainerLoadChanged');
186
  						} else {
187
							toaster.pop({
188
								type: "error",
189
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_LOAD")}),
190
								body: $translate.instant(response.data.description),
191
								showCloseButton: true,
192
							});
193
  				   		}
194
  					});
195
  				}
196
  			});
197
  	};
198
199
  	$scope.getAllEnergyStorageContainers();
200
	$scope.getAllPoints();
201
	$scope.getAllMeters();
202
    $scope.$on('handleBroadcastEnergyStorageContainerChanged', function(event) {
203
      $scope.getAllEnergyStorageContainers();
204
  	});
205
206
  });
207
208
209
  app.controller('ModalAddEnergyStorageContainerLoadCtrl', function($scope, $uibModalInstance, params) {
210
211
  	$scope.operation = "ENERGY_STORAGE_CONTAINER.ADD_ENERGY_STORAGE_CONTAINER_LOAD";
212
	$scope.points=params.points;
213
	$scope.meters=params.meters;
214
  	$scope.ok = function() {
215
  		$uibModalInstance.close($scope.energystoragecontainerload);
216
  	};
217
218
  	$scope.cancel = function() {
219
  		$uibModalInstance.dismiss('cancel');
220
  	};
221
  });
222
223
  app.controller('ModalEditEnergyStorageContainerLoadCtrl', function($scope, $uibModalInstance, params) {
224
  	$scope.operation = "ENERGY_STORAGE_CONTAINER.EDIT_ENERGY_STORAGE_CONTAINER_LOAD";
225
  	$scope.energystoragecontainerload = params.energystoragecontainerload;
226
	$scope.points=params.points;
227
	$scope.meters=params.meters;
228
  	$scope.ok = function() {
229
  		$uibModalInstance.close($scope.energystoragecontainerload);
230
  	};
231
232
  	$scope.cancel = function() {
233
  		$uibModalInstance.dismiss('cancel');
234
  	};
235
  });
236