forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap-tooltip.js
More file actions
1 lines (1 loc) · 6.01 KB
/
bootstrap-tooltip.js
File metadata and controls
1 lines (1 loc) · 6.01 KB
1
!function($){"use strict";var Tooltip=function(element,options){this.init("tooltip",element,options)};Tooltip.prototype={constructor:Tooltip,init:function(type,element,options){var eventIn,eventOut,triggers,trigger,i;for(this.type=type,this.$element=$(element),this.options=this.getOptions(options),this.enabled=!0,i=(triggers=this.options.trigger.split(" ")).length;i--;)"click"==(trigger=triggers[i])?this.$element.on("click."+this.type,this.options.selector,$.proxy(this.toggle,this)):"manual"!=trigger&&(eventIn="hover"==trigger?"mouseenter":"focus",eventOut="hover"==trigger?"mouseleave":"blur",this.$element.on(eventIn+"."+this.type,this.options.selector,$.proxy(this.enter,this)),this.$element.on(eventOut+"."+this.type,this.options.selector,$.proxy(this.leave,this)));this.options.selector?this._options=$.extend({},this.options,{trigger:"manual",selector:""}):this.fixTitle()},getOptions:function(options){return(options=$.extend({},$.fn[this.type].defaults,this.$element.data(),options)).delay&&"number"==typeof options.delay&&(options.delay={show:options.delay,hide:options.delay}),options},enter:function(e){var defaults=$.fn[this.type].defaults,options={},self;if(this._options&&$.each(this._options,function(key,value){defaults[key]!=value&&(options[key]=value)},this),!(self=$(e.currentTarget)[this.type](options).data(this.type)).options.delay||!self.options.delay.show)return self.show();clearTimeout(this.timeout),self.hoverState="in",this.timeout=setTimeout(function(){"in"==self.hoverState&&self.show()},self.options.delay.show)},leave:function(e){var self=$(e.currentTarget)[this.type](this._options).data(this.type);if(this.timeout&&clearTimeout(this.timeout),!self.options.delay||!self.options.delay.hide)return self.hide();self.hoverState="out",this.timeout=setTimeout(function(){"out"==self.hoverState&&self.hide()},self.options.delay.hide)},show:function(){var $tip,pos,actualWidth,placement,tp,e=$.Event("show");if(this.hasContent()&&this.enabled){if(this.$element.trigger(e),e.isDefaultPrevented())return;switch($tip=this.tip(),this.setContent(),this.options.animation&&$tip.addClass("fade"),placement="function"==typeof this.options.placement?this.options.placement.call(this,$tip[0],this.$element[0]):this.options.placement,$tip.detach().css({top:0,left:0,display:"block"}),this.options.container?$tip.appendTo(this.options.container):$tip.insertAfter(this.$element),pos=this.getPosition(),actualWidth=$tip[0].offsetWidth,placement){case"bottom":tp={top:pos.top+pos.height,left:pos.left+pos.width/2-actualWidth/2};break;case"top":tp={top:pos.top-actualHeight,left:pos.left+pos.width/2-actualWidth/2};break;case"left":tp={top:pos.top+pos.height/2-actualHeight/2,left:pos.left-actualWidth};break;case"right":tp={top:pos.top+pos.height/2-actualHeight/2,left:pos.left+pos.width}}this.applyPlacement(tp,placement),this.$element.trigger("shown")}},applyPlacement:function(offset,placement){var $tip=this.tip(),width=$tip[0].offsetWidth,height=$tip[0].offsetHeight,actualWidth,actualHeight,delta,replace;$tip.offset(offset).addClass(placement).addClass("in"),actualWidth=$tip[0].offsetWidth,actualHeight=$tip[0].offsetHeight,"top"==placement&&actualHeight!=height&&(offset.top=offset.top+height-actualHeight,replace=!0),"bottom"==placement||"top"==placement?(delta=0,offset.left<0&&(delta=-2*offset.left,offset.left=0,$tip.offset(offset),actualWidth=$tip[0].offsetWidth,actualHeight=$tip[0].offsetHeight),this.replaceArrow(delta-width+actualWidth,actualWidth,"left")):this.replaceArrow(actualHeight-height,actualHeight,"top"),replace&&$tip.offset(offset)},replaceArrow:function(delta,dimension,position){this.arrow().css(position,delta?50*(1-delta/dimension)+"%":"")},setContent:function(){var $tip=this.tip(),title=this.getTitle();$tip.find(".tooltip-inner")[this.options.html?"html":"text"](title),$tip.removeClass("fade in top bottom left right")},hide:function(){var that=this,$tip=this.tip(),e=$.Event("hide");if(this.$element.trigger(e),!e.isDefaultPrevented())return $tip.removeClass("in"),$.support.transition&&this.$tip.hasClass("fade")?removeWithAnimation():$tip.detach(),this.$element.trigger("hidden"),this;function removeWithAnimation(){var timeout=setTimeout(function(){$tip.off($.support.transition.end).detach()},500);$tip.one($.support.transition.end,function(){clearTimeout(timeout),$tip.detach()})}},fixTitle:function(){var $e=this.$element;($e.attr("title")||"string"!=typeof $e.attr("data-original-title"))&&$e.attr("data-original-title",$e.attr("title")||"").attr("title","")},hasContent:function(){return this.getTitle()},getPosition:function(){var el=this.$element[0];return $.extend({},"function"==typeof el.getBoundingClientRect?el.getBoundingClientRect():{width:el.offsetWidth,height:el.offsetHeight},this.$element.offset())},getTitle:function(){var title,$e=this.$element,o=this.options;return title=$e.attr("data-original-title")||("function"==typeof o.title?o.title.call($e[0]):o.title)},tip:function(){return this.$tip=this.$tip||$(this.options.template)},arrow:function(){return this.$arrow=this.$arrow||this.tip().find(".tooltip-arrow")},validate:function(){this.$element[0].parentNode||(this.hide(),this.$element=null,this.options=null)},enable:function(){this.enabled=!0},disable:function(){this.enabled=!1},toggleEnabled:function(){this.enabled=!this.enabled},toggle:function(e){var self=e?$(e.currentTarget)[this.type](this._options).data(this.type):this;self.tip().hasClass("in")?self.hide():self.show()},destroy:function(){this.hide().$element.off("."+this.type).removeData(this.type)}};var old=$.fn.tooltip;$.fn.tooltip=function(option){return this.each(function(){var $this=$(this),data=$this.data("tooltip"),options="object"==typeof option&&option;data||$this.data("tooltip",data=new Tooltip(this,options)),"string"==typeof option&&data[option]()})},$.fn.tooltip.Constructor=Tooltip,$.fn.tooltip.defaults={animation:!0,placement:"top",selector:!1,template:'<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',trigger:"hover focus",title:"",delay:0,html:!1,container:!1},$.fn.tooltip.noConflict=function(){return $.fn.tooltip=old,this}}(window.jQuery);