tooltip.js 14 KB

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