Passed
Push — master ( 83b21b...a685b8 )
by Guangyu
08:05 queued 11s
created

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

Complexity

Total Complexity 53
Complexity/F 1.56

Size

Lines of Code 308
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 53
eloc 213
mnd 19
bc 19
fnc 34
dl 0
loc 308
rs 6.96
bpm 0.5588
cpm 1.5588
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.FAILURE_TITLE"),
157
						body: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.SPACE") }),
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.FAILURE_TITLE"),
210
						body: $translate.instant("TOASTER.ERROR_UPDATE_BODY", { template: $translate.instant("COMMON.SPACE") }),
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 if (angular.isDefined(response.status) && response.status === 400) {
244
							toaster.pop({
245
								type: "success",
246
								title: $translate.instant(response.data.title),
247
								body: $translate.instant(response.data.description),
248
								showCloseButton: true,
249
							});
250
						} else {
251
							toaster.pop({
252
								type: "error",
253
								title: $translate.instant("TOASTER.FAILURE_TITLE"),
254
								body: $translate.instant("TOASTER.ERROR_DELETE_BODY", { template: $translate.instant("COMMON.SPACE") }),
255
								showCloseButton: true,
256
							});
257
						}
258
					});
259
				}
260
			});
261
	};
262
263
	$scope.getAllSpaces();
264
	$scope.getAllTimezones();
265
	$scope.getAllCostCenters();
266
	$scope.getAllContacts();
267
268
	$scope.$on('handleBroadcastSpaceChanged', function (event) {
269
		$scope.refreshSpaceTree();
270
	});
271
272
});
273
274
app.controller('ModalAddSpaceCtrl', function ($scope, $uibModalInstance, params) {
275
276
	$scope.operation = "SETTING.ADD_SPACE";
277
	$scope.timezones = params.timezones;
278
	$scope.costcenters = params.costcenters;
279
	$scope.contacts = params.contacts;
280
	$scope.space = {
281
		parent_space_id: params.parent_space_id,
282
		is_input_counted: false,
283
		is_output_counted: false,
284
	};
285
	$scope.ok = function () {
286
		$uibModalInstance.close($scope.space);
287
	};
288
289
	$scope.cancel = function () {
290
		$uibModalInstance.dismiss('cancel');
291
	};
292
});
293
294
app.controller('ModalEditSpaceCtrl', function ($scope, $uibModalInstance, params) {
295
	$scope.operation = "SETTING.EDIT_SPACE";
296
	$scope.space = params.space;
297
	$scope.timezones = params.timezones;
298
	$scope.costcenters = params.costcenters;
299
	$scope.contacts = params.contacts;
300
301
	$scope.ok = function () {
302
		$uibModalInstance.close($scope.space);
303
	};
304
305
	$scope.cancel = function () {
306
		$uibModalInstance.dismiss('cancel');
307
	};
308
});
309