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

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

Complexity

Total Complexity 41
Complexity/F 1.28

Size

Lines of Code 252
Function Count 32

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 41
eloc 178
dl 0
loc 252
rs 9.1199
c 0
b 0
f 0
mnd 9
bc 9
fnc 32
bpm 0.2812
cpm 1.2811
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/energystoragecontainer/energystoragecontainerpowerconversionsystem.controller.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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