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

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

Complexity

Total Complexity 43
Complexity/F 1.26

Size

Lines of Code 269
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 190
mnd 9
bc 9
fnc 34
dl 0
loc 269
rs 8.96
bpm 0.2647
cpm 1.2647
noi 1
c 0
b 0
f 0

How to fix   Complexity   

Complexity

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