-
Notifications
You must be signed in to change notification settings - Fork 213
/
jquery.socialist.min.js
8 lines (8 loc) · 14.7 KB
/
jquery.socialist.min.js
1
2
3
4
5
6
7
8
/**
* jQuery.socialist - social media plugin
* ---
* @author Carol Skelly (http://iatek.com)
* @version 1.01
* @license MIT license (http://opensource.org/licenses/mit-license.php)
* ---
*/(function($,window,document,undefined){$.fn.socialist=function(method){var methods={init:function(e){this.socialist.settings=$.extend({},this.socialist.defaults,e);var t=this.socialist.settings.networks,n=this.socialist.settings,r=[],i=[];return this.each(function(){var e=$(this),s,o=e.is(":visible");e.addClass("socialist-loader");if(n.feed)i.push(helpers.doRequest(n.feed,"json",function(e){var t=$("<div></div>");for(var i=0;i<e.data.length;i++){s=e.data[i];var o=$('<div class="socialist"></div>');o.addClass("socialist-"+s.api);o=helpers.buildItem(s,o,n.fields);o.appendTo(t)}r.push(t)},null,n));else for(var u=0;u<t.length;u++){s=t[u];var a=helpers.networkDefs[s.name];a.cb=function(e){r.push(e)};var f=a.url;f=f.replace("|id|",encodeURIComponent(s.id));f=f.replace("|areaName|",s.areaName);f=f.replace("|apiKey|",s.apiKey);f=f.replace("|num|",n.maxResults);i.push(helpers.doRequest(f,a.dataType,a.cb,a.parser,n))}$.when.apply($,i).always(function(){for(var t=0;t<r.length;t++)r[t].children().appendTo(e);n.isotope?e.imagesLoaded(function(){e.isotope({animationEngine:"jquery"});e.removeClass("socialist-loader");n.random&&e.isotope("shuffle",function(){})}):e.removeClass("socialist-loader")},function(){})})}},helpers={parseResults:function(apiParser,data,settings){var container=$("<div></div>");apiParser.resultsSelector=apiParser.resultsSelector.replace("|num|",settings.maxResults);$.each(eval(apiParser.resultsSelector),function(i,item){var $elem=$(item),heading,txt,linkHref,imgSrc,imgHref,imgAlt,date;try{if(eval(apiParser.preCondition)){var $div=$('<div class="socialist"></div>');$div.addClass("socialist-"+apiParser.name);settings.fixed&&$div.addClass("socialist-fixed");settings.theme&&$div.addClass("socialist-"+settings.theme);settings.size&&$div.addClass("socialist-"+settings.size);settings.isotope||$div.addClass("socialist-simple");settings.width&&$div.css("width",settings.width);settings.margin&&$div.css("margin",settings.margin);settings.border&&$div.css("border",settings.border);settings.padding&&$div.css("padding",settings.padding);apiParser.headingSelector!==null?heading=helpers.shorten(helpers.stripHtml(eval(apiParser.headingSelector)),settings.headingLength):heading=apiParser.heading;txt=eval(apiParser.txtSelector);txt!==null?txt=helpers.shorten(txt,settings.textLength):txt="";linkHref="#";if(apiParser.imgSrcSelector===null)imgSrc=apiParser.imgSrc;else{imgSrc=eval(apiParser.imgSrcSelector);imgSrc!==null&&apiParser.imgSrcProcessor!==null?imgSrc=eval(apiParser.imgSrcProcessor):imgSrc===null&&(imgSrc="")}apiParser.imgHrefSelector===null?imgHref=apiParser.imgHref:imgHref=eval(apiParser.imgHrefSelector);apiParser.imgAltSelector!==null&&(imgAlt=eval(apiParser.imgAltSelector));date=eval(apiParser.dateSelector);if(typeof date=="undefined"||date===null)date="";var itemObj={api:apiParser.name,heading:heading,txt:txt,img:{src:imgSrc,href:imgHref,alt:imgAlt},link:{href:linkHref,title:imgAlt},date:date};$div=helpers.buildItem(itemObj,$div,settings.fields);$div.appendTo(container)}}catch(e){}});return container},doRequest:function(e,t,n,r,i){return $.ajax({url:e,type:"GET",dataType:t,success:function(e){r?n($(helpers.parseResults(r,e,i))):n(e)},error:function(e){n($("<div></div>"))}})},buildItem:function(e,t,n){var r=$('<div class="head"/>'),i=$('<div class="source"></div>'),s=$('<a href="'+e.img.href+'" title="'+e.link.title+'"></a>'),o=$("<div/>"),u=$('<div class="api"></div>'),a=$('<a href="'+e.img.href+'"></a>'),f=$('<div class="content"/>'),l=$("<div>"+e.txt+" </div>"),c=$('<a href="'+e.img.href+'" title="'+e.link.title+'"></a>'),h=$('<image src="'+e.img.src+'" alt="'+helpers.stripHtml(e.img.alt)+'">'),p=$('<div class="share"><a href="#" title='+e.api+'>fb</a>|<a href="#" class="x">tw</a></div>'),d=$('<div class="date"/>'),v=$('<div class="foot"/>');if(n.indexOf("image")!=-1&&e.img.src){h.appendTo(c);c.appendTo(f)}(n.indexOf("text")!=-1||typeof e.img.src=="undefined")&&l.appendTo(f);(n.indexOf("text")!=-1||n.indexOf("image")!=-1)&&f.appendTo(t);i.appendTo(v);s.text(e.heading);if(n.indexOf("source")!=-1){s.appendTo(o);o.appendTo(i);a.appendTo(u);u.appendTo(v);i.appendTo(v)}else s.appendTo(l);if(n.indexOf("date")!=-1){d.text(e.date);d.appendTo(o)}(n.indexOf("source")!=-1||n.indexOf("date")!=-1)&&v.appendTo(t);return t},networkDefs:{rss:{url:"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=|num|&callback=?&q=|id|",dataType:"json",parser:{name:"rss",resultsSelector:"data.responseData.feed.entries",heading:"RSS",headingSelector:"data.responseData.feed.title",txtSelector:"item.title",dateSelector:"item.publishedDate.substring(0,17)",imgSrc:null,imgSrcSelector:"$(item.content).find(\"img:lt(1)\").attr('src')",imgSrcProcessor:null,imgHref:"",imgHrefSelector:"$(item.content).find(\"img:lt(1)\").parent().attr('href')||$(item.content).find(\"a:lt(1)\").attr('href')",imgAltSelector:"item.contentSnippet",link:"",linkSelector:null,linkTipSelector:"item.contentSnippet",preProcessor:null,preCondition:"$(item.content).find(\"img[src]:contains('http')\")"}},facebook:{url:"http://graph.facebook.com/|id|/photos?limit=|num|",img:"",dataType:"json",parser:{name:"facebook",resultsSelector:"data.data",heading:"Facebook",headingSelector:"item.from.name",txtSelector:"item.from.name",dateSelector:"helpers.timeAgo(item.created_time)",imgSrcSelector:"(item.images[2].source)||'/spacer.gif'",imgSrcProcessor:null,imgHrefSelector:"item.link",imgAltSelector:"item.from.name.substring(0,12)",link:"#",preProcessor:null,preCondition:"true"}},youtube:{url:"https://gdata.youtube.com/feeds/api/users/|id|/uploads?alt=json&max-results=|num|",dataType:"jsonp",img:"",parser:{name:"youtube",resultsSelector:"data.feed.entry",heading:"YouTube",headingSelector:"item.title.$t",txtSelector:"item.content.$t",dateSelector:"helpers.timeAgo(item.updated.$t)",imgSrcSelector:"item.media$group.media$thumbnail[0].url",imgSrcProcessor:null,imgHrefSelector:"item.link[0].href",imgAltSelector:"item.title.$t",preProcessor:null,preCondition:"true"}},twitter:{url:"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=|id|&count=|num|",dataType:"jsonp",img:"",parser:{name:"twitter",resultsSelector:"data",heading:"Twitter",headingSelector:"item.user.screen_name",txtSelector:"item.text",dateSelector:"helpers.timeAgo(helpers.fixTwitterDate(item.created_at))",imgSrcSelector:"(item.user.profile_image_url)||'/assets/spacer.gif'",imgSrcProcessor:null,imgHrefSelector:"((item.entities.urls[0]||{urls:''}).url)||'http://www.twitter.com/'+item.user.screen_name",imgAltSelector:"item.user.screen_name",link:"#",preProcessor:null,preCondition:"true"}},linkedin:{url:"http://www.linkedin.com/company/|id|/",img:"",dataType:"text",parser:{name:"linkedin",resultsSelector:"$(data.responseText).find('div.feed-body:lt(|num|)')",heading:"LinkedIn",headingSelector:"$elem.find('a:first').text()",txtSelector:"($elem.find('a:last').text())||$elem.find('p.share-desc').html()",imgSrcSelector:"$elem.find('.feed-photo').attr('src')||$elem.find('.has-photo img').attr('src')",imgSrcProcessor:null,imgHrefSelector:"$elem.find('a').attr('href')",imgAltSelector:"$elem.find('a').text()",dateSelector:"$elem.find('span.nus-timestamp').text()",link:"#",preProcessor:null,preCondition:"true"}},tumblr:{url:"http://|id|.tumblr.com/api/read/json?callback=helpers.cb&num=|num|",dataType:"jsonp",parser:{name:"tumblr",resultsSelector:"data.posts",heading:"tumblr",headingSelector:"(item['photo-caption'])||data.tumblelog.title",txtSelector:"(helpers.stripHtml(item['regular-body']))||(item['link-description'])||(item['regular-title'])||item['photo-caption']",dateSelector:"item.date",imgSrcSelector:"(item['photo-url-250'])||$(item['link-description']).find('img').attr('src')",imgSrcProcessor:null,imgHrefSelector:"item.url",imgAltSelector:"(item['regular-title'])||item.tags.toString()",link:"#",preProcessor:null,preCondition:"true"}},flickr:{url:"http://api.flickr.com/services/rest/?extras=tags%2Cdescription%2Cdate_upload&nojsoncallback=1&api_key=|apiKey|&method=flickr.people.getPublicPhotos&format=json&per_page=|num|&user_id=|id|",dataType:"json",parser:{name:"flickr",resultsSelector:"data.photos.photo",heading:"Flickr",headingSelector:"item.title",dateSelector:"new Date(item.dateupload)",txtSelector:"(item.description._content)||item.tags",imgSrcSelector:"'http://farm' + item.farm + '.staticflickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_n.jpg'",imgHrefSelector:'"http://flickr.com/photos/" + item.owner + "/" + item.id + ""',imgAltSelector:"item.title",imgSrcProcessor:null,preCondition:"true"}},googleplus:{url:"https://plus.google.com/|id|",dataType:"text",parser:{name:"google",resultsSelector:"$(data.responseText).find('div.zg:lt(|num|)')",heading:"Google+",headingSelector:"$elem.find('a.YF').text()",txtSelector:"$elem.find('div.XF').text()",imgSrcSelector:"$elem.find('a.Mn img').attr('src')",imgSrcProcessor:null,imgHrefSelector:"$elem.find('a.YF').attr('href')",imgAltSelector:"($elem.find('a.Mn img').attr('alt'))||'Google'",dateSelector:"$elem.parents('div.qf').find('a.Bf').text()",link:"#",preProcessor:null,preCondition:"true"}},pinterest:{url:"http://pinterest.com/|id|/",dataType:"text",parser:{name:"pinterest",resultsSelector:"$(data.responseText).find('div.pin:lt(|num|),a.PinImage:lt(|num|)')",heading:"Pinterest",headingSelector:"($elem.find('p.NoImage a').text())||$elem.find('.serif a').text()",txtSelector:"($elem.find('img').attr('alt'))||$elem.find('.serif a').text()",imgSrcSelector:"($elem.find('img.PinImageImg').attr('src'))||$elem.find('span.cover img').attr('src')",imgSrcProcessor:null,imgHrefSelector:"\"http://pinterest.com\"+(($elem.find('a.link').attr('href'))||$elem.find('a.PinImage').attr('href'))",imgAltSelector:"($elem.find('img').attr('alt'))||'Pinterest'",link:"#",preProcessor:null,preCondition:"true"}},quora:{url:"http://www.quora.com/|id|/feed/",dataType:"text",parser:{name:"quora",resultsSelector:"$(data.responseText).find('div.feed_item:lt(|num|)')",heading:"Quora",headingSelector:"$elem.find('a.question_link').text()",txtSelector:"($elem.find('div.truncated_q_text:first-child').text())",imgSrcSelector:"",imgSrcProcessor:null,imgHrefSelector:"$elem.find('a').attr('href')",imgAltSelector:"$elem.find('a').text()",dateSelector:"$elem.find('span.timestamp').text()",link:"#",preProcessor:null,preCondition:"true"}},instagram:{url:"https://api.instagram.com/v1/tags/|id|/media/recent?client_id=|apiKey|",dataType:"jsonp",parser:{name:"instagram",resultsSelector:"data.data",heading:"Instagr.am",headingSelector:"item.caption.text",txtSelector:"item.caption.text",imgSrcSelector:"(item.images.low_resolution.url)||'/assets/spacer.gif'",imgHrefSelector:"item.link",imgSrcProcessor:null,imgAltSelector:"item.caption.text",dateSelector:null,link:"#",linkSelector:null,preCondition:"true",locationSelector:"item.location",tagsSelector:"item.tags"}},craigslist:{url:"http://|areaName|.craigslist.org/|id|",dataType:"text",parser:{name:"craigslist",resultsSelector:"$(data.responseText).find(\"p.row:contains('pic'):lt(|num|)\")",heading:"Craigslist",headingSelector:null,txtSelector:"helpers.fixCase($elem.find('a,font').text())",imgSrcSelector:"\"http://images.craigslist.org/\"+$elem.find('span.i').attr('data-id')",imgSrcProcessor:"imgSrc.replace('/thumb/',\"\")",imgHrefSelector:"$elem.find('a').attr('href')",imgAltSelector:"$elem.find('span.itempp').text()",link:"#",preProcessor:null,preCondition:"true"}},vimeo:{url:"http://vimeo.com/api/v2/|id|/videos.json",dataType:"jsonp",parser:{name:"vimeo",resultsSelector:"data",heading:"Vimeo",headingSelector:"item.title",txtSelector:"item.description",dateSelector:"helpers.timeAgo(item.upload_date)",imgSrcSelector:"(item.thumbnail_medium)||'/assets/spacer.gif'",imgSrcProcessor:null,imgHrefSelector:"'http://vimeo.com/'+item.id",imgAltSelector:"item.title",link:"#",preProcessor:null,preCondition:"true"}}},cb:function(e){return e},fixCase:function(e){if(e===null)return;return e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()},shorten:function(e,t){if(typeof e=="undefined"||e===null)return;return e.length>t?e.substring(0,t)+"..":e},stripHtml:function(e){if(typeof e=="undefined"||e===null)return;return e.replace(/(<([^>]+)>)|nbsp;|\s{2,}|/ig,"")},timeAgo:function(e){e=e.replace("+0000","Z");var t=[[60,"just now",1],[120,"1 minute ago","1 minute from now"],[3600,"minutes",60],[7200,"1 hour ago","1 hour from now"],[86400,"hours",3600],[172800,"yesterday","tomorrow"],[604800,"days",86400],[1209600,"last week","next week"],[2419200,"weeks",604800],[4838400,"last month","next month"],[29030400,"months",2419200],[58060800,"last year","next year"],[290304e4,"years",29030400],[580608e4,"last century","next century"],[580608e5,"centuries",290304e4]],n=(""+e).replace(/-/g,"/").replace(/[TZ]/g," ").replace(/^\s\s*/,"").replace(/\s\s*$/,"");n.substr(n.length-4,1)=="."&&(n=n.substr(0,n.length-4));var r=(new Date-new Date(n))/1e3,i="ago",s=1;if(r<0){r=Math.abs(r);i="from now";s=2}var o=0,u;while(u=t[o++])if(r<u[0])return typeof u[2]=="string"?u[s]:Math.floor(r/u[2])+" "+u[1]+" "+i;return n},fixTwitterDate:function(e){var t=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],n=/\s/,r,i,s,o,u,a;e=e.split(n);for(var f=0;f<e.length;f++){r=e[0];i=e[2];s=e[1];o=0+t.indexOf(s)+1;u=e[5];a=e[3]}e=u+"-"+o+"-"+i+"T"+a+"Z";if(e!==undefined)return e}};if(methods[method])return methods[method].apply(this,Array.prototype.slice.call(arguments,1));if(typeof method=="object"||!method)return methods.init.apply(this,arguments);$.error('Method "'+method+'" does not exist in social plugin')};$.fn.socialist.defaults={networks:[{name:"linkedin",id:"iatek-llc"},{name:"facebook",id:"in1dotcom"},{name:"twitter",id:"in1dotcom"}],random:!0,isotope:!0,headingLength:31,textLength:160,maxResults:7,autoShow:!0,fields:["source","heading","text","date","image","followers","likes","share"]};$.fn.socialist.settings={}})(jQuery);Array.prototype.indexOf||(Array.prototype.indexOf=function(e,t){for(var n=t||0,r=this.length;n<r;n++)if(this[n]===e)return n;return-1});jQuery.ajax=function(e){function o(e){return!r.test(e)&&/:\/\//.test(e)}var t=location.protocol,n=location.hostname,r=RegExp(t+"//"+n),i="http"+(/^https/.test(t)?"s":"")+"://query.yahooapis.com/v1/public/yql?callback=?",s='select * from html where url="{URL}" and xpath="*"';return function(t){var n=t.url;if(/get/i.test(t.type)&&!/json/i.test(t.dataType)&&o(n)){t.url=i;t.dataType="json";t.data={q:s.replace("{URL}",n+(t.data?(/\?/.test(n)?"&":"?")+jQuery.param(t.data):"")),format:"xml"};if(!t.success&&t.complete){t.success=t.complete;delete t.complete}t.success=function(e){return function(t){e&&e.call(this,{responseText:(t.results[0]||"").replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi,"")},"success")}}(t.success)}return e.apply(this,arguments)}}(jQuery.ajax);