bootstrap-tooltip.js 9.68 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ===========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-tooltip.js v2.3.0
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * http://twitter.github.com/bootstrap/javascript.html#tooltips
 * Inspired by the original jQuery.tipsy by Jason Frame
 * ===========================================================
 * Copyright 2012 Twitter, Inc.
 *
 * 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
23
24
!function ($) {

  "use strict"; // jshint ;_;
Jacob Thornton's avatar
Jacob Thornton committed
25
26
27
28
29


 /* TOOLTIP PUBLIC CLASS DEFINITION
  * =============================== */

30
  var Tooltip = function (element, options) {
Jacob Thornton's avatar
Jacob Thornton committed
31
32
33
34
35
36
37
    this.init('tooltip', element, options)
  }

  Tooltip.prototype = {

    constructor: Tooltip

38
  , init: function (type, element, options) {
Jacob Thornton's avatar
Jacob Thornton committed
39
40
      var eventIn
        , eventOut
41
42
43
        , triggers
        , trigger
        , i
Jacob Thornton's avatar
Jacob Thornton committed
44
45
46
47
48
49

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

50
51
52
53
54
55
56
57
58
59
60
61
      triggers = this.options.trigger.split(' ')

      for (i = triggers.length; i--;) {
        trigger = triggers[i]
        if (trigger == 'click') {
          this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
        } else if (trigger != 'manual') {
          eventIn = trigger == 'hover' ? 'mouseenter' : 'focus'
          eventOut = trigger == 'hover' ? 'mouseleave' : 'blur'
          this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
          this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
        }
Jacob Thornton's avatar
Jacob Thornton committed
62
63
64
65
66
67
68
      }

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

69
  , getOptions: function (options) {
70
      options = $.extend({}, $.fn[this.type].defaults, this.$element.data(), options)
Jacob Thornton's avatar
Jacob Thornton committed
71
72
73
74
75
76
77
78
79
80
81

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

      return options
    }

82
  , enter: function (e) {
83
84
85
86
87
88
89
90
91
92
      var defaults = $.fn[this.type].defaults
        , options = {}
        , self


      this._options && $.each(this._options, function (key, value) {
        if (defaults[key] != value) options[key] = value
      }, this)

      self = $(e.currentTarget)[this.type](options).data(this.type)
Jacob Thornton's avatar
Jacob Thornton committed
93

94
95
96
97
98
99
100
      if (!self.options.delay || !self.options.delay.show) return self.show()

      clearTimeout(this.timeout)
      self.hoverState = 'in'
      this.timeout = setTimeout(function() {
        if (self.hoverState == 'in') self.show()
      }, self.options.delay.show)
Jacob Thornton's avatar
Jacob Thornton committed
101
102
    }

103
  , leave: function (e) {
Jacob Thornton's avatar
Jacob Thornton committed
104
105
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

Jacob Thornton's avatar
Jacob Thornton committed
106
      if (this.timeout) clearTimeout(this.timeout)
107
108
109
110
111
112
      if (!self.options.delay || !self.options.delay.hide) return self.hide()

      self.hoverState = 'out'
      this.timeout = setTimeout(function() {
        if (self.hoverState == 'out') self.hide()
      }, self.options.delay.hide)
Jacob Thornton's avatar
Jacob Thornton committed
113
114
115
116
117
118
119
120
121
    }

  , show: function () {
      var $tip
        , pos
        , actualWidth
        , actualHeight
        , placement
        , tp
122
        , e = $.Event('show')
Jacob Thornton's avatar
Jacob Thornton committed
123
124

      if (this.hasContent() && this.enabled) {
125
126
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
127
128
129
130
131
132
133
134
135
136
137
138
        $tip = this.tip()
        this.setContent()

        if (this.options.animation) {
          $tip.addClass('fade')
        }

        placement = typeof this.options.placement == 'function' ?
          this.options.placement.call(this, $tip[0], this.$element[0]) :
          this.options.placement

        $tip
Jacob Thornton's avatar
rebuild    
Jacob Thornton committed
139
          .detach()
Jacob Thornton's avatar
Jacob Thornton committed
140
          .css({ top: 0, left: 0, display: 'block' })
141
142

        this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
Jacob Thornton's avatar
Jacob Thornton committed
143

144
        pos = this.getPosition()
Jacob Thornton's avatar
Jacob Thornton committed
145
146
147
148

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

149
        switch (placement) {
Jacob Thornton's avatar
Jacob Thornton committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
          case 'bottom':
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'top':
            tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'left':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
            break
          case 'right':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
            break
        }

fat's avatar
fat committed
164
        this.applyPlacement(tp, placement)
165
        this.$element.trigger('shown')
Jacob Thornton's avatar
Jacob Thornton committed
166
167
168
      }
    }

fat's avatar
fat committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  , applyPlacement: function(offset, placement){
      var $tip = this.tip()
        , width = $tip[0].offsetWidth
        , height = $tip[0].offsetHeight
        , actualWidth
        , actualHeight
        , delta
        , replace

      $tip
        .offset(offset)
        .addClass(placement)
        .addClass('in')

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

      if (placement == 'top' && actualHeight != height) {
        offset.top = offset.top + height - actualHeight
        replace = true
      }

      if (placement == 'bottom' || placement == 'top') {
        delta = 0

        if (offset.left < 0){
          delta = offset.left * -2
          offset.left = 0
          $tip.offset(offset)
          actualWidth = $tip[0].offsetWidth
          actualHeight = $tip[0].offsetHeight
        }

        this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
      } else {
        this.replaceArrow(actualHeight - height, actualHeight, 'top')
      }

      if (replace) $tip.offset(offset)
    }

  , replaceArrow: function(delta, dimension, position){
      this
        .arrow()
        .css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
    }

Jacob Thornton's avatar
Jacob Thornton committed
216
217
  , setContent: function () {
      var $tip = this.tip()
218
219
        , title = this.getTitle()

220
      $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
Jacob Thornton's avatar
Jacob Thornton committed
221
222
223
224
225
226
      $tip.removeClass('fade in top bottom left right')
    }

  , hide: function () {
      var that = this
        , $tip = this.tip()
227
228
229
230
        , e = $.Event('hide')

      this.$element.trigger(e)
      if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
231
232
233
234
235

      $tip.removeClass('in')

      function removeWithAnimation() {
        var timeout = setTimeout(function () {
Jacob Thornton's avatar
rebuild    
Jacob Thornton committed
236
          $tip.off($.support.transition.end).detach()
Jacob Thornton's avatar
Jacob Thornton committed
237
238
239
240
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
Jacob Thornton's avatar
rebuild    
Jacob Thornton committed
241
          $tip.detach()
Jacob Thornton's avatar
Jacob Thornton committed
242
243
244
245
246
        })
      }

      $.support.transition && this.$tip.hasClass('fade') ?
        removeWithAnimation() :
Jacob Thornton's avatar
rebuild    
Jacob Thornton committed
247
        $tip.detach()
Jacob Thornton's avatar
Jacob Thornton committed
248

249
250
      this.$element.trigger('hidden')

Jacob Thornton's avatar
Jacob Thornton committed
251
      return this
Jacob Thornton's avatar
Jacob Thornton committed
252
253
254
255
256
    }

  , fixTitle: function () {
      var $e = this.$element
      if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
257
        $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
Jacob Thornton's avatar
Jacob Thornton committed
258
259
260
261
262
263
264
      }
    }

  , hasContent: function () {
      return this.getTitle()
    }

265
266
  , getPosition: function () {
      var el = this.$element[0]
fat's avatar
fat committed
267
      return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
268
269
270
        width: el.offsetWidth
      , height: el.offsetHeight
      }, this.$element.offset())
Jacob Thornton's avatar
Jacob Thornton committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    }

  , getTitle: function () {
      var title
        , $e = this.$element
        , o = this.options

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

      return title
    }

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

fat's avatar
fat committed
288
289
290
291
  , arrow: function(){
      return this.$arrow = this.$arrow || this.tip().find(".tooltip-arrow")
    }

Jacob Thornton's avatar
Jacob Thornton committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  , validate: function () {
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

  , enable: function () {
      this.enabled = true
    }

  , disable: function () {
      this.enabled = false
    }

  , toggleEnabled: function () {
      this.enabled = !this.enabled
    }

Jacob Thornton's avatar
Jacob Thornton committed
312
  , toggle: function (e) {
313
314
      var self = e ? $(e.currentTarget)[this.type](this._options).data(this.type) : this
      self.tip().hasClass('in') ? self.hide() : self.show()
Jacob Thornton's avatar
Jacob Thornton committed
315
316
    }

Mark Otto's avatar
Mark Otto committed
317
  , destroy: function () {
Jacob Thornton's avatar
Jacob Thornton committed
318
      this.hide().$element.off('.' + this.type).removeData(this.type)
Mark Otto's avatar
Mark Otto committed
319
320
    }

Jacob Thornton's avatar
Jacob Thornton committed
321
322
323
324
325
326
  }


 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */

327
328
  var old = $.fn.tooltip

Jacob Thornton's avatar
Jacob Thornton committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  $.fn.tooltip = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('tooltip')
        , options = typeof option == 'object' && option
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.tooltip.Constructor = Tooltip

  $.fn.tooltip.defaults = {
    animation: true
  , placement: 'top'
344
345
  , selector: false
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
346
  , trigger: 'hover focus'
Jacob Thornton's avatar
Jacob Thornton committed
347
  , title: ''
348
  , delay: 0
349
  , html: false
350
  , container: false
Jacob Thornton's avatar
Jacob Thornton committed
351
352
  }

353
354
355
356
357
358
359
360
361

 /* TOOLTIP NO CONFLICT
  * =================== */

  $.fn.tooltip.noConflict = function () {
    $.fn.tooltip = old
    return this
  }

362
}(window.jQuery);