Passed
Push — master ( bc2901...844492 )
by
unknown
11:09
created

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

Complexity

Total Complexity 44
Complexity/F 1.26

Size

Lines of Code 279
Function Count 35

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 196
dl 0
loc 279
rs 8.8798
c 0
b 0
f 0
mnd 9
bc 9
fnc 35
bpm 0.2571
cpm 1.2571
noi 0

How to fix   Complexity   

Complexity

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