1 /* ========================================================================
2 * Bootstrap: tooltip.js v3.3.4
3 * http://getbootstrap.com/javascript/#tooltip
4 * Inspired by the original jQuery.tipsy by Jason Frame
5 * ========================================================================
6 * Copyright 2011-2015 Twitter, Inc.
7 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8 * ======================================================================== */
14 // TOOLTIP PUBLIC CLASS DEFINITION
15 // ===============================
17 var Tooltip = function (element, options) {
22 this.hoverState = null
25 this.init('tooltip', element, options)
28 Tooltip.VERSION = '3.3.4'
30 Tooltip.TRANSITION_DURATION = 150
36 template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
37 trigger: 'hover focus',
48 Tooltip.prototype.init = function (type, element, options) {
51 this.$element = $(element)
52 this.options = this.getOptions(options)
53 this.$viewport = this.options.viewport && $(this.options.viewport.selector || this.options.viewport)
55 if (this.$element[0] instanceof document.constructor && !this.options.selector) {
56 throw new Error('`selector` option must be specified when initializing ' + this.type + ' on the window.document object!')
59 var triggers = this.options.trigger.split(' ')
61 for (var i = triggers.length; i--;) {
62 var trigger = triggers[i]
64 if (trigger == 'click') {
65 this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
66 } else if (trigger != 'manual') {
67 var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin'
68 var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
70 this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
71 this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
75 this.options.selector ?
76 (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
80 Tooltip.prototype.getDefaults = function () {
81 return Tooltip.DEFAULTS
84 Tooltip.prototype.getOptions = function (options) {
85 options = $.extend({}, this.getDefaults(), this.$element.data(), options)
87 if (options.delay && typeof options.delay == 'number') {
97 Tooltip.prototype.getDelegateOptions = function () {
99 var defaults = this.getDefaults()
101 this._options && $.each(this._options, function (key, value) {
102 if (defaults[key] != value) options[key] = value
108 Tooltip.prototype.enter = function (obj) {
109 var self = obj instanceof this.constructor ?
110 obj : $(obj.currentTarget).data('bs.' + this.type)
112 if (self && self.$tip && self.$tip.is(':visible')) {
113 self.hoverState = 'in'
118 self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
119 $(obj.currentTarget).data('bs.' + this.type, self)
122 clearTimeout(self.timeout)
124 self.hoverState = 'in'
126 if (!self.options.delay || !self.options.delay.show) return self.show()
128 self.timeout = setTimeout(function () {
129 if (self.hoverState == 'in') self.show()
130 }, self.options.delay.show)
133 Tooltip.prototype.leave = function (obj) {
134 var self = obj instanceof this.constructor ?
135 obj : $(obj.currentTarget).data('bs.' + this.type)
138 self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
139 $(obj.currentTarget).data('bs.' + this.type, self)
142 clearTimeout(self.timeout)
144 self.hoverState = 'out'
146 if (!self.options.delay || !self.options.delay.hide) return self.hide()
148 self.timeout = setTimeout(function () {
149 if (self.hoverState == 'out') self.hide()
150 }, self.options.delay.hide)
153 Tooltip.prototype.show = function () {
154 var e = $.Event('show.bs.' + this.type)
156 if (this.hasContent() && this.enabled) {
157 this.$element.trigger(e)
159 var inDom = $.contains(this.$element[0].ownerDocument.documentElement, this.$element[0])
160 if (e.isDefaultPrevented() || !inDom) return
163 var $tip = this.tip()
165 var tipId = this.getUID(this.type)
168 $tip.attr('id', tipId)
169 this.$element.attr('aria-describedby', tipId)
171 if (this.options.animation) $tip.addClass('fade')
173 var placement = typeof this.options.placement == 'function' ?
174 this.options.placement.call(this, $tip[0], this.$element[0]) :
175 this.options.placement
177 var autoToken = /\s?auto?\s?/i
178 var autoPlace = autoToken.test(placement)
179 if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
183 .css({ top: 0, left: 0, display: 'block' })
185 .data('bs.' + this.type, this)
187 this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
189 var pos = this.getPosition()
190 var actualWidth = $tip[0].offsetWidth
191 var actualHeight = $tip[0].offsetHeight
194 var orgPlacement = placement
195 var $container = this.options.container ? $(this.options.container) : this.$element.parent()
196 var containerDim = this.getPosition($container)
198 placement = placement == 'bottom' && pos.bottom + actualHeight > containerDim.bottom ? 'top' :
199 placement == 'top' && pos.top - actualHeight < containerDim.top ? 'bottom' :
200 placement == 'right' && pos.right + actualWidth > containerDim.width ? 'left' :
201 placement == 'left' && pos.left - actualWidth < containerDim.left ? 'right' :
205 .removeClass(orgPlacement)
209 var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
211 this.applyPlacement(calculatedOffset, placement)
213 var complete = function () {
214 var prevHoverState = that.hoverState
215 that.$element.trigger('shown.bs.' + that.type)
216 that.hoverState = null
218 if (prevHoverState == 'out') that.leave(that)
221 $.support.transition && this.$tip.hasClass('fade') ?
223 .one('bsTransitionEnd', complete)
224 .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
229 Tooltip.prototype.applyPlacement = function (offset, placement) {
230 var $tip = this.tip()
231 var width = $tip[0].offsetWidth
232 var height = $tip[0].offsetHeight
234 // manually read margins because getBoundingClientRect includes difference
235 var marginTop = parseInt($tip.css('margin-top'), 10)
236 var marginLeft = parseInt($tip.css('margin-left'), 10)
238 // we must check for NaN for ie 8/9
239 if (isNaN(marginTop)) marginTop = 0
240 if (isNaN(marginLeft)) marginLeft = 0
242 offset.top = offset.top + marginTop
243 offset.left = offset.left + marginLeft
245 // $.fn.offset doesn't round pixel values
246 // so we use setOffset directly with our own function B-0
247 $.offset.setOffset($tip[0], $.extend({
248 using: function (props) {
250 top: Math.round(props.top),
251 left: Math.round(props.left)
258 // check to see if placing tip in new offset caused the tip to resize itself
259 var actualWidth = $tip[0].offsetWidth
260 var actualHeight = $tip[0].offsetHeight
262 if (placement == 'top' && actualHeight != height) {
263 offset.top = offset.top + height - actualHeight
266 var delta = this.getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight)
268 if (delta.left) offset.left += delta.left
269 else offset.top += delta.top
271 var isVertical = /top|bottom/.test(placement)
272 var arrowDelta = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
273 var arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight'
276 this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], isVertical)
279 Tooltip.prototype.replaceArrow = function (delta, dimension, isVertical) {
281 .css(isVertical ? 'left' : 'top', 50 * (1 - delta / dimension) + '%')
282 .css(isVertical ? 'top' : 'left', '')
285 Tooltip.prototype.setContent = function () {
286 var $tip = this.tip()
287 var title = this.getTitle()
289 $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
290 $tip.removeClass('fade in top bottom left right')
293 Tooltip.prototype.hide = function (callback) {
295 var $tip = $(this.$tip)
296 var e = $.Event('hide.bs.' + this.type)
298 function complete() {
299 if (that.hoverState != 'in') $tip.detach()
301 .removeAttr('aria-describedby')
302 .trigger('hidden.bs.' + that.type)
303 callback && callback()
306 this.$element.trigger(e)
308 if (e.isDefaultPrevented()) return
310 $tip.removeClass('in')
312 $.support.transition && $tip.hasClass('fade') ?
314 .one('bsTransitionEnd', complete)
315 .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
318 this.hoverState = null
323 Tooltip.prototype.fixTitle = function () {
324 var $e = this.$element
325 if ($e.attr('title') || typeof ($e.attr('data-original-title')) != 'string') {
326 $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
330 Tooltip.prototype.hasContent = function () {
331 return this.getTitle()
334 Tooltip.prototype.getPosition = function ($element) {
335 $element = $element || this.$element
338 var isBody = el.tagName == 'BODY'
340 var elRect = el.getBoundingClientRect()
341 if (elRect.width == null) {
342 // width and height are missing in IE8, so compute them manually; see https://github.com/twbs/bootstrap/issues/14093
343 elRect = $.extend({}, elRect, { width: elRect.right - elRect.left, height: elRect.bottom - elRect.top })
345 var elOffset = isBody ? { top: 0, left: 0 } : $element.offset()
346 var scroll = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop() }
347 var outerDims = isBody ? { width: $(window).width(), height: $(window).height() } : null
349 return $.extend({}, elRect, scroll, outerDims, elOffset)
352 Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
353 return placement == 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
354 placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
355 placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
356 /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
360 Tooltip.prototype.getViewportAdjustedDelta = function (placement, pos, actualWidth, actualHeight) {
361 var delta = { top: 0, left: 0 }
362 if (!this.$viewport) return delta
364 var viewportPadding = this.options.viewport && this.options.viewport.padding || 0
365 var viewportDimensions = this.getPosition(this.$viewport)
367 if (/right|left/.test(placement)) {
368 var topEdgeOffset = pos.top - viewportPadding - viewportDimensions.scroll
369 var bottomEdgeOffset = pos.top + viewportPadding - viewportDimensions.scroll + actualHeight
370 if (topEdgeOffset < viewportDimensions.top) { // top overflow
371 delta.top = viewportDimensions.top - topEdgeOffset
372 } else if (bottomEdgeOffset > viewportDimensions.top + viewportDimensions.height) { // bottom overflow
373 delta.top = viewportDimensions.top + viewportDimensions.height - bottomEdgeOffset
376 var leftEdgeOffset = pos.left - viewportPadding
377 var rightEdgeOffset = pos.left + viewportPadding + actualWidth
378 if (leftEdgeOffset < viewportDimensions.left) { // left overflow
379 delta.left = viewportDimensions.left - leftEdgeOffset
380 } else if (rightEdgeOffset > viewportDimensions.width) { // right overflow
381 delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset
388 Tooltip.prototype.getTitle = function () {
390 var $e = this.$element
393 title = $e.attr('data-original-title')
394 || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
399 Tooltip.prototype.getUID = function (prefix) {
400 do prefix += ~~(Math.random() * 1000000)
401 while (document.getElementById(prefix))
405 Tooltip.prototype.tip = function () {
406 return (this.$tip = this.$tip || $(this.options.template))
409 Tooltip.prototype.arrow = function () {
410 return (this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow'))
413 Tooltip.prototype.enable = function () {
417 Tooltip.prototype.disable = function () {
421 Tooltip.prototype.toggleEnabled = function () {
422 this.enabled = !this.enabled
425 Tooltip.prototype.toggle = function (e) {
428 self = $(e.currentTarget).data('bs.' + this.type)
430 self = new this.constructor(e.currentTarget, this.getDelegateOptions())
431 $(e.currentTarget).data('bs.' + this.type, self)
435 self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
438 Tooltip.prototype.destroy = function () {
440 clearTimeout(this.timeout)
441 this.hide(function () {
442 that.$element.off('.' + that.type).removeData('bs.' + that.type)
447 // TOOLTIP PLUGIN DEFINITION
448 // =========================
450 function Plugin(option) {
451 return this.each(function () {
453 var data = $this.data('bs.tooltip')
454 var options = typeof option == 'object' && option
456 if (!data && /destroy|hide/.test(option)) return
457 if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
458 if (typeof option == 'string') data[option]()
462 var old = $.fn.tooltip
464 $.fn.tooltip = Plugin
465 $.fn.tooltip.Constructor = Tooltip
468 // TOOLTIP NO CONFLICT
469 // ===================
471 $.fn.tooltip.noConflict = function () {