tooltip.js 11.2 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: tooltip.js v3.1.0
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.
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
9

10

XhmikosR's avatar
XhmikosR committed
11
+function ($) { 'use strict';
12

fat's avatar
fat committed
13
14
  // TOOLTIP PUBLIC CLASS DEFINITION
  // ===============================
15

16
  var Tooltip = function (element, options) {
fat's avatar
fat committed
17
18
19
20
21
22
23
    this.type       =
    this.options    =
    this.enabled    =
    this.timeout    =
    this.hoverState =
    this.$element   = null

24
    this.init('tooltip', element, options)
25
26
  }

fat's avatar
fat committed
27
  Tooltip.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
28
29
30
31
32
33
34
35
36
    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
fat's avatar
fat committed
37
  }
38

fat's avatar
fat committed
39
40
  Tooltip.prototype.init = function (type, element, options) {
    this.enabled  = true
fat's avatar
fat committed
41
    this.type     = type
fat's avatar
fat committed
42
    this.$element = $(element)
fat's avatar
fat committed
43
    this.options  = this.getOptions(options)
44

fat's avatar
fat committed
45
    var triggers = this.options.trigger.split(' ')
46

fat's avatar
fat committed
47
48
49
50
51
52
53
54
55
    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
56
        this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
fat's avatar
fat committed
57
        this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
58
      }
fat's avatar
fat committed
59
60
61
62
63
64
65
66
67
68
69
70
71
    }

    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)
72

fat's avatar
fat committed
73
74
    if (options.delay && typeof options.delay == 'number') {
      options.delay = {
Zlatan Vasović's avatar
Zlatan Vasović committed
75
76
        show: options.delay,
        hide: options.delay
fat's avatar
fat committed
77
      }
78
79
    }

fat's avatar
fat committed
80
81
    return options
  }
82

Jacob Thornton's avatar
Jacob Thornton committed
83
  Tooltip.prototype.getDelegateOptions = function () {
fat's avatar
fat committed
84
    var options  = {}
Jacob Thornton's avatar
Jacob Thornton committed
85
    var defaults = this.getDefaults()
86

fat's avatar
fat committed
87
88
    this._options && $.each(this._options, function (key, value) {
      if (defaults[key] != value) options[key] = value
fat's avatar
fat committed
89
    })
90

Jacob Thornton's avatar
Jacob Thornton committed
91
92
93
94
    return options
  }

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

Jacob Thornton's avatar
Jacob Thornton committed
98
    clearTimeout(self.timeout)
99

100
101
    self.hoverState = 'in'

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

104
    self.timeout = setTimeout(function () {
fat's avatar
fat committed
105
106
107
      if (self.hoverState == 'in') self.show()
    }, self.options.delay.show)
  }
108

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

Jacob Thornton's avatar
Jacob Thornton committed
113
    clearTimeout(self.timeout)
fat's avatar
fat committed
114

115
116
    self.hoverState = 'out'

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

119
    self.timeout = setTimeout(function () {
fat's avatar
fat committed
120
121
122
      if (self.hoverState == 'out') self.hide()
    }, self.options.delay.hide)
  }
123

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

fat's avatar
fat committed
127
128
    if (this.hasContent() && this.enabled) {
      this.$element.trigger(e)
129

fat's avatar
fat committed
130
      if (e.isDefaultPrevented()) return
131

fat's avatar
fat committed
132
      var $tip = this.tip()
Yohn's avatar
Yohn committed
133

fat's avatar
fat committed
134
      this.setContent()
135

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

fat's avatar
fat committed
138
139
140
      var placement = typeof this.options.placement == 'function' ?
        this.options.placement.call(this, $tip[0], this.$element[0]) :
        this.options.placement
141

142
143
144
145
      var autoToken = /\s?auto?\s?/i
      var autoPlace = autoToken.test(placement)
      if (autoPlace) placement = placement.replace(autoToken, '') || 'top'

fat's avatar
fat committed
146
147
148
      $tip
        .detach()
        .css({ top: 0, left: 0, display: 'block' })
149
        .addClass(placement)
fat's avatar
fat committed
150
151
152
153
154
155
156

      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

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
      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)
      }

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

fat's avatar
fat committed
179
      this.applyPlacement(calculatedOffset, placement)
180
      this.$element.trigger('shown.bs.' + this.type)
181
    }
fat's avatar
fat committed
182
  }
183

184
  Tooltip.prototype.applyPlacement = function (offset, placement) {
fat's avatar
fat committed
185
    var replace
fat's avatar
fat committed
186
187
188
    var $tip   = this.tip()
    var width  = $tip[0].offsetWidth
    var height = $tip[0].offsetHeight
189

190
    // manually read margins because getBoundingClientRect includes difference
fat's avatar
fat committed
191
192
193
194
195
196
197
198
199
    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
200

fat's avatar
fat committed
201
202
203
    $tip
      .offset(offset)
      .addClass('in')
204

fat's avatar
fat committed
205
    // check to see if placing tip in new offset caused the tip to resize itself
fat's avatar
fat committed
206
207
    var actualWidth  = $tip[0].offsetWidth
    var actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
208

fat's avatar
fat committed
209
    if (placement == 'top' && actualHeight != height) {
fat's avatar
fat committed
210
      replace = true
fat's avatar
fat committed
211
      offset.top = offset.top + height - actualHeight
fat's avatar
fat committed
212
213
    }

fat's avatar
fat committed
214
    if (/bottom|top/.test(placement)) {
fat's avatar
fat committed
215
      var delta = 0
216

fat's avatar
fat committed
217
      if (offset.left < 0) {
fat's avatar
fat committed
218
219
        delta       = offset.left * -2
        offset.left = 0
220

fat's avatar
fat committed
221
        $tip.offset(offset)
222

fat's avatar
fat committed
223
224
        actualWidth  = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
225
      }
226

fat's avatar
fat committed
227
228
229
      this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
    } else {
      this.replaceArrow(actualHeight - height, actualHeight, 'top')
fat's avatar
fat committed
230
231
    }

fat's avatar
fat committed
232
233
    if (replace) $tip.offset(offset)
  }
234

235
  Tooltip.prototype.replaceArrow = function (delta, dimension, position) {
XhmikosR's avatar
XhmikosR committed
236
    this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + '%') : '')
fat's avatar
fat committed
237
  }
238

fat's avatar
fat committed
239
240
241
  Tooltip.prototype.setContent = function () {
    var $tip  = this.tip()
    var title = this.getTitle()
Jacob Thornton's avatar
Jacob Thornton committed
242

fat's avatar
fat committed
243
244
245
    $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
    $tip.removeClass('fade in top bottom left right')
  }
246

fat's avatar
fat committed
247
248
249
  Tooltip.prototype.hide = function () {
    var that = this
    var $tip = this.tip()
250
    var e    = $.Event('hide.bs.' + this.type)
251

252
253
254
    function complete() {
      if (that.hoverState != 'in') $tip.detach()
    }
Jacob Thornton's avatar
Jacob Thornton committed
255

fat's avatar
fat committed
256
    this.$element.trigger(e)
257

fat's avatar
fat committed
258
    if (e.isDefaultPrevented()) return
259

fat's avatar
fat committed
260
261
262
    $tip.removeClass('in')

    $.support.transition && this.$tip.hasClass('fade') ?
263
      $tip
Jacob Thornton's avatar
Jacob Thornton committed
264
        .one($.support.transition.end, complete)
265
        .emulateTransitionEnd(150) :
Jacob Thornton's avatar
Jacob Thornton committed
266
      complete()
267

268
    this.$element.trigger('hidden.bs.' + this.type)
269

fat's avatar
fat committed
270
271
    return this
  }
272

fat's avatar
fat committed
273
274
275
276
  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', '')
277
    }
fat's avatar
fat committed
278
  }
279

fat's avatar
fat committed
280
281
282
  Tooltip.prototype.hasContent = function () {
    return this.getTitle()
  }
283

fat's avatar
fat committed
284
285
286
  Tooltip.prototype.getPosition = function () {
    var el = this.$element[0]
    return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
Zlatan Vasović's avatar
Zlatan Vasović committed
287
288
      width: el.offsetWidth,
      height: el.offsetHeight
fat's avatar
fat committed
289
290
    }, this.$element.offset())
  }
291

292
  Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
fat's avatar
fat committed
293
294
295
296
297
298
    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
299
300
301
302
  Tooltip.prototype.getTitle = function () {
    var title
    var $e = this.$element
    var o  = this.options
303

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

fat's avatar
fat committed
307
308
    return title
  }
309

fat's avatar
fat committed
310
311
312
  Tooltip.prototype.tip = function () {
    return this.$tip = this.$tip || $(this.options.template)
  }
313

Chris Rebert's avatar
Chris Rebert committed
314
  Tooltip.prototype.arrow = function () {
fat's avatar
fat committed
315
    return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
fat's avatar
fat committed
316
  }
317

fat's avatar
fat committed
318
319
320
321
322
  Tooltip.prototype.validate = function () {
    if (!this.$element[0].parentNode) {
      this.hide()
      this.$element = null
      this.options  = null
323
    }
fat's avatar
fat committed
324
  }
325

fat's avatar
fat committed
326
327
328
  Tooltip.prototype.enable = function () {
    this.enabled = true
  }
329

fat's avatar
fat committed
330
331
332
  Tooltip.prototype.disable = function () {
    this.enabled = false
  }
333

fat's avatar
fat committed
334
335
336
  Tooltip.prototype.toggleEnabled = function () {
    this.enabled = !this.enabled
  }
337

fat's avatar
fat committed
338
  Tooltip.prototype.toggle = function (e) {
Jacob Thornton's avatar
Jacob Thornton committed
339
    var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
fat's avatar
fat committed
340
    self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
fat's avatar
fat committed
341
  }
342

fat's avatar
fat committed
343
  Tooltip.prototype.destroy = function () {
344
    this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
345
346
347
  }


fat's avatar
fat committed
348
349
  // TOOLTIP PLUGIN DEFINITION
  // =========================
350

351
352
  var old = $.fn.tooltip

fat's avatar
fat committed
353
  $.fn.tooltip = function (option) {
354
    return this.each(function () {
fat's avatar
fat committed
355
      var $this   = $(this)
356
      var data    = $this.data('bs.tooltip')
fat's avatar
fat committed
357
358
      var options = typeof option == 'object' && option

359
      if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
360
361
      if (typeof option == 'string') data[option]()
    })
362
363
  }

364
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
365

366

fat's avatar
fat committed
367
368
  // TOOLTIP NO CONFLICT
  // ===================
369
370
371
372
373
374

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

375
}(jQuery);