forked from ellisonleao/sharer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharer.min.js
1 lines (1 loc) · 1.9 KB
/
sharer.min.js
1
(function(c,a){"use strict";var i=function(e){this.elem=e};i.init=function(){var e=a.querySelectorAll("[data-sharer]"),t,r=e.length;for(t=0;t<r;t++){e[t].addEventListener("click",i.add)}};i.add=function(e){var t=e.currentTarget||e.srcElement;var r=new i(t);r.share()};i.prototype={constructor:i,getValue:function(e){var t=this.elem.getAttribute("data-"+e);if(t&&e==="hashtag"){if(!t.startsWith("#")){t="#"+t}}return t},share:function(){var e=this.getValue("sharer").toLowerCase(),t={facebook:{shareUrl:"https://www.facebook.com/sharer/sharer.php",params:{u:this.getValue("url"),hashtag:this.getValue("hashtag")}},linkedin:{shareUrl:"https://www.linkedin.com/shareArticle",params:{url:this.getValue("url"),mini:true}},twitter:{shareUrl:"https://twitter.com/intent/tweet/",params:{text:this.getValue("title"),url:this.getValue("url"),hashtags:this.getValue("hashtags"),via:this.getValue("via")}},email:{shareUrl:"mailto:"+this.getValue("to")||"",params:{subject:this.getValue("subject"),body:this.getValue("title")+"\n"+this.getValue("url")},isLink:true},pinterest:{shareUrl:"https://www.pinterest.com/pin/create/button/",params:{url:this.getValue("url"),media:this.getValue("image"),description:this.getValue("description")}}},r=t[e];if(r){r.width=this.getValue("width");r.height=this.getValue("height")}return r!==undefined?this.urlSharer(r):false},urlSharer:function(e){var t=e.params||{},r=Object.keys(t),a,i=r.length>0?"?":"";for(a=0;a<r.length;a++){if(i!=="?"){i+="&"}if(t[r[a]]){i+=r[a]+"="+encodeURIComponent(t[r[a]])}}e.shareUrl+=i;if(!e.isLink){var s=e.width||600,h=e.height||480,n=c.innerWidth/2-s/2+c.screenX,l=c.innerHeight/2-h/2+c.screenY,u="scrollbars=no, width="+s+", height="+h+", top="+l+", left="+n,o=c.open(e.shareUrl,"",u);if(c.focus){o.focus()}}else{c.location.href=e.shareUrl}}};if(a.readyState==="complete"||a.readyState!=="loading"){i.init()}else{a.addEventListener("DOMContentLoaded",i.init)}c.Sharer=i})(window,document);