bootstrap-tooltip.js 7.5KB

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