-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.bar.min.js
2 lines (2 loc) · 8.96 KB
/
share.bar.min.js
1
2
/*! ShareBar - v3.1.3 - 2016-12-07 - * Copyright (c) 2016 Globo.com; Licensed MIT */
function ShareBar(a){"use strict";return this.init(a)}!function(a,b){"use strict";function c(b){b&&b.preventDefault?b.preventDefault():a.event&&(a.event.returnValue=!1)}function d(a,b,c){return a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent?a.attachEvent("on"+b,function(){c.call(a)}):void 0}var e="facebook",f="twitter",g="whatsapp",h="google",i="pinterest",j="email",k=34,l=110,m=4,n=6,o="share-button";ShareBar.prototype={init:function(a){this.eventName=this.getActionName(),this.verifyTouch(),this.createSVG(),this.mergeOptions(a),this.containers=b.querySelectorAll(this.selector),this.createBars()},getActionName:function(){return this.isTouch()?"mouseup":"click"},verifyTouch:function(){var a=b.querySelector("html"),c=this.isTouch(),d=" touch",e=" no-touch";c&&a.className.indexOf(d)===-1?a.className+=d:c||a.className.indexOf(e)!==-1||(a.className+=e)},isTouch:function(){var c=!1;return(void 0!==a.ontouchstart||a.DocumentTouch&&b instanceof DocumentTouch)&&(c=!0),c},createSVG:function(){var a=b.createElement("div");a.innerHTML='<svg xmlns="http://www.w3.org/2000/svg"><symbol viewBox="0 0 500 500" id="icon-email"><title>email</title><path d="M1 387c0 27 23 50 50 50h398c28 0 50-23 50-50V138L300 286c-28 17-70 18-99 0L1 138v249zm228-158c10 5 33 6 42 0 98-71 117-86 209-155-7-6-19-11-31-11H51c-12 0-23 5-31 12l209 154z"/></symbol><symbol viewBox="0 0 500 500" id="icon-facebook"><title>facebook</title><path d="M471 1H29C13 1 1 13 1 29v442c0 16 12 28 28 28h238V306h-65v-75h65v-55c0-65 39-100 97-100 27 0 51 2 58 3v67h-40c-31 0-37 15-37 37v48h74l-10 75h-64v193h126c16 0 28-12 28-28V29c0-16-12-28-28-28z"/></symbol><symbol viewBox="0 0 500 500" id="icon-googleplus"><title>googleplus</title><path d="M333 2H186C88 2 35 47 35 127c0 65 60 109 131 99-17 33 2 57 14 69C82 295 2 338 2 401c0 56 47 97 142 97 102 0 175-56 175-124 0-23-8-43-26-64-32-36-71-46-71-72 0-23 22-34 40-49 27-24 36-54 34-83-3-41-27-65-44-78l37 1 44-27zm-70 364c26 36 7 104-86 104-52 0-117-21-117-81 0-70 103-75 141-75 23 17 46 30 62 52zm-64-162c-45 18-82-10-99-68-15-48-4-94 29-106 44-15 79 10 99 63 21 60 7 95-29 111zm240 10v-59h-35v59h-59v36h59v59h35v-59h60v-36h-60z"/></symbol><symbol viewBox="0 0 500 500" id="icon-pinterest"><title>pinterest</title><path d="M250 1a248 249 0 0 0-100 476 218 218 0 0 1 4-57l33-135s-8-16-8-39c0-37 21-64 48-64 23 0 34 17 34 37 0 23-15 57-22 88-6 26 13 48 39 48 47 0 79-61 79-132 0-55-37-95-104-95-75 0-122 56-122 119 0 22 6 37 16 49 5 5 5 8 4 14l-5 20c-2 6-7 9-13 6-35-14-51-52-51-95 0-71 60-155 178-155 95 0 157 69 157 142 0 98-54 170-134 170-27 0-52-15-61-31 0 0-15 57-18 68a209 209 0 0 1-25 53 249 249 0 0 0 319-238A249 249 0 0 0 250 1z"/></symbol><symbol viewBox="0 0 500 500" id="icon-twitter"><title>twitter</title><path d="M499 96c-19 8-38 14-59 16 21-12 37-32 45-56-20 12-42 20-65 25a102 102 0 0 0-174 93c-84-5-160-45-210-107a101 101 0 0 0 32 136c-17 0-33-5-47-12v1c0 49 36 90 82 100a102 102 0 0 1-46 2c13 40 51 70 95 70A205 205 0 0 1 1 407c46 29 99 45 157 45a288 288 0 0 0 290-303c20-14 37-32 51-53z"/></symbol><symbol viewBox="0 0 500 500" id="icon-whatsapp"><title>whatsapp</title><path d="M255 1A243 243 0 0 0 46 368L2 498l135-43a245 245 0 0 0 362-212C499 109 389 1 255 1zm0 444c-42 0-80-13-112-34l-78 25 25-75c-24-33-39-74-39-118 0-111 92-201 204-201s203 90 203 201-91 202-203 202zm114-147l-42-22c-5-2-10-3-14 3s-17 19-21 23c-3 4-7 5-13 1-6-3-26-10-49-32-18-17-30-38-33-44-4-6 0-10 3-13l10-10 6-10c3-4 2-8 0-11l-17-47c-5-12-10-10-14-10l-12-1c-4-1-11 1-17 7s-22 20-23 50c-1 31 20 61 23 65 3 5 40 70 103 98 62 27 62 18 74 18 11-1 37-14 43-28s6-27 5-29c-2-3-6-5-12-8z"/></symbol></svg>',a.style.display="none",b.body.appendChild(a)},mergeOptions:function(a){var b,c={selector:".share-bar",campaign:"share-bar",classPopup:"share-popup",facebookAppId:"",networks:[e,f,g,h,i,j],theme:"natural",buttonWidth:k,buttonFullWidth:l,buttonPadding:m,maxSocialButtons:n,context:"desktop",onCreateBar:function(a){return!1},onCreateButton:function(a){return!1},onShare:function(a){return!1}};a||(a={});for(b in c)c.hasOwnProperty(b)&&(this[b]=a[b]||c[b])},validateNetworks:function(a){var b="The list of networks passed on initialization is wrong",c=0,d="",k="";if("[object Array]"!==Object.prototype.toString.call(a))throw new Error(b+" [Should be an Array]");for(c;c<a.length;c++)if("string"==typeof a[c]){if(d=a[c],d=d.substr(0,1).toUpperCase()+d.substr(1),k=ShareBar.prototype["create"+d+"Button"],!k)throw new Error(b+' [Network name "'+a[c]+'" is wrong, should be '+e+" or "+f+" or "+g+" or "+h+" or "+i+" or "+j+"]");a[c]=k}else if("function"!=typeof a[c])throw new Error(b+" [Should be string or function]");return a},createBars:function(){var a=this.containers,b=0;for(b=0;b<a.length;b++)this.createBar(a[b])},createBar:function(a,b){var c=" share-theme-",d=0,e=0,f=[];for(b=this.validateNetworks(b||this.networks),b=b.slice(0,this.maxSocialButtons),e=b.length,f=this.getButtonsSize(a.offsetWidth,e),d;d<e;d++)b[d].call(this,a,f[d]);c+=a.getAttribute("data-theme")||this.theme,a.className+=" share-bar-container"+c,this.bindOpenPopup(a),this.bindShare(a),this.onCreateBar(a)},getButtonsSize:function(a,b){var c=this.buttonFullWidth+this.buttonPadding,d=this.buttonWidth+this.buttonPadding,e=this.isSmallScreen();return b*d>a?this.getButtonsSmall(b,d,a):e?["","","","","",""]:this.getButtonsFull(b,c,d,a)},getButtonsSmall:function(a,b,c){var d=[],e=1,f=0,g=this.isSmallScreen();for(e;e<=a;e++)f=e*b,f<=c?d[e-1]=g?"":" share-small":d[e-1]=" share-hidden";return d},getButtonsFull:function(a,b,c,d){var e=[],f=1,g=0,h=0;for(f;f<=a;f++)g=f*b,h=(a-f)*c,h+g<=d?e[f-1]=" share-full":e[f-1]=" share-small";return e},bindOpenPopup:function(a){var b=a.querySelectorAll("."+this.classPopup),e=0,f=this,g=function(a){f.openPopup.call(this,a)};for(e;e<b.length;e++)d(b[e],this.eventName,g),d(b[e],"click",c)},bindShare:function(a){var b=a.querySelectorAll("."+o),c=0,e=this,f=function(a){e.onShare(this)};for(c;c<b.length;c++)d(b[c],this.eventName,f)},openPopup:function(b){var c=a.open(this.getAttribute("href"),"popup","height=400,width=500,left=10,top=10,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no");c.focus()},getMetadataFromElement:function(b){var c,d=a.encodeURIComponent,e=b.getAttribute("data-url")||"",f=e.split("#"),g="?utm_source=#source#&utm_medium=share-bar-"+this.context+"&utm_campaign="+this.campaign;return c=f.length>1?f[0]+g+"&#"+f[1]:e+g,{url:d(c),title:d(b.getAttribute("data-title")||""),imageUrl:d(b.getAttribute("data-image-url")||""),hashtags:d(b.getAttribute("data-hashtags")||"")}},isSmallScreen:function(){var b=768,c=a.innerWidth||screen.width;return c<b},createButton:function(a,c,d,e,f,g){var h=b.createElement("div"),i="";return f=f||c,d=d||"",h.className=o+" share-"+c+d,f=f[0].toUpperCase()+f.slice(1),e=e.replace("%23source%23",c),g||(i=this.classPopup),h.innerHTML=['<a class="'+i+'" href="'+e+'" title="Compartilhar via '+f+'" target="_blank" rel="external">',this.createContentButton(c,f),"</a>"].join(""),a.appendChild(h),this.onCreateButton(h),h},createContentButton:function(a,b){var c;return b=b||a,c=[' <div class="svg-size">',' <svg viewBox="0 0 100 100" class="share-icon">',' <use xlink:href="#icon-'+a+'"></use>'," </svg>"," </div>","<span>"+b+"</span>"].join("")},createFacebookButton:function(b,f){var g="",h="",i=this.getMetadataFromElement(b),j=i.url.replace("%23source%23",e);h=this.createButton(b,e,f,"http://www.facebook.com/","",!0),this.getFacebookUi(),g=function(){var b=a.decodeURIComponent;FB.ui({method:"feed",display:"popup",link:b(j),name:b(i.title),picture:b(i.imageUrl)})},d(h,this.eventName,g),d(h,"click",c)},getFacebookUi:function(){var c=this.facebookAppId||this.getOgFbAppId();return!a.FB&&void(c&&(a.fbAsyncInit=function(){FB.init({appId:c,xfbml:!0,version:"v2.8"})},function(a,b,c){var d,e=a.getElementsByTagName(b)[0];a.getElementById(c)||(d=a.createElement(b),d.id=c,d.src="//connect.facebook.net/en_US/sdk.js",e.parentNode.insertBefore(d,e))}(b,"script","facebook-jssdk")))},getOgFbAppId:function(){var a=b.querySelector("meta[property='fb:app_id']");if(null!==a)return a.getAttribute("content")},createTwitterButton:function(a,b){var c=this.getMetadataFromElement(a);this.createButton(a,f,b,"https://twitter.com/share?url="+c.url+"&text="+c.title+" "+c.hashtags)},createGoogleButton:function(a,b){var c=this.getMetadataFromElement(a);this.createButton(a,h+"plus",b,"https://plus.google.com/share?url="+c.url,h+"+")},createPinterestButton:function(a,b){var c=this.getMetadataFromElement(a);this.createButton(a,i,b,"http://br.pinterest.com/pin/create/button/?url="+c.url+"&media="+c.imageUrl+"&description="+c.title)},createWhatsappButton:function(a,b){var c=this.getMetadataFromElement(a);return!(!this.isSmallScreen()||!this.isTouch())&&void this.createButton(a,g,b,"whatsapp://send?text="+c.title+"%20"+c.url,"",!0)},createEmailButton:function(a,b){var c=this.getMetadataFromElement(a);return!!this.isTouch()&&void this.createButton(a,j,b,"mailto:?subject="+c.title+"&body="+c.url,"e-mail",!0)}}}(window,document);