1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
!function(c){"use strict";var s=function(t,e){this.type=this.options=this.enabled=this.timeout=this.hoverState=this.$element=null,this.init("tooltip",t,e)};s.DEFAULTS={animation:!0,placement:"top",selector:!1,template:'<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',trigger:"hover focus",title:"",delay:0,html:!1,container:!1,viewport:{selector:"body",padding:0}},s.prototype.init=function(t,e,i){this.enabled=!0,this.type=t,this.$element=c(e),this.options=this.getOptions(i),this.$viewport=this.options.viewport&&c(this.options.viewport.selector||this.options.viewport);for(var o=this.options.trigger.split(" "),s=o.length;s--;){var n=o[s];if("click"==n)this.$element.on("click."+this.type,this.options.selector,c.proxy(this.toggle,this));else if("manual"!=n){var r="hover"==n?"mouseenter":"focusin",p="hover"==n?"mouseleave":"focusout";this.$element.on(r+"."+this.type,this.options.selector,c.proxy(this.enter,this)),this.$element.on(p+"."+this.type,this.options.selector,c.proxy(this.leave,this))}}this.options.selector?this._options=c.extend({},this.options,{trigger:"manual",selector:""}):this.fixTitle()},s.prototype.getDefaults=function(){return s.DEFAULTS},s.prototype.getOptions=function(t){return(t=c.extend({},this.getDefaults(),this.$element.data(),t)).delay&&"number"==typeof t.delay&&(t.delay={show:t.delay,hide:t.delay}),t},s.prototype.getDelegateOptions=function(){var i={},o=this.getDefaults();return this._options&&c.each(this._options,function(t,e){o[t]!=e&&(i[t]=e)}),i},s.prototype.enter=function(t){var e=t instanceof this.constructor?t:c(t.currentTarget)[this.type](this.getDelegateOptions()).data("bs."+this.type);if(clearTimeout(e.timeout),e.hoverState="in",!e.options.delay||!e.options.delay.show)return e.show();e.timeout=setTimeout(function(){"in"==e.hoverState&&e.show()},e.options.delay.show)},s.prototype.leave=function(t){var e=t instanceof this.constructor?t:c(t.currentTarget)[this.type](this.getDelegateOptions()).data("bs."+this.type);if(clearTimeout(e.timeout),e.hoverState="out",!e.options.delay||!e.options.delay.hide)return e.hide();e.timeout=setTimeout(function(){"out"==e.hoverState&&e.hide()},e.options.delay.hide)},s.prototype.show=function(){var t=c.Event("show.bs."+this.type);if(this.hasContent()&&this.enabled){if(this.$element.trigger(t),t.isDefaultPrevented())return;var e=this,i=this.tip();this.setContent(),this.options.animation&&i.addClass("fade");var o="function"==typeof this.options.placement?this.options.placement.call(this,i[0],this.$element[0]):this.options.placement,s=/\s?auto?\s?/i,n=s.test(o);n&&(o=o.replace(s,"")||"top"),i.detach().css({top:0,left:0,display:"block"}).addClass(o),this.options.container?i.appendTo(this.options.container):i.insertAfter(this.$element);var r=this.getPosition(),p=i[0].offsetWidth,l=i[0].offsetHeight;if(n){var h=o,a=this.$element.parent(),f=this.getPosition(a);o="bottom"==o&&r.top+r.height+l-f.scroll>f.height?"top":"top"==o&&r.top-f.scroll-l<0?"bottom":"right"==o&&r.right+p>f.width?"left":"left"==o&&r.left-p<f.left?"right":o,i.removeClass(h).addClass(o)}var u=this.getCalculatedOffset(o,r,p,l);this.applyPlacement(u,o),this.hoverState=null;var d=function(){e.$element.trigger("shown.bs."+e.type)};c.support.transition&&this.$tip.hasClass("fade")?i.one(c.support.transition.end,d).emulateTransitionEnd(150):d()}},s.prototype.applyPlacement=function(t,e){var i=this.tip(),o=i[0].offsetWidth,s=i[0].offsetHeight,n=parseInt(i.css("margin-top"),10),r=parseInt(i.css("margin-left"),10);isNaN(n)&&(n=0),isNaN(r)&&(r=0),t.top=t.top+n,t.left=t.left+r,c.offset.setOffset(i[0],c.extend({using:function(t){i.css({top:Math.round(t.top),left:Math.round(t.left)})}},t),0),i.addClass("in");var p=i[0].offsetWidth,l=i[0].offsetHeight;"top"==e&&l!=s&&(t.top=t.top+s-l);var h=this.getViewportAdjustedDelta(e,t,p,l);h.left?t.left+=h.left:t.top+=h.top;var a=h.left?2*h.left-o+p:2*h.top-s+l,f=h.left?"left":"top",u=h.left?"offsetWidth":"offsetHeight";i.offset(t),this.replaceArrow(a,i[0][u],f)},s.prototype.replaceArrow=function(t,e,i){this.arrow().css(i,t?50*(1-t/e)+"%":"")},s.prototype.setContent=function(){var t=this.tip(),e=this.getTitle();t.find(".tooltip-inner")[this.options.html?"html":"text"](e),t.removeClass("fade in top bottom left right")},s.prototype.hide=function(){var t=this,e=this.tip(),i=c.Event("hide.bs."+this.type);function o(){"in"!=t.hoverState&&e.detach(),t.$element.trigger("hidden.bs."+t.type)}if(this.$element.trigger(i),!i.isDefaultPrevented())return e.removeClass("in"),c.support.transition&&this.$tip.hasClass("fade")?e.one(c.support.transition.end,o).emulateTransitionEnd(150):o(),this.hoverState=null,this},s.prototype.fixTitle=function(){var t=this.$element;(t.attr("title")||"string"!=typeof t.attr("data-original-title"))&&t.attr("data-original-title",t.attr("title")||"").attr("title","")},s.prototype.hasContent=function(){return this.getTitle()},s.prototype.getPosition=function(t){var e=(t=t||this.$element)[0],i="BODY"==e.tagName;return c.extend({},"function"==typeof e.getBoundingClientRect?e.getBoundingClientRect():null,{scroll:i?document.documentElement.scrollTop||document.body.scrollTop:t.scrollTop(),width:i?c(window).width():t.outerWidth(),height:i?c(window).height():t.outerHeight()},i?{top:0,left:0}:t.offset())},s.prototype.getCalculatedOffset=function(t,e,i,o){return"bottom"==t?{top:e.top+e.height,left:e.left+e.width/2-i/2}:"top"==t?{top:e.top-o,left:e.left+e.width/2-i/2}:"left"==t?{top:e.top+e.height/2-o/2,left:e.left-i}:{top:e.top+e.height/2-o/2,left:e.left+e.width}},s.prototype.getViewportAdjustedDelta=function(t,e,i,o){var s={top:0,left:0};if(!this.$viewport)return s;var n=this.options.viewport&&this.options.viewport.padding||0,r=this.getPosition(this.$viewport);if(/right|left/.test(t)){var p=e.top-n-r.scroll,l=e.top+n-r.scroll+o;p<r.top?s.top=r.top-p:l>r.top+r.height&&(s.top=r.top+r.height-l)}else{var h=e.left-n,a=e.left+n+i;h<r.left?s.left=r.left-h:a>r.width&&(s.left=r.left+r.width-a)}return s},s.prototype.getTitle=function(){var t=this.$element,e=this.options;return t.attr("data-original-title")||("function"==typeof e.title?e.title.call(t[0]):e.title)},s.prototype.tip=function(){return this.$tip=this.$tip||c(this.options.template)},s.prototype.arrow=function(){return this.$arrow=this.$arrow||this.tip().find(".tooltip-arrow")},s.prototype.validate=function(){this.$element[0].parentNode||(this.hide(),this.$element=null,this.options=null)},s.prototype.enable=function(){this.enabled=!0},s.prototype.disable=function(){this.enabled=!1},s.prototype.toggleEnabled=function(){this.enabled=!this.enabled},s.prototype.toggle=function(t){var e=t?c(t.currentTarget)[this.type](this.getDelegateOptions()).data("bs."+this.type):this;e.tip().hasClass("in")?e.leave(e):e.enter(e)},s.prototype.destroy=function(){clearTimeout(this.timeout),this.hide().$element.off("."+this.type).removeData("bs."+this.type)};var t=c.fn.tooltip;c.fn.tooltip=function(o){return this.each(function(){var t=c(this),e=t.data("bs.tooltip"),i="object"==typeof o&&o;(e||"destroy"!=o)&&(e||t.data("bs.tooltip",e=new s(this,i)),"string"==typeof o&&e[o]())})},c.fn.tooltip.Constructor=s,c.fn.tooltip.noConflict=function(){return c.fn.tooltip=t,this}}(jQuery); |