Completed
Push — js-scrutinizing ( b30283...91fb03 )
by Maxence
02:14
created

js/circles.app.actions.js   B

Complexity

Total Complexity 52
Complexity/F 1.86

Size

Lines of Code 281
Function Count 28

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 281
rs 7.9487
wmc 52
mnd 2
bc 40
fnc 28
bpm 1.4285
cpm 1.8571
noi 3

17 Functions

Rating   Name   Duplication   Size   Complexity  
A actions.onEventNewCircleType 0 6 1
A actions.removeMemberResult 0 16 3
B actions.getStringTypeFromType 0 14 5
A actions.leaveCircleResult 0 19 3
A actions.onEventNewCircleName 0 4 1
B actions.listCirclesResult 0 22 4
A actions.addMemberResult 0 13 3
A actions.initActions 0 4 1
A actions.joinCircleResult 0 19 4
A actions.onEventNewCircle 0 9 1
A actions.searchMembersRequest 0 16 2
A actions.searchMembersResult 0 19 4
A actions.initElementsCircleActions 0 18 1
A actions.createCircleResult 0 14 3
A actions.selectCircle 0 6 1
A actions.initElementsMemberActions 0 9 1
B actions.selectCircleResult 0 24 3

How to fix   Complexity   

Complexity

Complex classes like js/circles.app.actions.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
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: nav */
31
/** global: elements */
32
/** global: curr */
33
/** global: api */
34
35
36
var actions = {
37
38
39
	initActions: function () {
40
		this.initElementsMemberActions();
41
		this.initElementsCircleActions();
42
	},
43
44
	initElementsMemberActions: function () {
45
46
		elements.addMember.on('input propertychange paste focus', function () {
47
			actions.searchMembersRequest($(this).val().trim());
48
		}).blur(function () {
49
			elements.membersSearchResult.fadeOut(400);
50
		});
51
52
	},
53
54
55
	joinCircleResult: function (result) {
56
		if (result.status == 1) {
57
58
			elements.removeMemberslistEntry(result.name);
59
			if (result.member.level == 1) {
60
				OCA.notification.onSuccess(
61
					"You have successfully joined this circle");
62
			} else {
63
				OCA.notification.onSuccess(
64
					"You have requested an invitation to join this circle");
65
			}
66
			actions.selectCircle(result.circle_id);
67
			return;
68
		}
69
70
		OCA.notification.onFail(
71
			"Cannot join this circle: " +
72
			((result.error) ? result.error : 'no error message'));
73
	},
74
75
76
	leaveCircleResult: function (result) {
77
		if (result.status == 1) {
78
79
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
80
				function () {
81
					$(this).hide(300);
82
				});
83
84
			actions.selectCircle(result.circle_id);
85
			OCA.notification.onSuccess(
86
				"You have successfully left this circle");
87
			return;
88
		}
89
90
		OCA.notification.onFail(
91
			"Cannot leave this circle: " +
92
			((result.error) ? result.error : 'no error message'));
93
94
	},
95
96
97
	initElementsCircleActions: function () {
98
99
		elements.joinCircle.on('click', function () {
100
			api.joinCircle(curr.circle, actions.joinCircleResult);
101
		});
102
103
		elements.leaveCircle.on('click', function () {
104
			api.leaveCircle(curr.circle, actions.leaveCircleResult);
105
		});
106
107
		elements.joinCircleAccept.on('click', function () {
108
			api.joinCircle(curr.circle, actions.joinCircleResult);
109
		});
110
111
		elements.joinCircleReject.on('click', function () {
112
			api.leaveCircle(curr.circle, actions.leaveCircleResult);
113
		});
114
	}
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before ,.
Loading history...
115
	,
0 ignored issues
show
introduced by
Comma warnings can be turned off with 'laxcomma'.
Loading history...
116
117
118
	createCircleResult: function (result) {
119
		var str = actions.getStringTypeFromType(result.type);
120
121
		if (result.status == 1) {
122
			OCA.notification.onSuccess(str + " '" + result.name + "' created");
123
			nav.displayCirclesList(result.circle.type);
124
			actions.selectCircle(result.circle.id);
125
			return;
126
		}
127
128
		OCA.notification.onFail(
129
			str + " '" + result.name + "' NOT created: " +
130
			((result.error) ? result.error : 'no error message'));
131
	},
132
133
134
	selectCircleResult: function (result) {
135
136
		elements.mainUIMembers.emptyTable();
137
		if (result.status < 1) {
138
			OCA.notification.onFail(
139
				'Issue while retreiving the details of a circle: ' +
140
				((result.error) ? result.error : 'no error message'));
141
			return;
142
		}
143
144
		elements.navigation.children('.circle').removeClass('selected');
145
		elements.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
146
			function () {
147
				$(this).addClass('selected');
148
			});
149
150
		elements.emptyContent.hide(800);
151
		elements.mainUI.fadeIn(800);
152
		curr.circle = result.circle_id;
153
		curr.circleLevel = result.details.user.level;
154
155
		nav.displayMembersInteraction(result.details);
156
		nav.displayMembers(result.details.members);
157
	},
158
159
160
	listCirclesResult: function (result) {
161
162
		if (result.status < 1) {
163
			OCA.notification.onFail(
164
				'Issue while retreiving the list of the Circles: ' +
165
				((result.error) ? result.error : 'no error message'));
166
			return;
167
		}
168
169
		elements.resetCirclesList();
170
171
		var data = result.data;
172
		for (var i = 0; i < data.length; i++) {
173
			var tmpl = elements.generateTmplCircle(data[i]);
174
			elements.navigation.append(
175
				'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
176
		}
177
178
		elements.navigation.children('.circle').on('click', function () {
179
			actions.selectCircle($(this).attr('circle-id'));
180
		});
181
	},
182
183
184
	selectCircle: function (circle_id) {
185
		curr.searchUser = '';
186
		elements.addMember.val('');
187
188
		api.detailsCircle(circle_id, actions.selectCircleResult);
189
	},
190
191
192
	/**
193
	 *
194
	 * @param search
195
	 */
196
	searchMembersRequest: function (search) {
197
198
		if (curr.searchUser == search) {
199
			return;
200
		}
201
202
		curr.searchUser = search;
203
204
		$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
205
			{
206
				format: 'json',
207
				search: search,
208
				perPage: 200,
209
				itemType: 'principals'
210
			}, actions.searchMembersResult);
211
	},
212
213
214
	searchMembersResult: function (response) {
215
216
		if (response === null ||
217
			(response.ocs.data.users === 0 && response.ocs.data.exact.users === 0)) {
218
			elements.membersSearchResult.fadeOut(300);
219
			return;
220
		}
221
222
		elements.membersSearchResult.children().remove();
223
224
		elements.fillMembersSearch(response.ocs.data.exact.users, response.ocs.data.users);
225
226
		$('.members_search').on('click', function () {
227
			api.addMember(curr.circle, $(this).attr('searchresult'),
228
				actions.addMemberResult);
229
		});
230
		elements.membersSearchResult.fadeIn(300);
231
232
	},
233
234
235
	addMemberResult: function (result) {
236
237
		if (result.status == 1) {
238
			OCA.notification.onSuccess(
239
				"Member '" + result.name + "' successfully added to the circle");
240
241
			nav.displayMembers(result.members);
242
			return;
243
		}
244
		OCA.notification.onFail(
245
			"Member '" + result.name + "' NOT added to the circle: " +
246
			((result.error) ? result.error : 'no error message'));
247
	},
248
249
250
	getStringTypeFromType: function (type) {
251
		switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
252
			case '1':
253
				return 'Personal circle';
254
			case '2':
255
				return 'Hidden circle';
256
			case '4':
257
				return 'Private circle';
258
			case '8':
259
				return 'Public circle';
260
		}
261
262
		return 'Circle';
263
	},
264
265
266
	removeMemberResult: function (result) {
267
		if (result.status == 1) {
268
269
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
270
				function () {
271
					$(this).hide(300);
272
				});
273
			OCA.notification.onSuccess(
274
				"Member '" + result.name + "' successfully removed from the circle");
275
			return;
276
		}
277
278
		OCA.notification.onFail(
279
			"Member '" + result.name + "' NOT removed from the circle: " +
280
			((result.error) ? result.error : 'no error message'));
281
	},
282
283
284
	/**
285
	 *
286
	 */
287
	onEventNewCircle: function () {
288
		curr.circle = 0;
289
		curr.circleLevel = 0;
290
291
		elements.navigation.hide('slide', 800);
292
		elements.circlesList.children('div').removeClass('selected');
293
		elements.emptyContent.show(800);
294
		elements.mainUI.fadeOut(800);
295
	},
296
297
	/**
298
	 *
299
	 */
300
	onEventNewCircleName: function () {
301
		this.onEventNewCircle();
302
		nav.displayOptionsNewCircle((elements.newName.val() !== ''));
303
	},
304
305
	/**
306
	 *
307
	 */
308
	onEventNewCircleType: function () {
309
		this.onEventNewCircle();
310
		elements.newTypeDefinition.children('div').fadeOut(300);
311
		$('#circles_new_type_' + elements.newType.children('option:selected').val()).fadeIn(
312
			300);
313
	}
314
315
316
};
317