-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.js
476 lines (391 loc) · 13.6 KB
/
facebook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* Event Invite Buttons
*/
$(document).delegate('.fb-invite', 'click', function (e) {
e.preventDefault();
var eventid = $(this).data('eventid'),
eventTitle = $(this).data('title');
nlm_fb_event.invite(eventid, eventTitle, function (aFriends) {
payInvitations(aFriends);
});
return false;
});
/**
* Event Share Buttons
*/
$(document).delegate('.fb-share', 'click', function (e) {
e.preventDefault();
var eventid = $(this).data('eventid'),
eventTitle = $(this).data('title'),
eventText = $(this).data('text'),
eventpic = $(this).data('pic');
nlm_fb_event.share(eventid, eventTitle, eventText, eventpic, function (data) {
});
return false;
});
/**
* Event Attend Buttons
*/
$(document).delegate('.fb-attend', 'click', function (e) {
e.preventDefault();
var eventid = $(this).data('eventid'),
toDelete = this;
nlm_fb_event.attend(eventid, function (data) {
$(toDelete).remove();
});
return false;
});
var fbUploading = false;
$(document).delegate('.pp_share, .fb-pic-upload', 'click', function (e) {
e.preventDefault();
if (!fbUploading) {
fbUploading = true;
$(this).addClass('disabled');
$(this).attr('disabled', true);
var uri = $('#fullResImage').attr('src'),
title = $('.ppt').eq(0).text();
nlm_fb_gallery.upload(uri, title, club_name, function (answer) {
fbUploading = false;
$(this).removeClass('disabled');
$(this).attr('disabled', false);
alert('Das Foto wurde in dein Facebook Fotoalbum hoch geladen');
});
}
});
/**
* load Facebook and init
*/
$(document).ready(function () {
// async facebook file load
var js, id = 'facebook-jssdk', ref = document.getElementsByTagName('script')[0];
if (document.getElementById(id)) {
return;
}
js = document.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/de_DE/all.js";
ref.parentNode.insertBefore(js, ref);
// hide non fb events
$('[data-eventid=""]').remove();
});
/**
* async facebook initiation
*/
window.fbAsyncInit = function () {
FB.init({
appId: appId, // App ID
channelUrl: '//dejavue-versmold.de/channel.php',
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth: true
});
$('.fb-attend').each(function () {
var $btn = $(this),
event_id = $btn.data('eventid');
nlm_fb_event.me_attending(event_id, function (data) {
if (typeof data[0] != 'undefined') {
if (data[0].rsvp_status == 'attending') {
$btn.remove();
}
}
});
});
};
/**
* friends invitation calculation
* @param aFriends
*/
function payInvitations(aFriends) {
if (aFriends) {
var add = 0,
alreadyThere = 0,
count = 0;
function countInvitePoints(resp) {
if (resp.result.add) {
add += resp.result.add;
}
else {
alreadyThere++;
}
count++;
if (count == aFriends.length) {
text = 'Für ' + count + ' Freunde wurden dir ' + add + ' Punkte gut geschrieben';
if (alreadyThere > 0) {
text = text + '. Für ' + alreadyThere + ' Freunde gab es keine Punkte weil du sie schon eingeladen hast';
}
$(pageDataData.pointsSelector).html(resp.result.total);
printAlert(text);
}
}
for (var i in aFriends) {
$.ajax({
url: '/api/json/?request=user&action=points&type=invite&singleUseParam=' + eventid + '_' + aFriends[i],
dataType: 'json',
success: function (data) {
countInvitePoints(data);
}
});
}
}
}
/**
* Alert text to user
* @param text
*/
function printAlert(text) {
alert(text);
}
/**
* facebook actions for club events (share, attend, read attendees
* @type {{attending: Function, append_attendees: Function, attend: Function, action_invite: Function, invite: Function, share: Function}}
*/
var nlm_fb_event = {
/**
* facebook action for reading event attendees
* @param eventid the facebook event id
* @param callback the callback function (for data processing)
*/
attending: function (eventid, callback, limit) {
if (!limit) {
limit = '';
}
var fql = 'SELECT uid, name FROM user WHERE uid in (SELECT uid FROM event_member WHERE eid=' + eventid + ') ' + limit;
FB.api(
{
method: 'fql.query',
query: fql,
access_token: app_access_token
},
function (data) {
callback(data);
}
);
},
/**
* read event attendees and append them to a template
* @param eventid
* @param template
*/
append_attendees: function (eventid, template, limit) {
var parent = template.parent(), t = template.parent().find('.template').clone(true, true);
parent.empty();
parent.children(':not(.template)').remove();
nlm_fb_event.attending(eventid, function (response) {
$('[data-fb-event="' + eventid + '"] .event-facebook-attendeeCount').text(response.length);
for (var i in response) {
if (typeof response[i].uid != "undefined") {
var cln = t.clone(true, true);
cln.find('[href]').each(function () {
$(this).attr('href', 'https://www.facebook.com/' + response[i].uid)
});
cln.find('[title]').each(function () {
$(this).attr('title', response[i].name)
});
cln.find('[alt]').each(function () {
$(this).attr('alt', response[i].name)
});
cln.find('[src]').each(function () {
$(this).attr('src', 'https://graph.facebook.com/' + response[i].uid + '/picture')
});
cln.css('display', 'inline-block');
cln.removeClass('template');
cln.show();
parent.append(cln);
if (access.userdata) {
if (response[i].uid == access.userdata.result.facebook.customer_id) {
$('[data-fb-event="' + eventid + '"] .btn-facebook-attend').addClass('disabled').click(function (e) {
e.preventDefault;
});
}
}
}
}
}, limit);
},
/**
* user attends to an event
* @param eventid facebook event id
* @param callback callback function (eg. for point subscription
*/
attend: function (eventid, callback) {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
window.FB.api(eventid + '/attending', 'post', {}, function (response) {
if (response.error) {
callback('error');
}
if (response === true) {
callback('success');
}
});
} else if (response.status === 'not_authorized') {
callback('auth_error');
} else {
callback('auth_error');
}
});
},
/**
* facebook action for inviting facebook friends to an event
* @param eventid facebook event ID
* @param aFriends guys to invite
* @param callback calback for points
*/
action_invite: function (eventid, aFriends, callback) {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
FB.api({ 'method': 'events.invite',
'eid': eventid,
'uids': aFriends,
'access_token': response.authResponse.accessToken
}, function () {
callback(aFriends);
}
);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
},
/**
* invite people to an event
* @param eventid facebook event ID
* @param title text of the invitation
* @param callback a callback for points
*/
invite: function (eventid, title, callback) {
FB.ui({method: 'apprequests',
message: title
}, function (response) {
if (response) {
nlm_fb_event.action_invite(eventid, response.to, callback);
}
});
},
/**
* share an event on facebook walls
* @param eventid facebook event id
* @param title title of the event
* @param text text of the event
* @param picUrl relative url to an event image
* @param callback callback funtion for points
*/
share: function (eventid, title, text, picUrl, callback) {
var baseUrl = window.location.protocol + '//' + window.location.host;
var obj = {
method: 'feed',
redirect_uri: baseUrl,
link: 'https://www.facebook.com/events/' + eventid + '/',
picture: baseUrl + picUrl,
name: title,
caption: text,
description: text
};
FB.ui(obj, callback);
},
/**
* facebook action for reading event attendees
* @param eventid the facebook event id
* @param callback the callback function (for data processing)
*/
me_attending: function (eventid, callback) {
var fql = 'SELECT rsvp_status FROM event_member WHERE eid=' + eventid + ' AND uid = ' + FB.getUserID();
FB.api(
{
method: 'fql.query',
query: fql,
access_token: app_access_token
},
function (data) {
callback(data);
}
);
}
};
/**
* functions for facebook connection and the gallery
* @type {{share: Function, upload: Function, actionUpload: Function}}
*/
var nlm_fb_gallery = {
/**
* call to facebook jssdk for sharing a gallery
* @param galleryTitle title to be shown on facebook
* @param galleryPic relative url to the image
* @param galleryLink relative url to the gallery
* @param callback callback function after facebook (for giving the user points for sharing etc.)
*/
share: function (galleryTitle, galleryPic, galleryLink, callback) {
var baseUrl = window.location.protocol + '//' + window.location.host;
var obj = {
method: 'feed',
link: baseUrl + galleryLink,
picture: baseUrl + '/' + galleryPic,
name: galleryTitle,
caption: galleryTitle,
description: galleryTitle
};
FB.ui(obj, callback);
},
/**
* upload a image to facebook album
* @param url relative url to the image
* @param msg message to be shown next to the image on facebook
* @param albumName the album name to be posted in
* @param callback callback function after facebook (for giving the user points for sharing etc.)
*/
upload: function (url, msg, albumName, callback) {
if (!albumName) {
albumName = fbAlbumName;
}
FB.getLoginStatus(function (response) {
// if (response.status === 'connected') {
var fql = 'SELECT object_id FROM album WHERE owner=me() AND name="' + albumName + '"';
FB.api(
{
method: 'fql.query',
query: fql,
access_token: response.authResponse.accessToken
},
function (data) {
if (data.length > 0) {
nlm_fb_gallery.actionUpload(data[0].object_id, url, msg, callback, response.authResponse.accessToken);
}
else {
FB.api('/me/albums', 'post', {
name: albumName,
message: albumName
}, function (answer) {
nlm_fb_gallery.actionUpload(answer.id, url, msg, callback, response.authResponse.accessToken);
});
}
}
);
return false;
});
},
/**
* upload a pic to facebook
* @param albumId id of the facebook album (must exsist!)
* @param url relative url to the image
* @param msg message to be shown next to the image
* @param callback
* @param at facebook access_token
* @returns {boolean} always think positive
*/
actionUpload: function (albumId, url, msg, callback, at) {
var baseUrl = window.location.protocol + '//' + window.location.host;
FB.api(albumId + '/photos', 'post', {
message: msg,
access_token: at,
url: baseUrl + '/' + url
}, function (answer) {
callback(answer);
});
return true;
}
};