-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.external_links.min.js
1 lines (1 loc) · 1.81 KB
/
jquery.external_links.min.js
1
!function(e){var t="$external-link-data",n={favicon:!0,favicon_url:"http://favicon.yandex.net/favicon",url_seperator:"/",link_class:"external-link",link_selector:"a",span_class:"ext-favicon",span_position:"right",special_hosts:[[/github\.com$/,"github.com"]]},r={init:function(r){if(this.data(t))return this;r&&e.isArray(r.special_hosts)&&e.merge(r.special_hosts,n.special_hosts),r=e.extend({},n,r);var i="prependTo",s=r.favicon?r.favicon_url:null,o=[],u=[],a=typeof r.link_selector=="string"&&r.link_selector.length?this.filter(r.link_selector):this;return"right"===r.span_position&&(i="appendTo"),a.addClass(r.link_class).each(function(n){var i=e(this),a=this.hostname;if(s){e.isArray(r.special_hosts)&&e.each(r.special_hosts,function(t,n){if(!e.isArray(n)||n.length!==2)return;a.match(n[0])&&(a=n[1])});var f=u.indexOf(a);f===-1?(s+=r.url_seperator+a,o[n]=u.length,u.push(a)):o[n]=f}i.data(t,{link_class:r.link_class,span_class:s?r.span_class:null,orig_target:i.attr("target"),favicon_url:s,span_position:r.span_position==="right"?"right":"left"})}).attr("target","_blank"),s&&a.each(function(t){e('<span class="'+r.span_class+'" style="background-image: url('+s+");background-position : right -"+o[t]*16+'px;"></span>')[i](this)}),this},destroy:function(){return this.each(function(n){var r=e(this),i=r.data(t);if(!e.isPlainObject(i)||!i.span_position)return;i.link_class&&r.removeClass(i.link_class);if(i.favicon_url){var s=i.span_class?"."+i.span_class:"",o=i.span_position==="right"?":last-child":":first-child";r.children("span"+o+s).remove()}r.attr("target",i.orig_target?i.orig_target:null)})}};e.fn.externalLinks=function(t){if(r[t])return r[t].apply(this,Array.prototype.slice.call(arguments,1));if(typeof t=="object"||!t)return r.init.apply(this,arguments);e.error("Method "+t+" does not exist on jQuery.externalLinks")}}(window.jQuery);