-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial-likes.min.js
3 lines (2 loc) · 9.47 KB
/
social-likes.min.js
1
2
3
/*! Social Likes v3.1.3 by Artem Sapegin - http://sapegin.github.com/social-likes - Licensed MIT */
!function(t){"function"==typeof define&&define.amd?define(["jquery"],t):t(jQuery)}(function(t,e){"use strict";function i(t,e){this.container=t,this.options=e,this.init()}function n(e,i){this.widget=e,this.options=t.extend({},i),this.detectService(),this.service&&this.init()}function o(t){var e={},i=t.data();for(var n in i){var o=i[n];"yes"===o?o=!0:"no"===o&&(o=!1),e[n.replace(/-(\w)/g,function(t,e){return e.toUpper()})]=o}return e}function s(t,e){return r(t,e,encodeURIComponent)}function r(t,e,i){return t.replace(/\{([^}]+)\}/g,function(t,n){return n in e?i?i(e[n]):e[n]:t})}function c(t,e){var i=p+t;return i+" "+i+"_"+e}function u(e,i){function n(r){"keydown"===r.type&&27!==r.which||t(r.target).closest(e).length||(e.removeClass(h),o.off(s,n),t.isFunction(i)&&i())}var o=t(document),s="click touchstart keydown";o.on(s,n)}function a(t){if(document.documentElement.getBoundingClientRect){var e=parseInt(t.css("left"),10),i=parseInt(t.css("top"),10),n=t[0].getBoundingClientRect();n.left<10?t.css("left",10-n.left+e):n.right>window.innerWidth-10&&t.css("left",window.innerWidth-n.right-10+e),n.top<10?t.css("top",10-n.top+i):n.bottom>window.innerHeight-10&&t.css("top",window.innerHeight-n.bottom-10+i)}t.addClass(h)}var l="social-likes",p=l+"__",h=l+"_opened",d="https:"===location.protocol?"https:":"http:",f={facebook:{counterUrl:"https://graph.facebook.com/?id={url}",convertNumber:function(t){return t.share.share_count},popupUrl:"https://www.facebook.com/sharer/sharer.php?u={url}",popupWidth:600,popupHeight:359},twitter:{counters:!1,popupUrl:"https://twitter.com/intent/tweet?url={url}&text={title}",popupWidth:600,popupHeight:250,click:function(){return/[.?:\-–—]\s*$/.test(this.options.title)||(this.options.title+=":"),!0}},mailru:{counterUrl:d+"//connect.mail.ru/share_count?url_list={url}&callback=1&func=?",convertNumber:function(t){for(var e in t)if(t.hasOwnProperty(e))return t[e].shares},popupUrl:"https://connect.mail.ru/share?share_url={url}&title={title}",popupWidth:492,popupHeight:500},vkontakte:{counterUrl:"https://vk.com/share.php?act=count&url={url}&index={index}",counter:function(e,i){var n=f.vkontakte;n._||(n._=[],window.VK||(window.VK={}),window.VK.Share={count:function(t,e){n._[t].resolve(e)}});var o=n._.length;n._.push(i),t.getScript(s(e,{index:o})).fail(i.reject)},popupUrl:"https://vk.com/share.php?url={url}&title={title}",popupWidth:655,popupHeight:450},odnoklassniki:{counterUrl:d+"//connect.ok.ru/dk?st.cmd=extLike&ref={url}&uid={index}",counter:function(e,i){var n=f.odnoklassniki;n._||(n._=[],window.ODKL||(window.ODKL={}),window.ODKL.updateCount=function(t,e){n._[t].resolve(e)});var o=n._.length;n._.push(i),t.getScript(s(e,{index:o})).fail(i.reject)},popupUrl:"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&service=odnoklassniki&st.shareUrl={url}",popupWidth:580,popupHeight:336},plusone:{counters:!1,popupUrl:"https://plus.google.com/share?url={url}",popupWidth:500,popupHeight:550},pinterest:{counterUrl:d+"//api.pinterest.com/v1/urls/count.json?url={url}&callback=?",convertNumber:function(t){return t.count},popupUrl:"https://pinterest.com/pin/create/button/?url={url}&description={title}",popupWidth:740,popupHeight:550}},g={promises:{},fetch:function(e,i,n){g.promises[e]||(g.promises[e]={});var o=g.promises[e];if(!n.forceUpdate&&o[i])return o[i];var r=t.extend({},f[e],n),c=t.Deferred(),u=r.counterUrl&&s(r.counterUrl,{url:i});return u&&t.isFunction(r.counter)?r.counter(u,c):r.counterUrl?t.getJSON(u).done(function(e){try{var i=e;t.isFunction(r.convertNumber)&&(i=r.convertNumber(e)),c.resolve(i)}catch(t){c.reject()}}).fail(c.reject):c.reject(),o[i]=c.promise(),o[i]}};t.fn.socialLikes=function(e){return this.each(function(){var n=t(this),s=n.data(l);s?t.isPlainObject(e)&&s.update(e):(s=new i(n,t.extend({},t.fn.socialLikes.defaults,e,o(n))),n.data(l,s))})},t.fn.socialLikes.defaults={url:window.location.href.replace(window.location.hash,""),title:document.title,counters:!0,zeroes:!1,wait:500,timeout:1e4,popupCheckInterval:500,singleTitle:"Share"},i.prototype={init:function(){this.container.addClass(l),this.single=this.container.hasClass(l+"_single"),this.initUserButtons(),this.countersLeft=0,this.number=0,this.container.on("counter."+l,t.proxy(this.updateCounter,this));var e=this.container.children();this.makeSingleButton(),this.buttons=[],e.each(t.proxy(function(e,i){var o=new n(t(i),this.options);this.buttons.push(o),o.options.counterUrl&&this.countersLeft++},this)),this.options.counters?(this.timer=setTimeout(t.proxy(this.appear,this),this.options.wait),this.timeout=setTimeout(t.proxy(this.ready,this,!0),this.options.timeout)):this.appear()},initUserButtons:function(){!this.userButtonInited&&window.socialLikesButtons&&t.extend(!0,f,socialLikesButtons),this.userButtonInited=!0},makeSingleButton:function(){if(this.single){var e=this.container;e.addClass(l+"_vertical"),e.wrap(t("<div>",{class:l+"_single-w"})),e.wrapInner(t("<div>",{class:l+"__single-container"}));var i=e.parent(),n=t("<div>",{class:c("widget","single")}),o=t(r('<div class="{buttonCls}"><span class="{iconCls}"></span>{title}</div>',{buttonCls:c("button","single"),iconCls:c("icon","single"),title:this.options.singleTitle}));n.append(o),i.append(n),n.on("click",function(){var t=l+"__widget_active";return n.toggleClass(t),n.hasClass(t)?(e.css({left:-(e.width()-n.width())/2,top:-e.height()}),a(e),u(e,function(){n.removeClass(t)})):e.removeClass(h),!1}),this.widget=n}},update:function(e){if(e.forceUpdate||e.url!==this.options.url){this.number=0,this.countersLeft=this.buttons.length,this.widget&&this.widget.find("."+l+"__counter").remove(),t.extend(this.options,e);for(var i=0;i<this.buttons.length;i++)this.buttons[i].update(e)}},updateCounter:function(t,e,i){((i=i||0)||this.options.zeroes)&&(this.number+=i,this.single&&this.getCounterElem().text(this.number)),0===--this.countersLeft&&(this.appear(),this.ready())},appear:function(){this.container.addClass(l+"_visible")},ready:function(t){this.timeout&&clearTimeout(this.timeout),this.container.addClass(l+"_ready"),t||this.container.trigger("ready."+l,this.number)},getCounterElem:function(){var e=this.widget.find("."+p+"counter_single");return e.length||(e=t("<span>",{class:c("counter","single")}),this.widget.append(e)),e}},n.prototype={init:function(){this.detectParams(),this.initHtml(),setTimeout(t.proxy(this.initCounter,this),0)},update:function(e){t.extend(this.options,{forceUpdate:!1},e),this.widget.find("."+l+"__counter").remove(),this.initCounter()},detectService:function(){var e=this.widget.data("service");if(!e){for(var i=this.widget[0],n=i.classList||i.className.split(" "),o=0;o<n.length;o++){var s=n[o];if(f[s]){e=s;break}}if(!e)return}this.service=e,t.extend(this.options,f[e])},detectParams:function(){var t=this.widget.data();if(t.counter){var e=parseInt(t.counter,10);isNaN(e)?this.options.counterUrl=t.counter:this.options.counterNumber=e}t.title&&(this.options.title=t.title),t.url&&(this.options.url=t.url)},initHtml:function(){var e=this.options,i=this.widget,n=i.find("a");n.length&&this.cloneDataAttrs(n,i);var o=t("<span>",{class:this.getElementClassNames("button"),html:i.html()});if(e.clickUrl){var r=s(e.clickUrl,{url:e.url,title:e.title}),c=t("<a>",{href:r});this.cloneDataAttrs(i,c),i.replaceWith(c),this.widget=c,i=c}else i.on("click",t.proxy(this.click,this));i.removeClass(this.service),i.addClass(this.getElementClassNames("widget")),o.prepend(t("<span>",{class:this.getElementClassNames("icon")})),i.empty().append(o),this.button=o},initCounter:function(){if(this.options.counters)if(this.options.counterNumber)this.updateCounter(this.options.counterNumber);else{var e={counterUrl:this.options.counterUrl,forceUpdate:this.options.forceUpdate};g.fetch(this.service,this.options.url,e).always(t.proxy(this.updateCounter,this))}},cloneDataAttrs:function(t,e){var i=t.data();for(var n in i)i.hasOwnProperty(n)&&e.data(n,i[n])},getElementClassNames:function(t){return c(t,this.service)},updateCounter:function(e){e=parseInt(e,10)||0;var i={class:this.getElementClassNames("counter"),text:e};e||this.options.zeroes||(i.class+=" "+l+"__counter_empty",i.text="");var n=this.widget.find("."+p+"counter_"+this.service);n.length||(n=t("<span>",i),this.widget.append(n)),this.widget.trigger("counter."+l,[this.service,e])},click:function(e){var i=this.options,n=!0;if(t.isFunction(i.click)&&(n=i.click.call(this,e)),n){var o=s(i.popupUrl,{url:i.url,title:i.title});o=this.addAdditionalParamsToUrl(o),this.openPopup(o,{width:i.popupWidth,height:i.popupHeight})}return!1},addAdditionalParamsToUrl:function(e){var i=t.param(t.extend(this.widget.data(),this.options.data));return t.isEmptyObject(i)?e:e+(-1===e.indexOf("?")?"?":"&")+i},openPopup:function(i,n){var o=window.screenLeft!==e?window.screenLeft:screen.left,s=window.screenTop!==e?window.screenTop:screen.top,r=window.innerWidth?window.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:screen.width,c=window.innerHeight?window.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:screen.height,u=Math.round(r/2-n.width/2)+o,a=0;c>n.height&&(a=Math.round(c/3-n.height/2)+s);var p=window.open(i,"sl_"+this.service,"left="+u+",top="+a+",width="+n.width+",height="+n.height+",personalbar=0,toolbar=0,scrollbars=1,resizable=1");if(p){p.focus(),this.widget.trigger("popup_opened."+l,[this.service,p]);var h=setInterval(t.proxy(function(){p.closed&&(clearInterval(h),this.widget.trigger("popup_closed."+l,this.service))},this),this.options.popupCheckInterval)}else location.href=i}},t(function(){t("."+l).socialLikes()})});