@donil/ngx-sharebuttons
Version:
Simple, lightweight, customizable share buttons for Angular
2 lines (1 loc) • 17.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/http"),require("rxjs/Observable"),require("rxjs/add/operator/catch"),require("rxjs/add/observable/empty"),require("@angular/common")):"function"==typeof define&&define.amd?define(["exports","@angular/core","@angular/http","rxjs/Observable","rxjs/add/operator/catch","rxjs/add/observable/empty","@angular/common"],e):e(t.ngxSharebuttons=t.ngxSharebuttons||{},t.ng.core,t.ng.http,t.Rx,t.Rx.Observable.prototype,t.Rx.Observable.prototype,t.ng.common)}(this,function(t,e,o,r,n,s,i){"use strict";function u(){return"undefined"!=typeof window?window:void 0}var a=function(){function t(){}return t.prototype.transform=function(t,e){if("number"!=typeof t)throw new Error("A number is expected for nFormatter");return l.nFormatter(t,e)},t}();a.decorators=[{type:e.Pipe,args:[{name:"nFormatter"}]}],a.ctorParameters=function(){return[]},function(t){t[t.FACEBOOK=0]="FACEBOOK",t[t.TWITTER=1]="TWITTER",t[t.REDDIT=2]="REDDIT",t[t.STUMBLEUPON=3]="STUMBLEUPON",t[t.LINKEDIN=4]="LINKEDIN",t[t.GOOGLEPLUS=5]="GOOGLEPLUS",t[t.TUMBLR=6]="TUMBLR",t[t.PINTEREST=7]="PINTEREST",t[t.WHATSAPP=8]="WHATSAPP"}(t.ShareProvider||(t.ShareProvider={}));var p;!function(t){function e(t){var e="https://www.facebook.com/sharer/sharer.php";return e+="?u="+t.url,t.title&&(e+="&title="+t.title),t.description&&(e+="&description="+t.description),t.image&&(e+="&picture="+t.image),e}function o(t){var e="https://twitter.com/intent/tweet";return e+="?url="+t.url,t.title&&(e+="&text="+t.title),t.via&&(e+="&via="+t.via),t.tags&&(e+="&hashtags="+t.tags),e}function r(t){var e="http://www.linkedin.com/shareArticle";return e+="?url="+t.url,t.title&&(e+="&title="+t.title),t.description&&(e+="&summary="+t.description),e}function n(t){var e="http://www.reddit.com/submit";return e+="?url="+t.url,t.title&&(e+="&title="+t.title),e}function s(t){var e="http://tumblr.com/widgets/share/tool";return e+="?canonicalUrl="+t.url,t.description&&(e+="&caption="+t.description),t.tags&&(e+="&tags="+t.tags),e}function i(t){var e;return e=t.mobile?"stumbleupon://":"http://www.stumbleupon.com/submit",e+="?url="+t.url}function u(t){return"https://plus.google.com/share?url="+t.url}function a(t){var e="https://in.pinterest.com/pin/create/button/?url="+t.url;if(t.description)e+="&description="+t.description;else if(document){var o=document.querySelector('meta[property="og:description"]');o&&(e+="&description="+o.getAttribute("content"))}if(t.image)e+="&media="+t.image;else if(document){var r=document.querySelector('meta[property="og:image"]');r&&(e+="&media="+r.getAttribute("content"))}return e}function p(t){var e=(t.mobile?"whatsapp://":"https://web.whatsapp.com/")+"send?text=";return t.title&&(e+=t.title+" %0A"),t.description&&(e+=t.description+" | "),e+=""+t.url}t.fbShare=e,t.twitterShare=o,t.linkedInShare=r,t.redditShare=n,t.tumblrShare=s,t.stumbleShare=i,t.gPlusShare=u,t.pinShare=a,t.whatsappShare=p}(p||(p={}));var l;!function(e){e.gplusCountBody=function(t){return[{method:"pos.plusones.get",id:"p",params:{nolog:!0,id:decodeURIComponent(t),source:"widget",userId:"@viewer",groupId:"@self"},jsonrpc:"2.0",key:"p",apiVersion:"v1"}]},e.shareFactory=function(e,o){switch(e){case t.ShareProvider.FACEBOOK:return p.fbShare(o);case t.ShareProvider.TWITTER:return p.twitterShare(o);case t.ShareProvider.LINKEDIN:return p.linkedInShare(o);case t.ShareProvider.REDDIT:return p.redditShare(o);case t.ShareProvider.TUMBLR:return p.tumblrShare(o);case t.ShareProvider.STUMBLEUPON:return p.stumbleShare(o);case t.ShareProvider.GOOGLEPLUS:return p.gPlusShare(o);case t.ShareProvider.PINTEREST:return p.pinShare(o);case t.ShareProvider.WHATSAPP:return p.whatsappShare(o);default:return""}},e.nFormatter=function(t,e){var o,r=[{value:1e18,symbol:"E"},{value:1e15,symbol:"P"},{value:1e12,symbol:"T"},{value:1e9,symbol:"G"},{value:1e6,symbol:"M"},{value:1e3,symbol:"K"}],n=/\.0+$|(\.[0-9]*[1-9])0+$/;for(o=0;o<r.length;o++)if(t>=r[o].value)return(t/r[o].value).toFixed(e).replace(n,"$1")+r[o].symbol;return t.toFixed(e).replace(n,"$1")},e.getEnumValue=function(t,e){var o;return void 0!==t&&("string"==typeof t&&e[t.toUpperCase()]>=0?o=e[t.toUpperCase()]:"number"==typeof t&&e[""+t]&&(o=e[e[""+t]])),o}}(l||(l={}));var c=function(){function t(t,e,o){this.provider=t,this.template=e,this.classes=o}return t}(),h=function(){function t(t,e,o,r,n,s,i){this.url=t,this.title=e,this.description=o,this.image=r,this.tags=n,this.via=s,this.mobile=i}return t}(),b=function(){function o(){this.showCount=!1,this.defaultStyle=!0,this.buttonClass="",this.facebook='<i class="fa fa-facebook"></i>',this.twitter='<i class="fa fa-twitter"></i>',this.linkedIn='<i class="fa fa-linkedin"></i>',this.tumblr='<i class="fa fa-tumblr"></i>',this.google='<i class="fa fa-google-plus"></i>',this.pinterest='<i class="fa fa-pinterest-p"></i>',this.stumbleUpOn='<i class="fa fa-stumbleupon"></i>',this.reddit='<i class="fa fa-reddit-alien"></i>',this.whatsApp='<i class="fa fa-whatsapp"></i>',this.count=new e.EventEmitter,this.popUpClosed=new e.EventEmitter,this.tCount=0}return o.prototype.ngOnInit=function(){this.buttons=[],this.facebook&&this.buttons.push(new c(t.ShareProvider.FACEBOOK,this.facebook,"facebook "+this.buttonClass)),this.twitter&&this.buttons.push(new c(t.ShareProvider.TWITTER,this.twitter,"twitter "+this.buttonClass)),this.google&&this.buttons.push(new c(t.ShareProvider.GOOGLEPLUS,this.google,"googleplus "+this.buttonClass)),this.pinterest&&this.buttons.push(new c(t.ShareProvider.PINTEREST,this.pinterest,"pinterest "+this.buttonClass)),this.linkedIn&&this.buttons.push(new c(t.ShareProvider.LINKEDIN,this.linkedIn,"linkedin "+this.buttonClass)),this.tumblr&&this.buttons.push(new c(t.ShareProvider.TUMBLR,this.tumblr,"tumblr "+this.buttonClass)),this.reddit&&this.buttons.push(new c(t.ShareProvider.REDDIT,this.reddit,"reddit "+this.buttonClass)),this.stumbleUpOn&&this.buttons.push(new c(t.ShareProvider.STUMBLEUPON,this.stumbleUpOn,"stumbleupon "+this.buttonClass)),this.whatsApp&&this.buttons.push(new c(t.ShareProvider.WHATSAPP,this.whatsApp,"whatsapp "+this.buttonClass))},o.prototype.ngOnChanges=function(t){if(t.url){var e=t.url.currentValue,o=t.url.previousValue;e&&e!==o&&(this.tCount=0)}},o.prototype.counter=function(t){this.count.emit(t)},o.prototype.shareClosed=function(t){this.popUpClosed.emit(t)},o}();b.decorators=[{type:e.Component,args:[{selector:"share-buttons",template:'\n <div class="sb-buttons" [ngClass]="(defaultStyle) ? [\'sb-style\', \'sb-style-colors\']: \'\'">\n <share-button class="sb-button" *ngFor="let button of buttons"\n [ngClass]="button.classes"\n [button]="button"\n [url]="url"\n [image]="image"\n [title]="title"\n [description]="description"\n [tags]="tags"\n [showCount]="showCount"\n (count)="counter($event)"\n (popUpClosed)="shareClosed($event)"></share-button>\n </div>\n ',styles:["\n .sb-style.sb-style-colors .facebook button{background:#4267b2}.sb-style.sb-style-colors .facebook button:hover{background:#35528e}.sb-style.sb-style-colors .twitter button{background:#00acee}.sb-style.sb-style-colors .twitter button:hover{background:#008abe}.sb-style.sb-style-colors .googleplus button{background:#db4437}.sb-style.sb-style-colors .googleplus button:hover{background:#af362c}.sb-style.sb-style-colors .stumbleupon button{background:#eb4924}.sb-style.sb-style-colors .stumbleupon button:hover{background:#bc3a1d}.sb-style.sb-style-colors .linkedin button{background:#006fa6}.sb-style.sb-style-colors .linkedin button:hover{background:#005985}.sb-style.sb-style-colors .pinterest button{background:#bd081c}.sb-style.sb-style-colors .pinterest button:hover{background:#970616}.sb-style.sb-style-colors .reddit button{background:#ff4006}.sb-style.sb-style-colors .reddit button:hover{background:#cc3305}.sb-style.sb-style-colors .tumblr button{background:#36465d}.sb-style.sb-style-colors .tumblr button:hover{background:#2b384a}.sb-style.sb-style-colors .whatsapp button{background:#25d366}.sb-style.sb-style-colors .whatsapp button:hover{background:#1ea952}share-buttons{width:100%}.sb-style{display:-webkit-inline-box;display:-ms-inline-flexbox;display:inline-flex;margin:1em auto;-ms-flex-wrap:wrap;flex-wrap:wrap}.sb-style .sb-button{min-width:60px;padding:5px;-webkit-box-orient:vertical;-ms-flex-direction:column;flex-direction:column}.sb-style .sb-button,.sb-style button{display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-direction:normal}.sb-style button{-webkit-box-orient:horizontal;-ms-flex-direction:row;flex-direction:row;-webkit-box-align:center;-ms-flex-align:center;align-items:center;background-color:transparent;border-radius:.2em;padding:.5em .9em;font-size:1em;color:#fff}.sb-style button:hover{transition:all .3s ease}.sb-style .sb-show-count{-ms-flex-pack:distribute;justify-content:space-around;max-width:6em}.sb-style .sb-template{margin:0 auto}.sb-style .sb-count{margin-left:.8em;font-size:.75em;font-family:Tahoma;text-align:center;position:relative;color:#fff;font-weight:700}a:hover,button{cursor:pointer;outline:0;border:0}.facebook{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}.twitter{-webkit-box-ordinal-group:3;-ms-flex-order:2;order:2}.googleplus{-webkit-box-ordinal-group:4;-ms-flex-order:3;order:3}.pinterest{-webkit-box-ordinal-group:6;-ms-flex-order:5;order:5}.linkedin{-webkit-box-ordinal-group:5;-ms-flex-order:4;order:4}.tumblr{-webkit-box-ordinal-group:9;-ms-flex-order:8;order:8}.reddit{-webkit-box-ordinal-group:8;-ms-flex-order:7;order:7}.stumbleupon{-webkit-box-ordinal-group:10;-ms-flex-order:9;order:9}.whatsapp{-webkit-box-ordinal-group:7;-ms-flex-order:6;order:6}\n "],encapsulation:e.ViewEncapsulation.None,changeDetection:e.ChangeDetectionStrategy.OnPush}]}],b.ctorParameters=function(){return[]},b.propDecorators={url:[{type:e.Input}],title:[{type:e.Input}],description:[{type:e.Input}],image:[{type:e.Input}],tags:[{type:e.Input}],showCount:[{type:e.Input}],defaultStyle:[{type:e.Input}],buttonClass:[{type:e.Input}],facebook:[{type:e.Input}],twitter:[{type:e.Input}],linkedIn:[{type:e.Input}],tumblr:[{type:e.Input}],google:[{type:e.Input}],pinterest:[{type:e.Input}],stumbleUpOn:[{type:e.Input}],reddit:[{type:e.Input}],whatsApp:[{type:e.Input}],count:[{type:e.Output}],popUpClosed:[{type:e.Output}]};var d=function(){function t(){this.showCount=!1,this.count=new e.EventEmitter,this.popUpClosed=new e.EventEmitter}return t.prototype.counter=function(t){this.shareCount=t,this.count.emit(t)},t.prototype.shareClosed=function(t){this.popUpClosed.emit(t)},t}();d.decorators=[{type:e.Component,args:[{selector:"share-button",template:'\n <button [shareButton]="button.provider"\n [sbUrl]="url"\n [sbImage]="image"\n [sbTitle]="title"\n [sbDescription]="description"\n [sbTags]="tags"\n [sbShowCount]="showCount"\n (sbCount)="counter($event)"\n (sbPopUpClosed)="shareClosed($event)"\n [class.sb-show-count]="showCount"\n >\n\n <div class="sb-template" [innerHtml]="button.template"></div>\n <span *ngIf="showCount && shareCount" class="sb-count">{{ shareCount | nFormatter: 1 }}</span>\n </button>\n ',changeDetection:e.ChangeDetectionStrategy.OnPush}]}],d.ctorParameters=function(){return[]},d.propDecorators={url:[{type:e.Input}],title:[{type:e.Input}],description:[{type:e.Input}],image:[{type:e.Input}],tags:[{type:e.Input}],button:[{type:e.Input}],showCount:[{type:e.Input}],count:[{type:e.Output}],popUpClosed:[{type:e.Output}]};var f=function(){function t(){}return Object.defineProperty(t.prototype,"nativeWindow",{get:function(){return u()},enumerable:!0,configurable:!0}),t}();f.decorators=[{type:e.Injectable}],f.ctorParameters=function(){return[]};var m=function(){function e(t,e,o){this.http=e,this.jsonp=o,this.windowWidth=500,this.windowHeight=400,this.window=t.nativeWindow}return e.prototype.validateUrl=function(t){if(t){if(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(t))return encodeURIComponent(t);console.warn("[ShareButtons]: Invalid URL, fallback to Window URL")}return this.window?encodeURIComponent(this.window.location.href):global.url||""},e.prototype.share=function(t,e,o){var r=this;this.twitterAccount&&(e=Object.assign({},e,{via:this.twitterAccount})),this.window.innerWidth<=480&&(e=Object.assign({},e,{mobile:!0}));var n=this.window.open(l.shareFactory(t,e),"newwindow",this.windowAttr());if(this.window&&n)var s=this.window.setInterval(function(){n.closed&&(r.window.clearInterval(s),o.emit(t))},200)},e.prototype.count=function(e,o,r){switch(e){case t.ShareProvider.FACEBOOK:this.fbCount(o,r);break;case t.ShareProvider.LINKEDIN:this.linkedInCount(o,r);break;case t.ShareProvider.REDDIT:this.redditCount(o,r);break;case t.ShareProvider.TUMBLR:this.tumblrCount(o,r);break;case t.ShareProvider.GOOGLEPLUS:this.gPlusCount(o,r);break;case t.ShareProvider.PINTEREST:this.pinCount(o,r)}},e.prototype.fbCount=function(t,e){this.fetch("https://graph.facebook.com/?id="+t).subscribe(function(t){if(t=t.json(),t.hasOwnProperty("share")&&t.share.hasOwnProperty("share_count"))return void e.emit(t.share.share_count);e.emit(0)})},e.prototype.linkedInCount=function(t,e){this.fetchJsonp("https://www.linkedin.com/countserv/count/share?url="+t).subscribe(function(t){t=t.json(),e.emit(t.count||0)})},e.prototype.redditCount=function(t,e){this.fetch("https://buttons.reddit.com/button_info.json?url="+t).subscribe(function(t){if(t=t.json(),t.hasOwnProperty("data")&&t.data.hasOwnProperty("children")&&t.data.children.length)return void e.emit(t.data.children[0].data.score);e.emit(0)})},e.prototype.gPlusCount=function(t,e){var o=l.gplusCountBody(t);this.post("https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ",o).subscribe(function(t){if(t=t.json(),t[0]&&t[0].hasOwnProperty("result"))return void e.emit(t[0].result.metadata.globalCounts.count);e.emit(0)})},e.prototype.pinCount=function(t,e){this.fetch("https://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url="+t).subscribe(function(t){t=t.text();var o=JSON.parse(t.replace(/^receiveCount\((.*)\)/,"$1"));e.emit(o.count||0)})},e.prototype.tumblrCount=function(t,e){this.fetchJsonp("https://api.tumblr.com/v2/share/stats?url="+t).subscribe(function(t){if(t=t.json(),t.hasOwnProperty("response")&&t.response.hasOwnProperty("note_count"))return void e.emit(t.response.note_count);e.emit(0)})},e.prototype.post=function(t,e){var n=new o.Headers({"Content-Type":"application/json"}),s=new o.RequestOptions({headers:n});return this.http.post(t,e,s).catch(function(t){return r.Observable.empty()})},e.prototype.fetch=function(t){return this.http.get(t).catch(function(t){return r.Observable.empty()})},e.prototype.fetchJsonp=function(t){return this.jsonp.request(t+"&format=jsonp&callback=JSONP_CALLBACK").catch(function(t){return r.Observable.empty()})},e.prototype.windowAttr=function(){return"width="+this.windowWidth+", height="+this.windowHeight},e}();m.decorators=[{type:e.Injectable}],m.ctorParameters=function(){return[{type:f},{type:o.Http},{type:o.Jsonp}]};var y=function(){function o(t){this.sbService=t,this.sbCount=new e.EventEmitter,this.sbPopUpClosed=new e.EventEmitter}return Object.defineProperty(o.prototype,"shareButton",{set:function(e){if(this.provider=l.getEnumValue(e,t.ShareProvider),void 0===this.provider)throw new Error("[shareButton] must be set to one of the values (numeric or string) of ShareProvider enum: was '"+e+"'")},enumerable:!0,configurable:!0}),o.prototype.onClick=function(){this.share()},o.prototype.ngOnChanges=function(t){if(this.sbUrl=this.sbService.validateUrl(this.sbUrl),t.sbUrl){var e=t.sbUrl.currentValue,o=t.sbUrl.previousValue;e&&e!==o&&this.sbShowCount&&this.sbService.count(this.provider,this.sbUrl,this.sbCount)}},o.prototype.share=function(){var t=new h(this.sbUrl,this.sbTitle,this.sbDescription,this.sbImage,this.sbTags);this.sbService.share(this.provider,t,this.sbPopUpClosed)},o}();y.decorators=[{type:e.Directive,args:[{selector:"[shareButton]"}]}],y.ctorParameters=function(){return[{type:m}]},y.propDecorators={shareButton:[{type:e.Input}],sbUrl:[{type:e.Input}],sbTitle:[{type:e.Input}],sbDescription:[{type:e.Input}],sbImage:[{type:e.Input}],sbTags:[{type:e.Input}],sbShowCount:[{type:e.Input}],sbCount:[{type:e.Output}],sbPopUpClosed:[{type:e.Output}],onClick:[{type:e.HostListener,args:["click"]}]};var w=function(){function t(){}return t.forRoot=function(){return{ngModule:t,providers:[m,f]}},t}();w.decorators=[{type:e.NgModule,args:[{declarations:[b,d,y,a],imports:[i.CommonModule,o.JsonpModule],exports:[b,d,y,a]}]}],w.ctorParameters=function(){return[]},t.ShareButtonsModule=w,t.ShareButtonsComponent=b,t.ShareButtonComponent=d,t.ShareButtonDirective=y,t.ShareButton=c,t.NFormatterPipe=a,t.ShareButtonsService=m,t.ShareArgs=h,Object.defineProperty(t,"__esModule",{value:!0})});