tooltip.js 12 KB

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