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

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

Complexity

Total Complexity 59
Complexity/F 1.31

Size

Lines of Code 355
Function Count 45

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 59
eloc 254
dl 0
loc 355
rs 4.08
c 0
b 0
f 0
mnd 14
bc 14
fnc 45
bpm 0.3111
cpm 1.3111
noi 0

How to fix   Complexity   

Complexity

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