bootstrap-tooltip.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* ===========================================================
  2. * bootstrap-tooltip.js v2.0.4
  3. * http://twitter.github.com/bootstrap/javascript.html#tooltips
  4. * Inspired by the original jQuery.tipsy by Jason Frame
  5. * ===========================================================
  6. * Copyright 2012 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 ($) {
  21. "use strict"; // jshint ;_;
  22. /* TOOLTIP PUBLIC CLASS DEFINITION
  23. * =============================== */
  24. var Tooltip = function (element, options) {
  25. this.init('tooltip', element, options)
  26. }
  27. Tooltip.prototype = {
  28. constructor: Tooltip
  29. , init: function (type, element, options) {
  30. var eventIn
  31. , eventOut
  32. this.type = type
  33. this.$element = $(element)
  34. this.options = this.getOptions(options)
  35. this.enabled = true
  36. if (this.options.trigger != 'manual') {
  37. eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
  38. eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
  39. this.$element.on(eventIn, this.options.selector, $.proxy(this.enter, this))
  40. this.$element.on(eventOut, this.options.selector, $.proxy(this.leave, this))
  41. }
  42. this.options.selector ?
  43. (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
  44. this.fixTitle()
  45. }
  46. , getOptions: function (options) {
  47. options = $.extend({}, $.fn[this.type].defaults, options, this.$element.data())
  48. if (options.delay && typeof options.delay == 'number') {
  49. options.delay = {
  50. show: options.delay
  51. , hide: options.delay
  52. }
  53. }
  54. return options
  55. }
  56. , enter: function (e) {
  57. var self = $(e.currentTarget)[this.type](this._options).data(this.type)
  58. if (!self.options.delay || !self.options.delay.show) return self.show()
  59. clearTimeout(this.timeout)
  60. self.hoverState = 'in'
  61. this.timeout = setTimeout(function() {
  62. if (self.hoverState == 'in') self.show()
  63. }, self.options.delay.show)
  64. }
  65. , leave: function (e) {
  66. var self = $(e.currentTarget)[this.type](this._options).data(this.type)
  67. if (this.timeout) clearTimeout(this.timeout)
  68. if (!self.options.delay || !self.options.delay.hide) return self.hide()
  69. self.hoverState = 'out'
  70. this.timeout = setTimeout(function() {
  71. if (self.hoverState == 'out') self.hide()
  72. }, self.options.delay.hide)
  73. }
  74. , show: function () {
  75. var $tip
  76. , inside
  77. , pos
  78. , actualWidth
  79. , actualHeight
  80. , placement
  81. , tp
  82. if (this.hasContent() && this.enabled) {
  83. $tip = this.tip()
  84. this.setContent()
  85. if (this.options.animation) {
  86. $tip.addClass('fade')
  87. }
  88. placement = typeof this.options.placement == 'function' ?
  89. this.options.placement.call(this, $tip[0], this.$element[0]) :
  90. this.options.placement
  91. inside = /in/.test(placement)
  92. $tip
  93. .remove()
  94. .css({ top: 0, left: 0, display: 'block' })
  95. .appendTo(inside ? this.$element : document.body)
  96. pos = this.getPosition(inside)
  97. actualWidth = $tip[0].offsetWidth
  98. actualHeight = $tip[0].offsetHeight
  99. switch (inside ? placement.split(' ')[1] : placement) {
  100. case 'bottom':
  101. tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
  102. break
  103. case 'top':
  104. tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
  105. break
  106. case 'left':
  107. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
  108. break
  109. case 'right':
  110. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
  111. break
  112. }
  113. $tip
  114. .css(tp)
  115. .addClass(placement)
  116. .addClass('in')
  117. }
  118. }
  119. , isHTML: function(text) {
  120. // html string detection logic adapted from jQuery
  121. return typeof text != 'string'
  122. || ( text.charAt(0) === "<"
  123. && text.charAt( text.length - 1 ) === ">"
  124. && text.length >= 3
  125. ) || /^(?:[^<]*<[\w\W]+>[^>]*$)/.exec(text)
  126. }
  127. , setContent: function () {
  128. var $tip = this.tip()
  129. , title = this.getTitle()
  130. $tip.find('.tooltip-inner')[this.isHTML(title) ? 'html' : 'text'](title)
  131. $tip.removeClass('fade in top bottom left right')
  132. }
  133. , hide: function () {
  134. var that = this
  135. , $tip = this.tip()
  136. $tip.removeClass('in')
  137. function removeWithAnimation() {
  138. var timeout = setTimeout(function () {
  139. $tip.off($.support.transition.end).remove()
  140. }, 500)
  141. $tip.one($.support.transition.end, function () {
  142. clearTimeout(timeout)
  143. $tip.remove()
  144. })
  145. }
  146. $.support.transition && this.$tip.hasClass('fade') ?
  147. removeWithAnimation() :
  148. $tip.remove()
  149. }
  150. , fixTitle: function () {
  151. var $e = this.$element
  152. if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
  153. $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
  154. }
  155. }
  156. , hasContent: function () {
  157. return this.getTitle()
  158. }
  159. , getPosition: function (inside) {
  160. return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
  161. width: this.$element[0].offsetWidth
  162. , height: this.$element[0].offsetHeight
  163. })
  164. }
  165. , getTitle: function () {
  166. var title
  167. , $e = this.$element
  168. , o = this.options
  169. title = $e.attr('data-original-title')
  170. || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
  171. return title
  172. }
  173. , tip: function () {
  174. return this.$tip = this.$tip || $(this.options.template)
  175. }
  176. , validate: function () {
  177. if (!this.$element[0].parentNode) {
  178. this.hide()
  179. this.$element = null
  180. this.options = null
  181. }
  182. }
  183. , enable: function () {
  184. this.enabled = true
  185. }
  186. , disable: function () {
  187. this.enabled = false
  188. }
  189. , toggleEnabled: function () {
  190. this.enabled = !this.enabled
  191. }
  192. , toggle: function () {
  193. this[this.tip().hasClass('in') ? 'hide' : 'show']()
  194. }
  195. }
  196. /* TOOLTIP PLUGIN DEFINITION
  197. * ========================= */
  198. $.fn.tooltip = function ( option ) {
  199. return this.each(function () {
  200. var $this = $(this)
  201. , data = $this.data('tooltip')
  202. , options = typeof option == 'object' && option
  203. if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
  204. if (typeof option == 'string') data[option]()
  205. })
  206. }
  207. $.fn.tooltip.Constructor = Tooltip
  208. $.fn.tooltip.defaults = {
  209. animation: true
  210. , placement: 'top'
  211. , selector: false
  212. , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  213. , trigger: 'hover'
  214. , title: ''
  215. , delay: 0
  216. }
  217. }(window.jQuery);