Completed
Push — federated-circles ( 80cab6...74a72b )
by Maxence
02:32
created

actions.linkCircleResult   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.439
c 1
b 0
f 0
cc 5
nc 6
nop 1
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: curr */
34
/** global: api */
35
36
37
var actions = {
38
39
40
	joinCircleResult: function (result) {
41
		if (result.status === 0) {
42
			OCA.notification.onFail(
43
				t('circles', "Cannot join this circle") + ': ' +
44
				((result.error) ? result.error : t('circles', 'no error message')));
45
			return;
46
		}
47
48
		elements.removeMemberslistEntry(result.member.user_id);
49
		if (result.member.level === 1) {
50
			OCA.notification.onSuccess(
51
				t('circles', "You have successfully joined this circle"));
52
		} else {
53
			OCA.notification.onSuccess(
54
				t('circles', "You have requested to join this circle"));
55
		}
56
		actions.selectCircle(result.circle_id);
57
	},
58
59
60
	leaveCircleResult: function (result) {
61
		if (result.status === 1) {
62
63
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
64
				function () {
65
					$(this).hide(300);
66
				});
67
68
			actions.selectCircle(result.circle_id);
69
			OCA.notification.onSuccess(
70
				t('circles', "You have successfully left this circle"));
71
			return;
72
		}
73
74
		OCA.notification.onFail(
75
			t('circles', "Cannot leave this circle") + ': ' +
76
			((result.error) ? result.error : t('circles', 'no error message')));
77
	},
78
79
80
	destroyCircleResult: function (result) {
81
		if (result.status === 1) {
82
83
			actions.unselectCircle(result.circle_id);
84
			OCA.notification.onSuccess(
85
				t('circles', "You have successfully deleted this circle"));
86
			return;
87
		}
88
89
		OCA.notification.onFail(
90
			t('circles', "Cannot delete this circle") + ': ' +
91
			((result.error) ? result.error : t('circles', 'no error message')));
92
	},
93
94
95
	createCircleResult: function (result) {
96
		var type = actions.getStringTypeFromType(result.type);
97
98
		if (result.status === 1) {
99
			OCA.notification.onSuccess(t('circles', " {type} '{name}' created", {
100
				type: type,
101
				name: result.name
102
			}));
103
			nav.displayCirclesList(result.circle.type);
104
			actions.selectCircle(result.circle.id);
105
			return;
106
		}
107
108
		OCA.notification.onFail(
109
			t('circles', " {type} '{name}' could not be created", {
110
				type: type,
111
				name: result.name
112
			}) + ': ' +
113
			((result.error) ? result.error : t('circles', 'no error message')));
114
	},
115
116
117
	selectCircleResult: function (result) {
118
119
		elements.mainUIMembers.emptyTable();
120
		if (result.status < 1) {
121
			OCA.notification.onFail(
122
				t('circles', 'Issue while retrieving the details of this circle') + '" ' +
123
				((result.error) ? result.error : t('circles', 'no error message')));
124
			return;
125
		}
126
127
		elements.navigation.children('.circle').removeClass('selected');
128
		elements.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
129
			function () {
130
				$(this).addClass('selected');
131
			});
132
133
		elements.emptyContent.hide(800);
134
		elements.mainUI.fadeIn(800);
135
		curr.circle = result.circle_id;
136
		curr.circleLevel = result.details.user.level;
137
138
		nav.displayCircleDetails(result.details);
139
		nav.displayMembersInteraction(result.details);
140
		nav.displayMembers(result.details.members);
141
	},
142
143
144
	listCirclesResult: function (result) {
145
146
		if (result.status < 1) {
147
			OCA.notification.onFail(
148
				t('circles', 'Issue while retrieving the list of circles') + '; ' +
149
				((result.error) ? result.error : t('circles', 'no error message')));
150
			return;
151
		}
152
153
		elements.resetCirclesList();
154
155
		var data = result.data;
156
		for (var i = 0; i < data.length; i++) {
157
			var tmpl = elements.generateTmplCircle(data[i]);
158
			elements.navigation.append(
159
				'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
160
		}
161
162
		elements.navigation.children('.circle').on('click', function () {
163
			actions.selectCircle($(this).attr('circle-id'));
164
		});
165
	},
166
167
	linkCircleResult: function (result) {
168
169
		//elements.linkCircle.val('');
170
		if (result.status !== 1) {
171
			OCA.notification.onFail(
172
				t('circles', "A link to <b>{link}</b> could not be initiated", {
173
					link: result.link
174
				}) + ': ' +
175
				((result.error) ? result.error : t('circles', 'no error message')));
176
			return;
177
		}
178
179
		if (result.remote === 6) {
180
			OCA.notification.onSuccess(
181
				t('circles', "A link to <b>{link}</b> has been requested.", {
182
					link: result.link
183
				}));
184
		}
185
186
		if (result.remote === 9) {
187
			OCA.notification.onSuccess(
188
				t('circles', "the link to <b>{link}</b> is now up and running.", {
189
					link: result.link
190
				}));
191
		}
192
193
	},
194
195
196
	selectCircle: function (circle_id) {
197
		curr.searchUser = '';
198
		elements.addMember.val('');
199
		elements.linkCircle.val('');
200
201
		api.detailsCircle(circle_id, actions.selectCircleResult);
202
	},
203
204
205
	unselectCircle: function (circle_id) {
206
		elements.mainUIMembers.emptyTable();
207
		elements.navigation.children(".circle[circle-id='" + circle_id + "']").remove();
208
		elements.emptyContent.show(800);
209
		elements.mainUI.fadeOut(800);
210
211
		curr.circle = 0;
212
		curr.circleLevel = 0;
213
	},
214
215
216
	/**
217
	 *
218
	 * @param search
219
	 */
220
	searchMembersRequest: function (search) {
221
222
		if (curr.searchUser === search) {
223
			return;
224
		}
225
226
		curr.searchUser = search;
227
228
		$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
229
			{
230
				format: 'json',
231
				search: search,
232
				perPage: 200,
233
				itemType: 'principals'
234
			}, actions.searchMembersResult);
235
	},
236
237
238
	searchMembersResult: function (response) {
239
240
		elements.membersSearchResult.children().remove();
241
242
		if (response === null ||
243
			(response.ocs.data.users.length === 0 && response.ocs.data.exact.users.length === 0)) {
244
			elements.membersSearchResult.fadeOut(0);
245
			return;
246
		}
247
248
		elements.fillMembersSearch(response.ocs.data.exact.users, response.ocs.data.users);
249
250
		$('.members_search').on('click', function () {
251
			api.addMember(curr.circle, $(this).attr('searchresult'),
252
				actions.addMemberResult);
253
		});
254
		elements.membersSearchResult.fadeIn(300);
255
	},
256
257
258
	addMemberResult: function (result) {
259
260
		if (result.status === 1) {
261
			OCA.notification.onSuccess(
262
				t('circles', "Member '{name}' successfully added to the circle",
263
					{name: result.name}));
264
265
			nav.displayMembers(result.members);
266
			return;
267
		}
268
		OCA.notification.onFail(
269
			t('circles', "Member '{name}' could not be added to the circle", {name: result.name}) +
270
			': ' +
271
			((result.error) ? result.error : t('circles', 'no error message')));
272
	},
273
274
275
	getStringTypeFromType: function (type) {
276
		switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
277
			case '1':
278
				return t('circles', 'Personal circle');
279
			case '2':
280
				return t('circles', 'Hidden circle');
281
			case '4':
282
				return t('circles', 'Private circle');
283
			case '8':
284
				return t('circles', 'Public circle');
285
		}
286
287
		return t('circles', 'Circle');
288
	},
289
290
291
	removeMemberResult: function (result) {
292
		if (result.status === 1) {
293
294
			elements.rightPanel.fadeOut(300);
295
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
296
				function () {
297
					$(this).hide(300);
298
				});
299
			OCA.notification.onSuccess(
300
				t('circles', "Member '{name}' successfully removed from the circle",
301
					{name: result.name}));
302
			return;
303
		}
304
305
		OCA.notification.onFail(
306
			t('circles', "Member '{name}' could not be removed from the circle",
307
				{name: result.name}) +
308
			': ' +
309
			((result.error) ? result.error : t('circles', 'no error message')));
310
	},
311
312
313
	/**
314
	 *
315
	 */
316
	onEventNewCircle: function () {
317
		curr.circle = 0;
318
		curr.circleLevel = 0;
319
320
		elements.circlesList.children('div').removeClass('selected');
321
		elements.emptyContent.show(800);
322
		elements.mainUI.fadeOut(800);
323
	},
324
325
326
	/**
327
	 *
328
	 */
329
	onEventNewCircleName: function () {
330
		this.onEventNewCircle();
331
		nav.displayOptionsNewCircle((elements.newName.val() !== ''));
332
	},
333
334
335
	/**
336
	 *
337
	 */
338
	onEventNewCircleType: function () {
339
		this.onEventNewCircle();
340
		elements.newTypeDefinition.children('div').fadeOut(300);
341
		var selectedType = elements.newType.children('option:selected').val();
342
		if (selectedType === '') {
343
			elements.newType.addClass('select_none');
344
		}
345
		else {
346
			elements.newType.removeClass('select_none');
347
			$('#circles_new_type_' + selectedType).fadeIn(
348
				300);
349
		}
350
	}
351
352
353
};
354