shr-buttons
Advanced tools
Comparing version 2.0.0-beta.4 to 2.0.0-beta.8
@@ -8,5 +8,5 @@ { | ||
}, | ||
"docs.js": { | ||
"src": "./docs/src/js/docs.js", | ||
"dist": "./docs/dist/", | ||
"app.js": { | ||
"src": "./demo/src/js/app.js", | ||
"dist": "./dist/", | ||
"formats": ["iife"] | ||
@@ -16,5 +16,5 @@ } | ||
"css": { | ||
"docs.css": { | ||
"src": "./docs/src/less/docs.less", | ||
"dist": "./docs/dist/" | ||
"app.css": { | ||
"src": "./demo/src/less/app.less", | ||
"dist": "./dist/" | ||
}, | ||
@@ -30,8 +30,4 @@ "shr.css": { | ||
"dist": "./dist" | ||
}, | ||
"docs.svg": { | ||
"src": "./src/sprite/*.svg", | ||
"dist": "./docs/dist" | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"docs": { | ||
"demo": { | ||
"bucket": "shr.one", | ||
@@ -10,0 +10,0 @@ "domain": "shr.one", |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Shr",e):(t=t||self).Shr=e()}(this,function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}function r(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function o(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],r=!0,o=!1,i=void 0;try{for(var u,c=t[Symbol.iterator]();!(r=(u=c.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==c.return||c.return()}finally{if(o)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var i=function(t){return null!=t?t.constructor:null},u=function(t,e){return!!(t&&e&&t instanceof e)},c=function(t){return null==t},s=function(t){return i(t)===Object},a=function(t){return i(t)===String},l=function(t){return Array.isArray(t)},h=function(t){return u(t,NodeList)},f={nullOrUndefined:c,object:s,number:function(t){return i(t)===Number&&!Number.isNaN(t)},string:a,boolean:function(t){return i(t)===Boolean},function:function(t){return i(t)===Function},array:l,nodeList:h,element:function(t){return u(t,Element)},event:function(t){return u(t,Event)},empty:function(t){return c(t)||(a(t)||l(t)||h(t))&&!t.length||s(t)&&!Object.keys(t).length}},g={facebook:{domain:"facebook.com",url:function(t){return"https://graph.facebook.com/?id=".concat(t,"&fields=og_object{engagement}")},shareCount:function(t){return t.og_object.engagement.count},popup:{width:640,height:360}},twitter:{domain:"twitter.com",url:function(){return null},shareCount:function(){return null},popup:{width:640,height:240}},pinterest:{domain:"pinterest.com",url:function(t){return"https://widgets.pinterest.com/v1/urls/count.json?url=".concat(t)},shareCount:function(t){return t.count},popup:{width:830,height:700}},github:{domain:"github.com",url:function(t,e){return"https://api.github.com/repos/".concat(t).concat(f.string(e)?"?access_token=".concat(e):"")},shareCount:function(t){return t.data.stargazers_count}},youtube:{domain:"youtube.com",url:function(t,e){return"https://www.googleapis.com/youtube/v3/channels?part=statistics&id=".concat(t,"&key=").concat(e)},shareCount:function(t){if(!f.empty(t.error))return null;var e=o(t.items,1)[0];return f.empty(e)?null:e.statistics.subscriberCount}}},p={debug:!1,count:{classname:"shr-count",displayZero:!0,format:!0,position:"after",increment:!0,html:function(t,e,n){return'<span class="'.concat(e," ").concat(e,"--").concat(n,'">').concat(t,"</span>")}},tokens:{github:"",youtube:""},storage:{enabled:!0,key:"shr",ttl:3e5}};var d=function(){},m=function(){function e(){var n=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0];t(this,e),this.enabled=window.console&&n,this.enabled&&this.log("Debugging enabled")}return n(e,[{key:"log",get:function(){return this.enabled?Function.prototype.bind.call(console.log,console):d}},{key:"warn",get:function(){return this.enabled?Function.prototype.bind.call(console.warn,console):d}},{key:"error",get:function(){return this.enabled?Function.prototype.bind.call(console.error,console):d}}]),e}();function w(){for(var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=arguments.length,n=Array(1<e?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];if(!n.length)return t;var i=n.shift();return f.object(i)?(Object.keys(i).forEach(function(e){f.object(i[e])?(!Object.keys(t).includes(e)&&Object.assign(t,r({},e,{})),w(t[e],i[e])):Object.assign(t,r({},e,i[e]))}),w.apply(void 0,[t].concat(n))):t}var y=function(){function e(n,r){var o=!(2<arguments.length&&void 0!==arguments[2])||arguments[2];t(this,e),this.enabled=o&&e.supported,this.key=n,this.ttl=r}return n(e,[{key:"get",value:function(t){if(!e.supported||!this.enabled)return null;var n=window.localStorage.getItem(this.key);if(f.empty(n))return null;var r=window.localStorage.getItem("".concat(this.key,"_ttl"));if(f.empty(r)||r<Date.now())return null;var o=JSON.parse(n);return f.string(t)&&t.length?o[t]:o}},{key:"set",value:function(t){if(e.supported&&this.enabled&&f.object(t)){var n=this.get();f.empty(n)&&(n={}),w(n,t),window.localStorage.setItem(this.key,JSON.stringify(n)),window.localStorage.setItem("".concat(this.key,"_ttl"),Date.now()+this.ttl)}}}],[{key:"supported",get:function(){try{return"localStorage"in window&&(window.localStorage.setItem("___test","___test"),window.localStorage.removeItem("___test"),!0)}catch(t){return!1}}}]),e}();return function(){function e(n,r){var o=this;t(this,e),this.elements={count:null,trigger:null,popup:null},f.element(n)?this.elements.trigger=n:f.string(n)&&(this.elements.trigger=document.querySelector(n)),f.element(this.elements.trigger)&&f.empty(this.elements.trigger.shr)&&(this.config=w({},p,r,{networks:g}),this.console=new m(this.config.debug),this.storage=new y(this.config.storage.key,this.config.storage.ttl,this.config.storage.enabled),this.getCount().then(function(t){return o.updateDisplay(t)}).catch(function(){}),this.listeners(!0),this.elements.trigger.shr=this)}return n(e,[{key:"destroy",value:function(){this.listeners(!1)}},{key:"listeners",value:function(){var t=this,e=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0]?"addEventListener":"removeEventListener";this.elements.trigger[e]("click",function(e){return t.share(e)},!1)}},{key:"share",value:function(t){var e=this;this.openPopup(t),this.getCount().then(function(t){return e.updateDisplay(t,e.config.increment)}).catch(function(){})}},{key:"openPopup",value:function(t){if(f.event(t)&&!f.empty(this.network)&&this.networkConfig.popup){t.preventDefault();var e=this.networkConfig.popup,n=e.width,r=e.height,o="shr-popup--".concat(this.network);if(this.popup&&!this.popup.closed)this.popup.focus(),this.console.log("Popup re-focused.");else{var i=void 0===window.screenLeft?window.screen.left:window.screenLeft,u=void 0===window.screenTop?window.screen.top:window.screenTop,c=window.screen.width/2-n/2+i,s=window.screen.height/2-r/2+u;this.popup=window.open(this.href,o,"top=".concat(s,",left=").concat(c,",width=").concat(n,",height=").concat(r)),!this.popup||this.popup.closed||!f.boolean(this.popup.closed)?this.console.error("Popup blocked."):(this.popup.focus(),this.console.log("Popup opened."))}}}},{key:"getCount",value:function(){var t=this,e=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];return new Promise(function(n,o){var i=t.apiUrl;if(f.empty(i))o(new Error("No URL available for ".concat(t.network,".")));else{if(e){var u=t.storage.get(t.target);if(!f.empty(u)&&Object.keys(u).includes(t.network))return n(u[t.network]),void t.console.log("getCount resolved from cache.")}(function(t){return new Promise(function(e,n){var r="jsonp_callback_".concat(Math.round(1e5*Math.random())),o=document.createElement("script");o.addEventListener("error",function(t){return n(t)}),window[r]=function(t){delete window[r],document.body.removeChild(o),e(t)};var i=new URL(t);i.searchParams.set("callback",r),o.setAttribute("src",i.toString()),document.body.appendChild(o)})})(i).then(function(e){var o=0,i=t.elements.trigger.getAttribute("data-shr-display");o=f.empty(i)?t.networkConfig.shareCount(e):e[i],f.empty(o)&&(o=0),o=parseInt(o,10),t.storage.set(r({},t.target,r({},t.network,o))),n(o)}).catch(o)}})}},{key:"updateDisplay",value:function(t){var e=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1]?t+1:t,n=this.config.count.position.toLowerCase();if(0<e||this.config.count.displayZero){var r,o="after"===n;r=this.config.count.format&&1e6<e?"".concat(Math.round(e/1e6),"M"):this.config.count.format&&1e3<e?"".concat(Math.round(e/1e3),"K"):function(t){var e=/\./.test(1.1.toLocaleString())?".":",",n=new RegExp("\\".concat(e,"\\d+$"));return Math.round(t).toLocaleString().replace(n,"")}(e),f.element(this.elements.count)?this.elements.count.textContent=r:(this.elements.trigger.insertAdjacentHTML(o?"afterend":"beforebegin",this.config.count.html(r,this.config.count.classname,n)),this.elements.count=this.elements.trigger[o?"nextSibling":"previousSibling"])}}},{key:"href",get:function(){return f.element(this.elements.trigger)?this.elements.trigger.href:null}},{key:"network",get:function(){var t=this;if(!f.element(this.elements.trigger))return null;var e=this.config.networks;return Object.keys(e).find(function(n){return function(t){var e=new URL(t).hostname,n=e.split("."),r=n.length;return 2<r&&(e="".concat(n[r-2],".").concat(n[r-1]),2===n[r-2].length&&2===n[r-1].length&&(e="".concat(n[r-3],".").concat(e))),e}(t.href)===e[n].domain})}},{key:"networkConfig",get:function(){return f.empty(this.network)?null:this.config.networks[this.network]}},{key:"target",get:function(){if(f.empty(this.network))return null;var t=new URL(this.href);switch(this.network){case"facebook":return t.searchParams.get("u");case"github":return t.pathname.substring(1);case"youtube":return t.pathname.split("/").pop();default:return t.searchParams.get("url")}}},{key:"apiUrl",get:function(){if(f.empty(this.network))return null;var t=this.config.tokens;switch(this.network){case"github":return this.networkConfig.url(this.target,t.github);case"youtube":return this.networkConfig.url(this.target,t.youtube);default:return this.networkConfig.url(encodeURIComponent(this.target))}}}],[{key:"setup",value:function(t){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=null;if(f.string(t)?r=Array.from(document.querySelectorAll(t)):f.element(t)?r=[t]:f.nodeList(t)?r=Array.from(t):f.array(t)&&(r=t.filter(f.element)),f.empty(r))return null;var o=Object.assign({},p,n);f.string(t)&&o.watch&&new MutationObserver(function(n){Array.from(n).forEach(function(n){Array.from(n.addedNodes).forEach(function(n){f.element(n)&&function(t,e){return function(){return Array.from(document.querySelectorAll(e)).includes(this)}.call(t,e)}(n,t)&&new e(n,o)})})}).observe(document.body,{childList:!0,subtree:!0});return r.map(function(t){return new e(t,n)})}}]),e}()}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Shr",e):(t=t||self).Shr=e()}(this,function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}function r(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function o(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],r=!0,o=!1,i=void 0;try{for(var u,c=t[Symbol.iterator]();!(r=(u=c.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==c.return||c.return()}finally{if(o)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var i=function(t){return null!=t?t.constructor:null},u=function(t,e){return!!(t&&e&&t instanceof e)},c=function(t){return null==t},s=function(t){return i(t)===Object},a=function(t){return i(t)===String},l=function(t){return Array.isArray(t)},h=function(t){return u(t,NodeList)},f={nullOrUndefined:c,object:s,number:function(t){return i(t)===Number&&!Number.isNaN(t)},string:a,boolean:function(t){return i(t)===Boolean},function:function(t){return i(t)===Function},array:l,nodeList:h,element:function(t){return u(t,Element)},event:function(t){return u(t,Event)},empty:function(t){return c(t)||(a(t)||l(t)||h(t))&&!t.length||s(t)&&!Object.keys(t).length}},p={facebook:{domain:"facebook.com",url:function(t){return"https://graph.facebook.com/?id=".concat(t,"&fields=og_object{engagement}")},shareCount:function(t){return t.og_object.engagement.count},popup:{width:640,height:360}},twitter:{domain:"twitter.com",url:function(){return null},shareCount:function(){return null},popup:{width:640,height:240}},pinterest:{domain:"pinterest.com",url:function(t){return"https://widgets.pinterest.com/v1/urls/count.json?url=".concat(t)},shareCount:function(t){return t.count},popup:{width:830,height:700}},github:{domain:"github.com",url:function(t,e){return"https://api.github.com/repos/".concat(t).concat(f.string(e)?"?access_token=".concat(e):"")},shareCount:function(t){return t.data.stargazers_count}},youtube:{domain:"youtube.com",url:function(t,e){return"https://www.googleapis.com/youtube/v3/channels?part=statistics&id=".concat(t,"&key=").concat(e)},shareCount:function(t){if(!f.empty(t.error))return null;var e=o(t.items,1)[0];return f.empty(e)?null:e.statistics.subscriberCount}}},g={debug:!1,wrapper:{className:"shr"},count:{className:"shr__count",displayZero:!0,format:!0,position:"after",increment:!0},tokens:{github:"",youtube:""},storage:{enabled:!0,key:"shr",ttl:3e5}};var d=function(){},m=function(){function e(){var n=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0];t(this,e),this.enabled=window.console&&n,this.enabled&&this.log("Debugging enabled")}return n(e,[{key:"log",get:function(){return this.enabled?Function.prototype.bind.call(console.log,console):d}},{key:"warn",get:function(){return this.enabled?Function.prototype.bind.call(console.warn,console):d}},{key:"error",get:function(){return this.enabled?Function.prototype.bind.call(console.error,console):d}}]),e}();function w(t,e,n){var r=document.createElement(t);return f.object(e)&&function(t,e){!f.element(t)||f.empty(e)||Object.entries(e).filter(function(t){var e=o(t,2)[1];return!f.nullOrUndefined(e)}).forEach(function(e){var n=o(e,2),r=n[0],i=n[1];return t.setAttribute(r,i)})}(r,e),f.string(n)&&(r.innerText=n),r}function y(){for(var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=arguments.length,n=Array(1<e?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];if(!n.length)return t;var i=n.shift();return f.object(i)?(Object.keys(i).forEach(function(e){f.object(i[e])?(!Object.keys(t).includes(e)&&Object.assign(t,r({},e,{})),y(t[e],i[e])):Object.assign(t,r({},e,i[e]))}),y.apply(void 0,[t].concat(n))):t}var b=function(){function e(n,r){var o=!(2<arguments.length&&void 0!==arguments[2])||arguments[2];t(this,e),this.enabled=o&&e.supported,this.key=n,this.ttl=r}return n(e,[{key:"get",value:function(t){if(!e.supported||!this.enabled)return null;var n=window.localStorage.getItem(this.key);if(f.empty(n))return null;var r=window.localStorage.getItem("".concat(this.key,"_ttl"));if(f.empty(r)||r<Date.now())return null;var o=JSON.parse(n);return f.string(t)&&t.length?o[t]:o}},{key:"set",value:function(t){if(e.supported&&this.enabled&&f.object(t)){var n=this.get();f.empty(n)&&(n={}),y(n,t),window.localStorage.setItem(this.key,JSON.stringify(n)),window.localStorage.setItem("".concat(this.key,"_ttl"),Date.now()+this.ttl)}}}],[{key:"supported",get:function(){try{return"localStorage"in window&&(window.localStorage.setItem("___test","___test"),window.localStorage.removeItem("___test"),!0)}catch(t){return!1}}}]),e}();return function(){function e(n,r){var o=this;t(this,e),this.elements={count:null,trigger:null,popup:null},f.element(n)?this.elements.trigger=n:f.string(n)&&(this.elements.trigger=document.querySelector(n)),f.element(this.elements.trigger)&&f.empty(this.elements.trigger.shr)&&(this.config=y({},g,r,{networks:p}),this.console=new m(this.config.debug),this.storage=new b(this.config.storage.key,this.config.storage.ttl,this.config.storage.enabled),this.getCount().then(function(t){return o.updateDisplay(t)}).catch(function(){}),this.listeners(!0),this.elements.trigger.shr=this)}return n(e,[{key:"destroy",value:function(){this.listeners(!1)}},{key:"listeners",value:function(){var t=this,e=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0]?"addEventListener":"removeEventListener";this.elements.trigger[e]("click",function(e){return t.share(e)},!1)}},{key:"share",value:function(t){var e=this;this.openPopup(t),this.getCount().then(function(t){return e.updateDisplay(t,e.config.increment)}).catch(function(){})}},{key:"openPopup",value:function(t){if(f.event(t)&&!f.empty(this.network)&&this.networkConfig.popup){t.preventDefault();var e=this.networkConfig.popup,n=e.width,r=e.height,o="shr-popup--".concat(this.network);if(this.popup&&!this.popup.closed)this.popup.focus(),this.console.log("Popup re-focused.");else{var i=void 0===window.screenLeft?window.screen.left:window.screenLeft,u=void 0===window.screenTop?window.screen.top:window.screenTop,c=window.screen.width/2-n/2+i,s=window.screen.height/2-r/2+u;this.popup=window.open(this.href,o,"top=".concat(s,",left=").concat(c,",width=").concat(n,",height=").concat(r)),!this.popup||this.popup.closed||!f.boolean(this.popup.closed)?this.console.error("Popup blocked."):(this.popup.focus(),this.console.log("Popup opened."))}}}},{key:"getCount",value:function(){var t=this,e=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];return new Promise(function(n,o){var i=t.apiUrl;if(f.empty(i))o(new Error("No URL available for ".concat(t.network,".")));else{if(e){var u=t.storage.get(t.target);if(!f.empty(u)&&Object.keys(u).includes(t.network))return n(u[t.network]),void t.console.log("getCount resolved from cache.")}(function(t){return new Promise(function(e,n){var r="jsonp_callback_".concat(Math.round(1e5*Math.random())),o=document.createElement("script");o.addEventListener("error",function(t){return n(t)}),window[r]=function(t){delete window[r],document.body.removeChild(o),e(t)};var i=new URL(t);i.searchParams.set("callback",r),o.setAttribute("src",i.toString()),document.body.appendChild(o)})})(i).then(function(e){var o=0,i=t.elements.trigger.getAttribute("data-shr-display");o=f.empty(i)?t.networkConfig.shareCount(e):e[i],f.empty(o)&&(o=0),o=parseInt(o,10),t.storage.set(r({},t.target,r({},t.network,o))),n(o)}).catch(o)}})}},{key:"updateDisplay",value:function(t){var e=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1]?t+1:t,n=this.config.count.position.toLowerCase();if(0<e||this.config.count.displayZero){var r,o=function(t){return Math.round(e/t*10)/10};if(1e3>e||!this.config.count.format?r=function(t){var e=/\./.test(1.1.toLocaleString())?".":",",n=new RegExp("\\".concat(e,"\\d+$"));return Math.round(t).toLocaleString().replace(n,"")}(e):(1e6<e&&(r="".concat(o(1e6),"M")),r="".concat(o(1e3),"K")),f.element(this.elements.count))this.elements.count.textContent=r;else{var i=this.config,u=i.count,c=i.wrapper;(function(t,e){var n=t.length?t:[t];Array.from(n).reverse().forEach(function(t,n){var r=0<n?e.cloneNode(!0):e,o=t.parentNode,i=t.nextSibling;r.appendChild(t),i?o.insertBefore(r,i):o.appendChild(r)})})(this.elements.trigger,w("span",{class:c.className})),this.elements.count=w("span",{class:"".concat(u.className," ").concat(u.className,"--").concat(n)},r),this.elements.trigger.insertAdjacentElement("after"===n?"afterend":"beforebegin",this.elements.count)}}}},{key:"href",get:function(){return f.element(this.elements.trigger)?this.elements.trigger.href:null}},{key:"network",get:function(){var t=this;if(!f.element(this.elements.trigger))return null;var e=this.config.networks;return Object.keys(e).find(function(n){return function(t){var e=new URL(t).hostname,n=e.split("."),r=n.length;return 2<r&&(e="".concat(n[r-2],".").concat(n[r-1]),2===n[r-2].length&&2===n[r-1].length&&(e="".concat(n[r-3],".").concat(e))),e}(t.href)===e[n].domain})}},{key:"networkConfig",get:function(){return f.empty(this.network)?null:this.config.networks[this.network]}},{key:"target",get:function(){if(f.empty(this.network))return null;var t=new URL(this.href);switch(this.network){case"facebook":return t.searchParams.get("u");case"github":return t.pathname.substring(1);case"youtube":return t.pathname.split("/").pop();default:return t.searchParams.get("url")}}},{key:"apiUrl",get:function(){if(f.empty(this.network))return null;var t=this.config.tokens;switch(this.network){case"github":return this.networkConfig.url(this.target,t.github);case"youtube":return this.networkConfig.url(this.target,t.youtube);default:return this.networkConfig.url(encodeURIComponent(this.target))}}}],[{key:"setup",value:function(t){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=null;if(f.string(t)?r=Array.from(document.querySelectorAll(t)):f.element(t)?r=[t]:f.nodeList(t)?r=Array.from(t):f.array(t)&&(r=t.filter(f.element)),f.empty(r))return null;var o=Object.assign({},g,n);f.string(t)&&o.watch&&new MutationObserver(function(n){Array.from(n).forEach(function(n){Array.from(n.addedNodes).forEach(function(n){f.element(n)&&function(t,e){return function(){return Array.from(document.querySelectorAll(e)).includes(this)}.call(t,e)}(n,t)&&new e(n,o)})})}).observe(document.body,{childList:!0,subtree:!0});return r.map(function(t){return new e(t,n)})}}]),e}()}); | ||
//# sourceMappingURL=shr.js.map |
118
gulpfile.js
@@ -68,15 +68,13 @@ // ========================================================================== | ||
}, | ||
docs: { | ||
demo: { | ||
// Source paths | ||
src: { | ||
less: path.join(root, 'docs/src/less/**/*'), | ||
js: path.join(root, 'docs/src/js/**/*'), | ||
sprite: path.join(root, 'docs/src/sprite/**/*'), | ||
less: path.join(root, 'demo/src/less/**/*'), | ||
js: path.join(root, 'demo/src/js/**/*'), | ||
sprite: path.join(root, 'demo/src/sprite/**/*'), | ||
}, | ||
// Output paths | ||
dist: path.join(root, 'docs/dist/'), | ||
// Docs | ||
root: path.join(root, 'docs/'), | ||
root: path.join(root, 'demo/'), | ||
}, | ||
upload: [path.join(root, 'dist/**'), path.join(root, 'docs/dist/**')], | ||
upload: [path.join(root, 'dist/**')], | ||
}; | ||
@@ -135,4 +133,4 @@ | ||
format, | ||
} | ||
) | ||
}, | ||
), | ||
) | ||
@@ -143,7 +141,7 @@ .pipe(terser()) | ||
extname: `.${format === 'es' ? 'mjs' : 'js'}`, | ||
}) | ||
}), | ||
) | ||
.pipe(size(sizeOptions)) | ||
.pipe(sourcemaps.write('')) | ||
.pipe(gulp.dest(entry.dist)) | ||
.pipe(gulp.dest(entry.dist)), | ||
); | ||
@@ -166,7 +164,7 @@ }); | ||
cascade: false, | ||
}) | ||
}), | ||
) | ||
.pipe(clean()) | ||
.pipe(size(sizeOptions)) | ||
.pipe(gulp.dest(entry.dist)) | ||
.pipe(gulp.dest(entry.dist)), | ||
); | ||
@@ -184,7 +182,13 @@ }); | ||
.pipe(plumber()) | ||
.pipe(imagemin()) | ||
.pipe( | ||
imagemin([ | ||
imagemin.svgo({ | ||
plugins: [{ removeViewBox: false }], | ||
}), | ||
]), | ||
) | ||
.pipe(svgstore()) | ||
.pipe(rename({ basename: path.parse(filename).name })) | ||
.pipe(size(sizeOptions)) | ||
.pipe(gulp.dest(entry.dist)) | ||
.pipe(gulp.dest(entry.dist)), | ||
); | ||
@@ -200,18 +204,11 @@ }); | ||
// Docs | ||
gulp.watch(paths.docs.src.js, gulp.parallel(...tasks.js)); | ||
gulp.watch(paths.docs.src.less, gulp.parallel(...tasks.css)); | ||
// Demo | ||
gulp.watch(paths.demo.src.js, gulp.parallel(...tasks.js)); | ||
gulp.watch(paths.demo.src.less, gulp.parallel(...tasks.css)); | ||
}); | ||
// Default gulp task | ||
gulp.task( | ||
'default', | ||
gulp.series( | ||
'clean', | ||
gulp.parallel(...tasks.js, ...tasks.css, ...tasks.sprite), | ||
'watch' | ||
) | ||
); | ||
gulp.task('default', gulp.series('clean', gulp.parallel(...tasks.js, ...tasks.css, ...tasks.sprite), 'watch')); | ||
// Publish a version to CDN and docs | ||
// Publish a version to CDN and demo | ||
// -------------------------------------------- | ||
@@ -225,3 +222,3 @@ | ||
}, | ||
docs: { | ||
demo: { | ||
'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0', | ||
@@ -254,3 +251,3 @@ }, | ||
gzip: true, | ||
}) | ||
}), | ||
) | ||
@@ -260,3 +257,3 @@ .pipe( | ||
path.dirname = path.dirname.replace('.', version); | ||
}) | ||
}), | ||
) | ||
@@ -268,4 +265,4 @@ .pipe(publisher.publish(headers.cdn)) | ||
// Replace versioned files in readme.md | ||
gulp.task('docs:readme', () => { | ||
const { domain } = deploy.docs; | ||
gulp.task('demo:readme', () => { | ||
const { domain } = deploy.demo; | ||
@@ -279,11 +276,11 @@ return gulp | ||
// Replace versions in shr.js | ||
gulp.task('docs:src', () => | ||
gulp.task('demo:src', () => | ||
gulp | ||
.src(path.join(root, 'src/js/shr.js')) | ||
.pipe(replace(semver, `v${version}`)) | ||
.pipe(gulp.dest(path.join(root, 'src/js/'))) | ||
.pipe(gulp.dest(path.join(root, 'src/js/'))), | ||
); | ||
// Replace versions in shr.js | ||
gulp.task('docs:svg', () => { | ||
gulp.task('demo:svg', () => { | ||
const { domain, publisher } = deploy.cdn; | ||
@@ -296,3 +293,3 @@ | ||
return gulp | ||
.src(path.join(root, 'docs/dist/docs.js')) | ||
.src(path.join(root, 'dist/app.js')) | ||
.pipe(replace(localpath, `https://${domain}/${version}`)) | ||
@@ -302,3 +299,3 @@ .pipe( | ||
path.dirname = path.dirname.replace('.', version); | ||
}) | ||
}), | ||
) | ||
@@ -309,6 +306,6 @@ .pipe(publisher.publish(headers.cdn)) | ||
// Replace local file paths with remote paths in docs | ||
// Replace local file paths with remote paths in demo | ||
// e.g. "../dist/shr.js" to "https://cdn.shr.one/x.x.x/shr.js" | ||
gulp.task('docs:paths', () => { | ||
const { publisher } = deploy.docs; | ||
gulp.task('demo:paths', () => { | ||
const { publisher } = deploy.demo; | ||
const { domain } = deploy.cdn; | ||
@@ -321,11 +318,11 @@ | ||
return gulp | ||
.src([`${paths.docs.root}*.html`]) | ||
.src([`${paths.demo.root}*.html`]) | ||
.pipe(replace(localpath, `https://${domain}/${version}`)) | ||
.pipe(publisher.publish(headers.docs)) | ||
.pipe(publisher.publish(headers.demo)) | ||
.pipe(publish.reporter()); | ||
}); | ||
// Upload error.html to cdn (as well as docs site) | ||
gulp.task('docs:error', () => { | ||
const { publisher } = deploy.docs; | ||
// Upload error.html to cdn (as well as demo site) | ||
gulp.task('demo:error', () => { | ||
const { publisher } = deploy.demo; | ||
const { domain } = deploy.cdn; | ||
@@ -338,32 +335,15 @@ | ||
return gulp | ||
.src([`${paths.docs.root}error.html`]) | ||
.src([`${paths.demo.root}error.html`]) | ||
.pipe(replace(localpath, `https://${domain}/${version}`)) | ||
.pipe(publisher.publish(headers.docs)) | ||
.pipe(publisher.publish(headers.demo)) | ||
.pipe(publish.reporter()); | ||
}); | ||
// Publish to Docs bucket | ||
// Publish to Demo bucket | ||
gulp.task( | ||
'docs', | ||
gulp.series( | ||
'clean', | ||
gulp.parallel( | ||
'docs:readme', | ||
'docs:src', | ||
'docs:svg', | ||
'docs:paths', | ||
'docs:error' | ||
) | ||
) | ||
'demo', | ||
gulp.series('clean', gulp.parallel('demo:readme', 'demo:src', 'demo:svg', 'demo:paths', 'demo:error')), | ||
); | ||
// Do everything | ||
gulp.task( | ||
'deploy', | ||
gulp.series( | ||
'clean', | ||
gulp.parallel(...tasks.js, ...tasks.css, ...tasks.sprite), | ||
'cdn', | ||
'docs' | ||
) | ||
); | ||
gulp.task('deploy', gulp.series('clean', gulp.parallel(...tasks.js, ...tasks.css, ...tasks.sprite), 'cdn', 'demo')); |
{ | ||
"name": "shr-buttons", | ||
"version": "2.0.0-beta.4", | ||
"version": "2.0.0-beta.8", | ||
"description": "Simple, customizable sharing buttons", | ||
@@ -37,3 +37,3 @@ "homepage": "http://shr.one", | ||
"ansi-colors": "^3.2.4", | ||
"aws-sdk": "^2.441.0", | ||
"aws-sdk": "^2.443.0", | ||
"@babel/core": "^7.4.3", | ||
@@ -46,3 +46,3 @@ "@babel/preset-env": "^7.4.3", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^4.1.0", | ||
"eslint-config-prettier": "^4.2.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
@@ -54,3 +54,3 @@ "fancy-log": "^1.3.3", | ||
"gulp-better-rollup": "^4.0.1", | ||
"gulp-clean-css": "^4.0.0", | ||
"gulp-clean-css": "^4.2.0", | ||
"gulp-imagemin": "^5.0.3", | ||
@@ -57,0 +57,0 @@ "gulp-less": "^4.0.1", |
@@ -74,9 +74,9 @@ # Shr | ||
// XHR for Chrome/Firefox/Opera/Safari | ||
if ("withCredentials" in x) { | ||
x.open("GET", u, true); | ||
if ('withCredentials' in x) { | ||
x.open('GET', u, true); | ||
} | ||
// XDomainRequest for older IE | ||
else if (typeof XDomainRequest != "undefined") { | ||
else if (typeof XDomainRequest != 'undefined') { | ||
x = new XDomainRequest(); | ||
x.open("GET", u); | ||
x.open('GET', u); | ||
} else { | ||
@@ -88,8 +88,8 @@ return; | ||
x.onload = function() { | ||
var c = d.createElement("div"); | ||
c.setAttribute("hidden", ""); | ||
var c = d.createElement('div'); | ||
c.setAttribute('hidden', ''); | ||
c.innerHTML = x.responseText; | ||
b.insertBefore(c, b.childNodes[0]); | ||
}; | ||
})(document, "../dist/sprite.svg"); | ||
})(document, '../dist/sprite.svg'); | ||
</script> | ||
@@ -124,3 +124,3 @@ ``` | ||
shr.setup({ | ||
debug: true | ||
debug: true, | ||
}); | ||
@@ -137,3 +137,3 @@ ``` | ||
shr.setup({ | ||
selector: "data-your-selector-name" | ||
selector: 'data-your-selector-name', | ||
}); | ||
@@ -146,3 +146,3 @@ ``` | ||
When adding the share count element to the screen, this is the classname used to style it. | ||
When adding the share count element to the screen, this is the className used to style it. | ||
@@ -154,4 +154,4 @@ This is a nested option within the `count` object. | ||
count: { | ||
classname: "your-class-name" | ||
} | ||
className: 'your-class-name', | ||
}, | ||
}); | ||
@@ -171,4 +171,4 @@ ``` | ||
count: { | ||
displayZero: true | ||
} | ||
displayZero: true, | ||
}, | ||
}); | ||
@@ -188,4 +188,4 @@ ``` | ||
count: { | ||
format: false | ||
} | ||
format: false, | ||
}, | ||
}); | ||
@@ -205,4 +205,4 @@ ``` | ||
count: { | ||
position: "before" | ||
} | ||
position: 'before', | ||
}, | ||
}); | ||
@@ -222,4 +222,4 @@ ``` | ||
count: { | ||
increment: false | ||
} | ||
increment: false, | ||
}, | ||
}); | ||
@@ -246,5 +246,5 @@ ``` | ||
html: function(count, classname, position) { | ||
return "{YOUR CONSTRUCTED HTML HERE}"; | ||
} | ||
} | ||
return '{YOUR CONSTRUCTED HTML HERE}'; | ||
}, | ||
}, | ||
}); | ||
@@ -271,5 +271,5 @@ ``` | ||
width: 1024, | ||
height: 768 | ||
} | ||
} | ||
height: 768, | ||
}, | ||
}, | ||
}); | ||
@@ -296,5 +296,5 @@ ``` | ||
width: 1024, | ||
height: 768 | ||
} | ||
} | ||
height: 768, | ||
}, | ||
}, | ||
}); | ||
@@ -321,5 +321,5 @@ ``` | ||
width: 1024, | ||
height: 768 | ||
} | ||
} | ||
height: 768, | ||
}, | ||
}, | ||
}); | ||
@@ -346,5 +346,5 @@ ``` | ||
width: 1024, | ||
height: 768 | ||
} | ||
} | ||
height: 768, | ||
}, | ||
}, | ||
}); | ||
@@ -351,0 +351,0 @@ ``` |
@@ -52,4 +52,7 @@ /** | ||
debug: false, | ||
wrapper: { | ||
className: 'shr', | ||
}, | ||
count: { | ||
classname: 'shr-count', | ||
className: 'shr__count', | ||
displayZero: true, | ||
@@ -59,3 +62,2 @@ format: true, | ||
increment: true, | ||
html: (count, classname, position) => `<span class="${classname} ${classname}--${position}">${count}</span>`, | ||
}, | ||
@@ -62,0 +64,0 @@ tokens: { |
@@ -13,2 +13,3 @@ /** | ||
import { matches } from './utils/css'; | ||
import { createElement, wrap } from './utils/elements'; | ||
import is from './utils/is'; | ||
@@ -323,11 +324,14 @@ import { formatNumber } from './utils/numbers'; | ||
const isAfter = position === 'after'; | ||
const round = unit => Math.round((count / unit) * 10) / 10; | ||
// Format | ||
let label; | ||
if (this.config.count.format && count > 1000000) { | ||
label = `${Math.round(count / 1000000)}M`; | ||
} else if (this.config.count.format && count > 1000) { | ||
label = `${Math.round(count / 1000)}K`; | ||
if (count < 1000 || !this.config.count.format) { | ||
label = formatNumber(count); | ||
} else { | ||
label = formatNumber(count); | ||
if (count > 1000000) { | ||
label = `${round(1000000)}M`; | ||
} | ||
label = `${round(1000)}K`; | ||
} | ||
@@ -339,10 +343,23 @@ | ||
} else { | ||
// Insert count display | ||
this.elements.trigger.insertAdjacentHTML( | ||
isAfter ? 'afterend' : 'beforebegin', | ||
this.config.count.html(label, this.config.count.classname, position) | ||
const { count, wrapper } = this.config; | ||
// Add wrapper | ||
wrap( | ||
this.elements.trigger, | ||
createElement('span', { | ||
class: wrapper.className, | ||
}), | ||
); | ||
// Store reference | ||
this.elements.count = this.elements.trigger[isAfter ? 'nextSibling' : 'previousSibling']; | ||
// Create count display | ||
this.elements.count = createElement( | ||
'span', | ||
{ | ||
class: `${count.className} ${count.className}--${position}`, | ||
}, | ||
label, | ||
); | ||
// Insert count display | ||
this.elements.trigger.insertAdjacentElement(isAfter ? 'afterend' : 'beforebegin', this.elements.count); | ||
} | ||
@@ -349,0 +366,0 @@ } |
@@ -37,2 +37,2 @@ /** | ||
export default {}; | ||
export default { getJSONP }; |
@@ -19,2 +19,2 @@ // Element matches a selector | ||
export default {}; | ||
export default { matches }; |
@@ -13,2 +13,2 @@ export function formatNumber(number) { | ||
export default {}; | ||
export default { formatNumber }; |
@@ -34,2 +34,2 @@ // ========================================================================== | ||
export default {}; | ||
export default { extend }; |
@@ -20,2 +20,2 @@ export function getDomain(href) { | ||
export default {}; | ||
export default { getDomain }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
675340
1241