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

myems-admin/app/controllers/settings/distributionsystem/distributionsystem.controller.js   B

Complexity

Total Complexity 45
Complexity/F 1.25

Size

Lines of Code 280
Function Count 36

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 45
eloc 197
dl 0
loc 280
rs 8.8
c 0
b 0
f 0
mnd 9
bc 9
fnc 36
bpm 0.25
cpm 1.25
noi 0

How to fix   Complexity   

Complexity

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