forked from urin/jquery.balloon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.balloon.min.js
1 lines (1 loc) · 6.02 KB
/
jquery.balloon.min.js
1
(function(e){function n(){this.initialize.apply(this,arguments)}function r(r,i,s){function p(e,t,n,r,i){var s=Math.round(r/1.7320508);t.inactive()["setBorder"+n.camel.pos.f](r)["setBorder"+n.camel.pos.c1](s)["setBorder"+n.camel.pos.c2](s)["set"+n.camel.pos.p1](n.isTopLeft?-r:e.inner[n.size.p])["set"+n.camel.pos.c1](e.inner[n.size.c]/2-s).active().$.css("border-"+n.pos.f+"-color",i)}i.stop(true,true);var o,u,a={position:"absolute",height:"0",width:"0",border:"solid 0 transparent"},f=new n(r),l=new n(i);l.setTop(-s.offsetY+(s.position&&s.position.indexOf("top")>=0?f.top-l.height:s.position&&s.position.indexOf("bottom")>=0?f.bottom:f.center.top-l.height/2));l.setLeft(s.offsetX+(s.position&&s.position.indexOf("left")>=0?f.left-l.width:s.position&&s.position.indexOf("right")>=0?f.right:f.center.left-l.width/2));if(s.tipSize>0){if(i.data("outerTip")){i.data("outerTip").remove();i.removeData("outerTip")}if(i.data("innerTip")){i.data("innerTip").remove();i.removeData("innerTip")}o=new n(e("<div>").css(a).appendTo(i));u=new n(e("<div>").css(a).appendTo(i));var c;for(var h=0;h<t.pos.length;h++){c=t.getRelativeNames(h);if(l.center[c.pos.c1]>=f[c.pos.c1]&&l.center[c.pos.c1]<=f[c.pos.c2]){if(h%2==0){if(l[c.pos.o]>=f[c.pos.o]&&l[c.pos.f]>=f[c.pos.f])break}else{if(l[c.pos.o]<=f[c.pos.o]&&l[c.pos.f]<=f[c.pos.f])break}}c=null}if(c){l["set"+c.camel.pos.p1](l[c.pos.p1]+(c.isTopLeft?1:-1)*(s.tipSize-l["border"+c.camel.pos.o]));p(l,o,c,s.tipSize,i.css("border-"+c.pos.o+"-color"));p(l,u,c,s.tipSize-2*l["border"+c.camel.pos.o],i.css("background-color"));i.data("outerTip",o.$).data("innerTip",u.$)}else{e.each([o.$,u.$],function(){this.remove()})}}}function i(t,n){var r=t.data("balloon")&&t.data("balloon").get(0);return!(r&&(r===n.relatedTarget||e.contains(r,n.relatedTarget)))}var t={};t.pos=e.extend(["top","bottom","left","right"],{camel:["Top","Bottom","Left","Right"]});t.size=e.extend(["height","width"],{camel:["Height","Width"]});t.getRelativeNames=function(e){var n={pos:{o:e,f:e%2==0?e+1:e-1,p1:e%2==0?e:e-1,p2:e%2==0?e+1:e,c1:e<2?2:0,c2:e<2?3:1},size:{p:e<2?0:1,c:e<2?1:0}};var r={};for(var i in n){if(!r[i])r[i]={};for(var s in n[i]){r[i][s]=t[i][n[i][s]];if(!r.camel)r.camel={};if(!r.camel[i])r.camel[i]={};r.camel[i][s]=t[i].camel[n[i][s]]}}r.isTopLeft=r.pos.o==r.pos.p1;return r};(function(){function s(e,n){if(n==undefined){s(e,true);return s(e,false)}var r=t.getRelativeNames(n?0:2);e[r.size.p]=e.$["outer"+r.camel.size.p]();e[r.pos.f]=e[r.pos.o]+e[r.size.p];e.center[r.pos.o]=e[r.pos.o]+e[r.size.p]/2;e.inner[r.pos.o]=e[r.pos.o]+e["border"+r.camel.pos.o];e.inner[r.size.p]=e.$["inner"+r.camel.size.p]();e.inner[r.pos.f]=e.inner[r.pos.o]+e.inner[r.size.p];e.inner.center[r.pos.o]=e.inner[r.pos.f]+e.inner[r.size.p]/2;return e}var r={setBorder:function(e,t){return function(n){this.$.css("border-"+e.toLowerCase()+"-width",n+"px");this["border"+e]=n;return this.isActive?s(this,t):this}},setPosition:function(e,t){return function(n){this.$.css(e.toLowerCase(),n+"px");this[e.toLowerCase()]=n;return this.isActive?s(this,t):this}}};n.prototype={initialize:function(n){this.$=n;e.extend(true,this,this.$.offset(),{center:{},inner:{center:{}}});for(var r=0;r<t.pos.length;r++){this["border"+t.pos.camel[r]]=parseInt(this.$.css("border-"+t.pos[r]+"-width"))||0}this.active()},active:function(){this.isActive=true;s(this);return this},inactive:function(){this.isActive=false;return this}};for(var i=0;i<t.pos.length;i++){n.prototype["setBorder"+t.pos.camel[i]]=r.setBorder(t.pos.camel[i],i<2);if(i%2==0)n.prototype["set"+t.pos.camel[i]]=r.setPosition(t.pos.camel[i],i<2)}})();e.fn.balloon=function(t){return this.one("mouseenter",function(n){var r=e(this),s=this;var o=r.unbind("mouseenter",arguments.callee).showBalloon(t).mouseenter(function(e){i(r,e)&&r.showBalloon()}).data("balloon");if(o){o.mouseleave(function(t){if(s===t.relatedTarget||e.contains(s,t.relatedTarget))return;r.hideBalloon()}).mouseenter(function(e){o.stop(true,true);r.showBalloon()})}}).mouseleave(function(t){var n=e(this);i(n,t)&&n.hideBalloon()})};e.fn.showBalloon=function(t){var n,i,s;if(t||!this.data("options")){if(e.balloon.defaults.css===null)e.balloon.defaults.css={};this.data("options",e.extend(true,{},e.balloon.defaults,t||{}))}t=this.data("options");return this.each(function(){var o,u;n=e(this);(s=n.data("offTimer"))&&clearTimeout(s);u=e.isFunction(t.contents)?t.contents():t.contents||(t.contents=n.attr("title")||n.attr("alt"));o=!(i=n.data("balloon"));if(o)i=e("<div>").append(u);if(!t.url&&(!i||i.html()==""))return;if(!o&&u&&u!=i.html())i.empty().append(u);n.removeAttr("title");if(t.url){i.load(e.isFunction(t.url)?t.url(this):t.url,function(e,s,o){if(t.ajaxComplete)t.ajaxComplete(e,s,o);r(n,i,t)})}if(o){i.addClass(t.classname).css(t.css||{}).css({visibility:"hidden",position:"absolute"}).appendTo("body");n.data("balloon",i);r(n,i,t);i.hide().css("visibility","visible")}else{r(n,i,t)}n.data("onTimer",setTimeout(function(){if(t.showAnimation){t.showAnimation.apply(i.stop(true,true),[t.showDuration,t.showComplete])}else{i.show(t.showDuration,function(){if(this.style.removeAttribute){this.style.removeAttribute("filter")}t.showComplete&&t.showComplete.apply(i)})}},t.delay))})};e.fn.hideBalloon=function(){var t=this.data("options"),n,r;if(!this.data("balloon"))return this;return this.each(function(){var i=e(this);(n=i.data("onTimer"))&&clearTimeout(n);(r=i.data("offTimer"))&&clearTimeout(r);i.data("offTimer",setTimeout(function(){var e=i.data("balloon");if(t.hideAnimation){e&&t.hideAnimation.apply(e.stop(true,true),[t.hideDuration,t.hideComplete])}else{e&&e.stop(true,true).hide(t.hideDuration,t.hideComplete)}},t.minLifetime))})};e.balloon={defaults:{contents:null,url:null,ajaxComplete:null,classname:null,position:"top",offsetX:0,offsetY:0,tipSize:12,delay:0,minLifetime:200,showDuration:100,showAnimation:null,hideDuration:80,hideAnimation:function(e){this.fadeOut(e)},showComplete:null,hideComplete:null,css:{minWidth:"20px",padding:"5px",borderRadius:"6px",border:"solid 1px #777",boxShadow:"4px 4px 4px #555",color:"#666",backgroundColor:"#efefef",opacity:e.support.opacity?"0.85":null,zIndex:"32767",textAlign:"left"}}}})(jQuery)