Completed
Push — js-scrutinizing ( 571e11...5b02b9 )
by Maxence
02:30
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
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
$(document).ready(function () {
31
32
	/**
33
	 * @constructs Navigation
34
	 */
35
	var Navigation = function () {
36
		this.initialize();
37
	};
38
39
	Navigation.prototype = {
40
41
		initialize: function () {
42
			var self = this;
43
			var api = OCA.Circles.api;
44
45
			var currCirclesType = '';
46
			var currentCircle = 0;
47
			var currentCircleLevel = 0;
48
			var lastSearchCircle = '';
49
			var lastSearchUser = '';
50
51
			var divNewTypeDefinition = $('#circles_new_type_definition');
52
			var divNewType = $('#circles_new_type');
53
			var divNewSubmit = $('#circles_new_submit');
54
			var divNewName = $('#circles_new_name');
55
			var divNavigation = $('#app-navigation.circles');
56
			var divCirclesList = $('#circles_list');
57
			var divEmptyContent = $('#emptycontent');
58
			var divMainUI = $('#mainui');
59
			var divMainUIMembers = $('#memberslist_table');
60
			var divMembersSearchResult = $('#members_search_result');
61
62
			var divJoinCircleAccept = $('#joincircle_acceptinvit');
63
			var divJoinCircleReject = $('#joincircle_rejectinvit');
64
			var divJoinCircleRequest = $('#joincircle_request');
65
			var divJoinCircleInvite = $('#joincircle_invit');
66
			var divJoinCircle = $('#joincircle');
67
			var divLeaveCircle = $('#leavecircle');
68
69
			this.UIReset = function () {
70
				divNewTypeDefinition.children('div').fadeOut(0);
71
				$('#circles_new_type_' + divNewType.children('option:selected').val()).fadeIn(0);
72
73
				divNewType.hide();
74
				divNewSubmit.hide();
75
				divNewTypeDefinition.hide();
76
77
				$('.icon-circles').css('background-image',
78
					'url(' + OC.imagePath('circles', 'colored') + ')');
79
80
				divMembersSearchResult.hide();
81
			};
82
83
84
			this.initTweaks = function () {
85
				$.fn.emptyTable = function () {
86
					this.children('tr').each(function () {
87
						if ($(this).attr('class') != 'header') {
88
							$(this).remove();
89
						}
90
					});
91
				}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
92
93
				// $.fn.hideEntry = function () {
94
				// 	this.children('tr').each(function () {
95
				// 		if ($(this).attr('class') != 'header') {
96
				// 			$(this).remove();
97
				// 		}
98
				// 	});
99
				// }
100
			};
101
102
			this.initAnimationNewCircle = function () {
103
104
				divNewName.on('keyup', function () {
105
					currentCircle = 0;
106
					currentCircleLevel = 0;
107
108
					divNavigation.hide('slide', 800);
109
					divCirclesList.children('div').removeClass('selected');
110
					divEmptyContent.show(800);
111
					divMainUI.fadeOut(800);
112
113
					if (divNewName.val() !== '') {
114
						divNewType.fadeIn(300);
115
						divNewSubmit.fadeIn(500);
116
						divNewTypeDefinition.fadeIn(700);
117
					}
118
					else {
119
						divNewType.fadeOut(700);
120
						divNewSubmit.fadeOut(500);
121
						divNewTypeDefinition.fadeOut(300);
122
					}
123
				});
124
125
				divNewType.on('change', function () {
126
127
					currentCircle = 0;
128
					currentCircleLevel = 0;
129
130
					divNavigation.hide('slide', 800);
131
					divCirclesList.children('div').removeClass('selected');
132
					divEmptyContent.show(800);
133
					divMainUI.fadeOut(800);
134
135
					divNewTypeDefinition.children('div').fadeOut(300);
136
					$('#circles_new_type_' + divNewType.children('option:selected').val()).fadeIn(
137
						300);
138
				});
139
140
				divNewSubmit.on('click', function () {
141
					api.createCircle(divNewType.val(), divNewName.val(),
142
						self.createCircleResult);
143
				});
144
145
			};
146
147
148
			/**
149
			 *
150
			 */
151
			this.initExperienceCirclesList = function () {
152
153
				divCirclesList.children('div').on('click', function () {
154
					self.displayCirclesList($(this).attr('circle-type'));
155
				});
156
157
				$('#circles_search').on('input propertychange paste focus', function () {
158
					if (lastSearchCircle == $(this).val().trim()) {
159
						return;
160
					}
161
162
					lastSearchCircle = $(this).val().trim();
163
					api.searchCircles(currCirclesType, $(this).val().trim(),
164
						self.listCirclesResult);
165
				});
166
167
			};
168
169
170
			this.initExperienceManagerMembers = function () {
171
				$('#joincircle').on('click', function () {
172
					api.joinCircle(currentCircle, self.joinCircleResult);
173
				});
174
175
				$('#leavecircle').on('click', function () {
176
					api.leaveCircle(currentCircle, self.leaveCircleResult);
177
				});
178
179
				$('#joincircle_acceptinvit').on('click', function () {
180
					api.joinCircle(currentCircle, self.joinCircleResult);
181
				});
182
183
				$('#joincircle_rejectinvit').on('click', function () {
184
					api.leaveCircle(currentCircle, self.leaveCircleResult);
185
				});
186
187
				$('#addmember').on('input propertychange paste focus', function () {
188
					self.searchMembersRequest($(this).val().trim());
189
				}).blur(function () {
190
					divMembersSearchResult.fadeOut(400);
191
				});
192
			};
193
194
195
			//
196
			//
197
			//
198
			this.createCircleResult = function (result) {
199
				var str = this.getStringTypeFromType(result.type);
200
201
				if (result.status == 1) {
202
					OCA.notification.onSuccess(str + " '" + result.name + "' created");
203
					self.displayCirclesList(result.circle.type);
204
					self.selectCircle(result.circle.id);
205
					return;
206
				}
207
208
				OCA.notification.onFail(
209
					str + " '" + result.name + "' NOT created: " +
210
					((result.error) ? result.error : 'no error message'));
211
			};
212
213
214
			//
215
			//
216
			//
217
			this.getStringTypeFromType = function (type) {
218
				switch (type) {
219
					case '1':
220
						return 'Personal circle';
221
					case '2':
222
						return 'Hidden circle';
223
					case '4':
224
						return 'Private circle';
225
					case '8':
226
						return 'Public circle';
227
					default:
228
						return 'Circle';
229
				}
230
			};
231
232
233
			//
234
			//
235
			// Circles List
236
			this.displayCirclesList = function (type) {
237
238
				currCirclesType = type;
239
				lastSearchCircle = '';
240
				lastSearchUser = '';
241
242
				currentCircle = 0;
243
				currentCircleLevel = 0;
244
245
				divNavigation.show('slide', 800);
246
				divEmptyContent.show(800);
247
				divMainUI.fadeOut(800);
248
249
				$('#circles_search').val('');
250
				$('#addmember').val('');
251
252
				divNavigation.addClass('selected');
253
				divCirclesList.children('div').removeClass('selected');
254
255
				divCirclesList.children().each(function () {
256
					if ($(this).attr('circle-type') == type.toLowerCase()) {
257
						$(this).addClass('selected');
258
					}
259
				});
260
261
				divNavigation.children().each(function () {
262
					if ($(this).attr('id') != 'circles_search') {
263
						$(this).remove();
264
					}
265
				});
266
				api.listCircles(type, self.listCirclesResult);
267
			};
268
269
270
			this.listCirclesResult = function (result) {
271
272
				if (result.status < 1) {
273
					OCA.notification.onFail(
274
						'Issue while retreiving the list of the Circles: ' +
275
						((result.error) ? result.error : 'no error message'));
276
					return;
277
				}
278
279
				divNavigation.children().each(function () {
280
					if ($(this).attr('id') != 'circles_search') {
281
						$(this).remove();
282
					}
283
				});
284
285
				var data = result.data;
286
				for (var i = 0; i < data.length; i++) {
287
288
					//	var curr = self.getCurrentCircleTemplate(data[i].id);
289
290
					var tmpl = $('#tmpl_circle').html();
291
292
					tmpl = tmpl.replace(/%title%/, data[i].name);
293
					tmpl = tmpl.replace(/%type%/, data[i].type);
294
					tmpl = tmpl.replace(/%owner%/, data[i].owner.user_id);
295
					tmpl = tmpl.replace(/%status%/, data[i].user.status);
296
					tmpl = tmpl.replace(/%level_string%/, data[i].user.level_string);
297
					tmpl = tmpl.replace(/%count%/, data[i].count);
298
					tmpl = tmpl.replace(/%creation%/, data[i].creation);
299
300
					//	if (curr == null) {
301
					divNavigation.append(
302
						'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
303
					//	} else {
304
					//		$(curr).html(tmpl);
305
					//	}
306
				}
307
308
				divNavigation.children('.circle').on('click', function () {
309
					self.selectCircle($(this).attr('circle-id'));
310
				});
311
			};
312
313
314
			this.selectCircle = function (circle_id) {
315
				lastSearchUser = '';
316
				$('#addmember').val('');
317
318
				api.detailsCircle(circle_id, this.selectCircleResult);
319
			};
320
321
322
			this.selectCircleResult = function (result) {
323
324
				divMainUIMembers.emptyTable();
325
326
				if (result.status < 1) {
327
					OCA.notification.onFail(
328
						'Issue while retreiving the details of a circle: ' +
329
						((result.error) ? result.error : 'no error message'));
330
					return;
331
				}
332
333
				divNavigation.children('.circle').each(function () {
334
					if ($(this).attr('circle-id') == result.circle_id) {
335
						$(this).addClass('selected');
336
					} else {
337
						$(this).removeClass('selected');
338
					}
339
				});
340
				divEmptyContent.hide(800);
341
				divMainUI.fadeIn(800);
342
				currentCircle = result.circle_id;
343
				currentCircleLevel = result.details.user.level;
344
345
				if (result.details.user.level < 6) {
346
					$('#addmember').hide();
347
				} else {
348
					$('#addmember').show();
349
				}
350
351
352
				divJoinCircleAccept.hide();
353
				divJoinCircleReject.hide();
354
				divJoinCircleRequest.hide();
355
				divJoinCircleInvite.hide();
356
357
				if (result.details.user.level == 9) {
358
					divJoinCircle.hide();
359
					divLeaveCircle.hide();
360
				}
361
				else if (result.details.user.level >= 1) {
362
					divJoinCircle.hide();
363
					divLeaveCircle.show();
364
				} else {
365
					if (result.details.user.status == 'Invited') {
366
						divJoinCircleInvite.show();
367
						divJoinCircleAccept.show();
368
						divJoinCircleReject.show();
369
						divJoinCircle.hide();
370
						divLeaveCircle.hide();
371
					}
372
					else if (result.details.user.status == 'Requesting') {
373
						divJoinCircleRequest.show();
374
						divJoinCircle.hide();
375
						divLeaveCircle.show();
376
					}
377
					else {
378
						divJoinCircle.show();
379
						divLeaveCircle.hide();
380
					}
381
				}
382
383
				self.displayMembers(result.details.members);
384
			};
385
386
387
			this.searchMembersRequest = function (search) {
388
389
				if (lastSearchUser == search) {
390
					return;
391
				}
392
393
				lastSearchUser = search;
394
395
				$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
396
					{
397
						format: 'json',
398
						search: search,
399
						perPage: 200,
400
						itemType: 'principals'
401
					}, self.searchMembersResult);
402
			};
403
404
			this.searchMembersResult = function (response) {
405
406
				if (response === null ||
407
					(response.ocs.data.users === 0 && response.ocs.data.exact.users === 0)) {
408
					divMembersSearchResult.fadeOut(300);
409
				}
410
				else {
411
					var currSearch = $('#addmember').val().trim();
412
					divMembersSearchResult.children().remove();
413
414
					$.each(response.ocs.data.exact.users, function (index, value) {
415
						divMembersSearchResult.append(
416
							'<div class="members_search exact" searchresult="' +
417
							value.value.shareWith + '">' + value.label + '   (' +
418
							value.value.shareWith + ')</div>');
419
					});
420
421
					$.each(response.ocs.data.users, function (index, value) {
422
						var line = value.label + '   (' + value.value.shareWith + ')';
423
						if (currSearch.length > 0) {
424
							line =
425
								line.replace(new RegExp('(' + currSearch + ')', 'gi'), '<b>$1</b>');
426
						}
427
428
						divMembersSearchResult.append(
429
							'<div class="members_search" searchresult="' + value.value.shareWith +
430
							'">' + line + '</div>');
431
					});
432
433
					divMembersSearchResult.children().first().css('border-top-width', '0px');
434
435
					$('.members_search').on('click', function () {
436
						api.addMember(currentCircle, $(this).attr('searchresult'),
437
							self.addMemberResult);
438
					});
439
					divMembersSearchResult.fadeIn(300);
440
				}
441
442
			};
443
444
445
			this.addMemberResult = function (result) {
446
447
				if (result.status == 1) {
448
					OCA.notification.onSuccess(
449
						"Member '" + result.name + "' successfully added to the circle");
450
451
					self.displayMembers(result.members);
452
				}
453
				else {
454
					OCA.notification.onFail(
455
						"Member '" + result.name + "' NOT added to the circle: " +
456
						((result.error) ? result.error : 'no error message'));
457
				}
458
			};
459
460
461
			this.displayMembers = function (members) {
462
463
				divMainUIMembers.emptyTable();
464
465
				if (members === null) {
466
					divMainUIMembers.hide(200);
467
					return;
468
				}
469
470
				divMainUIMembers.show(200);
471
				for (var i = 0; i < members.length; i++) {
472
473
					var tmpl = $('#tmpl_member').html();
474
475
					tmpl = tmpl.replace(/%username%/g, members[i].user_id);
476
					tmpl = tmpl.replace(/%level%/g, members[i].level);
477
					tmpl = tmpl.replace(/%levelstring%/g, members[i].level_string);
478
					tmpl = tmpl.replace(/%status%/, members[i].status);
479
					tmpl = tmpl.replace(/%joined%/, members[i].joined);
480
					tmpl = tmpl.replace(/%note%/,
481
						((members[i].note) ? members[i].note : ''));
482
483
					divMainUIMembers.append(tmpl);
484
				}
485
486
				divMainUIMembers.children().each(function () {
487
					if ($(this).attr('member-level') == '9' || currentCircleLevel < 6) {
488
						$(this).children('.delete').hide(0);
489
					}
490
				});
491
492
				divMainUIMembers.children('.delete').on('click', function () {
493
					var member = $(this).parent().attr('member-id');
494
					api.removeMember(currentCircle, member, self.removeMemberResult);
495
				});
496
			};
497
498
499
			this.removeMemberResult = function (result) {
500
				if (result.status == 1) {
501
502
					divMainUIMembers.children().each(function () {
503
						if ($(this).attr('member-id') == result.name) {
504
							$(this).hide(300);
505
						}
506
					});
507
508
					OCA.notification.onSuccess(
509
						"Member '" + result.name + "' successfully removed from the circle");
510
				}
511
				else {
512
					OCA.notification.onFail(
513
						"Member '" + result.name + "' NOT removed from the circle: " +
514
						((result.error) ? result.error : 'no error message'));
515
				}
516
517
			};
518
519
520
			this.joinCircleResult = function (result) {
521
				if (result.status == 1) {
522
523
					divMainUIMembers.children().each(function () {
524
						if ($(this).attr('member-id') == result.name) {
525
							$(this).hide(300);
526
						}
527
					});
528
529
					if (result.member.level == 1) {
530
						OCA.notification.onSuccess(
531
							"You have successfully joined this circle");
532
					} else {
533
						OCA.notification.onSuccess(
534
							"You have requested an invitation to join this circle");
535
					}
536
					self.selectCircle(result.circle_id);
537
538
				}
539
				else {
540
					OCA.notification.onFail(
541
						"Cannot join this circle: " +
542
						((result.error) ? result.error : 'no error message'));
543
				}
544
			};
545
546
			this.leaveCircleResult = function (result) {
547
				if (result.status == 1) {
548
549
					divMainUIMembers.children().each(function () {
550
						if ($(this).attr('member-id') == result.name) {
551
							$(this).hide(300);
552
						}
553
					});
554
555
					OCA.notification.onSuccess(
556
						"You have successfully left this circle");
557
558
					self.selectCircle(result.circle_id);
559
				}
560
				else {
561
					OCA.notification.onFail(
562
						"Cannot leave this circle: " +
563
						((result.error) ? result.error : 'no error message'));
564
				}
565
			};
566
567
568
			/**
569
			 * Inits
570
			 */
571
			this.initTweaks();
572
			this.UIReset();
573
			this.initAnimationNewCircle();
574
			this.initExperienceCirclesList();
575
			this.initExperienceManagerMembers();
576
577
		}
578
	};
579
580
581
	/**
582
	 * @constructs Notification
583
	 */
584
	var Notification = function () {
585
		this.initialize();
586
	};
587
588
	Notification.prototype = {
589
590
		initialize: function () {
591
592
			//noinspection SpellCheckingInspection
593
			var notyf = new Notyf({
594
				delay: 5000
595
			});
596
597
			this.onSuccess = function (text) {
598
				notyf.confirm(text);
599
			};
600
601
			this.onFail = function (text) {
602
				notyf.alert(text);
603
			};
604
605
		}
606
607
	};
608
609
	OCA.Circles.Navigation = Navigation;
610
	OCA.Circles.navigation = new Navigation();
611
612
	OCA.Notification = Notification;
613
	OCA.notification = new Notification();
614
615
});
616
617