Passed
Push — master ( 98975f...e9b166 )
by
unknown
09:13
created

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

Complexity

Total Complexity 49
Complexity/F 1.26

Size

Lines of Code 305
Function Count 39

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 49
eloc 208
dl 0
loc 305
rs 8.48
c 0
b 0
f 0
mnd 10
bc 10
fnc 39
bpm 0.2564
cpm 1.2564
noi 0

How to fix   Complexity   

Complexity

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