Completed
Push — some-scrutinizing ( 7755b5...18fbfb )
by Maxence
03:27
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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