Completed
Pull Request — master (#72)
by Maxence
06:31
created

Circles.initialize   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
29
(function () {
30
31
32
	/**
33
	 * @constructs Circles
34
	 */
35
	var Circles = function () {
36
		this.initialize();
37
	};
38
39
	Circles.prototype = {
40
41
42
		initialize: function () {
43
44
			var self = this;
45
46
			this.createCircle = function (type, name, callback) {
47
48
				var result = {status: -1};
49
				$.ajax({
50
					method: 'PUT',
51
					url: OC.generateUrl('/apps/circles/circles'),
52
					data: {
53
						type: type,
54
						name: name
55
					}
56
				}).done(function (res) {
57
					self.onCallback(callback, res);
58
				}).fail(function () {
59
					self.onCallback(callback, result);
60
				});
61
			};
62
63
64
			// this.listCircles = function (type, callback) {
65
			// 	var result = {status: -1};
66
			// 	$.ajax({
67
			// 		method: 'GET',
68
			// 		url: OC.generateUrl(OC.linkTo('circles', 'circles')),
69
			// 		data: {
70
			// 			type: type
71
			// 		}
72
			// 	}).done(function (res) {
73
			// 		self.onCallback(callback, res);
74
			// 	}).fail(function () {
75
			// 		self.onCallback(callback, result);
76
			// 	});
77
			// };
78
79
80
			this.searchCircles = function (type, name, level, callback) {
81
				var result = {status: -1};
82
				$.ajax({
83
					method: 'GET',
84
					url: OC.generateUrl('/apps/circles/circles'),
85
					data: {
86
						type: type,
87
						name: name,
88
						level: level
89
					}
90
				}).done(function (res) {
91
					self.onCallback(callback, res);
92
				}).fail(function () {
93
					self.onCallback(callback, result);
94
				});
95
			};
96
97
98
			this.detailsCircle = function (circleId, callback) {
99
				var result = {status: -1};
100
				$.ajax({
101
					method: 'GET',
102
					url: OC.generateUrl('/apps/circles/circles/' + circleId)
103
				}).done(function (res) {
104
					self.onCallback(callback, res);
105
				}).fail(function () {
106
					self.onCallback(callback, result);
107
				});
108
			};
109
110
111
			this.addMember = function (circleId, member, callback) {
112
				var result = {status: -1};
113
				$.ajax({
114
					method: 'PUT',
115
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/members'),
116
					data: {
117
						name: member
118
					}
119
				}).done(function (res) {
120
					self.onCallback(callback, res);
121
				}).fail(function () {
122
					self.onCallback(callback, result);
123
				});
124
			};
125
126
127
			this.removeMember = function (circleId, member, callback) {
128
				var result = {status: -1};
129
				$.ajax({
130
					method: 'DELETE',
131
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/members'),
132
					data: {
133
						member: member
134
					}
135
				}).done(function (res) {
136
					self.onCallback(callback, res);
137
				}).fail(function () {
138
					self.onCallback(callback, result);
139
				});
140
			};
141
142
143
			this.joinCircle = function (circleId, callback) {
144
				var result = {status: -1};
145
				$.ajax({
146
					method: 'GET',
147
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/join'),
148
					data: {}
149
				}).done(function (res) {
150
					self.onCallback(callback, res);
151
				}).fail(function () {
152
					self.onCallback(callback, result);
153
				});
154
			};
155
156
157
			this.leaveCircle = function (circleId, callback) {
158
				var result = {status: -1};
159
				$.ajax({
160
					method: 'GET',
161
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/leave'),
162
					data: {}
163
				}).done(function (res) {
164
					self.onCallback(callback, res);
165
				}).fail(function () {
166
					self.onCallback(callback, result);
167
				});
168
			};
169
170
171
			this.destroyCircle = function (circleId, callback) {
172
				var result = {status: -1};
173
				$.ajax({
174
					method: 'DELETE',
175
					url: OC.generateUrl('/apps/circles/circles/' + circleId),
176
					data: {}
177
				}).done(function (res) {
178
					self.onCallback(callback, res);
179
				}).fail(function () {
180
					self.onCallback(callback, result);
181
				});
182
			};
183
184
185
			this.shareToCircle = function (circleId, source, type, item, callback) {
186
				var result = {status: -1};
187
				$.ajax({
188
					method: 'PUT',
189
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/share'),
190
					data: {
191
						source: source,
192
						type: type,
193
						item: item
194
					}
195
				}).done(function (res) {
196
					self.onCallback(callback, res);
197
				}).fail(function () {
198
					self.onCallback(callback, result);
199
				});
200
			};
201
202
203
			this.linkCircle = function (circleId, remote, callback) {
204
				var result = {status: -1};
205
				$.ajax({
206
					method: 'PUT',
207
					url: OC.generateUrl('/apps/circles/circles/' + circleId + '/link'),
208
					data: {
209
						remote: remote
210
					}
211
				}).done(function (res) {
212
					self.onCallback(callback, res);
213
				}).fail(function () {
214
					self.onCallback(callback, result);
215
				});
216
			};
217
218
219
			this.onCallback = function (callback, result) {
220
				if (callback && (typeof callback === "function")) {
221
					callback(result);
222
				}
223
			};
224
225
		}
226
227
	};
228
229
	OCA.Circles = Circles;
230
	OCA.Circles.api = new Circles();
231
232
})();
233
234
235