forked from pinceladasdaweb/Socialight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocialight.min.js
13 lines (12 loc) · 9.44 KB
/
socialight.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
--------------------------------
Socialight
--------------------------------
+ Get Social Network Share Counts with Vanilla JS
+ https://github.com/pinceladasdaweb/Socialight
+ version 0.0.5
+ Copyright 2014 Pedro Rogerio
+ Licensed under the MIT license
+ Documentation: https://github.com/pinceladasdaweb/Socialight
*/
Array.prototype.forEach||(Array.prototype.forEach=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if("function"!=typeof a)throw TypeError();var d,e=arguments[1];for(d=0;c>d;d++)d in b&&a.call(e,b[d],d,b)}),Function.prototype.bind||(Function.prototype.bind=function(a){function b(){}if("function"!=typeof this)throw TypeError("Bind must be called on a function");var c=[].slice,d=c.call(arguments,1),e=this,f=function(){return e.apply(this instanceof b?this:a||{},d.concat(c.call(arguments)))};return b.prototype=e.prototype,f.prototype=new b,f}),!function(){var a,b,c,d;!function(){var e={},f={};a=function(a,b,c){e[a]={deps:b,callback:c}},d=c=b=function(a){function c(b){if("."!==b.charAt(0))return b;for(var c=b.split("/"),d=a.split("/").slice(0,-1),e=0,f=c.length;f>e;e++){var g=c[e];if(".."===g)d.pop();else{if("."===g)continue;d.push(g)}}return d.join("/")}if(d._eak_seen=e,f[a])return f[a];if(f[a]={},!e[a])throw new Error("Could not find module "+a);for(var g,h=e[a],i=h.deps,j=h.callback,k=[],l=0,m=i.length;m>l;l++)k.push("exports"===i[l]?g={}:b(c(i[l])));var n=j.apply(this,k);return f[a]=g||n}}(),a("promise/all",["./utils","exports"],function(a,b){"use strict";function c(a){var b=this;if(!d(a))throw new TypeError("You must pass an array to all.");return new b(function(b,c){function d(a){return function(b){f(a,b)}}function f(a,c){h[a]=c,0===--i&&b(h)}var g,h=[],i=a.length;0===i&&b([]);for(var j=0;j<a.length;j++)g=a[j],g&&e(g.then)?g.then(d(j),c):f(j,g)})}var d=a.isArray,e=a.isFunction;b.all=c}),a("promise/asap",["exports"],function(a){"use strict";function b(){return function(){process.nextTick(e)}}function c(){var a=0,b=new i(e),c=document.createTextNode("");return b.observe(c,{characterData:!0}),function(){c.data=a=++a%2}}function d(){return function(){j.setTimeout(e,1)}}function e(){for(var a=0;a<k.length;a++){var b=k[a],c=b[0],d=b[1];c(d)}k=[]}function f(a,b){var c=k.push([a,b]);1===c&&g()}var g,h="undefined"!=typeof window?window:{},i=h.MutationObserver||h.WebKitMutationObserver,j="undefined"!=typeof global?global:void 0===this?window:this,k=[];g="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?b():i?c():d(),a.asap=f}),a("promise/config",["exports"],function(a){"use strict";function b(a,b){return 2!==arguments.length?c[a]:void(c[a]=b)}var c={instrument:!1};a.config=c,a.configure=b}),a("promise/polyfill",["./promise","./utils","exports"],function(a,b,c){"use strict";function d(){var a;a="undefined"!=typeof global?global:"undefined"!=typeof window&&window.document?window:self;var b="Promise"in a&&"resolve"in a.Promise&&"reject"in a.Promise&&"all"in a.Promise&&"race"in a.Promise&&function(){var b;return new a.Promise(function(a){b=a}),f(b)}();b||(a.Promise=e)}var e=a.Promise,f=b.isFunction;c.polyfill=d}),a("promise/promise",["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],function(a,b,c,d,e,f,g,h){"use strict";function i(a){if(!v(a))throw new TypeError("You must pass a resolver function as the first argument to the promise constructor");if(!(this instanceof i))throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");this._subscribers=[],j(a,this)}function j(a,b){function c(a){o(b,a)}function d(a){q(b,a)}try{a(c,d)}catch(e){d(e)}}function k(a,b,c,d){var e,f,g,h,i=v(c);if(i)try{e=c(d),g=!0}catch(j){h=!0,f=j}else e=d,g=!0;n(b,e)||(i&&g?o(b,e):h?q(b,f):a===D?o(b,e):a===E&&q(b,e))}function l(a,b,c,d){var e=a._subscribers,f=e.length;e[f]=b,e[f+D]=c,e[f+E]=d}function m(a,b){for(var c,d,e=a._subscribers,f=a._detail,g=0;g<e.length;g+=3)c=e[g],d=e[g+b],k(b,c,d,f);a._subscribers=null}function n(a,b){var c,d=null;try{if(a===b)throw new TypeError("A promises callback cannot return that same promise.");if(u(b)&&(d=b.then,v(d)))return d.call(b,function(d){return c?!0:(c=!0,void(b!==d?o(a,d):p(a,d)))},function(b){return c?!0:(c=!0,void q(a,b))}),!0}catch(e){return c?!0:(q(a,e),!0)}return!1}function o(a,b){a===b?p(a,b):n(a,b)||p(a,b)}function p(a,b){a._state===B&&(a._state=C,a._detail=b,t.async(r,a))}function q(a,b){a._state===B&&(a._state=C,a._detail=b,t.async(s,a))}function r(a){m(a,a._state=D)}function s(a){m(a,a._state=E)}var t=a.config,u=(a.configure,b.objectOrFunction),v=b.isFunction,w=(b.now,c.all),x=d.race,y=e.resolve,z=f.reject,A=g.asap;t.async=A;var B=void 0,C=0,D=1,E=2;i.prototype={constructor:i,_state:void 0,_detail:void 0,_subscribers:void 0,then:function(a,b){var c=this,d=new this.constructor(function(){});if(this._state){var e=arguments;t.async(function(){k(c._state,d,e[c._state-1],c._detail)})}else l(this,d,a,b);return d},"catch":function(a){return this.then(null,a)}},i.all=w,i.race=x,i.resolve=y,i.reject=z,h.Promise=i}),a("promise/race",["./utils","exports"],function(a,b){"use strict";function c(a){var b=this;if(!d(a))throw new TypeError("You must pass an array to race.");return new b(function(b,c){for(var d,e=0;e<a.length;e++)d=a[e],d&&"function"==typeof d.then?d.then(b,c):b(d)})}var d=a.isArray;b.race=c}),a("promise/reject",["exports"],function(a){"use strict";function b(a){var b=this;return new b(function(b,c){c(a)})}a.reject=b}),a("promise/resolve",["exports"],function(a){"use strict";function b(a){if(a&&"object"==typeof a&&a.constructor===this)return a;var b=this;return new b(function(b){b(a)})}a.resolve=b}),a("promise/utils",["exports"],function(a){"use strict";function b(a){return c(a)||"object"==typeof a&&null!==a}function c(a){return"function"==typeof a}function d(a){return"[object Array]"===Object.prototype.toString.call(a)}var e=Date.now||function(){return(new Date).getTime()};a.objectOrFunction=b,a.isFunction=c,a.isArray=d,a.now=e}),b("promise/polyfill").polyfill()}();var xhr=function(a,b){"use strict";var c={_id:0,_createTagScript:function(a){var c,d;return c=b.createElement("script"),c.type="text/javascript",d=c.cloneNode(),d.src=a,d},jsonp:function(d,e,f){var g,h=b.head||b.getElementsByTagName("head")[0],i="jsonp_"+c._id++;d+=d.match(/\?/)?"&callback="+i:"?callback="+i,g=c._createTagScript(d),a[i]=function(b){e.call(f||a,b),h.removeChild(g),g=null,delete a[i]},h.appendChild(g)},json:function(b,c){var d=new a.XMLHttpRequest;b.url=b.url||location.href,b.data=b.data||null,d.open("GET",b.url,!0),d.send(b.data),d.onreadystatechange=function(){200===d.status&&4===d.readyState&&c(JSON.parse(d.responseText))}}};return{jsonp:c.jsonp,json:c.json}}(window,document);!function(a){"use strict";a.Buffer=function(){},Buffer.prototype={_request:function(a){this.endpoint="https://api.bufferapp.com/1/links/shares.json?url={url}".replace("{url}",this.url),xhr.jsonp(this.endpoint,function(b){a({count:b.shares,name:"share-buffer"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window),function(a){"use strict";a.Facebook=function(){},Facebook.prototype={_request:function(a){this.endpoint="https://graph.facebook.com/?id={url}".replace("{url}",this.url),xhr.jsonp(this.endpoint,function(b){a({count:b.shares,name:"share-facebook"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window),function(a){"use strict";a.GooglePlus=function(){},GooglePlus.prototype={_request:function(a){this.endpoint="./plus.php?url={url}".replace("{url}",this.url),xhr.json({url:this.endpoint},function(b){a({count:b.counter,name:"share-gplus"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window),function(a){"use strict";a.LinkedIn=function(){},LinkedIn.prototype={_request:function(a){this.endpoint="https://www.linkedin.com/countserv/count/share?url={url}".replace("{url}",this.url),xhr.jsonp(this.endpoint,function(b){a({count:b.count,name:"share-linkedin"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window),function(a){"use strict";a.Twitter=function(){},Twitter.prototype={_request:function(a){this.endpoint="https://cdn.api.twitter.com/1/urls/count.json?url={url}".replace("{url}",this.url),xhr.jsonp(this.endpoint,function(b){a({count:b.count,name:"share-twitter"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window),function(a){"use strict";a.Pinterest=function(){},Pinterest.prototype={_request:function(a){this.endpoint="https://api.pinterest.com/v1/urls/count.json?&url={url}".replace("{url}",this.url),xhr.jsonp(this.endpoint,function(b){a({count:b.count,name:"share-pinterest"})})},promise:function(a){return this.url=a,new Promise(this._request.bind(this))}}}(window);var Socialight=function(a){this.$container=a.container,this.url=a.url,this.leaf=[]};Socialight.prototype={_create:function(a,b){var c,d=document.createElement(a);for(c in b)b.hasOwnProperty(c)&&(d[c]=b[c]);return d},_abbrNum:function(a,b){b=Math.pow(10,b);for(var c=["k","m","b","t"],d=c.length-1;d>=0;d--){var e=Math.pow(10,3*(d+1));if(a>=e){a=Math.round(a*b/e)/b,1e3===a&&d<c.length-1&&(a=1,d++),a+=c[d];break}}return a},_attach:function(a,b){var c=this,d=c._create("span",{className:"shares "+b}),e=document.createTextNode(a);return d.appendChild(e),d},add:function(a){this.leaf.push(a)},draw:function(){{var a=this;a.url}a.leaf.forEach(function(b){var c=b.promise(a.url);c.then(function(b){a.$container.appendChild(a._attach(a._abbrNum(b.count,1),b.name).cloneNode(!0))})})}};