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

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

Complexity

Total Complexity 43
Complexity/F 1.23

Size

Lines of Code 279
Function Count 35

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 195
dl 0
loc 279
rs 8.96
c 0
b 0
f 0
mnd 8
bc 8
fnc 35
bpm 0.2285
cpm 1.2285
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/gateway/gateway.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('GatewayController', function($scope,
4
	$rootScope,
5
	$window,
6
	$translate,
7
	$uibModal,
8
	GatewayService,
9
	toaster,
10
	SweetAlert) {
11
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
12
	$scope.exportdata = '';
13
	$scope.importdata = '';
14
15
	$scope.getAllGateways = function() {
16
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
17
		GatewayService.getAllGateways(headers, function (response) {
18
			if (angular.isDefined(response.status) && response.status === 200) {
19
				$scope.gateways = response.data;
20
			} else {
21
				$scope.gateways = [];
22
			}
23
		});
24
25
	};
26
27
	$scope.addGateway = function() {
28
		var modalInstance = $uibModal.open({
29
			templateUrl: 'views/settings/gateway/gateway.model.html',
30
			controller: 'ModalAddGatewayCtrl',
31
			windowClass: "animated fadeIn",
32
			resolve: {
33
				params: function() {
34
					return {
35
						gateways: angular.copy($scope.gateways),
36
					};
37
				}
38
			}
39
		});
40
		modalInstance.result.then(function(gateway) {
41
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
42
			GatewayService.addGateway(gateway, headers, function(response) {
43
				if (angular.isDefined(response.status) && response.status === 201) {
44
					toaster.pop({
45
						type: "success",
46
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
47
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
48
						showCloseButton: true,
49
					});
50
					$scope.getAllGateways();
51
					$scope.$emit('handleEmitGatewayChanged');
52
				} else {
53
					toaster.pop({
54
						type: "error",
55
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
56
						body: $translate.instant(response.data.description),
57
						showCloseButton: true,
58
					});
59
				}
60
			});
61
		}, function() {
62
63
		});
64
		$rootScope.modalInstance = modalInstance;
65
	};
66
67
	$scope.editGateway = function(gateway) {
68
		var modalInstance = $uibModal.open({
69
			windowClass: "animated fadeIn",
70
			templateUrl: 'views/settings/gateway/gateway.model.html',
71
			controller: 'ModalEditGatewayCtrl',
72
			resolve: {
73
				params: function() {
74
					return {
75
						gateway: angular.copy(gateway),
76
						gateways: angular.copy($scope.gateways),
77
					};
78
				}
79
			}
80
		});
81
82
		modalInstance.result.then(function(modifiedGateway) {
83
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
84
			GatewayService.editGateway(modifiedGateway, headers, function(response) {
85
				if (angular.isDefined(response.status) && response.status === 200) {
86
					toaster.pop({
87
						type: "success",
88
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
89
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
90
						showCloseButton: true,
91
					});
92
					$scope.getAllGateways();
93
					$scope.$emit('handleEmitGatewayChanged');
94
				} else {
95
					toaster.pop({
96
						type: "error",
97
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
98
						body: $translate.instant(response.data.description),
99
						showCloseButton: true,
100
					});
101
				}
102
			});
103
		}, function() {
104
			//do nothing;
105
		});
106
		$rootScope.modalInstance = modalInstance;
107
	};
108
109
	$scope.deleteGateway = function(gateway) {
110
		SweetAlert.swal({
111
			title: $translate.instant("SWEET.TITLE"),
112
			text: $translate.instant("SWEET.TEXT"),
113
			type: "warning",
114
			showCancelButton: true,
115
			confirmButtonColor: "#DD6B55",
116
			confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
117
			cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
118
			closeOnConfirm: true,
119
			closeOnCancel: true
120
		},
121
		function(isConfirm) {
122
			if (isConfirm) {
123
				let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
124
				GatewayService.deleteGateway(gateway, headers, function(response) {
125
					if (angular.isDefined(response.status) && response.status === 204) {
126
						toaster.pop({
127
							type: "success",
128
							title: $translate.instant("TOASTER.SUCCESS_TITLE"),
129
							body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
130
							showCloseButton: true,
131
						});
132
						$scope.getAllGateways();
133
						$scope.$emit('handleEmitGatewayChanged');
134
					} else {
135
						toaster.pop({
136
							type: "error",
137
							title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
138
							body: $translate.instant(response.data.description),
139
							showCloseButton: true,
140
						});
141
					}
142
				});
143
			}
144
		});
145
	};
146
147
	$scope.exportGateway = function(gateway) {
148
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
149
		GatewayService.exportGateway(gateway, headers, function(response) {
150
			if (angular.isDefined(response.status) && response.status === 200) {
151
				$scope.exportdata = JSON.stringify(response.data);
152
				var modalInstance = $uibModal.open({
153
					windowClass: "animated fadeIn",
154
					templateUrl: 'views/common/export.html',
155
					controller: 'ModalExportCtrl',
156
					resolve: {
157
						params: function() {
158
							return {
159
								exportdata: angular.copy($scope.exportdata)
160
							};
161
						}
162
					}
163
				});
164
				modalInstance.result.then(function() {
165
					//do nothing;
166
				}, function() {
167
					//do nothing;
168
				});
169
				$rootScope.modalInstance = modalInstance;
170
			} else {
171
				$scope.exportdata = null;
172
			}
173
		});
174
	};
175
176
	$scope.cloneGateway = function(gateway){
177
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
178
		GatewayService.cloneGateway(gateway, headers, function(response) {
179
			if (angular.isDefined(response.status) && response.status === 201) {
180
				toaster.pop({
181
					type: "success",
182
					title: $translate.instant("TOASTER.SUCCESS_TITLE"),
183
					body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
184
					showCloseButton: true,
185
				});
186
				$scope.$emit('handleEmitGatewayChanged');
187
				$scope.getAllGateways();
188
			}else {
189
				toaster.pop({
190
					type: "error",
191
					title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
192
					body: $translate.instant(response.data.description),
193
					showCloseButton: true,
194
				});
195
			}
196
		});
197
	};
198
199
	$scope.importGateway = function() {
200
		var modalInstance = $uibModal.open({
201
			templateUrl: 'views/common/import.html',
202
			controller: 'ModalImportCtrl',
203
			windowClass: "animated fadeIn",
204
			resolve: {
205
				params: function() {
206
					return {
207
					};
208
				}
209
			}
210
		});
211
		modalInstance.result.then(function(importdata) {
212
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
213
			GatewayService.importGateway(importdata, headers, function(response) {
214
				if (angular.isDefined(response.status) && response.status === 201) {
215
					toaster.pop({
216
						type: "success",
217
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
218
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("GATEWAY.GATEWAY")}),
219
						showCloseButton: true,
220
					});
221
					$scope.$emit('handleEmitGatewayChanged');
222
					$scope.getAllGateways();
223
				} else {
224
					toaster.pop({
225
						type: "error",
226
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("GATEWAY.GATEWAY") }),
227
						body: $translate.instant(response.data.description),
228
						showCloseButton: true,
229
					});
230
				}
231
			});
232
		}, function() {
233
234
		});
235
		$rootScope.modalInstance = modalInstance;
236
	};
237
238
    $scope.copyToClipboardGateway = function (gateway) {
239
        let tempInput = document.createElement("input");
240
        tempInput.value = gateway.token;
241
        document.body.appendChild(tempInput);
242
        tempInput.select();
243
        document.execCommand("copy");
244
        document.body.removeChild(tempInput);
245
        toaster.pop({
246
            type: "success",
247
            title: $translate.instant("TOASTER.SUCCESS_TITLE"),
248
            body: $translate.instant("TOASTER.COPY_SUCCESS"), 
249
            showCloseButton: true,
250
        });         
251
    };
252
253
	$scope.getAllGateways();
254
});
255
256
app.controller('ModalAddGatewayCtrl', function($scope, $uibModalInstance, params) {
257
258
	$scope.operation = "GATEWAY.ADD_GATEWAY";
259
	$scope.ok = function() {
260
		$uibModalInstance.close($scope.gateway);
261
	};
262
263
	$scope.cancel = function() {
264
		$uibModalInstance.dismiss('cancel');
265
	};
266
});
267
268
app.controller('ModalEditGatewayCtrl', function($scope, $uibModalInstance, params) {
269
	$scope.operation = "GATEWAY.EDIT_GATEWAY";
270
	$scope.gateway = params.gateway;
271
	$scope.gateways = params.gateways;
272
	$scope.ok = function() {
273
		$uibModalInstance.close($scope.gateway);
274
	};
275
276
	$scope.cancel = function() {
277
		$uibModalInstance.dismiss('cancel');
278
	};
279
});
280