bootstrap-tooltip.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* ===========================================================
  2. * bootstrap-tooltip.js v2.0.2
  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"
  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) {
  59. self.show()
  60. } else {
  61. self.hoverState = 'in'
  62. setTimeout(function() {
  63. if (self.hoverState == 'in') {
  64. self.show()
  65. }
  66. }, self.options.delay.show)
  67. }
  68. }
  69. , leave: function ( e ) {
  70. var self = $(e.currentTarget)[this.type](this._options).data(this.type)
  71. if (!self.options.delay || !self.options.delay.hide) {
  72. self.hide()
  73. } else {
  74. self.hoverState = 'out'
  75. setTimeout(function() {
  76. if (self.hoverState == 'out') {
  77. self.hide()
  78. }
  79. }, self.options.delay.hide)
  80. }
  81. }
  82. , show: function () {
  83. var $tip
  84. , inside
  85. , pos
  86. , actualWidth
  87. , actualHeight
  88. , placement
  89. , tp
  90. if (this.hasContent() && this.enabled) {
  91. $tip = this.tip()
  92. this.setContent()
  93. if (this.options.animation) {
  94. $tip.addClass('fade')
  95. }
  96. placement = typeof this.options.placement == 'function' ?
  97. this.options.placement.call(this, $tip[0], this.$element[0]) :
  98. this.options.placement
  99. inside = /in/.test(placement)
  100. $tip
  101. .remove()
  102. .css({ top: 0, left: 0, display: 'block' })
  103. .appendTo(inside ? this.$element : document.body)
  104. pos = this.getPosition(inside)
  105. actualWidth = $tip[0].offsetWidth
  106. actualHeight = $tip[0].offsetHeight
  107. switch (inside ? placement.split(' ')[1] : placement) {
  108. case 'bottom':
  109. tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
  110. break
  111. case 'top':
  112. tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
  113. break
  114. case 'left':
  115. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
  116. break
  117. case 'right':
  118. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
  119. break
  120. }
  121. $tip
  122. .css(tp)
  123. .addClass(placement)
  124. .addClass('in')
  125. }
  126. }
  127. , setContent: function () {
  128. var $tip = this.tip()
  129. $tip.find('.tooltip-inner').html(this.getTitle())
  130. $tip.removeClass('fade in top bottom left right')
  131. }
  132. , hide: function () {
  133. var that = this
  134. , $tip = this.tip()
  135. $tip.removeClass('in')
  136. function removeWithAnimation() {
  137. var timeout = setTimeout(function () {
  138. $tip.off($.support.transition.end).remove()
  139. }, 500)
  140. $tip.one($.support.transition.end, function () {
  141. clearTimeout(timeout)
  142. $tip.remove()
  143. })
  144. }
  145. $.support.transition && this.$tip.hasClass('fade') ?
  146. removeWithAnimation() :
  147. $tip.remove()
  148. }
  149. , fixTitle: function () {
  150. var $e = this.$element
  151. if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
  152. $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
  153. }
  154. }
  155. , hasContent: function () {
  156. return this.getTitle()
  157. }
  158. , getPosition: function (inside) {
  159. return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
  160. width: this.$element[0].offsetWidth
  161. , height: this.$element[0].offsetHeight
  162. })
  163. }
  164. , getTitle: function () {
  165. var title
  166. , $e = this.$element
  167. , o = this.options
  168. title = $e.attr('data-original-title')
  169. || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
  170. title = (title || '').toString().replace(/(^\s*|\s*$)/, "")
  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. , delay: 0
  211. , selector: false
  212. , placement: 'top'
  213. , trigger: 'hover'
  214. , title: ''
  215. , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  216. }
  217. }( window.jQuery );