tooltip.js 9.83 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: tooltip.js v3.0.0
3
 * http://twitter.github.com/bootstrap/javascript.html#affix
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
 *
 * 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.
19
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
20

21

22
+function ($) { "use strict";
23

fat's avatar
fat committed
24
25
  // TOOLTIP PUBLIC CLASS DEFINITION
  // ===============================
26

27
  var Tooltip = function (element, options) {
fat's avatar
fat committed
28
29
30
31
32
33
34
    this.type       =
    this.options    =
    this.enabled    =
    this.timeout    =
    this.hoverState =
    this.$element   = null

35
    this.init('tooltip', element, options)
36
37
  }

fat's avatar
fat committed
38
39
40
41
42
43
44
45
46
47
48
  Tooltip.DEFAULTS = {
    animation: true
  , placement: 'top'
  , selector: false
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  , trigger: 'hover focus'
  , title: ''
  , delay: 0
  , html: false
  , container: false
  }
49

fat's avatar
fat committed
50
51
  Tooltip.prototype.init = function (type, element, options) {
    this.enabled  = true
fat's avatar
fat committed
52
    this.type     = type
fat's avatar
fat committed
53
    this.$element = $(element)
fat's avatar
fat committed
54
    this.options  = this.getOptions(options)
55

fat's avatar
fat committed
56
    var triggers = this.options.trigger.split(' ')
57

fat's avatar
fat committed
58
59
60
61
62
63
64
65
66
67
68
    for (var i = triggers.length; i--;) {
      var trigger = triggers[i]

      if (trigger == 'click') {
        this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
      } else if (trigger != 'manual') {
        var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focus'
        var 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))
69
      }
fat's avatar
fat committed
70
71
72
73
74
75
76
77
78
79
80
81
82
    }

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

  Tooltip.prototype.getDefaults = function () {
    return Tooltip.DEFAULTS
  }

  Tooltip.prototype.getOptions = function (options) {
    options = $.extend({}, this.getDefaults(), this.$element.data(), options)
83

fat's avatar
fat committed
84
85
86
87
88
    if (options.delay && typeof options.delay == 'number') {
      options.delay = {
        show: options.delay
      , hide: options.delay
      }
89
90
    }

fat's avatar
fat committed
91
92
    return options
  }
93

fat's avatar
fat committed
94
  Tooltip.prototype.enter = function (obj) {
fat's avatar
fat committed
95
    var defaults = this.getDefaults()
fat's avatar
fat committed
96
    var options  = {}
97

fat's avatar
fat committed
98
99
    this._options && $.each(this._options, function (key, value) {
      if (defaults[key] != value) options[key] = value
fat's avatar
fat committed
100
    })
101

fat's avatar
fat committed
102
103
    var self = obj instanceof this.constructor ?
      obj : $(obj.currentTarget)[this.type](options).data('bs.' + this.type)
104

fat's avatar
fat committed
105
    if (!self.options.delay || !self.options.delay.show) return self.show()
106

fat's avatar
fat committed
107
    clearTimeout(this.timeout)
108

fat's avatar
fat committed
109
    self.hoverState = 'in'
fat's avatar
fat committed
110
    this.timeout    = setTimeout(function () {
fat's avatar
fat committed
111
112
113
      if (self.hoverState == 'in') self.show()
    }, self.options.delay.show)
  }
114

fat's avatar
fat committed
115
116
117
118
119
  Tooltip.prototype.leave = function (obj) {
    var self = obj instanceof this.constructor ?
      obj : $(obj.currentTarget)[this.type](this._options).data('bs.' + this.type)

    clearTimeout(this.timeout)
fat's avatar
fat committed
120
121

    if (!self.options.delay || !self.options.delay.hide) return self.hide()
122

fat's avatar
fat committed
123
    self.hoverState = 'out'
fat's avatar
fat committed
124
    this.timeout    = setTimeout(function () {
fat's avatar
fat committed
125
126
127
      if (self.hoverState == 'out') self.hide()
    }, self.options.delay.hide)
  }
128

fat's avatar
fat committed
129
  Tooltip.prototype.show = function () {
130
    var e = $.Event('show.bs.'+ this.type)
131

fat's avatar
fat committed
132
133
    if (this.hasContent() && this.enabled) {
      this.$element.trigger(e)
134

fat's avatar
fat committed
135
      if (e.isDefaultPrevented()) return
136

fat's avatar
fat committed
137
      var $tip = this.tip()
Yohn's avatar
Yohn committed
138

fat's avatar
fat committed
139
      this.setContent()
140

fat's avatar
fat committed
141
      if (this.options.animation) $tip.addClass('fade')
142

fat's avatar
fat committed
143
144
145
      var placement = typeof this.options.placement == 'function' ?
        this.options.placement.call(this, $tip[0], this.$element[0]) :
        this.options.placement
146

fat's avatar
fat committed
147
148
149
      $tip
        .detach()
        .css({ top: 0, left: 0, display: 'block' })
150
        .addClass(placement)
fat's avatar
fat committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

      this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)

      var tp
      var pos          = this.getPosition()
      var actualWidth  = $tip[0].offsetWidth
      var actualHeight = $tip[0].offsetHeight

      switch (placement) {
        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
172
      }
fat's avatar
fat committed
173
174

      this.applyPlacement(tp, placement)
175
      this.$element.trigger('shown.bs.' + this.type)
176
    }
fat's avatar
fat committed
177
  }
178

fat's avatar
fat committed
179
  Tooltip.prototype.applyPlacement = function(offset, placement) {
fat's avatar
fat committed
180
    var replace
fat's avatar
fat committed
181
182
183
    var $tip   = this.tip()
    var width  = $tip[0].offsetWidth
    var height = $tip[0].offsetHeight
184

fat's avatar
fat committed
185
186
187
    $tip
      .offset(offset)
      .addClass('in')
188

fat's avatar
fat committed
189
190
    var actualWidth  = $tip[0].offsetWidth
    var actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
191

fat's avatar
fat committed
192
    if (placement == 'top' && actualHeight != height) {
fat's avatar
fat committed
193
      replace = true
fat's avatar
fat committed
194
195
196
197
198
      offset.top  = offset.top + height - actualHeight
    }

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

fat's avatar
fat committed
200
201
202
      if (offset.left < 0){
        delta       = offset.left * -2
        offset.left = 0
203

fat's avatar
fat committed
204
        $tip.offset(offset)
205

fat's avatar
fat committed
206
207
        actualWidth  = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
208
      }
209

fat's avatar
fat committed
210
211
212
      this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
    } else {
      this.replaceArrow(actualHeight - height, actualHeight, 'top')
fat's avatar
fat committed
213
214
    }

fat's avatar
fat committed
215
216
    if (replace) $tip.offset(offset)
  }
217

fat's avatar
fat committed
218
  Tooltip.prototype.replaceArrow = function(delta, dimension, position) {
fat's avatar
fat committed
219
220
    this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
  }
221

fat's avatar
fat committed
222
223
224
  Tooltip.prototype.setContent = function () {
    var $tip  = this.tip()
    var title = this.getTitle()
Jacob Thornton's avatar
Jacob Thornton committed
225

fat's avatar
fat committed
226
227
228
    $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
    $tip.removeClass('fade in top bottom left right')
  }
229

fat's avatar
fat committed
230
231
232
  Tooltip.prototype.hide = function () {
    var that = this
    var $tip = this.tip()
233
    var e    = $.Event('hide.bs.' + this.type)
234

fat's avatar
fat committed
235
    this.$element.trigger(e)
236

fat's avatar
fat committed
237
    if (e.isDefaultPrevented()) return
238

fat's avatar
fat committed
239
240
241
242
243
244
    $tip.removeClass('in')

    function removeWithAnimation() {
      var timeout = setTimeout(function () {
        $tip.off($.support.transition.end).detach()
      }, 500)
245

fat's avatar
fat committed
246
247
      $tip.one($.support.transition.end, function () {
        clearTimeout(timeout)
frntz's avatar
frntz committed
248
        $tip.detach()
fat's avatar
fat committed
249
250
      })
    }
Jacob Thornton's avatar
Jacob Thornton committed
251

fat's avatar
fat committed
252
253
254
    $.support.transition && this.$tip.hasClass('fade') ?
      removeWithAnimation() :
      $tip.detach()
255

256
    this.$element.trigger('hidden.bs.' + this.type)
257

fat's avatar
fat committed
258
259
    return this
  }
260

fat's avatar
fat committed
261
262
263
264
  Tooltip.prototype.fixTitle = function () {
    var $e = this.$element
    if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
      $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
265
    }
fat's avatar
fat committed
266
  }
267

fat's avatar
fat committed
268
269
270
  Tooltip.prototype.hasContent = function () {
    return this.getTitle()
  }
271

fat's avatar
fat committed
272
273
274
275
276
277
278
  Tooltip.prototype.getPosition = function () {
    var el = this.$element[0]
    return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
      width: el.offsetWidth
    , height: el.offsetHeight
    }, this.$element.offset())
  }
279

fat's avatar
fat committed
280
281
282
283
  Tooltip.prototype.getTitle = function () {
    var title
    var $e = this.$element
    var o  = this.options
284

fat's avatar
fat committed
285
286
    title = $e.attr('data-original-title')
      || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
287

fat's avatar
fat committed
288
289
    return title
  }
290

fat's avatar
fat committed
291
292
293
  Tooltip.prototype.tip = function () {
    return this.$tip = this.$tip || $(this.options.template)
  }
294

fat's avatar
fat committed
295
296
297
  Tooltip.prototype.arrow =function(){
    return this.$arrow = this.$arrow || this.tip().find(".tooltip-arrow")
  }
298

fat's avatar
fat committed
299
300
301
302
303
  Tooltip.prototype.validate = function () {
    if (!this.$element[0].parentNode) {
      this.hide()
      this.$element = null
      this.options  = null
304
    }
fat's avatar
fat committed
305
  }
306

fat's avatar
fat committed
307
308
309
  Tooltip.prototype.enable = function () {
    this.enabled = true
  }
310

fat's avatar
fat committed
311
312
313
  Tooltip.prototype.disable = function () {
    this.enabled = false
  }
314

fat's avatar
fat committed
315
316
317
  Tooltip.prototype.toggleEnabled = function () {
    this.enabled = !this.enabled
  }
318

fat's avatar
fat committed
319
  Tooltip.prototype.toggle = function (e) {
320
    var self = e ? $(e.currentTarget)[this.type](this._options).data('bs.' + this.type) : this
fat's avatar
fat committed
321
    self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
fat's avatar
fat committed
322
  }
323

fat's avatar
fat committed
324
  Tooltip.prototype.destroy = function () {
325
    this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
326
327
328
  }


fat's avatar
fat committed
329
330
  // TOOLTIP PLUGIN DEFINITION
  // =========================
331

332
333
  var old = $.fn.tooltip

fat's avatar
fat committed
334
  $.fn.tooltip = function (option) {
335
    return this.each(function () {
fat's avatar
fat committed
336
      var $this   = $(this)
337
      var data    = $this.data('bs.tooltip')
fat's avatar
fat committed
338
339
      var options = typeof option == 'object' && option

340
      if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
341
342
      if (typeof option == 'string') data[option]()
    })
343
344
  }

345
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
346

347

fat's avatar
fat committed
348
349
  // TOOLTIP NO CONFLICT
  // ===================
350
351
352
353
354
355

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

Yohn's avatar
Yohn committed
356
}(window.jQuery);