tooltip.js 11.6 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: tooltip.js v3.0.3
3
 * http://getbootstrap.com/javascript/#tooltip
4
 * Inspired by the original jQuery.tipsy by Jason Frame
5
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
6
 * Copyright 2013 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
    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'

Jacob Thornton's avatar
Jacob Thornton committed
67
        this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
fat's avatar
fat committed
68
        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

Jacob Thornton's avatar
Jacob Thornton committed
94
  Tooltip.prototype.getDelegateOptions = function () {
fat's avatar
fat committed
95
    var options  = {}
Jacob Thornton's avatar
Jacob Thornton committed
96
    var defaults = this.getDefaults()
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

Jacob Thornton's avatar
Jacob Thornton committed
102
103
104
105
    return options
  }

  Tooltip.prototype.enter = function (obj) {
fat's avatar
fat committed
106
    var self = obj instanceof this.constructor ?
Jacob Thornton's avatar
Jacob Thornton committed
107
      obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
108

Jacob Thornton's avatar
Jacob Thornton committed
109
    clearTimeout(self.timeout)
110

111
112
    self.hoverState = 'in'

Jacob Thornton's avatar
Jacob Thornton committed
113
114
    if (!self.options.delay || !self.options.delay.show) return self.show()

115
    self.timeout = setTimeout(function () {
fat's avatar
fat committed
116
117
118
      if (self.hoverState == 'in') self.show()
    }, self.options.delay.show)
  }
119

fat's avatar
fat committed
120
121
  Tooltip.prototype.leave = function (obj) {
    var self = obj instanceof this.constructor ?
Jacob Thornton's avatar
Jacob Thornton committed
122
      obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
fat's avatar
fat committed
123

Jacob Thornton's avatar
Jacob Thornton committed
124
    clearTimeout(self.timeout)
fat's avatar
fat committed
125

126
127
    self.hoverState = 'out'

fat's avatar
fat committed
128
    if (!self.options.delay || !self.options.delay.hide) return self.hide()
129

130
    self.timeout = setTimeout(function () {
fat's avatar
fat committed
131
132
133
      if (self.hoverState == 'out') self.hide()
    }, self.options.delay.hide)
  }
134

fat's avatar
fat committed
135
  Tooltip.prototype.show = function () {
Chris Rebert's avatar
Chris Rebert committed
136
    var e = $.Event('show.bs.' + this.type)
137

fat's avatar
fat committed
138
139
    if (this.hasContent() && this.enabled) {
      this.$element.trigger(e)
140

fat's avatar
fat committed
141
      if (e.isDefaultPrevented()) return
142

fat's avatar
fat committed
143
      var $tip = this.tip()
Yohn's avatar
Yohn committed
144

fat's avatar
fat committed
145
      this.setContent()
146

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

fat's avatar
fat committed
149
150
151
      var placement = typeof this.options.placement == 'function' ?
        this.options.placement.call(this, $tip[0], this.$element[0]) :
        this.options.placement
152

153
154
155
156
      var autoToken = /\s?auto?\s?/i
      var autoPlace = autoToken.test(placement)
      if (autoPlace) placement = placement.replace(autoToken, '') || 'top'

fat's avatar
fat committed
157
158
159
      $tip
        .detach()
        .css({ top: 0, left: 0, display: 'block' })
160
        .addClass(placement)
fat's avatar
fat committed
161
162
163
164
165
166
167

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

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

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
      if (autoPlace) {
        var $parent = this.$element.parent()

        var orgPlacement = placement
        var docScroll    = document.documentElement.scrollTop || document.body.scrollTop
        var parentWidth  = this.options.container == 'body' ? window.innerWidth  : $parent.outerWidth()
        var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
        var parentLeft   = this.options.container == 'body' ? 0 : $parent.offset().left

        placement = placement == 'bottom' && pos.top   + pos.height  + actualHeight - docScroll > parentHeight  ? 'top'    :
                    placement == 'top'    && pos.top   - docScroll   - actualHeight < 0                         ? 'bottom' :
                    placement == 'right'  && pos.right + actualWidth > parentWidth                              ? 'left'   :
                    placement == 'left'   && pos.left  - actualWidth < parentLeft                               ? 'right'  :
                    placement

        $tip
          .removeClass(orgPlacement)
          .addClass(placement)
      }

188
      var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
fat's avatar
fat committed
189

fat's avatar
fat committed
190
      this.applyPlacement(calculatedOffset, placement)
191
      this.$element.trigger('shown.bs.' + this.type)
192
    }
fat's avatar
fat committed
193
  }
194

195
  Tooltip.prototype.applyPlacement = function (offset, placement) {
fat's avatar
fat committed
196
    var replace
fat's avatar
fat committed
197
198
199
    var $tip   = this.tip()
    var width  = $tip[0].offsetWidth
    var height = $tip[0].offsetHeight
200

201
    // manually read margins because getBoundingClientRect includes difference
fat's avatar
fat committed
202
203
204
205
206
207
208
209
210
    var marginTop = parseInt($tip.css('margin-top'), 10)
    var marginLeft = parseInt($tip.css('margin-left'), 10)

    // we must check for NaN for ie 8/9
    if (isNaN(marginTop))  marginTop  = 0
    if (isNaN(marginLeft)) marginLeft = 0

    offset.top  = offset.top  + marginTop
    offset.left = offset.left + marginLeft
211

fat's avatar
fat committed
212
213
214
    $tip
      .offset(offset)
      .addClass('in')
215

fat's avatar
fat committed
216
    // check to see if placing tip in new offset caused the tip to resize itself
fat's avatar
fat committed
217
218
    var actualWidth  = $tip[0].offsetWidth
    var actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
219

fat's avatar
fat committed
220
    if (placement == 'top' && actualHeight != height) {
fat's avatar
fat committed
221
      replace = true
fat's avatar
fat committed
222
      offset.top = offset.top + height - actualHeight
fat's avatar
fat committed
223
224
    }

fat's avatar
fat committed
225
    if (/bottom|top/.test(placement)) {
fat's avatar
fat committed
226
      var delta = 0
227

fat's avatar
fat committed
228
      if (offset.left < 0) {
fat's avatar
fat committed
229
230
        delta       = offset.left * -2
        offset.left = 0
231

fat's avatar
fat committed
232
        $tip.offset(offset)
233

fat's avatar
fat committed
234
235
        actualWidth  = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
236
      }
237

fat's avatar
fat committed
238
239
240
      this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
    } else {
      this.replaceArrow(actualHeight - height, actualHeight, 'top')
fat's avatar
fat committed
241
242
    }

fat's avatar
fat committed
243
244
    if (replace) $tip.offset(offset)
  }
245

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

fat's avatar
fat committed
250
251
252
  Tooltip.prototype.setContent = function () {
    var $tip  = this.tip()
    var title = this.getTitle()
Jacob Thornton's avatar
Jacob Thornton committed
253

fat's avatar
fat committed
254
255
256
    $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
    $tip.removeClass('fade in top bottom left right')
  }
257

fat's avatar
fat committed
258
259
260
  Tooltip.prototype.hide = function () {
    var that = this
    var $tip = this.tip()
261
    var e    = $.Event('hide.bs.' + this.type)
262

263
264
265
    function complete() {
      if (that.hoverState != 'in') $tip.detach()
    }
Jacob Thornton's avatar
Jacob Thornton committed
266

fat's avatar
fat committed
267
    this.$element.trigger(e)
268

fat's avatar
fat committed
269
    if (e.isDefaultPrevented()) return
270

fat's avatar
fat committed
271
272
273
    $tip.removeClass('in')

    $.support.transition && this.$tip.hasClass('fade') ?
274
      $tip
Jacob Thornton's avatar
Jacob Thornton committed
275
        .one($.support.transition.end, complete)
276
        .emulateTransitionEnd(150) :
Jacob Thornton's avatar
Jacob Thornton committed
277
      complete()
278

279
    this.$element.trigger('hidden.bs.' + this.type)
280

fat's avatar
fat committed
281
282
    return this
  }
283

fat's avatar
fat committed
284
285
286
287
  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', '')
288
    }
fat's avatar
fat committed
289
  }
290

fat's avatar
fat committed
291
292
293
  Tooltip.prototype.hasContent = function () {
    return this.getTitle()
  }
294

fat's avatar
fat committed
295
296
297
298
299
300
301
  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())
  }
302

303
  Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
fat's avatar
fat committed
304
305
306
307
308
309
    return placement == 'bottom' ? { top: pos.top + pos.height,   left: pos.left + pos.width / 2 - actualWidth / 2  } :
           placement == 'top'    ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2  } :
           placement == 'left'   ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
        /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width   }
  }

fat's avatar
fat committed
310
311
312
313
  Tooltip.prototype.getTitle = function () {
    var title
    var $e = this.$element
    var o  = this.options
314

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

fat's avatar
fat committed
318
319
    return title
  }
320

fat's avatar
fat committed
321
322
323
  Tooltip.prototype.tip = function () {
    return this.$tip = this.$tip || $(this.options.template)
  }
324

Chris Rebert's avatar
Chris Rebert committed
325
  Tooltip.prototype.arrow = function () {
fat's avatar
fat committed
326
    return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
fat's avatar
fat committed
327
  }
328

fat's avatar
fat committed
329
330
331
332
333
  Tooltip.prototype.validate = function () {
    if (!this.$element[0].parentNode) {
      this.hide()
      this.$element = null
      this.options  = null
334
    }
fat's avatar
fat committed
335
  }
336

fat's avatar
fat committed
337
338
339
  Tooltip.prototype.enable = function () {
    this.enabled = true
  }
340

fat's avatar
fat committed
341
342
343
  Tooltip.prototype.disable = function () {
    this.enabled = false
  }
344

fat's avatar
fat committed
345
346
347
  Tooltip.prototype.toggleEnabled = function () {
    this.enabled = !this.enabled
  }
348

fat's avatar
fat committed
349
  Tooltip.prototype.toggle = function (e) {
Jacob Thornton's avatar
Jacob Thornton committed
350
    var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
fat's avatar
fat committed
351
    self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
fat's avatar
fat committed
352
  }
353

fat's avatar
fat committed
354
  Tooltip.prototype.destroy = function () {
355
    this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
356
357
358
  }


fat's avatar
fat committed
359
360
  // TOOLTIP PLUGIN DEFINITION
  // =========================
361

362
363
  var old = $.fn.tooltip

fat's avatar
fat committed
364
  $.fn.tooltip = function (option) {
365
    return this.each(function () {
fat's avatar
fat committed
366
      var $this   = $(this)
367
      var data    = $this.data('bs.tooltip')
fat's avatar
fat committed
368
369
      var options = typeof option == 'object' && option

370
      if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
371
372
      if (typeof option == 'string') data[option]()
    })
373
374
  }

375
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
376

377

fat's avatar
fat committed
378
379
  // TOOLTIP NO CONFLICT
  // ===================
380
381
382
383
384
385

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

386
}(jQuery);