1if (typeof Object.create !== 'function') { 2 Object.create = function(obj) { 3 function F() {} 4 F.prototype = obj; 5 return new F(); 6 }; 7} 8 9(function($, window, document, undefined) { 10 $.fn.socialfeed = function(_options) { 11 12 13 var defaults = { 14 plugin_folder: '', // a folder in which the plugin is located (with a slash in the end) 15 template: 'template.html', // a path to the template file 16 show_media: false, // show images of attachments if available 17 media_min_width: 300, 18 length: 500, // maximum length of post message shown 19 date_format: 'll' 20 }; 21 //--------------------------------------------------------------------------------- 22 var options = $.extend(defaults, _options), 23 container = $(this), 24 template, 25 social_networks = ['facebook', 'instagram', 'vk', 'google', 'blogspot', 'pinterest', 'rss'], 26 posts_to_load_count = 0, 27 loaded_post_count = 0; 28 // container.empty().css('display', 'block'); 29 //--------------------------------------------------------------------------------- 30 31 //--------------------------------------------------------------------------------- 32 // This function performs consequent data loading from all of the sources by calling corresponding functions 33 function calculatePostsToLoadCount() { 34 social_networks.forEach(function(network) { 35 if (options[network]) { 36 if (options[network].accounts) { 37 posts_to_load_count += options[network].limit * options[network].accounts.length; 38 } else { 39 posts_to_load_count += options[network].limit; 40 } 41 } 42 }); 43 } 44 45 calculatePostsToLoadCount(); 46 47 function fireCallback() { 48 var fire = true; 49 /*$.each(Object.keys(loaded), function() { 50 if (loaded[this] > 0) 51 fire = false; 52 });*/ 53 if (fire && options.callback) { 54 options.callback(); 55 } 56 } 57 58 var Utility = { 59 request: function(url, callback) { 60 $.ajax({
61 url: url, 62 dataType: 'jsonp', 63 success: callback 64 }); 65 }, 66 get_request: function(url, callback) { 67 $.get(url, callback, 'json'); 68 }, 69 wrapLinks: function(string, social_network) { 70 var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; 71 if (social_network === 'google-plus') { 72 string = string.replace(/(@|#)([a-z0-9_]+['])/ig, Utility.wrapGoogleplusTagTemplate); 73 } else { 74 string = string.replace(exp, Utility.wrapLinkTemplate); 75 } 76 return string; 77 }, 78 wrapLinkTemplate: function(string) { 79 return '<a target="_blank" href="' + string + '">' + string + '<\/a>'; 80 }, 81 wrapGoogleplusTagTemplate: function(string) { 82 return '<a target="_blank" href="https://plus.google.com/s/' + string + '" >' + string + '<\/a>'; 83 }, 84 shorten: function(string) { 85 string = $.trim(string); 86 if (string.length > options.length) { 87 return jQuery.trim(string).substring(0, options.length).split(" ").slice(0, -1).join(" ") + "..."; 88 } else { 89 return string; 90 } 91 }, 92 stripHTML: function(string) { 93 if (typeof string === "undefined" || string === null) { 94 return ''; 95 } 96 return string.replace(/(<([^>]+)>)|nbsp;|\s{2,}|/ig, ""); 97 } 98 }; 99 100 function SocialFeedPost(social_network, data) { 101 this.content = data; 102 this.content.social_network = social_network; 103 this.content.attachment = (this.content.attachment === undefined) ? '' : this.content.attachment; 104 this.content.time_ago = data.dt_create.fromNow(); 105 this.content.date = data.dt_create.format(options.date_format); 106 this.content.dt_create = this.content.dt_create.valueOf(); 107 this.content.text = Utility.wrapLinks(Utility.shorten(data.message + ' ' + data.description), data.social_network); 108 this.content.moderation_passed = (options.moderation) ? options.moderation(this.content) : true; 109 110 Feed[social_network].posts.push(this); 111 } 112 SocialFeedPost.prototype = { 113 render: function() { 114 var rendered_html = Feed.template(this.content); 115 var data = this.content; 116 117 if ($(container).children('[social-feed-id=' + data.id + ']').length !== 0) { 118 return false; 119 } 120 if ($(container).children().length === 0) { 121 $(container).append(rendered_html); 122 } else { 123 var i = 0, 124 insert_index = -1; 125 $.each($(container).children(), function() { 126 if ($(this).attr('dt-create') < data.dt_create) { 127 insert_index = i; 128 return false; 129 } 130 i++; 131 }); 132 $(container).append(rendered_html); 133 if (insert_index >= 0) { 134 insert_index++; 135 var before = $(container).children('div:nth-child(' + insert_index + ')'), 136 current = $(container).children('div:last-child'); 137 $(current).insertBefore(before); 138 } 139 140 } 141 if (options.media_min_width) { 142 143 var query = '[social-feed-id=' + data.id + '] img.attachment'; 144 var image = $(query); 145 146 // preload the image 147 var height, width = ''; 148 var img = new Image(); 149 var imgSrc = image.attr("src"); 150 151 $(img).load(function() { 152
153 if (img.width < options.media_min_width) { 154 image.hide(); 155 } 156 // garbage collect img 157 delete img; 158 159 }).error(function() { 160 // image couldnt be loaded 161 image.hide(); 162 163 }).attr({ 164 src: imgSrc 165 }); 166 167 } 168 169 loaded_post_count++; 170 if (loaded_post_count == posts_to_load_count) { 171 fireCallback(); 172 } 173 174 } 175 176 }; 177 178 var Feed = { 179 template: false, 180 init: function() { 181 Feed.getTemplate(function() { 182 social_networks.forEach(function(network) { 183 if (options[network]) { 184 if ( options[network].accounts ) { 185 //loaded[network] = 0; 186 options[network].accounts.forEach(function(account) { 187 //loaded[network]++; 188 Feed[network].getData(account); 189 }); 190 } else if ( options[network].urls ) { 191 options[network].urls.forEach(function(url) { 192 Feed[network].getData(url); 193 }); 194 } else { 195 Feed[network].getData(); 196 } 197 } 198 }); 199 }); 200 }, 201 getTemplate: function(callback) { 202 if (Feed.template) 203 return callback(); 204 else { 205 if (options.template_html) { 206 Feed.template = doT.template(options.template_html); 207 return callback(); 208 } else { 209 $.get(options.template, function(template_html) { 210 Feed.template = doT.template(template_html); 211 return callback(); 212 }); 213 } 214 } 215 }, 216 facebook: { 217 posts: [], 218 graph: 'https://graph.facebook.com/', 219 loaded: false, 220 getData: function(account) { 221 var proceed = function(request_url){ 222 Utility.request(request_url, Feed.facebook.utility.getPosts); 223 }; 224 var fields = '?fields=id,from,name,message,created_time,story,description,link'; 225 fields += (options.show_media === true)?',picture,object_id':''; 226 var request_url, limit = '&limit=' + options.facebook.limit,
227 query_extention = '&access_token=' + options.facebook.access_token + '&callback=?'; 228 switch (account[0]) { 229 case '@': 230 var username = account.substr(1); 231 Feed.facebook.utility.getUserId(username, function(userdata) { 232 if (userdata.id !== '') { 233 request_url = Feed.facebook.graph + 'v2.4/' + userdata.id + '/posts'+ fields + limit + query_extention; 234 proceed(request_url); 235 } 236 }); 237 break; 238 case '!': 239 var page = account.substr(1); 240 request_url = Feed.facebook.graph + 'v2.4/' + page + '/feed'+ fields + limit + query_extention; 241 proceed(request_url); 242 break; 243 default: 244 proceed(request_url); 245 } 246 }, 247 utility: { 248 getUserId: function(username, callback) {
249 var query_extention = '&access_token=' + options.facebook.access_token + '&callback=?'; 250 var url = 'https://graph.facebook.com/' + username + '?' + query_extention; 251 var result = ''; 252 $.get(url, callback, 'json'); 253 }, 254 prepareAttachment: function(element) { 255 var image_url = element.picture; 256 if (image_url.indexOf('_b.') !== -1) { 257 //do nothing it is already big 258 } else if (image_url.indexOf('safe_image.php') !== -1) { 259 image_url = Feed.facebook.utility.getExternalImageURL(image_url, 'url'); 260 261 } else if (image_url.indexOf('app_full_proxy.php') !== -1) { 262 image_url = Feed.facebook.utility.getExternalImageURL(image_url, 'src'); 263 264 } else if (element.object_id) { 265 image_url = Feed.facebook.graph + element.object_id + '/picture/?type=normal'; 266 } 267 return '<img class="attachment" src="' + image_url + '" />'; 268 }, 269 getExternalImageURL: function(image_url, parameter) { 270 image_url = decodeURIComponent(image_url).split(parameter + '=')[1]; 271 if (image_url.indexOf('fbcdn-sphotos') === -1) { 272 return image_url.split('&')[0]; 273 } else { 274 return image_url; 275 } 276 277 }, 278 getPosts: function(json) { 279 if (json['data']) { 280 json['data'].forEach(function(element) { 281 var post = new SocialFeedPost('facebook', Feed.facebook.utility.unifyPostData(element)); 282 post.render(); 283 }); 284 } 285 }, 286 unifyPostData: function(element) { 287 var post = {}, 288 text = (element.message) ? element.message : element.story; 289 290 post.id = element.id; 291 post.dt_create = moment(element.created_time); 292 post.author_link = 'http://facebook.com/' + element.from.id; 293 post.author_picture = Feed.facebook.graph + element.from.id + '/picture'; 294 post.author_name = element.from.name; 295 post.name = element.name || ""; 296 post.message = (text) ? text : ''; 297 post.description = (element.description) ? element.description : ''; 298 post.link = (element.link) ? element.link : 'http://facebook.com/' + element.from.id; 299 300 if (options.show_media === true) { 301 if (element.picture) { 302 var attachment = Feed.facebook.utility.prepareAttachment(element); 303 if (attachment) { 304 post.attachment = attachment; 305 } 306 } 307 } 308 return post; 309 } 310 } 311 }, 312 google: { 313 posts: [], 314 loaded: false, 315 api: 'https://www.googleapis.com/plus/v1/', 316 getData: function(account) { 317 var request_url; 318 switch (account[0]) { 319 case '#': 320 var hashtag = account.substr(1); 321 request_url = Feed.google.api + 'activities?query=' + hashtag + '&key=' + options.google.access_token + '&maxResults=' + options.google.limit; 322 Utility.get_request(request_url, Feed.google.utility.getPosts); 323 break; 324 case '@': 325 var username = account.substr(1); 326 request_url = Feed.google.api + 'people/' + username + '/activities/public?key=' + options.google.access_token + '&maxResults=' + options.google.limit; 327 Utility.get_request(request_url, Feed.google.utility.getPosts); 328 break; 329 default: 330 } 331 }, 332 utility: { 333 getPosts: function(json) { 334 if (json.items) { 335 $.each(json.items, function(i) { 336 var post = new SocialFeedPost('google', Feed.google.utility.unifyPostData(json.items[i])); 337 post.render(); 338 }); 339 } 340 }, 341 unifyPostData: function(element) { 342 var post = {}; 343
344 post.id = element.id; 345 post.attachment = ''; 346 post.description = ''; 347 post.dt_create = moment(element.published); 348 post.author_link = element.actor.url; 349 post.author_picture = element.actor.image.url; 350 post.author_name = element.actor.displayName; 351 352 if (options.show_media === true) { 353 if (element.object.attachments) { 354 $.each(element.object.attachments, function() { 355 var image = ''; 356 if (this.fullImage) { 357 image = this.fullImage.url; 358 } else { 359 if (this.objectType === 'album') { 360 if (this.thumbnails && this.thumbnails.length > 0) { 361 if (this.thumbnails[0].image) { 362 image = this.thumbnails[0].image.url; 363 } 364 } 365 } 366 } 367 post.attachment = '<img class="attachment" src="' + image
367+ '"/>'; 368 }); 369 } 370 } 371 post.message = element.title; 372 post.link = element.url; 373 374 return post; 375 } 376 } 377 }, 378 instagram: { 379 posts: [], 380 api: 'https://api.instagram.com/v1/', 381 loaded: false, 382 accessType: function() { 383 // If we have both the client_id and access_token set in options, 384 // use access_token for authentication. If client_id is not set 385 // then use access_token. If neither are set, log an error to console. 386 if (typeof options.instagram.access_token === 'undefined' && typeof options.instagram.client_id === 'undefined') { 387 console.log('You need to define a client_id or access_token to authenticate with Instagram\'s API.'); 388 return undefined; 389 } 390 if (options.instagram.access_token) { options.instagram.client_id = undefined; } 391 options.instagram.access_type = (typeof options.instagram.client_id === 'undefined' ? 'access_token' : 'client_id'); 392 return options.instagram.access_type; 393 }, 394 getData: function(account) { 395 var url; 396 397 // API endpoint URL depends on which authentication type we're using. 398 if (this.accessType() !== 'undefined') { 399 var authTokenParams = options.instagram.access_type + '=' + options.instagram[options.instagram.access_type]; 400 } 401 402 switch (account[0]) { 403 case '@': 404 var username = account.substr(1); 405 url = Feed.instagram.api + 'users/search/?q=' + username + '&' + authTokenParams + '&count=1' + '&callback=?'; 406 Utility.request(url, Feed.instagram.utility.getUsers); 407 break; 408 case '#': 409 var hashtag = account.substr(1); 410 url = Feed.instagram.api + 'tags/' + hashtag + '/media/recent/?' + authTokenParams + '&' + 'count=' + options.instagram.limit + '&callback=?'; 411 Utility.request(url, Feed.instagram.utility.getImages); 412 break; 413 case '&': 414 var id = account.substr(1); 415 url = Feed.instagram.api + 'users/' + id + '/?' + authTokenParams + '&' + 'count=' + options.instagram.limit + '&callback=?'; 416 Utility.request(url, Feed.instagram.utility.getUsers); 417 default: 418 } 419 }, 420 utility: { 421 getImages: function(json) { 422 if (json.data) { 423 json.data.forEach(function(element) { 424 var post = new SocialFeedPost('instagram', Feed.instagram.utility.unifyPostData(element)); 425 post.render(); 426 }); 427 } 428 }, 429 getUsers: function(json) { 430 // API endpoint URL depends on which authentication type we're using. 431 if (options.instagram.access_type !== 'undefined') { 432 var authTokenParams = options.instagram.access_type + '=' + options.instagram[options.instagram.access_type]; 433 } 434 435 if (!jQuery.isArray(json.data)) json.data = [json.data] 436 json.data.forEach(function(user) { 437 var url = Feed.instagram.api + 'users/' + user.id + '/media/recent/?' + authTokenParams + '&' + 'count=' + options.instagram.limit + '&callback=?'; 438 Utility.request(url, Feed.instagram.utility.getImages); 439 }); 440 }, 441 unifyPostData: function(element) { 442 var post = {}; 443
444 post.id = element.id; 445 post.dt_create = moment(element.created_time * 1000); 446 post.author_link = 'http://instagram.com/' + element.user.username; 447 post.author_picture = element.user.profile_picture; 448 post.author_name = element.user.full_name || element.user.username; 449 post.message = (element.caption && element.caption) ? element.caption.text : ''; 450 post.description = ''; 451 post.link = element.link; 452 if (options.show_media) { 453 post.attachment = '<img class="attachment" src="' + element.images.standard_resolution.url + '' + '" />'; 454 } 455 return post; 456 } 457 } 458 }, 459 vk: { 460 posts: [], 461 loaded: false, 462 base: 'http://vk.com/', 463 api: 'https://api.vk.com/method/', 464 user_json_template: 'https://api.vk.com/method/' + 'users.get?fields=first_name,%20last_name,%20screen_name,%20photo&uid=', 465 group_json_template: 'https://api.vk.com/method/' + 'groups.getById?fields=first_name,%20last_name,%20screen_name,%20photo&gid=', 466 getData: function(account) { 467 var request_url; 468 469 switch (account[0]) { 470 case '@': 471 var username = account.substr(1); 472 request_url = Feed.vk.api + 'wall.get?owner_id=' + username + '&filter=' + options.vk.source + '&count=' + options.vk.limit + '&callback=?'; 473 Utility.get_request(request_url, Feed.vk.utility.getPosts); 474 break; 475 case '#': 476 var hashtag = account.substr(1); 477 request_url = Feed.vk.api + 'newsfeed.search?q=' + hashtag + '&count=' + options.vk.limit + '&callback=?'; 478 Utility.get_request(request_url, Feed.vk.utility.getPosts); 479 break; 480 default: 481 } 482 }, 483 utility: { 484 getPosts: function(json) { 485 if (json.response) { 486 $.each(json.response, function() { 487 if (this != parseInt(this) && this.post_type === 'post') { 488 var owner_id = (this.owner_id) ? this.owner_id : this.from_id, 489 vk_wall_owner_url = (owner_id > 0) ? (Feed.vk.user_json_template + owner_id + '&callback=?') : (Feed.vk.group_json_template + (-1) * owner_id + '&callback=?'), 490 element = this; 491 Utility.get_request(vk_wall_owner_url, function(wall_owner) { 492 Feed.vk.utility.unifyPostData(wall_owner, element, json); 493 }); 494 } 495 }); 496 } 497 }, 498 unifyPostData: function(wall_owner, element, json) { 499 var post = {}; 500 501 post.id = element.id; 502 post.dt_create = moment.unix(element.date); 503 post.description = ' '; 504 post.message = Utility.stripHTML(element.text); 505 if (options.show_media) { 506 if (element.attachment) { 507 if (element.attachment.type === 'link') 508 post.attachment = '<img class="attachment" src="' + element.attachment.link.image_src + '" />'; 509 if (element.attachment.type === 'video') 510 post.attachment = '<img class="attachment" src="' + element.attachment.video.image_big + '" />'; 511 if (element.attachment.type === 'photo') 512 post.attachment = '<img class="attachment" src="' + element.attachment.photo.src_big + '" />'; 513 } 514 } 515 516 if (element.from_id > 0) { 517 var vk_user_json = Feed.vk.user_json_template + element.from_id + '&callback=?'; 518 Utility.get_request(vk_user_json, function(user_json) { 519 var vk_post = new SocialFeedPost('vk', Feed.vk.utility.getUser(user_json, post, element, json)); 520 vk_post.render(); 521 }); 522 523 } else { 524 var vk_group_json = Feed.vk.group_json_template + (-1) * element.from_id + '&callback=?'; 525 Utility.get_request(vk_group_json, function(user_json) { 526 var vk_post = new SocialFeedPost('vk', Feed.vk.utility.getGroup(user_json, post, element, json)); 527 vk_post.render(); 528 }); 529 } 530 }, 531 getUser: function(user_json, post, element, json) { 532 post.author_name = user_json.response[0].first_name + ' ' + user_json.response[0].last_name; 533 post.author_picture = user_json.response[0].photo; 534 post.author_link = Feed.vk.base + user_json.response[0].screen_name; 535 post.link = Feed.vk.base + user_json.response[0].screen_name + '?w=wall' + element.from_id + '_' + element.i
535d; 536 537 return post; 538 }, 539 getGroup: function(user_json, post, element, json) { 540 post.author_name = user_json.response[0].name; 541 post.author_picture = user_json.response[0].photo; 542 post.author_link = Feed.vk.base + user_json.response[0].screen_name; 543 post.link = Feed.vk.base + user_json.response[0].screen_name + '?w=wall-' + user_json.response[0].gid + '_' + element.id; 544 545 return post; 546 } 547 } 548 }, 549 blogspot: { 550 loaded: false, 551 getData: function(account) { 552 var url; 553 554 switch (account[0]) { 555 case '@': 556 var username = account.substr(1); 557 url = 'http://' + username + '.blogspot.com/feeds/posts/default?alt=json-in-script&callback=?'; 558 request(url, getPosts); 559 break; 560 default: 561 } 562 }, 563 utility: { 564 getPosts: function(json) { 565 $.each(json.feed.entry, function() { 566 var post = {}, 567 element = this; 568 post.id = element.id['$t'].replace(/[^a-z0-9]/gi, ''); 569 post.dt_create = moment((element.published['$t'])); 570 post.author_link = element.author[0]['uri']['$t']; 571 post.author_picture = 'http:' + element.author[0]['gd$image']['src']; 572 post.author_name = element.author[0]['name']['$t']; 573 post.message = element.title['$t'] + '</br></br>' + stripHTML(element.content['$t']); 574 post.description = ''; 575 post.link = element.link.pop().href; 576 577 if (options.show_media) { 578 if (element['media$thumbnail']) { 579 post.attachment = '<img class="attachment" src="' + element['media$thumbnail']['url'] + '" />'; 580 } 581 } 582 583 post.render(); 584 585 }); 586 } 587 } 588 }, 589 pinterest: { 590 posts: [], 591 loaded: false, 592 apiv1: 'https://api.pinterest.com/v1/', 593 594 getData: function(account) { 595 var request_url, 596 limit = 'limit=' + options.pinterest.limit, 597 fields = 'fields=id,created_at,link,note,creator(url,first_name,last_name,image),image', 598 query_extention = fields + '&access_token='
598+ options.pinterest.access_token + '&' + limit + '&callback=?'; 599 switch (account[0]) { 600 case '@': 601 var username = account.substr(1); 602 if (username === 'me') { 603 request_url = Feed.pinterest.apiv1 + 'me/pins/?' + query_extention; 604 } else { 605 request_url = Feed.pinterest.apiv1 + 'boards/' + username + '/pins?' + query_extention; 606 } 607 break; 608 default: 609 } 610 Utility.request(request_url, Feed.pinterest.utility.getPosts); 611 }, 612 utility: { 613 614 getPosts: function(json) { 615 json.data.forEach(function(element) { 616 var post = new SocialFeedPost('pinterest', Feed.pinterest.utility.unifyPostData(element)); 617 post.render(); 618 }); 619 }, 620 621 unifyPostData: function(element){ 622 var post = {}; 623 624 post.id = element.id; 625 post.dt_create= moment(element.created_at); 626 post.author_link = element.creator.url; 627 post.author_picture = element.creator.image['60x60' ].url; 628 post.author_name = element.creator.first_name + element.creator.last_name; 629 post.message = element.note; 630 post.description = ''; 631 post.social_network = 'pinterest'; 632 post.link = element.link ? element.link : 'https://www.pinterest.com/pin/' + element.id; 633 if (options.show_media) { 634 post.attachment = '<img class="attachment" src="' + element.image['original'].url + '" />'; 635 } 636 return post; 637 } 638 } 639 }, 640 rss : { 641 posts: [], 642 loaded: false, 643 api : 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0', 644 645 getData: function(url) { 646 var limit = '&num='+ options.rss.limit, 647 request_url = Feed.rss.api + limit + '&q=' + encodeURIComponent(url); 648 649 Utility.request(request_url, Feed.rss.utility.getPosts); 650 }, 651 utility: { 652 653 getPosts: function(json) { 654 $.each(json.responseData.feed.entries, function(index, element) { 655 var post = new SocialFeedPost('rss', Feed.rss.utility.unifyPostData(index, element)); 656 post.render(); 657 }); 658 }, 659 660 unifyPostData: function(index, element){ 661 var post = {}; 662 663 post.id = index; 664 post.dt_create= moment(element.publishedDate, 'ddd, DD MMM YYYY HH:mm:ss ZZ', 'en'); 665 post.author_link = ''; 666 post.author_picture = ''; 667 post.author_name = element.author; 668 post.message = Utility.stripHTML(element.title); 669 post.description = Utility.stripHTML(element.content); 670 post.social_network = 'rss'; 671 post.link = element.link; 672 if (options.show_media && element.mediaGroups ) { 673 post.attachment = '<img class="attachment" src="' + element.mediaGroups[0].contents[0].url + '" />'; 674 } 675 return post; 676 } 677 } 678 } 679 }; 680 681 //make the plugin chainable 682 return this.each(function() { 683 // Initialization 684 Feed.init(); 685 if (options.update_period) { 686 setInterval(function() { 687 return Feed.init(); 688 }, options.update_period); 689 } 690 }) 691 }; 692 693})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.