/*! * sharrre.com - make your sharing widget! * version: beta 1.3.5 * author: julien hany * license: mit http://en.wikipedia.org/wiki/mit_license or gplv2 http://en.wikipedia.org/wiki/gnu_general_public_license */ ;(function ( $, window, document, undefined ) { /* defaults ================================================== */ var pluginname = 'sharrre', defaults = { classname: 'sharrre', share: { googleplus: false, facebook: false, twitter: false, digg: false, delicious: false, stumbleupon: false, linkedin: false, pinterest: false }, sharetotal: 0, template: '', title: '', url: document.location.href, text: document.title, urlcurl: 'sharrre.php', //php script for google plus... count: {}, //counter by social network total: 0, //total of sharing shortertotal: true, //show total by k or m when number is to big enablehover: true, //disable if you want to personalize hover event with callback enablecounter: true, //disable if you just want use buttons enabletracking: false, //tracking with google analitycs hover: function(){}, //personalize hover event with this callback function hide: function(){}, //personalize hide event with this callback function click: function(){}, //personalize click event with this callback function render: function(){}, //personalize render event with this callback function buttons: { //settings for buttons googleplus : { //http://www.google.com/webmasters/+1/button/ url: '', //if you need to personnalize button url urlcount: false, //if you want to use personnalize button url on global counter size: 'medium', lang: 'en-us', annotation: '' }, facebook: { //http://developers.facebook.com/docs/reference/plugins/like/ url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter action: 'like', layout: 'button_count', width: '', send: 'false', faces: 'false', colorscheme: '', font: '', lang: 'en_us' }, twitter: { //http://twitter.com/about/resources/tweetbutton url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter count: 'horizontal', hashtags: '', via: '', related: '', lang: 'en' }, digg: { //http://about.digg.com/downloads/button/smart url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter type: 'diggcompact' }, delicious: { url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter size: 'medium' //medium or tall }, stumbleupon: { //http://www.stumbleupon.com/badges/ url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter layout: '1' }, linkedin: { //http://developer.linkedin.com/plugins/share-button url: '', //if you need to personalize url button urlcount: false, //if you want to use personnalize button url on global counter counter: '' }, pinterest: { //http://pinterest.com/about/goodies/ url: '', //if you need to personalize url button media: '', description: '', layout: 'horizontal' } } }, /* json url to get count number ================================================== */ urljson = { googleplus: "", //new fql method by sire facebook: "https://graph.facebook.com/fql?q=select%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20from%20link_stat%20where%20url=%27{url}%27&callback=?", //old method facebook: "http://graph.facebook.com/?id={url}&callback=?", //facebook : "http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getstats&urls={url}&format=json" twitter: "http://cdn.api.twitter.com/1/urls/count.json?url={url}&callback=?", digg: "http://services.digg.com/2.0/story.getinfo?links={url}&type=javascript&callback=?", delicious: 'http://feeds.delicious.com/v2/json/urlinfo/data?url={url}&callback=?', //stumbleupon: "http://www.stumbleupon.com/services/1.01/badge.getinfo?url={url}&format=jsonp&callback=?", stumbleupon: "", linkedin: "http://www.linkedin.com/countserv/count/share?format=jsonp&url={url}&callback=?", pinterest: "http://api.pinterest.com/v1/urls/count.json?url={url}&callback=?" }, /* load share buttons asynchronously ================================================== */ loadbutton = { googleplus : function(self){ var sett = self.options.buttons.googleplus; //$(self.element).find('.buttons').append('
'); $(self.element).find('.buttons').append(' '); window.___gcfg = { lang: self.options.buttons.googleplus.lang }; var loading = 0; if(typeof gapi === 'undefined' && loading == 0){ loading = 1; (function() { var po = document.createelement('script'); po.type = 'text/javascript'; po.async = true; po.src = '//apis.google.com/js/plusone.js'; var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(po, s); })(); } else{ gapi.plusone.go(); } }, facebook : function(self){ var sett = self.options.buttons.facebook; $(self.element).find('.buttons').append(' '); var loading = 0; if(typeof fb === 'undefined' && loading == 0){ loading = 1; (function(d, s, id) { var js, fjs = d.getelementsbytagname(s)[0]; if (d.getelementbyid(id)) {return;} js = d.createelement(s); js.id = id; js.src = '//connect.facebook.net/'+sett.lang+'/all.js#xfbml=1'; fjs.parentnode.insertbefore(js, fjs); }(document, 'script', 'facebook-jssdk')); } else{ fb.xfbml.parse(); } }, twitter : function(self){ var sett = self.options.buttons.twitter; $(self.element).find('.buttons').append(' '); var loading = 0; if(typeof twttr === 'undefined' && loading == 0){ loading = 1; (function() { var twitterscripttag = document.createelement('script'); twitterscripttag.type = 'text/javascript'; twitterscripttag.async = true; twitterscripttag.src = '//platform.twitter.com/widgets.js'; var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(twitterscripttag, s); })(); } else{ $.ajax({ url: '//platform.twitter.com/widgets.js', datatype: 'script', cache:true}); //http://stackoverflow.com/q/6536108 } }, digg : function(self){ var sett = self.options.buttons.digg; $(self.element).find('.buttons').append(' '); var loading = 0; if(typeof __dbw === 'undefined' && loading == 0){ loading = 1; (function() { var s = document.createelement('script'), s1 = document.getelementsbytagname('script')[0]; s.type = 'text/javascript'; s.async = true; s.src = '//widgets.digg.com/buttons.js'; s1.parentnode.insertbefore(s, s1); })(); } }, delicious : function(self){ if(self.options.buttons.delicious.size == 'tall'){//tall var css = 'width:50px;', csscount = 'height:35px;width:50px;font-size:15px;line-height:35px;', cssshare = 'height:18px;line-height:18px;margin-top:3px;'; } else{//medium var css = 'width:93px;', csscount = 'float:right;padding:0 3px;height:20px;width:26px;line-height:20px;', cssshare = 'float:left;height:20px;line-height:20px;'; } var count = self.shortertotal(self.options.count.delicious); if(typeof count === "undefined"){ count = 0; } $(self.element).find('.buttons').append( ' '); $(self.element).find('.delicious').on('click', function(){ self.openpopup('delicious'); }); }, stumbleupon : function(self){ var sett = self.options.buttons.stumbleupon; $(self.element).find('.buttons').append(' '); var loading = 0; if(typeof stmblpn === 'undefined' && loading == 0){ loading = 1; (function() { var li = document.createelement('script');li.type = 'text/javascript';li.async = true; li.src = '//platform.stumbleupon.com/1/widgets.js'; var s = document.getelementsbytagname('script')[0];s.parentnode.insertbefore(li, s); })(); s = window.settimeout(function(){ if(typeof stmblpn !== 'undefined'){ stmblpn.processwidgets(); clearinterval(s); } },500); } else{ stmblpn.processwidgets(); } }, linkedin : function(self){ var sett = self.options.buttons.linkedin; $(self.element).find('.buttons').append(''); var loading = 0; if(typeof window.in === 'undefined' && loading == 0){ loading = 1; (function() { var li = document.createelement('script');li.type = 'text/javascript';li.async = true; li.src = '//platform.linkedin.com/in.js'; var s = document.getelementsbytagname('script')[0];s.parentnode.insertbefore(li, s); })(); } else{ window.in.init(); } }, pinterest : function(self){ var sett = self.options.buttons.pinterest; $(self.element).find('.buttons').append(' '); (function() { var li = document.createelement('script');li.type = 'text/javascript';li.async = true; li.src = '//assets.pinterest.com/js/pinit.js'; var s = document.getelementsbytagname('script')[0];s.parentnode.insertbefore(li, s); })(); } }, /* tracking for google analytics ================================================== */ tracking = { googleplus: function(){}, facebook: function(){ //console.log('facebook'); fb = window.setinterval(function(){ if (typeof fb !== 'undefined') { fb.event.subscribe('edge.create', function(targeturl) { _gaq.push(['_tracksocial', 'facebook', 'like', targeturl]); }); fb.event.subscribe('edge.remove', function(targeturl) { _gaq.push(['_tracksocial', 'facebook', 'unlike', targeturl]); }); fb.event.subscribe('message.send', function(targeturl) { _gaq.push(['_tracksocial', 'facebook', 'send', targeturl]); }); //console.log('ok'); clearinterval(fb); } },1000); }, twitter: function(){ //console.log('twitter'); tw = window.setinterval(function(){ if (typeof twttr !== 'undefined') { twttr.events.bind('tweet', function(event) { if (event) { _gaq.push(['_tracksocial', 'twitter', 'tweet']); } }); //console.log('ok'); clearinterval(tw); } },1000); }, digg: function(){ //if somenone find a solution, mail me ! /*$(this.element).find('.digg').on('click', function(){ _gaq.push(['_tracksocial', 'digg', 'add']); });*/ }, delicious: function(){}, stumbleupon: function(){}, linkedin: function(){ function linkedinshare() { _gaq.push(['_tracksocial', 'linkedin', 'share']); } }, pinterest: function(){ //if somenone find a solution, mail me ! } }, /* popup for each social network ================================================== */ popup = { googleplus: function(opt){ window.open("https://plus.google.com/share?hl="+opt.buttons.googleplus.lang+"&url="+encodeuricomponent((opt.buttons.googleplus.url !== '' ? opt.buttons.googleplus.url : opt.url)), "", "toolbar=0, status=0, width=900, height=500"); }, facebook: function(opt){ window.open("http://www.facebook.com/sharer/sharer.php?u="+encodeuricomponent((opt.buttons.facebook.url !== '' ? opt.buttons.facebook.url : opt.url))+"&t="+opt.text+"", "", "toolbar=0, status=0, width=900, height=500"); }, twitter: function(opt){ window.open("https://twitter.com/intent/tweet?text="+encodeuricomponent(opt.text)+"&url="+encodeuricomponent((opt.buttons.twitter.url !== '' ? opt.buttons.twitter.url : opt.url))+(opt.buttons.twitter.via !== '' ? '&via='+opt.buttons.twitter.via : ''), "", "toolbar=0, status=0, width=650, height=360"); }, digg: function(opt){ window.open("http://digg.com/tools/diggthis/submit?url="+encodeuricomponent((opt.buttons.digg.url !== '' ? opt.buttons.digg.url : opt.url))+"&title="+opt.text+"&related=true&style=true", "", "toolbar=0, status=0, width=650, height=360"); }, delicious: function(opt){ window.open('http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeuricomponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url))+'&title='+opt.text, 'delicious', 'toolbar=no,width=550,height=550'); }, stumbleupon: function(opt){ window.open('http://www.stumbleupon.com/badge/?url='+encodeuricomponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url)), 'stumbleupon', 'toolbar=no,width=550,height=550'); }, linkedin: function(opt){ window.open('https://www.linkedin.com/cws/share?url='+encodeuricomponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url))+'&token=&isframed=true', 'linkedin', 'toolbar=no,width=550,height=550'); }, pinterest: function(opt){ window.open('http://pinterest.com/pin/create/button/?url='+encodeuricomponent((opt.buttons.pinterest.url !== '' ? opt.buttons.pinterest.url : opt.url))+'&media='+encodeuricomponent(opt.buttons.pinterest.media)+'&description='+opt.buttons.pinterest.description, 'pinterest', 'toolbar=no,width=700,height=300'); } }; /* plugin constructor ================================================== */ function plugin( element, options ) { this.element = element; this.options = $.extend( true, {}, defaults, options); this.options.share = options.share; //simple solution to allow order of buttons this._defaults = defaults; this._name = pluginname; this.init(); }; /* initialization method ================================================== */ plugin.prototype.init = function () { var self = this; if(this.options.urlcurl !== ''){ urljson.googleplus = this.options.urlcurl + '?url={url}&type=googleplus'; // php script for googleplus... urljson.stumbleupon = this.options.urlcurl + '?url={url}&type=stumbleupon'; // php script for stumbleupon... } $(this.element).addclass(this.options.classname); //add class //html5 custom data if(typeof $(this.element).data('title') !== 'undefined'){ this.options.title = $(this.element).attr('data-title'); } if(typeof $(this.element).data('url') !== 'undefined'){ this.options.url = $(this.element).data('url'); } if(typeof $(this.element).data('text') !== 'undefined'){ this.options.text = $(this.element).data('text'); } //how many social website have been selected $.each(this.options.share, function(name, val) { if(val === true){ self.options.sharetotal ++; } }); if(self.options.enablecounter === true){ //if for some reason you don't need counter //get count of social share that have been selected $.each(this.options.share, function(name, val) { if(val === true){ //self.getsocialjson(name); try { self.getsocialjson(name); } catch(e){ } } }); } else if(self.options.template !== ''){ //for personalized button (with template) this.options.render(this, this.options); } else{ // if you want to use official button like example 3 or 5 this.loadbuttons(); } //add hover event $(this.element).hover(function(){ //load social button if enable and 1 time if($(this).find('.buttons').length === 0 && self.options.enablehover === true){ self.loadbuttons(); } self.options.hover(self, self.options); }, function(){ self.options.hide(self, self.options); }); //click event $(this.element).click(function(){ self.options.click(self, self.options); return false; }); }; /* loadbuttons methode ================================================== */ plugin.prototype.loadbuttons = function () { var self = this; $(this.element).append(''); $.each(self.options.share, function(name, val) { if(val == true){ loadbutton[name](self); if(self.options.enabletracking === true){ //add tracking tracking[name](); } } }); }; /* getsocialjson methode ================================================== */ plugin.prototype.getsocialjson = function (name) { var self = this, count = 0, url = urljson[name].replace('{url}', encodeuricomponent(this.options.url)); if(this.options.buttons[name].urlcount === true && this.options.buttons[name].url !== ''){ url = urljson[name].replace('{url}', this.options.buttons[name].url); } //console.log('name : ' + name + ' - url : '+url); //debug if(url != '' && self.options.urlcurl !== ''){ //urlcurl = '' if you don't want to used php script but used social button $.getjson(url, function(json){ if(typeof json.count !== "undefined"){ //googleplus, stumbleupon, twitter, pinterest and digg var temp = json.count + ''; temp = temp.replace('\u00c2\u00a0', ''); //remove google plus special chars count += parseint(temp, 10); } //get the fb total count (shares, likes and more) else if(json.data && json.data.length > 0 && typeof json.data[0].total_count !== "undefined"){ //facebook total count count += parseint(json.data[0].total_count, 10); } else if(typeof json[0] !== "undefined"){ //delicious count += parseint(json[0].total_posts, 10); } else if(typeof json[0] !== "undefined"){ //stumbleupon } self.options.count[name] = count; self.options.total += count; self.renderer(); self.rendererperso(); //console.log(json); //debug }) .error(function() { self.options.count[name] = 0; self.rendererperso(); }); } else{ self.renderer(); self.options.count[name] = 0; self.rendererperso(); } }; /* launch render methode ================================================== */ plugin.prototype.rendererperso = function () { //check if this is the last social website to launch render var sharecount = 0; for (e in this.options.count) { sharecount++; } if(sharecount === this.options.sharetotal){ this.options.render(this, this.options); } }; /* render methode ================================================== */ plugin.prototype.renderer = function () { var total = this.options.total, template = this.options.template; if(this.options.shortertotal === true){ //format number like 1.2k or 5m total = this.shortertotal(total); } if(template !== ''){ //if there is a template template = template.replace('{total}', total); $(this.element).html(template); } else{ //template by defaults $(this.element).html( '