Completed
Push — master ( e66477...835a38 )
by Maxence
02:43
created

nav.displayMembers   C

Complexity

Conditions 8
Paths 30

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 62
rs 6.943
cc 8
nc 30
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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: actions */
31
/** global: nav */
32
/** global: elements */
33
/** global: settings */
34
/** global: resultCircles */
35
/** global: resultMembers */
36
/** global: resultGroups */
37
/** global: resultLinks */
38
/** global: curr */
39
/** global: api */
40
/** global: define */
41
42
var nav = {
43
44
	initNavigation: function () {
45
		this.initElementsAddMemberNavigation();
46
		this.initElementsLinkCircleNavigation();
47
		this.initElementsCircleNavigation();
48
49
		this.displayCirclesList('all');
50
	},
51
52
53
	initElementsAddMemberNavigation: function () {
54
55
		elements.addMember.hide();
56
		elements.addMember.on('input propertychange paste focus', function () {
57
			var search = $(this).val().trim();
58
			if (search === '') {
59
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
60
				return;
61
			}
62
63
			actions.searchMembersRequest(search);
64
			if (elements.membersSearchResult.children().length === 0) {
65
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
66
			} else {
67
				elements.membersSearchResult.fadeIn(curr.animationMenuSpeed);
68
			}
69
		}).blur(function () {
70
			setTimeout(function () {
71
				elements.membersSearchResult.fadeOut(curr.animationMenuSpeed);
72
				nav.circlesActionReturn();
73
			}, 100);
74
		});
75
		elements.addMember.on('keydown', function (e) {
76
			if (e.keyCode === 27) {
77
				nav.circlesActionReturn();
78
			}
79
			if (e.keyCode === 13) {
80
				api.addMember(curr.circle, $(this).val(), resultMembers.addMemberResult);
81
			}
82
		});
83
84
85
		elements.linkGroup.on('input propertychange paste focus', function () {
86
			var search = $(this).val().trim();
87
			if (search === '') {
88
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
89
				return;
90
			}
91
92
			actions.searchGroupsRequest(search);
93
			if (elements.groupsSearchResult.children().length === 0) {
94
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
95
			} else {
96
				elements.groupsSearchResult.fadeIn(curr.animationMenuSpeed);
97
			}
98
		}).blur(function () {
99
			setTimeout(function () {
100
				elements.groupsSearchResult.fadeOut(curr.animationMenuSpeed);
101
				nav.circlesActionReturn();
102
			}, 100);
103
		});
104
		elements.linkGroup.on('keydown', function (e) {
105
			if (e.keyCode === 27) {
106
				nav.circlesActionReturn();
107
			}
108
			if (e.keyCode === 13) {
109
				api.linkGroup(curr.circle, $(this).val(), resultGroups.linkGroupResult);
110
			}
111
		});
112
	},
113
114
115
	initElementsLinkCircleNavigation: function () {
116
117
		elements.linkCircle.hide();
118
		elements.linkCircle.on('keydown', function (e) {
119
120
			if (e.keyCode === 27) {
121
				nav.circlesActionReturn();
122
			}
123
			if (e.keyCode !== 13) {
124
				return;
125
			}
126
127
			api.linkCircle(curr.circle, elements.linkCircle.val().trim(),
128
				resultLinks.linkCircleResult);
129
		}).blur(function () {
130
			nav.circlesActionReturn();
131
		});
132
	},
133
134
135
	initElementsCircleNavigation: function () {
136
137
		elements.joinCircle.hide();
138
		elements.joinCircle.on('click', function () {
139
			api.joinCircle(curr.circle, resultCircles.joinCircleResult);
140
			nav.circlesActionReturn();
141
		});
142
143
		elements.leaveCircle.hide();
144
		elements.leaveCircle.on('click', function () {
145
			OC.dialogs.confirm(
146
				t('circles', 'Are you sure you want to leave this circle?'),
147
				t('circles', 'Please confirm'),
148
				function (e) {
149
					if (e === true) {
150
						api.leaveCircle(curr.circle, resultCircles.leaveCircleResult);
151
						nav.circlesActionReturn();
152
					}
153
				});
154
		});
155
156
		elements.destroyCircle.on('click', function () {
157
			OC.dialogs.confirm(
158
				t('circles', 'Are you sure you want to delete this circle?'),
159
				t('circles', 'This action is irreversible'),
160
				function (e) {
161
					if (e === true) {
162
						api.destroyCircle(curr.circle, resultCircles.destroyCircleResult);
163
					}
164
				});
165
		});
166
167
		elements.joinCircleAccept.on('click', function () {
168
			api.joinCircle(curr.circle, resultCircles.joinCircleResult);
169
		});
170
171
		elements.joinCircleReject.on('click', function () {
172
			api.leaveCircle(curr.circle, resultCircles.leaveCircleResult);
173
		});
174
	},
175
176
177
	displayCirclesList: function (type) {
178
179
		curr.circlesType = type;
180
		curr.searchCircle = '';
181
		curr.searchUser = '';
182
183
		curr.circle = 0;
184
		curr.circleLevel = 0;
185
186
		elements.navigation.show('slide', 800);
187
		elements.emptyContent.show(800);
188
		elements.mainUI.fadeOut(800);
189
190
		elements.circlesSearch.val('');
191
		elements.addMember.val('');
192
		elements.linkCircle.val('');
193
194
		this.resetCirclesTypeSelection(type);
195
		elements.resetCirclesList();
196
		api.listCircles(type, '', 0, resultCircles.listCirclesResult);
197
	},
198
199
200
	resetCirclesTypeSelection: function (type) {
201
		elements.circlesList.children('div').removeClass('selected');
202
		elements.circlesList.children().each(function () {
203
			if ($(this).attr('circle-type') === type.toLowerCase()) {
204
				$(this).addClass('selected');
205
			}
206
		});
207
	},
208
209
	circlesActionReturn: function () {
210
		nav.displayCircleButtons(true);
211
		settings.displaySettings(false);
212
		nav.displayAddMemberInput(false);
213
		nav.displayLinkGroupInput(false);
214
		nav.displayLinkCircleInput(false);
215
		nav.displayJoinCircleButton(false);
216
		nav.displayInviteCircleButtons(false);
217
	},
218
219
	joinCircleAction: function () {
220
		nav.displayCircleButtons(false);
221
		nav.displayAddMemberInput(false);
222
		nav.displayLinkCircleInput(false);
223
		nav.displayLinkGroupInput(false);
224
		nav.displayJoinCircleButton(true);
225
	},
226
227
	displayCircleButtons: function (display) {
228
		if (display) {
229
			elements.buttonCircleActionReturn.hide(define.animationMenuSpeed);
230
			elements.buttonCircleActions.delay(define.animationMenuSpeed).show(
231
				define.animationMenuSpeed);
232
		} else {
233
			elements.buttonCircleActions.hide(define.animationMenuSpeed);
234
			elements.buttonCircleActionReturn.delay(define.animationMenuSpeed).show(
235
				define.animationMenuSpeed);
236
		}
237
	},
238
239
	displayAddMemberInput: function (display) {
240
		if (display) {
241
			elements.addMember.val('');
242
			elements.addMember.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
243
				function () {
244
					$(this).focus();
245
				});
246
		} else {
247
			elements.addMember.hide(define.animationMenuSpeed);
248
		}
249
	},
250
251
	displayLinkGroupInput: function (display) {
252
		if (display) {
253
			elements.linkGroup.val('');
254
			elements.linkGroup.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
255
				function () {
256
					$(this).focus();
257
				});
258
		} else {
259
			elements.linkGroup.hide(define.animationMenuSpeed);
260
		}
261
	},
262
263
	displayLinkCircleInput: function (display) {
264
		if (display) {
265
			elements.linkCircle.val('');
266
			elements.linkCircle.delay(define.animationMenuSpeed).show(define.animationMenuSpeed,
267
				function () {
268
					$(this).focus();
269
				});
270
		} else {
271
			elements.linkCircle.hide(define.animationMenuSpeed);
272
		}
273
	},
274
275
276
	displayInviteCircleButtons: function (display) {
277
		if (display) {
278
			elements.joinCircleAccept.show(define.animationMenuSpeed);
279
			elements.joinCircleReject.delay(define.animationMenuSpeed).show(
280
				define.animationMenuSpeed);
281
		} else {
282
			elements.joinCircleAccept.hide(define.animationMenuSpeed);
283
			elements.joinCircleReject.hide(define.animationMenuSpeed);
284
		}
285
	},
286
287
	displayJoinCircleButton: function (display) {
288
		if (display) {
289
			if (curr.circleStatus === 'Invited') {
290
				elements.joinCircle.hide(define.animationMenuSpeed);
291
				elements.leaveCircle.hide(define.animationMenuSpeed);
292
				nav.displayInviteCircleButtons(true);
293
294
			} else {
295
				nav.displayInviteCircleButtons(false);
296
297
				if (curr.circleLevel === 0 && curr.circleStatus !== 'Requesting') {
298
					elements.joinCircle.delay(define.animationMenuSpeed).show(
299
						define.animationMenuSpeed);
300
					elements.leaveCircle.hide(define.animationMenuSpeed);
301
					elements.joinCircleAccept.hide(define.animationMenuSpeed);
302
					elements.joinCircleReject.hide(define.animationMenuSpeed);
303
304
				}
305
				else {
306
					elements.leaveCircle.delay(define.animationMenuSpeed).show(
307
						define.animationMenuSpeed);
308
					elements.joinCircle.hide(define.animationMenuSpeed);
309
				}
310
			}
311
		} else {
312
			elements.joinCircle.hide(define.animationMenuSpeed);
313
			elements.leaveCircle.hide(define.animationMenuSpeed);
314
		}
315
	},
316
317
318
	/**
319
	 *
320
	 * @param display
321
	 */
322
	displayOptionsNewCircle: function (display) {
323
		if (display) {
324
			elements.newType.fadeIn(300);
325
			elements.newSubmit.fadeIn(500);
326
			elements.newTypeDefinition.fadeIn(700);
327
		}
328
		else {
329
			elements.newType.fadeOut(700);
330
			elements.newSubmit.fadeOut(500);
331
			elements.newTypeDefinition.fadeOut(300);
332
		}
333
	},
334
335
336
	displayMembers: function (members) {
337
		if (members === '') {
338
			members = curr.circleMembers;
339
		} else {
340
			curr.circleMembers = members;
341
		}
342
343
		elements.mainUIMembersTable.emptyTable();
344
		if (members === null) {
345
			elements.mainUIMembersTable.hide(200);
346
			return;
347
		}
348
349
		elements.mainUIMembersTable.show(200);
350
		for (var i = 0; i < members.length; i++) {
351
			var tmpl = elements.generateTmplMember(members[i]);
352
			elements.mainUIMembersTable.append(tmpl);
353
		}
354
355
		for (i = 0; i < 10; i++) {
356
			if (curr.circleLevel < 9 && curr.circleLevel <= i
357
				|| curr.circleDetails.type === define.typePersonal) {
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before ||.
Loading history...
358
				$('.level-select option[value="' + i + '"]').attr('disabled', 'disabled');
359
			}
360
		}
361
362
363
		elements.mainUIMembersTable.children('tr.entry').each(function () {
364
365
				var userId = $(this).attr('member-id');
366
				if (userId === curr.userId) {
367
					$(this).find('td.username').css('font-weight', 'bold').css('font-style', 'italic');
368
				}
369
370
				//
371
				// level
372
				if (curr.circleDetails.type === define.typePersonal) {
373
					var levelString = $(this).attr('member-levelString');
374
					$(this).find('.level').text(levelString);
375
				} else {
376
					var level = $(this).attr('member-level');
377
					var levelSelect = $(this).find('.level-select');
378
					if (level === '0') {
379
						levelSelect.hide();
380
					}
381
					else {
382
						levelSelect.show(200).val(level);
383
					}
384
					levelSelect.on('change', function () {
385
						actions.changeMemberLevel(userId, $(this).val());
386
					});
387
				}
388
389
				//
390
				// status
391
				var status = $(this).attr('member-status');
392
				var statusSelect = $(this).find('.status-select');
393
394
				statusSelect.on('change', function () {
395
					actions.changeMemberStatus(userId, $(this).val());
396
				});
397
				statusSelect.append($('<option>', {
398
					value: status,
399
					text: t('circles', status)
400
				})).val(status);
401
402
				if (curr.circleLevel <= $(this).attr('member-level')) {
403
					return;
404
				}
405
406
				if (status === 'Member' || status === 'Invited') {
407
					statusSelect.append($('<option>', {
408
						value: 'remove_member',
409
						text: t('circles', 'Kick this member')
410
					}));
411
				}
412
413
				if (status === 'Requesting') {
414
					statusSelect.append($('<option>', {
415
						value: 'accept_request',
416
						text: t('circles', 'Accept the request')
417
					}));
418
					statusSelect.append($('<option>', {
419
						value: 'dismiss_request',
420
						text: t('circles', 'Dismiss the request')
421
					}));
422
				}
423
424
			}
425
		);
426
	},
427
428
429
	displayGroups: function (groups) {
430
		if (groups === '') {
431
			groups = curr.circleGroups;
432
		} else {
433
			curr.circleGroups = groups;
434
		}
435
436
		elements.mainUIGroupsTable.emptyTable();
437
		if (groups === null || groups.length === 0) {
438
			elements.mainUIGroupsTable.hide(curr.animationSpeed);
439
			return;
440
		}
441
442
		elements.mainUIGroupsTable.show(200);
443
		for (var i = 0; i < groups.length; i++) {
444
			var tmpl = elements.generateTmplGroup(groups[i]);
445
			elements.mainUIGroupsTable.append(tmpl);
446
		}
447
448
		for (i = 0; i < 10; i++) {
449
			if (curr.circleLevel < define.levelAdmin && curr.circleLevel <= i) {
450
				$('.level-select option[value="' + i + '"]').attr('disabled', 'disabled');
451
			}
452
			if (i > define.levelMember && curr.circleDetails.type === define.typePersonal) {
453
				$('.level-select option[value="' + i + '"]').remove();
454
			}
455
		}
456
457
		elements.mainUIGroupsTable.children('tr.entry').each(function () {
458
459
				var groupId = $(this).attr('group-id');
460
461
				var level = $(this).attr('group-level');
462
				var levelSelect = $(this).find('.level-select');
463
				if (level === '0') {
464
					levelSelect.hide();
465
				}
466
				else {
467
					levelSelect.show(200).val(level);
468
				}
469
				levelSelect.append($('<option>', {
470
					value: 'remove_group',
471
					text: t('circles', 'Unlink this group')
472
				}));
473
474
				levelSelect.on('change', function () {
475
					actions.changeGroupLevel(groupId, $(this).val());
476
				});
477
			}
478
		);
479
	},
480
481
482
	displayLinks: function (links) {
483
484
		if (links === '') {
485
			links = curr.circleLinks;
486
		} else {
487
			curr.circleLinks = links;
488
		}
489
490
		elements.mainUILinksTable.hide(curr.animationSpeed);
491
		elements.mainUILinksTable.emptyTable();
492
		if (links === null || links.length === 0) {
493
			return;
494
		}
495
496
		elements.mainUILinksTable.show(curr.animationSpeed);
497
		for (var i = 0; i < links.length; i++) {
498
			var tmpl = elements.generateTmplLink(links[i]);
499
			elements.mainUILinksTable.append(tmpl);
500
		}
501
502
503
		elements.mainUILinksTable.children('tr.entry').each(function () {
504
505
			var linkId = $(this).attr('link-id');
506
			var status = parseInt($(this).attr('link-status'));
507
508
509
			var statusSelect = $(this).find('.link-status-select');
510
511
			statusSelect.on('change', function () {
512
				actions.changeLinkStatus(linkId, $(this).val());
513
			});
514
			statusSelect.append($('<option>', {
515
				value: status,
516
				text: define.linkStatus(status)
517
			})).val(status);
518
519
			if (curr.circleLevel < define.levelAdmin) {
520
				return;
521
			}
522
523
			if (status === define.linkSetup || status === define.linkRefused ||
524
				status === define.linkUp || status === define.linkDown) {
525
				statusSelect.append($('<option>', {
526
					value: define.linkRemove,
527
					text: t('circles', 'Remove this link')
528
				}));
529
			}
530
531
			if (status === define.linkRequestSent) {
532
				statusSelect.append($('<option>', {
533
					value: define.linkRemove,
534
					text: t('circles', 'Cancel the link request')
535
				}));
536
			}
537
538
			if (status === define.linkRequested) {
539
				statusSelect.append($('<option>', {
540
					value: define.linkUp,
541
					text: t('circles', 'Accept the link request')
542
				}));
543
				statusSelect.append($('<option>', {
544
					value: define.linkRemove,
545
					text: t('circles', 'Reject the link request')
546
				}));
547
			}
548
		});
549
	},
550
551
552
	displayCircleDetails: function (details) {
553
		elements.circleDetails.children('#name').text(details.name);
554
		elements.circleDesc.text(details.description);
555
556
		elements.circleDetails.children('#type').text(t('circles', details.typeLongString));
557
		if (details.description !== '') {
558
			elements.circleDesc.html(
559
				escapeHTML(details.description).replace(/\n/g, '&nbsp;<br />')).show(
560
				define.animationSpeed);
561
		}
562
		else {
563
			elements.circleDesc.text('').hide(define.animationSpeed);
564
		}
565
566
		elements.buttonCircleActions.show(300);
567
		elements.addMember.hide(300);
568
	},
569
570
571
	displayMembersInteraction: function (details) {
572
		if (details.viewer.level < define.levelModerator) {
573
			elements.buttonAddMember.hide();
574
		} else {
575
			elements.buttonAddMember.show();
576
		}
577
578
		nav.displayMemberInteractionCircleLinks(details);
579
		nav.displayMemberInteractionGroupLinks(details);
580
581
		elements.joinCircleInteraction.hide();
582
		elements.buttonJoinCircle.show();
583
584
		this.displayNonMemberInteraction(details);
585
586
		if (details.viewer.level === define.levelOwner) {
587
			elements.destroyCircle.show();
588
			elements.buttonCircleSettings.show();
589
			elements.buttonJoinCircle.hide();
590
		}
591
	},
592
593
594
	displayMemberInteractionGroupLinks: function (details) {
595
		if (curr.allowed_linked_groups === '0' ||
596
			details.viewer.level < define.levelAdmin
597
		) {
598
			elements.buttonLinkGroup.hide();
599
		}
600
		else {
601
			elements.buttonLinkGroup.show();
602
		}
603
	},
604
605
606
	displayMemberInteractionCircleLinks: function (details) {
607
		if (curr.allowed_federated_circles === '0' ||
608
			curr.circleSettings['allow_links'] !== 'true' ||
0 ignored issues
show
Coding Style introduced by
['allow_links'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
609
			details.type === define.typePersonal ||
610
			details.viewer.level < define.levelAdmin
611
		) {
612
			elements.buttonLinkCircle.hide();
613
		}
614
		else {
615
			elements.buttonLinkCircle.show();
616
		}
617
	},
618
619
620
	displayNonMemberInteraction: function (details) {
621
		elements.joinCircleAccept.hide();
622
		elements.joinCircleReject.hide();
623
		elements.joinCircleRequest.hide();
624
		elements.joinCircleInvite.hide();
625
		elements.buttonCircleSettings.hide();
626
		elements.destroyCircle.hide();
627
628
		if (details.user.status === 'Requesting') {
629
			elements.joinCircleRequest.show();
630
			return;
631
		}
632
633
		if (details.user.status === 'Invited') {
634
			elements.joinCircleInvite.show();
635
			return;
636
		}
637
638
		if (details.viewer.level > 0) {
639
			return;
640
		}
641
642
		setTimeout(function () {
643
			nav.joinCircleAction();
644
		}, 200);
645
	}
646
647
};
648
649