zigojacko

Pintafeed

Mar 8th, 2015
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($){
  2.  
  3.     PinterestFeedObject = function(el, options) {
  4.         this.create(el, options);
  5.     };
  6.  
  7.     $.extend(PinterestFeedObject.prototype, {
  8.  
  9.         create: function(el, options) {
  10.  
  11.             // Set the default options
  12.             this.defaults = {
  13.                 id: '', // enter username, pinterest list ID or search keyword
  14.                 results: 10, // number of results to display
  15.                 tweetId: '' // twitter username for share links
  16.             };
  17.  
  18.             var o = $.extend(true,this.defaults,options);
  19.  
  20.             // Add ul tag to target element
  21.             //$(el).append('<div id="pinterest-feed" class="stream block-content"></div>');
  22.             //$(el).append('<span class="stream"></span>');
  23.  
  24.             // Set Pinterest RSS url using Google Feed API
  25.             var cp = o.id.split('/'),
  26.                 ext = cp.length > 1 ? '/rss' : '/feed.rss',
  27.                 href = 'http://www.pinterest.com/'+o.id,
  28.                 url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='+o.results+'&callback=?&q=' + encodeURIComponent(href+ext);
  29.  
  30.             // jQuery AJAX call
  31.             jQuery.ajax({
  32.                 url: url,
  33.                 cache: true,
  34.                 dataType: 'jsonp',
  35.                 success: function(a){
  36.                     a = a.responseData.feed.entries;
  37.                     $.each(a, function(i,item){
  38.                         if(i < o.results){
  39.                             var d = item.publishedDate,
  40.                                 img = '<a href="'+item.link+'"><img src="'+$('img',item.content).attr('src')+'" alt="" /></a>',
  41.                                 html = '' + img;
  42.  
  43.                             // Add share links
  44.                             //html += '<span class="section-share">'+shareLink(item.contentSnippet,item.link,o.tweetId)+'</span>';
  45.  
  46.                             // Get time since
  47.                             //d = d != '' ? html += '<span class="date">'+nicetime(new Date(d).getTime())+'</span></li>' : '' ;
  48.                         }
  49.  
  50.                         // Add pinterest feed items to stream
  51.                         $('#pinterest-feed').append(html);
  52.                     });
  53.                 },
  54.                 complete: function(){
  55.  
  56.                     // Code to open new popup window for share links + open other links in new browser tab
  57.                     $('.stream a',el).click(function(e){
  58.                         if($(this).parent().hasClass('section-share')){
  59.                             var u = $(this).attr('href');
  60.                             window.open(u,'sharer','toolbar=0,status=0,width=626,height=436');
  61.                             return false;
  62.                         } else {
  63.                             if(external){this.target = '_blank';}
  64.                         }
  65.                     });
  66.  
  67.                     // jQuery Isotope function
  68.                     //var $c = $('.stream',el);
  69.                     //$c.imagesLoaded( function(){
  70.                         //$c.isotope({itemSelector : 'li'});
  71.                         //$('li',$c).fadeIn();
  72.                     //});
  73.                 }
  74.             });
  75.         }
  76.     });
  77.  
  78.     $.fn.dcPinterestFeed = function(options, callback){
  79.         var d = {};
  80.         this.each(function(){
  81.             var s = $(this);
  82.             d = s.data("pinterestfeed");
  83.             if (!d){
  84.                 d = new PinterestFeedObject(this, options, callback);
  85.                 s.data("pinterestfeed", d);
  86.             }
  87.         });
  88.         return d;
  89.     };
  90.  
  91.     // Creates facebook & twitter share links
  92.     function shareLink(st,sq,tweetId){
  93.         var sq = encodeURIComponent(sq), st = encodeURIComponent(st);
  94.         var s = '<a href="http://www.facebook.com/sharer.php?u='+sq+'&t='+st+'" class="share-facebook"></a>';
  95.         s += '<a href="https://twitter.com/share?url='+sq+'&text='+st+'&via='+tweetId+'" class="share-twitter"></a>';
  96.         return s;
  97.     }
  98.  
  99.     // Function to convert date to relative date
  100.     function nicetime(a){
  101.         var d = Math.round((+new Date - a) / 1000), fuzzy;
  102.         var chunks = new Array();
  103.             chunks[0] = [60 * 60 * 24 * 365 , 'year'];
  104.             chunks[1] = [60 * 60 * 24 * 30 , 'month'];
  105.             chunks[2] = [60 * 60 * 24 * 7, 'week'];
  106.             chunks[3] = [60 * 60 * 24 , 'day'];
  107.             chunks[4] = [60 * 60 , 'hr'];
  108.             chunks[5] = [60 , 'min'];
  109.         var i = 0, j = chunks.length;
  110.         for (i = 0; i < j; i++) {
  111.             s = chunks[i][0];
  112.             n = chunks[i][1];
  113.             if ((xj = Math.floor(d / s)) != 0)
  114.                 break;
  115.         }
  116.         fuzzy = xj == 1 ? '1 '+n : xj+' '+n+'s';
  117.         if (i + 1 < j) {
  118.             s2 = chunks[i + 1][0];
  119.             n2 = chunks[i + 1][1];
  120.             if ( ((xj2 = Math.floor((d - (s * xj)) / s2)) != 0) )
  121.                 fuzzy += (xj2 == 1) ? ' + 1 '+n2 : ' + '+xj2+' '+n2+'s';
  122.         }
  123.         fuzzy += ' ago';
  124.         return fuzzy;
  125.     }
  126.  
  127. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment