-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsocialight.min.js
2 lines (2 loc) · 5.69 KB
/
socialight.min.js
1
2
/*! Socialight v2.2.3 | (c) 2016 Pedro Rogério | */
!function(t,e){"use strict";"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.Socialight=e()}(this,function(){"use strict";Array.prototype.includes||(Array.prototype.includes=function(t){var e,n,i,o=Object(this),r=parseInt(o.length)||0;if(0===r)return!1;for(n=parseInt(arguments[1])||0,n>=0?i=n:(i=r+n,i<0&&(i=0));i<r;){if(e=o[i],t===e||t!==t&&e!==e)return!0;i++}return!1});var t=function(){return this&&this instanceof t?(this.container=document.querySelectorAll("[data-socialight-url]"),this.channels=["facebook","twitter","linkedin","googleplus","pinterest","buffer"],void this.ready()):new t};return t.prototype={post:function(t,e,n,i){var o=new XMLHttpRequest;o.open("POST",t,!0),o.setRequestHeader("X-Requested-With","XMLHttpRequest"),o.setRequestHeader("Content-Type","application/json"),o.onreadystatechange=function(){if(4===this.readyState)if(this.status>=200&&this.status<300||304===this.status){var t=JSON.parse(this.responseText);n.call(this,t)}else i.call(this,this.responseText)},o.send(e),o=null},jsonp:function(t,e,n){var i,o,r,s;i="jsonp_"+Date.now()+"_"+Math.ceil(1e5*Math.random()),o=document.head||document.getElementsByTagName("head")[0],s=document.createElement("script"),s.type="text/javascript",r=s.cloneNode(),r.src=t+(t.indexOf("?")>=0?"&":"?")+"callback="+i,r.async=!0,o.appendChild(r),window[i]=function(t){e.call(n||window,t),o.removeChild(r),r=null,delete this.name}.bind(this)},abbrNum:function(t,e){e=Math.pow(10,e);var n,i,o=["k","m","b","t"];for(i=o.length-1;i>=0;i-=1)if(n=Math.pow(10,3*(i+1)),n<=t){t=Math.round(t*e/n)/e,1e3===t&&i<o.length-1&&(t=1,i+=1),t+=o[i];break}return t},each:function(t,e){for(var n=0,i=t.length;n<i;)e(t[n],n),n+=1},isJson:function(t){try{var e=JSON.parse(t);if(e&&"object"==typeof e&&null!==e)return e}catch(t){console.log('%c You must pass a valid JSON in attribute "data-socialight-url". View a example here: https://github.com/pinceladasdaweb/Socialight/blob/master/example/index.html#L27',"background: red; color: white")}return!1},template:function(t,e){var n;for(n in e)e.hasOwnProperty(n)&&(t=t.replace(new RegExp("{"+n+"}","g"),e[n]));return t},getAttributes:function(t){return{url:encodeURIComponent(t.parentNode.getAttribute("data-socialight-url")),title:encodeURIComponent(t.parentNode.getAttribute("data-socialight-title")),text:encodeURIComponent(t.parentNode.getAttribute("data-socialight-text")),image:encodeURIComponent(t.parentNode.getAttribute("data-socialight-image"))}},popup:function(t){t.addEventListener("click",function(t){t.preventDefault(),t.target&&"A"===t.target.nodeName&&window.open(t.target.href,"","toolbar=0,status=0,scrollbars=0,width=680,height=480")})},createChannels:function(){var t,e,n,i,o,r,s;for(i=0,s=this.container.length;i<s;i+=1)for(t=this.isJson(this.container[i].getAttribute("data-socialight-channels")),e=this.container[i].getAttribute("data-socialight-url"),this.popup(this.container[i]),o=0,r=t.length;o<r;o+=1)this.channels.includes(t[o])&&(n=this.template('<a data-socialight-channel="{channel}" data-socialight-url="{url}"></a>',{channel:t[o],url:e}),this.container[i].insertAdjacentHTML("beforeend",n))},facebook:function(){var t,e=document.querySelectorAll('[data-socialight-channel="facebook"]');this.each(e,function(e){t=this.getAttributes(e),e.setAttribute("href","https://www.facebook.com/sharer/sharer.php?u="+t.url),this.jsonp("https://graph.facebook.com/?id="+t.url,function(t){e.textContent=this.abbrNum(t.share.share_count,1)}.bind(this))}.bind(this))},twitter:function(){var t,e=document.querySelectorAll('[data-socialight-channel="twitter"]');this.each(e,function(e){t=this.getAttributes(e),e.setAttribute("href","https://twitter.com/share?url="+t.url+"&text="+t.title),e.textContent="Tweet"}.bind(this))},googlePlus:function(){var t,e,n=document.querySelectorAll('[data-socialight-channel="googleplus"]');this.each(n,function(n){t=this.getAttributes(n),e=JSON.stringify({method:"pos.plusones.get",id:decodeURIComponent(t.url),params:{nolog:!0,id:decodeURIComponent(t.url),source:"widget",userId:"@viewer",groupId:"@self"},jsonrpc:"2.0",key:"p",apiVersion:"v1"}),n.setAttribute("href","https://plus.google.com/share?url="+t.url),this.post("https://clients6.google.com/rpc",e,function(t){n.textContent=this.abbrNum(t.result.metadata.globalCounts.count,1)}.bind(this),function(t){console.log(t)})}.bind(this))},linkedin:function(){var t,e=document.querySelectorAll('[data-socialight-channel="linkedin"]');this.each(e,function(e){t=this.getAttributes(e),e.setAttribute("href","https://www.linkedin.com/shareArticle?mini=true&url="+t.url+"&title="+t.title+"&summary="+t.text+"&source="+t.image),this.jsonp("https://www.linkedin.com/countserv/count/share?url="+t.url,function(t){e.textContent=this.abbrNum(t.count,1)}.bind(this))}.bind(this))},buffer:function(){var t,e=document.querySelectorAll('[data-socialight-channel="buffer"]');this.each(e,function(e){t=this.getAttributes(e),e.setAttribute("href","https://buffer.com/add?url="+t.url+"&text="+t.title),this.jsonp("https://api.bufferapp.com/1/links/shares.json?url="+t.url,function(t){e.textContent=this.abbrNum(t.shares,1)}.bind(this))}.bind(this))},pinterest:function(){var t,e=document.querySelectorAll('[data-socialight-channel="pinterest"]');this.each(e,function(e){t=this.getAttributes(e),e.setAttribute("href","https://pinterest.com/pin/create/button/?url="+t.url+"&media="+t.image+"&description="+t.title),this.jsonp("https://api.pinterest.com/v1/urls/count.json?&url="+t.url,function(t){e.textContent=this.abbrNum(t.count,1)}.bind(this))}.bind(this))},getCounters:function(){this.facebook(),this.twitter(),this.googlePlus(),this.linkedin(),this.buffer(),this.pinterest()},ready:function(){this.createChannels(),this.getCounters()}},t});