tooltip.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* ========================================================================
  2. * Bootstrap: tooltip.js v3.3.7
  3. * http://getbootstrap.com/javascript/#tooltip
  4. * Inspired by the original jQuery.tipsy by Jason Frame
  5. * ========================================================================
  6. * Copyright 2011-2016 Twitter, Inc.
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  8. * ======================================================================== */
  9. +function ($) {
  10. 'use strict';
  11. // TOOLTIP PUBLIC CLASS DEFINITION
  12. // ===============================
  13. var Tooltip = function (element, options) {
  14. this.type = null
  15. this.options = null
  16. this.enabled = null
  17. this.timeout = null
  18. this.hoverState = null
  19. this.$element = null
  20. this.inState = null
  21. this.init('tooltip', element, options)
  22. }
  23. Tooltip.VERSION = '3.3.7'
  24. Tooltip.TRANSITION_DURATION = 150
  25. Tooltip.DEFAULTS = {
  26. animation: true,
  27. placement: 'top',
  28. selector: false,
  29. template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
  30. trigger: 'hover focus',
  31. title: '',
  32. delay: 0,
  33. html: false,
  34. container: false,
  35. viewport: {
  36. selector: 'body',
  37. padding: 0
  38. }
  39. }
  40. Tooltip.prototype.init = function (type, element, options) {
  41. this.enabled = true
  42. this.type = type
  43. this.$element = $(element)
  44. this.options = this.getOptions(options)
  45. this.$viewport = this.options.viewport && $($.isFunction(this.options.viewport) ? this.options.viewport.call(this, this.$element) : (this.options.viewport.selector || this.options.viewport))
  46. this.inState = { click: false, hover: false, focus: false }
  47. if (this.$element[0] instanceof document.constructor && !this.options.selector) {
  48. throw new Error('`selector` option must be specified when initializing ' + this.type + ' on the window.document object!')
  49. }
  50. var triggers = this.options.trigger.split(' ')
  51. for (var i = triggers.length; i--;) {
  52. var trigger = triggers[i]
  53. if (trigger == 'click') {
  54. this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
  55. } else if (trigger != 'manual') {
  56. var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin'
  57. var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
  58. this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
  59. this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
  60. }
  61. }
  62. this.options.selector ?
  63. (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
  64. this.fixTitle()
  65. }
  66. Tooltip.prototype.getDefaults = function () {
  67. return Tooltip.DEFAULTS
  68. }
  69. Tooltip.prototype.getOptions = function (options) {
  70. options = $.extend({}, this.getDefaults(), this.$element.data(), options)
  71. if (options.delay && typeof options.delay == 'number') {
  72. options.delay = {
  73. show: options.delay,
  74. hide: options.delay
  75. }
  76. }
  77. return options
  78. }
  79. Tooltip.prototype.getDelegateOptions = function () {
  80. var options = {}
  81. var defaults = this.getDefaults()
  82. this._options && $.each(this._options, function (key, value) {
  83. if (defaults[key] != value) options[key] = value
  84. })
  85. return options
  86. }
  87. Tooltip.prototype.enter = function (obj) {
  88. var self = obj instanceof this.constructor ?
  89. obj : $(obj.currentTarget).data('bs.' + this.type)
  90. if (!self) {
  91. self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
  92. $(obj.currentTarget).data('bs.' + this.type, self)
  93. }
  94. if (obj instanceof $.Event) {
  95. self.inState[obj.type == 'focusin' ? 'focus' : 'hover'] = true
  96. }
  97. if (self.tip().hasClass('in') || self.hoverState == 'in') {
  98. self.hoverState = 'in'
  99. return
  100. }
  101. clearTimeout(self.timeout)
  102. self.hoverState = 'in'
  103. if (!self.options.delay || !self.options.delay.show) return self.show()
  104. self.timeout = setTimeout(function () {
  105. if (self.hoverState == 'in') self.show()
  106. }, self.options.delay.show)
  107. }
  108. Tooltip.prototype.isInStateTrue = function () {
  109. for (var key in this.inState) {
  110. if (this.inState[key]) return true
  111. }
  112. return false
  113. }
  114. Tooltip.prototype.leave = function (obj) {
  115. var self = obj instanceof this.constructor ?
  116. obj : $(obj.currentTarget).data('bs.' + this.type)
  117. if (!self) {
  118. self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
  119. $(obj.currentTarget).data('bs.' + this.type, self)
  120. }
  121. if (obj instanceof $.Event) {
  122. self.inState[obj.type == 'focusout' ? 'focus' : 'hover'] = false
  123. }
  124. if (self.isInStateTrue()) return
  125. clearTimeout(self.timeout)
  126. self.hoverState = 'out'
  127. if (!self.options.delay || !self.options.delay.hide) return self.hide()
  128. self.timeout = setTimeout(function () {
  129. if (self.hoverState == 'out') self.hide()
  130. }, self.options.delay.hide)
  131. }
  132. Tooltip.prototype.show = function () {
  133. var e = $.Event('show.bs.' + this.type)
  134. if (this.hasContent() && this.enabled) {
  135. this.$element.trigger(e)
  136. var inDom = $.contains(this.$element[0].ownerDocument.documentElement, this.$element[0])
  137. if (e.isDefaultPrevented() || !inDom) return
  138. var that = this
  139. var $tip = this.tip()
  140. var tipId = this.getUID(this.type)
  141. this.setContent()
  142. $tip.attr('id', tipId)
  143. this.$element.attr('aria-describedby', tipId)
  144. if (this.options.animation) $tip.addClass('fade')
  145. var placement = typeof this.options.placement == 'function' ?
  146. this.options.placement.call(this, $tip[0], this.$element[0]) :
  147. this.options.placement
  148. var autoToken = /\s?auto?\s?/i
  149. var autoPlace = autoToken.test(placement)
  150. if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
  151. $tip
  152. .detach()
  153. .css({ top: 0, left: 0, display: 'block' })
  154. .addClass(placement)
  155. .data('bs.' + this.type, this)
  156. this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
  157. this.$element.trigger('inserted.bs.' + this.type)
  158. var pos = this.getPosition()
  159. var actualWidth = $tip[0].offsetWidth
  160. var actualHeight = $tip[0].offsetHeight
  161. if (autoPlace) {
  162. var orgPlacement = placement
  163. var viewportDim = this.getPosition(this.$viewport)
  164. placement = placement == 'bottom' && pos.bottom + actualHeight > viewportDim.bottom ? 'top' :
  165. placement == 'top' && pos.top - actualHeight < viewportDim.top ? 'bottom' :
  166. placement == 'right' && pos.right + actualWidth > viewportDim.width ? 'left' :
  167. placement == 'left' && pos.left - actualWidth < viewportDim.left ? 'right' :
  168. placement
  169. $tip
  170. .removeClass(orgPlacement)
  171. .addClass(placement)
  172. }
  173. var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
  174. this.applyPlacement(calculatedOffset, placement)
  175. var complete = function () {
  176. var prevHoverState = that.hoverState
  177. that.$element.trigger('shown.bs.' + that.type)
  178. that.hoverState = null
  179. if (prevHoverState == 'out') that.leave(that)
  180. }
  181. $.support.transition && this.$tip.hasClass('fade') ?
  182. $tip
  183. .one('bsTransitionEnd', complete)
  184. .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
  185. complete()
  186. }
  187. }
  188. Tooltip.prototype.applyPlacement = function (offset, placement) {
  189. var $tip = this.tip()
  190. var width = $tip[0].offsetWidth
  191. var height = $tip[0].offsetHeight
  192. // manually read margins because getBoundingClientRect includes difference
  193. var marginTop = parseInt($tip.css('margin-top'), 10)
  194. var marginLeft = parseInt($tip.css('margin-left'), 10)
  195. // we must check for NaN for ie 8/9
  196. if (isNaN(marginTop)) marginTop = 0
  197. if (isNaN(marginLeft)) marginLeft = 0
  198. offset.top += marginTop
  199. offset.left += marginLeft
  200. // $.fn.offset doesn't round pixel values
  201. // so we use setOffset directly with our own function B-0
  202. $.offset.setOffset($tip[0], $.extend({
  203. using: function (props) {
  204. $tip.css({
  205. top: Math.round(props.top),
  206. left: Math.round(props.left)
  207. })
  208. }
  209. }, offset), 0)
  210. $tip.addClass('in')
  211. // check to see if placing tip in new offset caused the tip to resize itself
  212. var actualWidth = $tip[0].offsetWidth
  213. var actualHeight = $tip[0].offsetHeight
  214. if (placement == 'top' && actualHeight != height) {
  215. offset.top = offset.top + height - actualHeight
  216. }
  217. var delta = this.getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight)
  218. if (delta.left) offset.left += delta.left
  219. else offset.top += delta.top
  220. var isVertical = /top|bottom/.test(placement)
  221. var arrowDelta = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
  222. var arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight'
  223. $tip.offset(offset)
  224. this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], isVertical)
  225. }
  226. Tooltip.prototype.replaceArrow = function (delta, dimension, isVertical) {
  227. this.arrow()
  228. .css(isVertical ? 'left' : 'top', 50 * (1 - delta / dimension) + '%')
  229. .css(isVertical ? 'top' : 'left', '')
  230. }
  231. Tooltip.prototype.setContent = function () {
  232. var $tip = this.tip()
  233. var title = this.getTitle()
  234. $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
  235. $tip.removeClass('fade in top bottom left right')
  236. }
  237. Tooltip.prototype.hide = function (callback) {
  238. var that = this
  239. var $tip = $(this.$tip)
  240. var e = $.Event('hide.bs.' + this.type)
  241. function complete() {
  242. if (that.hoverState != 'in') $tip.detach()
  243. if (that.$element) { // TODO: Check whether guarding this code with this `if` is really necessary.
  244. that.$element
  245. .removeAttr('aria-describedby')
  246. .trigger('hidden.bs.' + that.type)
  247. }
  248. callback && callback()
  249. }
  250. this.$element.trigger(e)
  251. if (e.isDefaultPrevented()) return
  252. $tip.removeClass('in')
  253. $.support.transition && $tip.hasClass('fade') ?
  254. $tip
  255. .one('bsTransitionEnd', complete)
  256. .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
  257. complete()
  258. this.hoverState = null
  259. return this
  260. }
  261. Tooltip.prototype.fixTitle = function () {
  262. var $e = this.$element
  263. if ($e.attr('title') || typeof $e.attr('data-original-title') != 'string') {
  264. $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
  265. }
  266. }
  267. Tooltip.prototype.hasContent = function () {
  268. return this.getTitle()
  269. }
  270. Tooltip.prototype.getPosition = function ($element) {
  271. $element = $element || this.$element
  272. var el = $element[0]
  273. var isBody = el.tagName == 'BODY'
  274. var elRect = el.getBoundingClientRect()
  275. if (elRect.width == null) {
  276. // width and height are missing in IE8, so compute them manually; see https://github.com/twbs/bootstrap/issues/14093
  277. elRect = $.extend({}, elRect, { width: elRect.right - elRect.left, height: elRect.bottom - elRect.top })
  278. }
  279. var isSvg = window.SVGElement && el instanceof window.SVGElement
  280. // Avoid using $.offset() on SVGs since it gives incorrect results in jQuery 3.
  281. // See https://github.com/twbs/bootstrap/issues/20280
  282. var elOffset = isBody ? { top: 0, left: 0 } : (isSvg ? null : $element.offset())
  283. var scroll = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop() }
  284. var outerDims = isBody ? { width: $(window).width(), height: $(window).height() } : null
  285. return $.extend({}, elRect, scroll, outerDims, elOffset)
  286. }
  287. Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
  288. return placement == 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
  289. placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
  290. placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
  291. /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
  292. }
  293. Tooltip.prototype.getViewportAdjustedDelta = function (placement, pos, actualWidth, actualHeight) {
  294. var delta = { top: 0, left: 0 }
  295. if (!this.$viewport) return delta
  296. var viewportPadding = this.options.viewport && this.options.viewport.padding || 0
  297. var viewportDimensions = this.getPosition(this.$viewport)
  298. if (/right|left/.test(placement)) {
  299. var topEdgeOffset = pos.top - viewportPadding - viewportDimensions.scroll
  300. var bottomEdgeOffset = pos.top + viewportPadding - viewportDimensions.scroll + actualHeight
  301. if (topEdgeOffset < viewportDimensions.top) { // top overflow
  302. delta.top = viewportDimensions.top - topEdgeOffset
  303. } else if (bottomEdgeOffset > viewportDimensions.top + viewportDimensions.height) { // bottom overflow
  304. delta.top = viewportDimensions.top + viewportDimensions.height - bottomEdgeOffset
  305. }
  306. } else {
  307. var leftEdgeOffset = pos.left - viewportPadding
  308. var rightEdgeOffset = pos.left + viewportPadding + actualWidth
  309. if (leftEdgeOffset < viewportDimensions.left) { // left overflow
  310. delta.left = viewportDimensions.left - leftEdgeOffset
  311. } else if (rightEdgeOffset > viewportDimensions.right) { // right overflow
  312. delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset
  313. }
  314. }
  315. return delta
  316. }
  317. Tooltip.prototype.getTitle = function () {
  318. var title
  319. var $e = this.$element
  320. var o = this.options
  321. title = $e.attr('data-original-title')
  322. || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
  323. return title
  324. }
  325. Tooltip.prototype.getUID = function (prefix) {
  326. do prefix += ~~(Math.random() * 1000000)
  327. while (document.getElementById(prefix))
  328. return prefix
  329. }
  330. Tooltip.prototype.tip = function () {
  331. if (!this.$tip) {
  332. this.$tip = $(this.options.template)
  333. if (this.$tip.length != 1) {
  334. throw new Error(this.type + ' `template` option must consist of exactly 1 top-level element!')
  335. }
  336. }
  337. return this.$tip
  338. }
  339. Tooltip.prototype.arrow = function () {
  340. return (this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow'))
  341. }
  342. Tooltip.prototype.enable = function () {
  343. this.enabled = true
  344. }
  345. Tooltip.prototype.disable = function () {
  346. this.enabled = false
  347. }
  348. Tooltip.prototype.toggleEnabled = function () {
  349. this.enabled = !this.enabled
  350. }
  351. Tooltip.prototype.toggle = function (e) {
  352. var self = this
  353. if (e) {
  354. self = $(e.currentTarget).data('bs.' + this.type)
  355. if (!self) {
  356. self = new this.constructor(e.currentTarget, this.getDelegateOptions())
  357. $(e.currentTarget).data('bs.' + this.type, self)
  358. }
  359. }
  360. if (e) {
  361. self.inState.click = !self.inState.click
  362. if (self.isInStateTrue()) self.enter(self)
  363. else self.leave(self)
  364. } else {
  365. self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
  366. }
  367. }
  368. Tooltip.prototype.destroy = function () {
  369. var that = this
  370. clearTimeout(this.timeout)
  371. this.hide(function () {
  372. that.$element.off('.' + that.type).removeData('bs.' + that.type)
  373. if (that.$tip) {
  374. that.$tip.detach()
  375. }
  376. that.$tip = null
  377. that.$arrow = null
  378. that.$viewport = null
  379. that.$element = null
  380. })
  381. }
  382. // TOOLTIP PLUGIN DEFINITION
  383. // =========================
  384. function Plugin(option) {
  385. return this.each(function () {
  386. var $this = $(this)
  387. var data = $this.data('bs.tooltip')
  388. var options = typeof option == 'object' && option
  389. if (!data && /destroy|hide/.test(option)) return
  390. if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
  391. if (typeof option == 'string') data[option]()
  392. })
  393. }
  394. var old = $.fn.tooltip
  395. $.fn.tooltip = Plugin
  396. $.fn.tooltip.Constructor = Tooltip
  397. // TOOLTIP NO CONFLICT
  398. // ===================
  399. $.fn.tooltip.noConflict = function () {
  400. $.fn.tooltip = old
  401. return this
  402. }
  403. }(jQuery);