Passed
Push — master ( 844492...7c39ee )
by
unknown
09:45 queued 11s
created

myems-admin/app/controllers/settings/energyflowdiagram/energyflowdiagram.controller.js   A

Complexity

Total Complexity 41
Complexity/F 1.24

Size

Lines of Code 254
Function Count 33

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 41
eloc 178
dl 0
loc 254
rs 9.1199
c 0
b 0
f 0
mnd 8
bc 8
fnc 33
bpm 0.2424
cpm 1.2424
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/energyflowdiagram/energyflowdiagram.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('EnergyFlowDiagramController', function(
4
    $scope,
5
    $rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	EnergyFlowDiagramService,
10
	toaster,
11
	SweetAlert) {
12
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
13
	$scope.exportdata = '';
14
	$scope.importdata = '';
15
	$scope.searchKeyword = '';
16
	$scope.getAllEnergyFlowDiagrams = function() {
17
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
18
		EnergyFlowDiagramService.getAllEnergyFlowDiagrams(headers, function (response) {
19
			if (angular.isDefined(response.status) && response.status === 200) {
20
				$scope.energyflowdiagrams = response.data;
21
			} else {
22
				$scope.energyflowdiagrams = [];
23
			}
24
		});
25
	};
26
27
	$scope.addEnergyFlowDiagram = function() {
28
		var modalInstance = $uibModal.open({
29
			templateUrl: 'views/settings/energyflowdiagram/energyflowdiagram.model.html',
30
			controller: 'ModalAddEnergyFlowDiagramCtrl',
31
			windowClass: "animated fadeIn",
32
		});
33
		modalInstance.result.then(function(energyflowdiagram) {
34
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
35
			EnergyFlowDiagramService.addEnergyFlowDiagram(energyflowdiagram, headers, function (response) {
36
				if (angular.isDefined(response.status) && response.status === 201) {
37
					toaster.pop({
38
						type: "success",
39
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
40
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
41
						showCloseButton: true,
42
					});
43
					$scope.getAllEnergyFlowDiagrams();
44
					$scope.$emit('handleEmitEnergyFlowDiagramChanged');
45
				} else {
46
					toaster.pop({
47
						type: "error",
48
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
49
						body: $translate.instant(response.data.description),
50
						showCloseButton: true,
51
					});
52
				}
53
			});
54
		}, function() {
55
56
		});
57
		$rootScope.modalInstance = modalInstance;
58
	};
59
60
	$scope.editEnergyFlowDiagram = function(energyflowdiagram) {
61
		var modalInstance = $uibModal.open({
62
			windowClass: "animated fadeIn",
63
			templateUrl: 'views/settings/energyflowdiagram/energyflowdiagram.model.html',
64
			controller: 'ModalEditEnergyFlowDiagramCtrl',
65
			resolve: {
66
				params: function() {
67
					return {
68
						energyflowdiagram: angular.copy(energyflowdiagram)
69
					};
70
				}
71
			}
72
		});
73
74
		modalInstance.result.then(function(modifiedEnergyFlowDiagram) {
75
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
76
			EnergyFlowDiagramService.editEnergyFlowDiagram(modifiedEnergyFlowDiagram, headers, function (response) {
77
				if (angular.isDefined(response.status) && response.status === 200) {
78
					toaster.pop({
79
						type: "success",
80
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
81
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
82
						showCloseButton: true,
83
					});
84
					$scope.getAllEnergyFlowDiagrams();
85
					$scope.$emit('handleEmitEnergyFlowDiagramChanged');
86
				} else {
87
					toaster.pop({
88
						type: "error",
89
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
90
						body: $translate.instant(response.data.description),
91
						showCloseButton: true,
92
					});
93
				}
94
			});
95
		}, function() {
96
			//do nothing;
97
		});
98
		$rootScope.modalInstance = modalInstance;
99
	};
100
101
	$scope.deleteEnergyFlowDiagram=function(energyflowdiagram){
102
		SweetAlert.swal({
103
		        title: $translate.instant("SWEET.TITLE"),
104
		        text: $translate.instant("SWEET.TEXT"),
105
		        type: "warning",
106
		        showCancelButton: true,
107
		        confirmButtonColor: "#DD6B55",
108
		        confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
109
		        cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
110
		        closeOnConfirm: true,
111
		        closeOnCancel: true },
112
		    function (isConfirm) {
113
		        if (isConfirm) {
114
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
115
		            EnergyFlowDiagramService.deleteEnergyFlowDiagram(energyflowdiagram, headers, function (response) {
116
		            	if (angular.isDefined(response.status) && response.status === 204) {
117
							toaster.pop({
118
								type: "success",
119
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
120
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
121
								showCloseButton: true,
122
							});
123
							$scope.getAllEnergyFlowDiagrams();
124
							$scope.$emit('handleEmitEnergyFlowDiagramChanged');
125
		            	} else {
126
							toaster.pop({
127
								type: "error",
128
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
129
								body: $translate.instant(response.data.description),
130
								showCloseButton: true,
131
							});
132
		            	}
133
		            });
134
		        }
135
		    });
136
	};
137
138
	$scope.exportEnergyFlowDiagram = function(energyflowdiagram) {
139
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
140
		EnergyFlowDiagramService.exportEnergyFlowDiagram(energyflowdiagram, headers, function(response) {
141
			if (angular.isDefined(response.status) && response.status === 200) {
142
				$scope.exportdata = JSON.stringify(response.data);
143
				var modalInstance = $uibModal.open({
144
					windowClass: "animated fadeIn",
145
					templateUrl: 'views/common/export.html',
146
					controller: 'ModalExportCtrl',
147
					resolve: {
148
						params: function() {
149
							return {
150
								exportdata: angular.copy($scope.exportdata)
151
							};
152
						}
153
					}
154
				});
155
				modalInstance.result.then(function() {
156
					//do nothing;
157
				}, function() {
158
					//do nothing;
159
				});
160
				$rootScope.modalInstance = modalInstance;
161
			} else {
162
				$scope.exportdata = null;
163
			}
164
		});
165
	};
166
167
	$scope.cloneEnergyFlowDiagram = function(energyflowdiagram){
168
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
169
		EnergyFlowDiagramService.cloneEnergyFlowDiagram(energyflowdiagram, headers, function(response) {
170
			if (angular.isDefined(response.status) && response.status === 201) {
171
				toaster.pop({
172
					type: "success",
173
					title: $translate.instant("TOASTER.SUCCESS_TITLE"),
174
					body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
175
					showCloseButton: true,
176
				});
177
				$scope.getAllEnergyFlowDiagrams();
178
				$scope.$emit('handleEmitEnergyFlowDiagramChanged');
179
			}else {
180
				toaster.pop({
181
					type: "error",
182
					title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
183
					body: $translate.instant(response.data.description),
184
					showCloseButton: true,
185
				});
186
			}
187
		});
188
	};
189
190
	$scope.importEnergyFlowDiagram = function() {
191
		var modalInstance = $uibModal.open({
192
			templateUrl: 'views/common/import.html',
193
			controller: 'ModalImportCtrl',
194
			windowClass: "animated fadeIn",
195
			resolve: {
196
				params: function() {
197
					return {
198
					};
199
				}
200
			}
201
		});
202
		modalInstance.result.then(function(importdata) {
203
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
204
			EnergyFlowDiagramService.importEnergyFlowDiagram(importdata, headers, function(response) {
205
				if (angular.isDefined(response.status) && response.status === 201) {
206
					toaster.pop({
207
						type: "success",
208
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
209
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM")}),
210
						showCloseButton: true,
211
					});
212
					$scope.getAllEnergyFlowDiagrams();
213
					$scope.$emit('handleEmitEnergyFlowDiagramChanged');
214
				} else {
215
					toaster.pop({
216
						type: "error",
217
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.ENERGY_FLOW_DIAGRAM") }),
218
						body: $translate.instant(response.data.description),
219
						showCloseButton: true,
220
					});
221
				}
222
			});
223
		}, function() {
224
225
		});
226
		$rootScope.modalInstance = modalInstance;
227
	};
228
229
	let searchDebounceTimer = null;
230
	function safeApply(scope) {
231
		if (!scope.$$phase && !scope.$root.$$phase) {
232
			scope.$apply();
233
		}
234
	}
235
	$scope.searchEnergyFlowDiagrams = function() {
236
		const headers = {
237
			"User-UUID": $scope.cur_user?.uuid,
238
			"Token": $scope.cur_user?.token
239
		};
240
241
		const rawKeyword = $scope.searchKeyword || "";
242
		const trimmedKeyword = rawKeyword.trim();
243
244
		if (searchDebounceTimer) {
245
			clearTimeout(searchDebounceTimer);
246
		}
247
248
		searchDebounceTimer = setTimeout(() => {
249
			if (!trimmedKeyword) {
250
				$scope.getAllEnergyFlowDiagrams();
251
				safeApply($scope);
252
				return;
253
			}
254
255
			EnergyFlowDiagramService.searchEnergyFlowDiagrams(trimmedKeyword, headers, (response) => {
256
				$scope.energyflowdiagrams = (response.status === 200) ? response.data : [];
257
				$scope.parentmeters = [...$scope.energyflowdiagrams];
258
			});
259
		}, 300);
260
	};
261
262
263
	$scope.getAllEnergyFlowDiagrams();
264
});
265
266
app.controller("ModalAddEnergyFlowDiagramCtrl", function(  $scope,  $uibModalInstance) {
267
  $scope.operation = "ENERGY_FLOW_DIAGRAM.ADD_ENERGY_FLOW_DIAGRAM";
268
  $scope.ok = function() {
269
    $uibModalInstance.close($scope.energyflowdiagram);
270
  };
271
272
  $scope.cancel = function() {
273
    $uibModalInstance.dismiss("cancel");
274
  };
275
});
276
277
app.controller("ModalEditEnergyFlowDiagramCtrl", function($scope, $uibModalInstance,  params) {
278
  $scope.operation = "ENERGY_FLOW_DIAGRAM.EDIT_ENERGY_FLOW_DIAGRAM";
279
  $scope.energyflowdiagram = params.energyflowdiagram;
280
281
  $scope.ok = function() {
282
    $uibModalInstance.close($scope.energyflowdiagram);
283
  };
284
285
  $scope.cancel = function() {
286
    $uibModalInstance.dismiss("cancel");
287
  };
288
});
289