diff --git a/src/js/facebook-stream.js b/src/js/facebook-stream.js new file mode 100644 index 0000000..545d1c9 --- /dev/null +++ b/src/js/facebook-stream.js @@ -0,0 +1,362 @@ +/*********************************************************** +* #### jQuery-Facebook-Streaming v4.0 #### +* Coded by Ican Bachors 2014. +* https://github.com/bachors/jQuery-Facebook-Stream +* Updates will be posted to this site. +***********************************************************/ + +$.fn.facebook_stream = function(n, o, p) { + var j = '
'; + j += '
'; + j += '
'; + j += ' X'; + j += '

Image

'; + j += '
'; + j += '
'; + j += '
', id = ($(this).attr("id") != null || $(this).attr("id") != undefined ? '#' + $(this).attr("id") : '.' + $(this).attr("class")), limit = o + 1; + $(this).html(j); + FS_gf(id, limit, 'until='); + + function FS_gf(k, l, m) { + $.ajax({ + url: 'https://graph.facebook.com/v2.9/' + n + '/feed?limit=' + l + '&fields=from,message,created_time,id,likes,comments,story,link,type,name,picture,caption,description,object_id,shares&access_token=' + p + '&' + m, + crossDomain: true, + dataType: 'json' + }).done(function(h) { + var j = ''; + $.each(h.data, function(i, a) { + if (i < o) { + j += '
'; + j += '

'; + j += '' + h.data[i].from.name + '
'; + var b = ''; + if (h.data[i].comments == null || h.data[i].comments == undefined) { + b += '0' + } else { + if (h.data[i].comments.data.length == 25) { + b += h.data[i].comments.data.length + '+' + } else { + b += h.data[i].comments.data.length + } + } + var c = ''; + if (h.data[i].likes == null || h.data[i].likes == undefined) { + c += '0' + } else { + if (h.data[i].likes.data.length == 25) { + c += h.data[i].likes.data.length + '+' + } else { + c += h.data[i].likes.data.length + } + } + j += ' ' + relative_time(h.data[i].created_time) + '

'; + if (h.data[i].message != null || h.data[i].message != undefined) { + j += '
' + htmlEntities(h.data[i].message) + '
' + } + if (h.data[i].story != null || h.data[i].story != undefined) { + j += h.data[i].story + '
' + } + if (h.data[i].name != null || h.data[i].name != undefined && h.data[i].type != 'photo') { + j += '
'; + if (h.data[i].picture != null || h.data[i].picture != undefined) { + j += '' + } + j += '

' + h.data[i].name + '

'; + if (h.data[i].caption != null || h.data[i].caption != undefined) { + j += '' + h.data[i].caption + '
' + } + if (h.data[i].description != null || h.data[i].description != undefined) { + j += h.data[i].description + } + j += '
' + } else { + if (h.data[i].picture != null || h.data[i].picture != undefined) { + j += '
' + } + } + var d = ''; + if (h.data[i].comments != null || h.data[i].comments != undefined) { + d += h.data[i].comments.data.length + } else { + d += 0 + } + var e = ''; + if (h.data[i].likes != null || h.data[i].likes != undefined) { + e += h.data[i].likes.data.length + } else { + e += 0 + } + var f = h.data[i].id.split('_'); + var g = ''; + if (h.data[i].shares != null || h.data[i].shares != undefined) { + g += h.data[i].shares.count + } + j += '
' + c + '
' + b + '
'; + if (h.data[i].likes != null || h.data[i].likes != undefined) { + j += '' + } + if (h.data[i].comments != null || h.data[i].comments != undefined) { + j += '' + } + j += '
' + } + }); + if (h.paging.next != null || h.paging.next != undefined) { + j += '

More

'; + } + $(k + ' .facebook-stream').append(j); + $('.FS_popup').click(function() { + $("html, body").animate({ + scrollTop: 0 + }, 0); + $(k + ' .modalDialog p').html(''); + $(k + ' .modalDialog').css("opacity", "1"); + $(k + ' .modalDialog').css("pointer-events", "auto"); + return false + }); + $(k + ' .modalDialog .close').click(function() { + $(k + ' .modalDialog').css("opacity", "0"); + $(k + ' .modalDialog').css("pointer-events", "none"); + return false + }); + $(k + ' .lihatlike').click(function() { + var a = $(this).data('liki').split(','); + if (a[1] > 0) { + FS_like(a[0]) + } + $('.FS_like' + a[0]).css("display", "block"); + $('.FS_blscm' + a[0]).css("display", "none"); + return false + }); + $(k + ' .lihatkom').click(function() { + var a = $(this).data('kom').split(','); + if (a[1] > 0) { + FS_morekomen(a[0], '', 'html') + } + $('.FS_like' + a[0]).css("display", "none"); + $('.FS_blscm' + a[0]).css("display", "block"); + return false + }); + $(k + ' .FS_moregf').click(function() { + if(h.paging.cursors !== undefined){ + var a = 'after=' + h.paging.cursors.after; + FS_gf(k, l, a); + $(k + ' .load_more').remove(); + }else{ + var a = h.paging.next; + var b = a.split('&'); + for (var i = 0; i < b.length; i++) { + var c = b[i].split('='); + if (c[0] == 'until') { + FS_gf(k, l, 'until=' + c[1]); + $(k + ' .load_more').remove(); + } + } + } + return false; + }) + }) + } + + function FS_like(e) { + $.ajax({ + url: 'https://graph.facebook.com/v2.9/' + e + '/likes?access_token=' + p, + crossDomain: true, + dataType: 'json' + }).done(function(b) { + var c = ''; + $.each(b.data, function(i, a) { + c += '' + }); + var d = ''; + if (b.data.length == 25) { + d += '& more ' + } + c += '' + d + 'like this.'; + $('.FS_like' + e).html(c) + }) + } + + function FS_morekomen(e, g, h) { + $.ajax({ + url: 'https://graph.facebook.com/v2.9/' + e + '/comments?fields=created_time,from,message,id,like_count&access_token=' + p + '&after=' + g, + crossDomain: true, + dataType: 'json' + }).done(function(c) { + var d = ''; + $.each(c.data, function(i, a) { + d += '
'; + d += '
' + c.data[i].from.name + ''; + if (c.data[i].message.length > 0) { + d += '

' + htmlEntities(c.data[i].message) + '
' + } else { + d += '

' + } + var b = c.data[i].id; + d += '

'; + FS_gambarg(b); + d += ' ' + relative_time(c.data[i].created_time) + '
'; + d += '
'; + FS_subkomen(c.data[i].id, '') + }); + if (c.paging != undefined) { + if (c.paging.next != undefined) { + d += '

View more comments

' + } + } + if (h == 'html') { + $('.FS_blscm' + e).html(d) + } else { + $('.FS_blscm' + e).append(d) + } + $('.fcm_more' + e).click(function() { + var a = c.paging.cursors.after; + if (a != null || a != undefined) { + FS_morekomen(e, a, 'append'); + $('.FS_blscm' + e + ' .load_more_komen').remove(); + return false + } + }) + }) + } + + function FS_subkomen(e, g) { + $.ajax({ + url: 'https://graph.facebook.com/v2.9/' + e + '/comments?fields=created_time,from,message,id,like_count&access_token=' + p + '&after=' + g, + crossDomain: true, + dataType: 'json' + }).done(function(c) { + var d = ''; + $.each(c.data, function(i, a) { + d += '
'; + d += '
' + c.data[i].from.name + ''; + if (c.data[i].message.length > 0) { + d += '

' + htmlEntities(c.data[i].message) + '
' + } else { + d += '

' + } + var b = c.data[i].id; + d += '

'; + FS_gambarg(b); + d += ' ' + relative_time(c.data[i].created_time) + '
'; + d += '
' + }); + if (c.paging != undefined) { + if (c.paging.next != undefined) { + d += '

View more comments

' + } + } + $('#komen_sub_' + e).append(d); + $('.fcm_moresub').click(function() { + var a = c.paging.cursors.after; + if (a != null || a != undefined) { + FS_subkomen(e, a); + $('#komen_sub_' + e + ' .load_more_komen_sub').remove(); + return false + } + }); + }) + } + + function FS_gambarg(c) { + $.ajax({ + url: 'https://graph.facebook.com/v2.9/' + c + '?fields=attachment&access_token=' + p, + crossDomain: true, + dataType: 'json' + }).done(function(a) { + var b = ''; + if (a.attachment != null || a.attachment != undefined) { + b += '' + } + $('.FS_comg' + c).html(b); + $('.FS_popup').click(function() { + $("html, body").animate({ + scrollTop: 0 + }, 0); + $(id + ' .modalDialog p').html(''); + $(id + ' .modalDialog').css("opacity", "1"); + $(id + ' .modalDialog').css("pointer-events", "auto"); + return false + }); + $(id + ' .modalDialog .close').click(function() { + $(id + ' .modalDialog').css("opacity", "0"); + $(id + ' .modalDialog').css("pointer-events", "none"); + return false + }) + }) + } + + function htmlEntities(a) { + return FS_urltag(String(a).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"')) + } + + function relative_time(a) { + if (!a) { + return + } + a = $.trim(a); + a = a.replace(/\.\d\d\d+/, ""); + a = a.replace(/-/, "/").replace(/-/, "/"); + a = a.replace(/T/, " ").replace(/Z/, " UTC"); + a = a.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); + var b = new Date(a); + var c = (arguments.length > 1) ? arguments[1] : new Date(); + var d = parseInt((c.getTime() - b) / 1000); + d = (d < 2) ? 2 : d; + var r = ''; + if (d < 60) { + r = 'Just now' + } else if (d < 120) { + r = 'a min' + } else if (d < (45 * 60)) { + r = (parseInt(d / 60, 10)).toString() + ' mins ago' + } else if (d < (2 * 60 * 60)) { + r = 'an hr' + } else if (d < (24 * 60 * 60)) { + r = '' + (parseInt(d / 3600, 10)).toString() + ' hrs ago' + } else if (d < (48 * 60 * 60)) { + r = 'a day' + } else { + r = (parseInt(d / 86400, 10)).toString() + ' days ago' + } + return r + } + + function FS_urltag(c, d) { + var e = { + lt: { + regex: /(<)/g, + template: '<' + }, + gt: { + regex: /(>)/g, + template: '>' + }, + dq: { + regex: /(")/g, + template: '"' + }, + sq: { + regex: /(')/g, + template: ''' + }, + link: { + regex: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, + template: "$1" + }, + hash: { + regex: /(^|\s)#(\w+)/g, + template: '$1#$2' + }, + email: { + regex: /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi, + template: '$1' + } + }; + var f = $.extend(e, d); + $.each(f, function(a, b) { + c = c.replace(b.regex, b.template) + }); + return c + } +}