tooltip.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* ========================================================================
  2. * Bootstrap: tooltip.js v3.0.3
  3. * http://getbootstrap.com/javascript/#tooltip
  4. * Inspired by the original jQuery.tipsy by Jason Frame
  5. * ========================================================================
  6. * Copyright 2013 Twitter, Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ======================================================================== */
  20. +function ($) { "use strict";
  21. // TOOLTIP PUBLIC CLASS DEFINITION
  22. // ===============================
  23. var Tooltip = function (element, options) {
  24. this.type =
  25. this.options =
  26. this.enabled =
  27. this.timeout =
  28. this.hoverState =
  29. this.$element = null
  30. this.init('tooltip', element, options)
  31. }
  32. Tooltip.DEFAULTS = {
  33. animation: true
  34. , placement: 'top'
  35. , selector: false
  36. , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  37. , trigger: 'hover focus'
  38. , title: ''
  39. , delay: 0
  40. , html: false
  41. , container: false
  42. }
  43. Tooltip.prototype.init = function (type, element, options) {
  44. this.enabled = true
  45. this.type = type
  46. this.$element = $(element)
  47. this.options = this.getOptions(options)
  48. var triggers = this.options.trigger.split(' ')
  49. for (var i = triggers.length; i--;) {
  50. var trigger = triggers[i]
  51. if (trigger == 'click') {
  52. this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
  53. } else if (trigger != 'manual') {
  54. var eventIn = trigger == 'hover' ? 'mouseenter' : 'focus'
  55. var eventOut = trigger == 'hover' ? 'mouseleave' : 'blur'
  56. this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
  57. this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
  58. }
  59. }
  60. this.options.selector ?
  61. (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
  62. this.fixTitle()
  63. }
  64. Tooltip.prototype.getDefaults = function () {
  65. return Tooltip.DEFAULTS
  66. }
  67. Tooltip.prototype.getOptions = function (options) {
  68. options = $.extend({}, this.getDefaults(), this.$element.data(), options)
  69. if (options.delay && typeof options.delay == 'number') {
  70. options.delay = {
  71. show: options.delay
  72. , hide: options.delay
  73. }
  74. }
  75. return options
  76. }
  77. Tooltip.prototype.getDelegateOptions = function () {
  78. var options = {}
  79. var defaults = this.getDefaults()
  80. this._options && $.each(this._options, function (key, value) {
  81. if (defaults[key] != value) options[key] = value
  82. })
  83. return options
  84. }
  85. Tooltip.prototype.enter = 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 = '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)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  98. clearTimeout(self.timeout)
  99. self.hoverState = 'out'
  100. if (!self.options.delay || !self.options.delay.hide) return self.hide()
  101. self.timeout = setTimeout(function () {
  102. if (self.hoverState == 'out') self.hide()
  103. }, self.options.delay.hide)
  104. }
  105. Tooltip.prototype.show = function () {
  106. var e = $.Event('show.bs.'+ this.type)
  107. if (this.hasContent() && this.enabled) {
  108. this.$element.trigger(e)
  109. if (e.isDefaultPrevented()) return
  110. var $tip = this.tip()
  111. this.setContent()
  112. if (this.options.animation) $tip.addClass('fade')
  113. var placement = typeof this.options.placement == 'function' ?
  114. this.options.placement.call(this, $tip[0], this.$element[0]) :
  115. this.options.placement
  116. var autoToken = /\s?auto?\s?/i
  117. var autoPlace = autoToken.test(placement)
  118. if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
  119. $tip
  120. .detach()
  121. .css({ top: 0, left: 0, display: 'block' })
  122. .addClass(placement)
  123. this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
  124. var pos = this.getPosition()
  125. var actualWidth = $tip[0].offsetWidth
  126. var actualHeight = $tip[0].offsetHeight
  127. if (autoPlace) {
  128. var $parent = this.$element.parent()
  129. var orgPlacement = placement
  130. var docScroll = document.documentElement.scrollTop || document.body.scrollTop
  131. var parentWidth = this.options.container == 'body' ? window.innerWidth : $parent.outerWidth()
  132. var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
  133. var parentLeft = this.options.container == 'body' ? 0 : $parent.offset().left
  134. placement = placement == 'bottom' && pos.top + pos.height + actualHeight - docScroll > parentHeight ? 'top' :
  135. placement == 'top' && pos.top - docScroll - actualHeight < 0 ? 'bottom' :
  136. placement == 'right' && pos.right + actualWidth > parentWidth ? 'left' :
  137. placement == 'left' && pos.left - actualWidth < parentLeft ? 'right' :
  138. placement
  139. $tip
  140. .removeClass(orgPlacement)
  141. .addClass(placement)
  142. }
  143. var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
  144. this.applyPlacement(calculatedOffset, placement)
  145. this.$element.trigger('shown.bs.' + this.type)
  146. }
  147. }
  148. Tooltip.prototype.applyPlacement = function(offset, placement) {
  149. var replace
  150. var $tip = this.tip()
  151. var width = $tip[0].offsetWidth
  152. var height = $tip[0].offsetHeight
  153. // manually read margins because getBoundingClientRect includes difference
  154. var marginTop = parseInt($tip.css('margin-top'), 10)
  155. var marginLeft = parseInt($tip.css('margin-left'), 10)
  156. // we must check for NaN for ie 8/9
  157. if (isNaN(marginTop)) marginTop = 0
  158. if (isNaN(marginLeft)) marginLeft = 0
  159. offset.top = offset.top + marginTop
  160. offset.left = offset.left + marginLeft
  161. $tip
  162. .offset(offset)
  163. .addClass('in')
  164. // check to see if placing tip in new offset caused the tip to resize itself
  165. var actualWidth = $tip[0].offsetWidth
  166. var actualHeight = $tip[0].offsetHeight
  167. if (placement == 'top' && actualHeight != height) {
  168. replace = true
  169. offset.top = offset.top + height - actualHeight
  170. }
  171. if (/bottom|top/.test(placement)) {
  172. var delta = 0
  173. if (offset.left < 0) {
  174. delta = offset.left * -2
  175. offset.left = 0
  176. $tip.offset(offset)
  177. actualWidth = $tip[0].offsetWidth
  178. actualHeight = $tip[0].offsetHeight
  179. }
  180. this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
  181. } else {
  182. this.replaceArrow(actualHeight - height, actualHeight, 'top')
  183. }
  184. if (replace) $tip.offset(offset)
  185. }
  186. Tooltip.prototype.replaceArrow = function(delta, dimension, position) {
  187. this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
  188. }
  189. Tooltip.prototype.setContent = function () {
  190. var $tip = this.tip()
  191. var title = this.getTitle()
  192. $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
  193. $tip.removeClass('fade in top bottom left right')
  194. }
  195. Tooltip.prototype.hide = function () {
  196. var that = this
  197. var $tip = this.tip()
  198. var e = $.Event('hide.bs.' + this.type)
  199. function complete() {
  200. if (that.hoverState != 'in') $tip.detach()
  201. }
  202. this.$element.trigger(e)
  203. if (e.isDefaultPrevented()) return
  204. $tip.removeClass('in')
  205. $.support.transition && this.$tip.hasClass('fade') ?
  206. $tip
  207. .one($.support.transition.end, complete)
  208. .emulateTransitionEnd(150) :
  209. complete()
  210. this.$element.trigger('hidden.bs.' + this.type)
  211. return this
  212. }
  213. Tooltip.prototype.fixTitle = function () {
  214. var $e = this.$element
  215. if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
  216. $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
  217. }
  218. }
  219. Tooltip.prototype.hasContent = function () {
  220. return this.getTitle()
  221. }
  222. Tooltip.prototype.getPosition = function () {
  223. var el = this.$element[0]
  224. return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
  225. width: el.offsetWidth
  226. , height: el.offsetHeight
  227. }, this.$element.offset())
  228. }
  229. Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
  230. return placement == 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
  231. placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
  232. placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
  233. /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
  234. }
  235. Tooltip.prototype.getTitle = function () {
  236. var title
  237. var $e = this.$element
  238. var o = this.options
  239. title = $e.attr('data-original-title')
  240. || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
  241. return title
  242. }
  243. Tooltip.prototype.tip = function () {
  244. return this.$tip = this.$tip || $(this.options.template)
  245. }
  246. Tooltip.prototype.arrow = function () {
  247. return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
  248. }
  249. Tooltip.prototype.validate = function () {
  250. if (!this.$element[0].parentNode) {
  251. this.hide()
  252. this.$element = null
  253. this.options = null
  254. }
  255. }
  256. Tooltip.prototype.enable = function () {
  257. this.enabled = true
  258. }
  259. Tooltip.prototype.disable = function () {
  260. this.enabled = false
  261. }
  262. Tooltip.prototype.toggleEnabled = function () {
  263. this.enabled = !this.enabled
  264. }
  265. Tooltip.prototype.toggle = function (e) {
  266. var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
  267. self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
  268. }
  269. Tooltip.prototype.destroy = function () {
  270. this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
  271. }
  272. // TOOLTIP PLUGIN DEFINITION
  273. // =========================
  274. var old = $.fn.tooltip
  275. $.fn.tooltip = function (option) {
  276. return this.each(function () {
  277. var $this = $(this)
  278. var data = $this.data('bs.tooltip')
  279. var options = typeof option == 'object' && option
  280. if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
  281. if (typeof option == 'string') data[option]()
  282. })
  283. }
  284. $.fn.tooltip.Constructor = Tooltip
  285. // TOOLTIP NO CONFLICT
  286. // ===================
  287. $.fn.tooltip.noConflict = function () {
  288. $.fn.tooltip = old
  289. return this
  290. }
  291. }(jQuery);