forked from lazerwalker/TipsterJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tipster.min.js
5 lines (5 loc) · 3.98 KB
/
jquery.tipster.min.js
1
2
3
4
5
/* Tipster.js
* Michael Walker
* https://github.com/lazerwalker/tipster-js
* Licensed under the MIT License
*/(function(e,t,n){e.fn.tooltip=function(r){function c(e){function t(t){if(Object.prototype.toString.call(e[t])=="[object String]"){val=+e[t].replace(/px$/,"");if(val!==val)throw"'"+e[t]+"' is not a valid "+t;e[t]=val}}if(e.type===n)throw"Invalid tooltip type: "+e.typeName;if(e.side===n)throw"Invalid side: "+e.sideName;return u.autoHeight||t("height"),u.autoWidth||t("width"),t("margin"),e}function h(){typeof u.onDestroy=="function"&&u.onDestroy(),e(t).off("resize updateTooltips",y)}function p(){e(".tooltip:not(.important, .hover)").remove(),e(".hasTooltip:not(.hasImportantTooltip)").removeClass("hasTooltip"),e("div.tooltip:not(.important,.hover)").size()===0&&e("body").off("click keyup",p),h()}function d(){e(".tooltip").remove(),e(".hasTooltip").removeClass("hasTooltip"),e("body").off("touchend",d),h()}function v(){var t=function(){e("div.tooltip.hover:not(.activeHover)").remove(),e(".hasTooltip.hasHoverTooltip").removeClass("hasTooltip hasHoverTooltip"),h()};setTimeout(t,100),e(this).off("mouseleave",v)}function m(t){l=p;switch(u.type){case o.touch:a.hasClass("hasTooltip")&&(d(),t.stopPropagation()),l=d,d();break;case o.hover:l=v;break;case o.important:l=e.tooltip.closeImportant}return l}function g(){var t=12,n=e.extend(a.offset(),{height:a.innerHeight(),width:a.width()}),r={width:u.width};u.autoHeight||(r.height=u.height),u.autoWidth||(r.width=u.width);switch(u.side){case s.top:r.top=n.top-(u.height+t+u.margin),r.left=n.left-(u.width-n.width)/2;break;case s.bottom:r.top=n.top+n.height+t+u.margin,r.left=n.left-(u.width-n.width)/2;break;case s.left:r.left=n.left-(u.width+t+u.margin),r.top=n.top-(u.height-n.height)/2;break;case s.right:r.left=n.left+n.width+t+u.margin,r.top=n.top-(u.height-n.height)/2}return r}function y(){f.css(g())}function b(){var t;u.attribute!==n&&a.attr(u.attribute)!==n?t=a.attr(u.attribute):typeof u.content=="function"?t=u.content():t=u.content,f=e("<div class='tooltip "+u.sideName+"'/>").html(t).css(g()).append("<div class='tail'><div class='shadow'></div></div>").appendTo("body"),u.className&&f.addClass(u.className),u.autoHeight&&(u.height=f.height(),u.autoHeight=!1,y()),u.autoWidth&&(u.width=f.width(),u.autoWidth=!1,y()),typeof u.forceInsideBounds=="function"&&u.forceInsideBounds(f),typeof u.onCreate=="function"&&u.onCreate()}function w(){switch(u.type){case o.standard:e("body").on("click keyup",l);break;case o.touch:e("body").on("touchend",l);break;case o.hover:f.addClass("hover"),a.addClass("hasHoverTooltip"),a.on("mouseleave",l),u.canMouseIn&&f.on("mouseenter mouseleave",function(){f.toggleClass("activeHover"),l()});break;case o.important:f.addClass("important"),a.addClass("hasImportantTooltip")}e(t).on("resize updateTooltips",y)}function E(t){var n=t.find("div.tail"),r=e(u.boundingElem),i=r.offset(),o=n.position(),a=u.side===s.top||u.side===s.bottom,f=e.extend(t.position(),{width:t.width(),height:t.height()}),l={},c={},h=!1,p=r.outerWidth()+i.left-f.left;p<f.width&&(l.left=f.left+(p-f.width),a&&(c.left=o.left-(p-f.width)),h=!0);var d=r.outerHeight()+i.top-(f.top+f.height);d<0&&(l.top=f.top+(d-10),a||(c.top=o.top-d),h=!0),h&&(t.css(l),n.css(c))}var i={content:"",attribute:n,side:"top",width:"auto",height:"auto",margin:10,type:"standard",touch:!1,canMouseIn:!1,forceInsideBounds:E,boundingElem:"body",onDestroy:function(){},onCreate:function(){}},s={top:0,right:1,bottom:2,left:3},o={standard:0,hover:1,important:2,touch:3},u=e.extend(i,r);u.typeName=u.type,u.type=o[u.type],u.sideName=u.side,u.side=s[u.side],u.touch&&(u.type=o.touch);if(u.height==="auto"||!u.height)u.autoHeight=!0;if(u.width==="auto"||!u.width)u.autoWidth=!0;c(u);var a,f,l;return this.each(function(t){return a=e(this),m(t),a.hasClass("hasTooltip")?(l(),this):(a.addClass("hasTooltip"),b(),w(),typeof u.onCreate=="function"&&u.onCreate(t),this)})},e.tooltip={closeImportant:function(){e(".tooltip").remove(),e(".hasTooltip.hasImportantTooltip").removeClass("hasTooltip hasImportantTooltip")}}})(jQuery,this);