Passed
Push — master ( a22cd3...a7d888 )
by Guangyu
09:44 queued 18s
created

admin/app/controllers/settings/space/space.controller.js   B

Complexity

Total Complexity 52
Complexity/F 1.53

Size

Lines of Code 301
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 52
eloc 207
mnd 18
bc 18
fnc 34
dl 0
loc 301
rs 7.44
bpm 0.5294
cpm 1.5294
noi 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like admin/app/controllers/settings/space/space.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('SpaceController', function ($scope, $uibModal, SpaceService, CostCenterService, ContactService, toaster, $translate, SweetAlert) {
4
	$scope.spaces = [];
5
	$scope.currentSpaceID = 1;
6
	$scope.currentSpace = {};
7
	$scope.currentSpaceChildren = [];
8
9
	$scope.getAllCostCenters = function () {
10
		CostCenterService.getAllCostCenters(function (response) {
11
			if (angular.isDefined(response.status) && response.status === 200) {
12
				$scope.costcenters = response.data;
13
			} else {
14
				$scope.costcenters = [];
15
			}
16
		});
17
	};
18
19
	$scope.getAllContacts = function () {
20
		ContactService.getAllContacts(function (response) {
21
			if (angular.isDefined(response.status) && response.status === 200) {
22
				$scope.contacts = response.data;
23
			} else {
24
				$scope.contacts = [];
25
			}
26
		});
27
	};
28
29
	$scope.getAllSpaces = function () {
30
		SpaceService.getAllSpaces(function (response) {
31
			if (angular.isDefined(response.status) && response.status === 200) {
32
				$scope.spaces = response.data;
33
			} else {
34
				$scope.spaces = [];
35
			}
36
			//create space tree
37
			var treedata = { 'core': { 'data': [], "multiple": false, }, "plugins": ["wholerow"] };
38
			for (var i = 0; i < $scope.spaces.length; i++) {
39
				if ($scope.spaces[i].id == 1) {
40
					var node = {
41
						"id": $scope.spaces[i].id.toString(),
42
						"parent": '#',
43
						"text": $scope.spaces[i].name,
44
						"state": { 'opened': true, 'selected': true },
45
					};
46
				} else {
47
					var node = {
48
						"id": $scope.spaces[i].id.toString(),
49
						"parent": $scope.spaces[i].parent_space.id.toString(),
50
						"text": $scope.spaces[i].name,
51
					};
52
				};
53
				treedata['core']['data'].push(node);
54
			}
55
56
			angular.element(spacetree).jstree(treedata);
57
			//space tree selected changed event handler
58
			angular.element(spacetree).on("changed.jstree", function (e, data) {
59
				if (data.action === 'ready' || data.action === 'select_node') {
60
					$scope.currentSpaceID = parseInt(data.selected[0]);
61
					$scope.getSpaceChildren($scope.currentSpaceID);
62
				}
63
			});
64
		});
65
	};
66
67
	$scope.refreshSpaceTree = function () {
68
		SpaceService.getAllSpaces(function (response) {
69
			if (angular.isDefined(response.status) && response.status === 200) {
70
				$scope.spaces = response.data;
71
			} else {
72
				$scope.spaces = [];
73
			}
74
			//create space tree
75
			var treedata = { 'core': { 'data': [], "multiple": false, }, "plugins": ["wholerow"] };
76
			for (var i = 0; i < $scope.spaces.length; i++) {
77
				if ($scope.spaces[i].id == 1) {
78
					var node = {
79
						"id": $scope.spaces[i].id.toString(),
80
						"parent": '#',
81
						"text": $scope.spaces[i].name,
82
						"state": { 'opened': true, 'selected': true },
83
					};
84
				} else {
85
					var node = {
86
						"id": $scope.spaces[i].id.toString(),
87
						"parent": $scope.spaces[i].parent_space.id.toString(),
88
						"text": $scope.spaces[i].name,
89
					};
90
				};
91
				treedata['core']['data'].push(node);
92
			}
93
			var spacetree = document.getElementById("spacetree");
94
			angular.element(spacetree).jstree(true).settings.core.data = treedata['core']['data'];
95
			angular.element(spacetree).jstree(true).refresh();
96
		});
97
	};
98
99
	$scope.getSpaceChildren = function (spaceid) {
100
		SpaceService.getSpaceChildren(spaceid, function (response) {
101
			if (angular.isDefined(response.status) && response.status === 200) {
102
				$scope.currentSpace = response.data["current"];
103
				$scope.currentSpaceChildren = response.data["children"];
104
			} else {
105
				$scope.currentSpace = {};
106
				$scope.currentSpaceChildren = [];
107
			}
108
		});
109
	};
110
111
	$scope.getAllTimezones = function () {
112
		SpaceService.getAllTimezones(function (response) {
113
			if (angular.isDefined(response.status) && response.status === 200) {
114
				$scope.timezones = response.data;
115
			} else {
116
				$scope.timezones = [];
117
			}
118
		});
119
	};
120
121
	$scope.addSpace = function () {
122
		var modalInstance = $uibModal.open({
123
			templateUrl: 'views/settings/space/space.model.html',
124
			controller: 'ModalAddSpaceCtrl',
125
			windowClass: "animated fadeIn",
126
			resolve: {
127
				params: function () {
128
					return {
129
						parent_space_id: angular.copy($scope.currentSpaceID),
130
						timezones: angular.copy($scope.timezones),
131
						costcenters: angular.copy($scope.costcenters),
132
						contacts: angular.copy($scope.contacts)
133
					};
134
				}
135
			}
136
		});
137
138
		modalInstance.result.then(function (space) {
139
			space.timezone_id = space.timezone.id;
140
			space.cost_center_id = space.cost_center.id;
141
			if (space.contact != null) {
142
				space.contact_id = space.contact.id;
143
			}
144
			SpaceService.addSpace(space, function (response) {
145
				if (angular.isDefined(response.status) && response.status === 201) {
146
					toaster.pop({
147
						type: "success",
148
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
149
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", { template: $translate.instant("COMMON.SPACE") }),
150
						showCloseButton: true,
151
					});
152
					$scope.$emit('handleEmitSpaceChanged');
153
				} else {
154
					toaster.pop({
155
						type: "error",
156
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.SPACE") }),
157
						body: $translate.instant(response.data.description),
158
						showCloseButton: true,
159
					});
160
				}
161
			});
162
		}, function () {
163
164
		});
165
	};
166
167
	$scope.editSpace = function (space) {
168
		var modalInstance = $uibModal.open({
169
			windowClass: "animated fadeIn",
170
			templateUrl: 'views/settings/space/space.model.html',
171
			controller: 'ModalEditSpaceCtrl',
172
			resolve: {
173
				params: function () {
174
					return {
175
						space: angular.copy(space),
176
						spaces: angular.copy($scope.spaces),
177
						timezones: angular.copy($scope.timezones),
178
						costcenters: angular.copy($scope.costcenters),
179
						contacts: angular.copy($scope.contacts),
180
					};
181
				}
182
			}
183
		});
184
185
		modalInstance.result.then(function (modifiedSpace) {
186
			if (modifiedSpace.parent_space != null) {
187
				modifiedSpace.parent_space_id = modifiedSpace.parent_space.id;
188
			} else {
189
				modifiedSpace.parent_space_id = null;
190
			}
191
			modifiedSpace.timezone_id = modifiedSpace.timezone.id;
192
			if (modifiedSpace.contact != null) {
193
				modifiedSpace.contact_id = modifiedSpace.contact.id;
194
			}
195
			 
196
			modifiedSpace.cost_center_id = modifiedSpace.cost_center.id;
197
			SpaceService.editSpace(modifiedSpace, function (response) {
198
				if (angular.isDefined(response.status) && response.status === 200) {
199
					toaster.pop({
200
						type: "success",
201
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
202
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", { template: $translate.instant("COMMON.SPACE") }),
203
						showCloseButton: true,
204
					});
205
					$scope.$emit('handleEmitSpaceChanged');
206
				} else {
207
					toaster.pop({
208
						type: "error",
209
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", { template: $translate.instant("COMMON.SPACE") }),
210
						body: $translate.instant(response.data.description),
211
						showCloseButton: true,
212
					});
213
				}
214
			});
215
		}, function () {
216
			//do nothing;
217
		});
218
	};
219
220
	$scope.deleteSpace = function (space) {
221
		SweetAlert.swal({
222
			title: $translate.instant("SWEET.TITLE"),
223
			text: $translate.instant("SWEET.TEXT"),
224
			type: "warning",
225
			showCancelButton: true,
226
			confirmButtonColor: "#DD6B55",
227
			confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
228
			cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
229
			closeOnConfirm: true,
230
			closeOnCancel: true
231
		},
232
			function (isConfirm) {
233
				if (isConfirm) {
234
					SpaceService.deleteSpace(space, function (response) {
235
						if (angular.isDefined(response.status) && response.status === 204) {
236
							toaster.pop({
237
								type: "success",
238
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
239
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", { template: $translate.instant("COMMON.SPACE") }),
240
								showCloseButton: true,
241
							});
242
							$scope.$emit('handleEmitSpaceChanged');
243
						} else {
244
							toaster.pop({
245
								type: "error",
246
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", { template: $translate.instant("COMMON.SPACE") }),
247
								body: $translate.instant(response.data.description),
248
								showCloseButton: true,
249
							});
250
						}
251
					});
252
				}
253
			});
254
	};
255
256
	$scope.getAllSpaces();
257
	$scope.getAllTimezones();
258
	$scope.getAllCostCenters();
259
	$scope.getAllContacts();
260
261
	$scope.$on('handleBroadcastSpaceChanged', function (event) {
262
		$scope.refreshSpaceTree();
263
	});
264
265
});
266
267
app.controller('ModalAddSpaceCtrl', function ($scope, $uibModalInstance, params) {
268
269
	$scope.operation = "SETTING.ADD_SPACE";
270
	$scope.timezones = params.timezones;
271
	$scope.costcenters = params.costcenters;
272
	$scope.contacts = params.contacts;
273
	$scope.space = {
274
		parent_space_id: params.parent_space_id,
275
		is_input_counted: false,
276
		is_output_counted: false,
277
	};
278
	$scope.ok = function () {
279
		$uibModalInstance.close($scope.space);
280
	};
281
282
	$scope.cancel = function () {
283
		$uibModalInstance.dismiss('cancel');
284
	};
285
});
286
287
app.controller('ModalEditSpaceCtrl', function ($scope, $uibModalInstance, params) {
288
	$scope.operation = "SETTING.EDIT_SPACE";
289
	$scope.space = params.space;
290
	$scope.timezones = params.timezones;
291
	$scope.costcenters = params.costcenters;
292
	$scope.contacts = params.contacts;
293
294
	$scope.ok = function () {
295
		$uibModalInstance.close($scope.space);
296
	};
297
298
	$scope.cancel = function () {
299
		$uibModalInstance.dismiss('cancel');
300
	};
301
});
302