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

myems-admin/app/controllers/settings/windfarm/windfarm.controller.js   D

Complexity

Total Complexity 58
Complexity/F 1.32

Size

Lines of Code 341
Function Count 44

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 58
eloc 242
dl 0
loc 341
rs 4.5599
c 0
b 0
f 0
mnd 14
bc 14
fnc 44
bpm 0.3181
cpm 1.3181
noi 0

How to fix   Complexity   

Complexity

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