bootstrap-tooltip.js 7.59 KB
Newer Older
1
/* ===========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-tooltip.js v2.0.2
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#tooltips
4
 * Inspired by the original jQuery.tipsy by Jason Frame
5
 * ===========================================================
Mark Otto's avatar
Mark Otto committed
6
 * Copyright 2012 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */

21
22

!function ( $ ) {
23

24
25
  "use strict"

26
27
 /* TOOLTIP PUBLIC CLASS DEFINITION
  * =============================== */
28

29
30
  var Tooltip = function ( element, options ) {
    this.init('tooltip', element, options)
31
32
  }

33
  Tooltip.prototype = {
34

35
    constructor: Tooltip
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  , init: function ( type, element, options ) {
      var eventIn
        , eventOut

      this.type = type
      this.$element = $(element)
      this.options = this.getOptions(options)
      this.enabled = true

      if (this.options.trigger != 'manual') {
        eventIn  = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
        eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
        this.$element.on(eventIn, this.options.selector, $.proxy(this.enter, this))
        this.$element.on(eventOut, this.options.selector, $.proxy(this.leave, this))
      }

      this.options.selector ?
        (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
        this.fixTitle()
    }

  , getOptions: function ( options ) {
      options = $.extend({}, $.fn[this.type].defaults, options, this.$element.data())

      if (options.delay && typeof options.delay == 'number') {
        options.delay = {
          show: options.delay
        , hide: options.delay
        }
      }

      return options
    }

  , enter: function ( e ) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

      if (!self.options.delay || !self.options.delay.show) {
        self.show()
      } else {
77
        clearTimeout(this.timeout)
78
        self.hoverState = 'in'
79
        this.timeout = setTimeout(function() {
80
81
82
83
84
85
86
87
88
89
90
91
92
          if (self.hoverState == 'in') {
            self.show()
          }
        }, self.options.delay.show)
      }
    }

  , leave: function ( e ) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

      if (!self.options.delay || !self.options.delay.hide) {
        self.hide()
      } else {
93
        clearTimeout(this.timeout)
94
        self.hoverState = 'out'
95
        this.timeout = setTimeout(function() {
96
97
98
99
100
101
102
103
104
          if (self.hoverState == 'out') {
            self.hide()
          }
        }, self.options.delay.hide)
      }
    }

  , show: function () {
      var $tip
105
        , inside
106
        , pos
107
108
109
110
111
        , actualWidth
        , actualHeight
        , placement
        , tp

112
      if (this.hasContent() && this.enabled) {
113
        $tip = this.tip()
Jacob Thornton's avatar
Jacob Thornton committed
114
        this.setContent()
115

Jacob Thornton's avatar
Jacob Thornton committed
116
        if (this.options.animation) {
117
118
119
          $tip.addClass('fade')
        }

120
        placement = typeof this.options.placement == 'function' ?
121
          this.options.placement.call(this, $tip[0], this.$element[0]) :
122
123
124
125
          this.options.placement

        inside = /in/.test(placement)

126
127
128
        $tip
          .remove()
          .css({ top: 0, left: 0, display: 'block' })
Jacob Thornton's avatar
Jacob Thornton committed
129
          .appendTo(inside ? this.$element : document.body)
130

131
        pos = this.getPosition(inside)
132
133
134

        actualWidth = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight
135

136
137
        switch (inside ? placement.split(' ')[1] : placement) {
          case 'bottom':
138
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
139
            break
140
          case 'top':
141
            tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
142
143
            break
          case 'left':
144
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
145
146
            break
          case 'right':
147
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
148
149
150
151
152
153
            break
        }

        $tip
          .css(tp)
          .addClass(placement)
154
          .addClass('in')
155
156
157
      }
    }

158
159
160
161
162
163
164
165
166
  , isHTML: function( text ) {
      // html string detection logic adapted from jQuery
      return typeof text != 'string'
        || ( text.charAt(0) === "<"
          && text.charAt( text.length - 1 ) === ">"
          && text.length >= 3
        ) || /^(?:[^<]*<[\w\W]+>[^>]*$)/.exec(text)
    }

Jacob Thornton's avatar
Jacob Thornton committed
167
168
  , setContent: function () {
      var $tip = this.tip()
169
170
        , title = this.getTitle()

171
      $tip.find('.tooltip-inner')[this.isHTML(title) ? 'html' : 'text'](title)
172
      $tip.removeClass('fade in top bottom left right')
Jacob Thornton's avatar
Jacob Thornton committed
173
174
    }

175
  , hide: function () {
176
177
178
      var that = this
        , $tip = this.tip()

179
      $tip.removeClass('in')
180

181
182
183
184
185
186
187
188
189
      function removeWithAnimation() {
        var timeout = setTimeout(function () {
          $tip.off($.support.transition.end).remove()
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
          $tip.remove()
        })
190
191
      }

192
      $.support.transition && this.$tip.hasClass('fade') ?
193
194
        removeWithAnimation() :
        $tip.remove()
195
196
    }

197
  , fixTitle: function () {
198
199
200
201
202
203
      var $e = this.$element
      if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
        $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
      }
    }

204
205
206
207
  , hasContent: function () {
      return this.getTitle()
    }

208
209
210
211
212
213
214
  , getPosition: function (inside) {
      return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
        width: this.$element[0].offsetWidth
      , height: this.$element[0].offsetHeight
      })
    }

215
  , getTitle: function () {
216
217
218
219
      var title
        , $e = this.$element
        , o = this.options

220
221
      title = $e.attr('data-original-title')
        || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
222

223
      return title
224
225
    }

226
  , tip: function () {
227
      return this.$tip = this.$tip || $(this.options.template)
228
229
    }

230
  , validate: function () {
231
232
233
234
235
236
237
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

238
  , enable: function () {
239
240
241
      this.enabled = true
    }

242
  , disable: function () {
243
244
245
      this.enabled = false
    }

246
  , toggleEnabled: function () {
247
248
249
      this.enabled = !this.enabled
    }

250
251
252
253
  , toggle: function () {
      this[this.tip().hasClass('in') ? 'hide' : 'show']()
    }

254
255
256
  }


257
258
 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */
259

260
  $.fn.tooltip = function ( option ) {
261
262
    return this.each(function () {
      var $this = $(this)
263
        , data = $this.data('tooltip')
264
        , options = typeof option == 'object' && option
265
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
266
267
      if (typeof option == 'string') data[option]()
    })
268
269
  }

270
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
271

272
  $.fn.tooltip.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
273
    animation: true
274
  , delay: 0
275
  , selector: false
276
  , placement: 'top'
277
  , trigger: 'hover'
278
  , title: ''
279
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
280
281
  }

282
}( window.jQuery );