tooltip.js 11.8 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: tooltip.js v3.1.1
3
 * http://getbootstrap.com/javascript/#tooltip
4
 * Inspired by the original jQuery.tipsy by Jason Frame
5
 * ========================================================================
6
 * Copyright 2011-2014 Twitter, Inc.
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
9

10

Zlatan Vasović's avatar
Zlatan Vasović committed
11
12
+function ($) {
  'use strict';
13

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

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

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

fat's avatar
fat committed
28
  Tooltip.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
29
30
31
    animation: true,
    placement: 'top',
    selector: false,
32
    template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
Zlatan Vasović's avatar
Zlatan Vasović committed
33
34
35
36
    trigger: 'hover focus',
    title: '',
    delay: 0,
    html: false,
37
    container: false
fat's avatar
fat committed
38
  }
39

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

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

fat's avatar
fat committed
48
49
50
51
52
53
    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') {
54
55
        var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin'
        var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
fat's avatar
fat committed
56

Jacob Thornton's avatar
Jacob Thornton committed
57
        this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
fat's avatar
fat committed
58
        this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
59
      }
fat's avatar
fat committed
60
61
62
63
64
65
66
67
68
69
70
71
72
    }

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

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

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

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

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

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

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

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

101
102
    self.hoverState = 'in'

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

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

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

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

116
117
    self.hoverState = 'out'

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

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

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

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

fat's avatar
fat committed
131
      if (e.isDefaultPrevented()) return
132
      var that = this;
133

fat's avatar
fat committed
134
      var $tip = this.tip()
Yohn's avatar
Yohn committed
135

fat's avatar
fat committed
136
      this.setContent()
137

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

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

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

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

      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

159
160
161
      if (autoPlace) {
        var $parent = this.$element.parent()

162
        var orgPlacement = placement
163
        var docScroll    = document.documentElement.scrollTop
164
165
166
167
168
169
170
171
        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'  :
172
173
174
175
176
177
178
                    placement

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

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

fat's avatar
fat committed
181
      this.applyPlacement(calculatedOffset, placement)
182
      this.hoverState = null
183
184
185
186
187
188

      var complete = function() {
        that.$element.trigger('shown.bs.' + that.type)
      }

      $.support.transition && this.$tip.hasClass('fade') ?
189
190
191
192
        $tip
          .one($.support.transition.end, complete)
          .emulateTransitionEnd(150) :
        complete()
193
    }
fat's avatar
fat committed
194
  }
195

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

202
    // manually read margins because getBoundingClientRect includes difference
fat's avatar
fat committed
203
204
205
206
207
208
209
210
211
    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
212

213
214
    // $.fn.offset doesn't round pixel values
    // so we use setOffset directly with our own function B-0
Kevin Sawicki's avatar
Kevin Sawicki committed
215
    $.offset.setOffset($tip[0], $.extend({
216
217
218
219
220
221
222
223
224
      using: function (props) {
        $tip.css({
          top: Math.round(props.top),
          left: Math.round(props.left)
        })
      }
    }, offset), 0)

    $tip.addClass('in')
225

fat's avatar
fat committed
226
    // check to see if placing tip in new offset caused the tip to resize itself
fat's avatar
fat committed
227
228
    var actualWidth  = $tip[0].offsetWidth
    var actualHeight = $tip[0].offsetHeight
fat's avatar
fat committed
229

fat's avatar
fat committed
230
    if (placement == 'top' && actualHeight != height) {
231
      replace = true
fat's avatar
fat committed
232
      offset.top = offset.top + height - actualHeight
fat's avatar
fat committed
233
234
    }

235
236
237
238
239
240
    if (/bottom|top/.test(placement)) {
      var delta = 0

      if (offset.left < 0) {
        delta       = offset.left * -2
        offset.left = 0
241

242
243
244
245
246
        $tip.offset(offset)

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

248
249
250
251
      this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
    } else {
      this.replaceArrow(actualHeight - height, actualHeight, 'top')
    }
fat's avatar
fat committed
252

253
    if (replace) $tip.offset(offset)
fat's avatar
fat committed
254
  }
255

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

fat's avatar
fat committed
260
261
262
  Tooltip.prototype.setContent = function () {
    var $tip  = this.tip()
    var title = this.getTitle()
Jacob Thornton's avatar
Jacob Thornton committed
263

fat's avatar
fat committed
264
265
266
    $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
    $tip.removeClass('fade in top bottom left right')
  }
267

fat's avatar
fat committed
268
269
270
  Tooltip.prototype.hide = function () {
    var that = this
    var $tip = this.tip()
271
    var e    = $.Event('hide.bs.' + this.type)
272

273
274
    function complete() {
      if (that.hoverState != 'in') $tip.detach()
275
      that.$element.trigger('hidden.bs.' + that.type)
276
    }
Jacob Thornton's avatar
Jacob Thornton committed
277

fat's avatar
fat committed
278
    this.$element.trigger(e)
279

fat's avatar
fat committed
280
    if (e.isDefaultPrevented()) return
281

fat's avatar
fat committed
282
283
284
    $tip.removeClass('in')

    $.support.transition && this.$tip.hasClass('fade') ?
285
      $tip
Jacob Thornton's avatar
Jacob Thornton committed
286
        .one($.support.transition.end, complete)
287
        .emulateTransitionEnd(150) :
Jacob Thornton's avatar
Jacob Thornton committed
288
      complete()
289

290
    this.hoverState = null
291

fat's avatar
fat committed
292
293
    return this
  }
294

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

fat's avatar
fat committed
302
303
304
  Tooltip.prototype.hasContent = function () {
    return this.getTitle()
  }
305

fat's avatar
fat committed
306
307
308
  Tooltip.prototype.getPosition = function () {
    var el = this.$element[0]
    return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
Zlatan Vasović's avatar
Zlatan Vasović committed
309
310
      width: el.offsetWidth,
      height: el.offsetHeight
fat's avatar
fat committed
311
312
    }, this.$element.offset())
  }
313

314
  Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
fat's avatar
fat committed
315
316
317
318
319
320
    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
321
322
323
324
  Tooltip.prototype.getTitle = function () {
    var title
    var $e = this.$element
    var o  = this.options
325

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

fat's avatar
fat committed
329
330
    return title
  }
331

fat's avatar
fat committed
332
333
334
  Tooltip.prototype.tip = function () {
    return this.$tip = this.$tip || $(this.options.template)
  }
335

Chris Rebert's avatar
Chris Rebert committed
336
  Tooltip.prototype.arrow = function () {
fat's avatar
fat committed
337
    return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
fat's avatar
fat committed
338
  }
339

fat's avatar
fat committed
340
341
342
343
344
  Tooltip.prototype.validate = function () {
    if (!this.$element[0].parentNode) {
      this.hide()
      this.$element = null
      this.options  = null
345
    }
fat's avatar
fat committed
346
  }
347

fat's avatar
fat committed
348
349
350
  Tooltip.prototype.enable = function () {
    this.enabled = true
  }
351

fat's avatar
fat committed
352
353
354
  Tooltip.prototype.disable = function () {
    this.enabled = false
  }
355

fat's avatar
fat committed
356
357
358
  Tooltip.prototype.toggleEnabled = function () {
    this.enabled = !this.enabled
  }
359

fat's avatar
fat committed
360
  Tooltip.prototype.toggle = function (e) {
Jacob Thornton's avatar
Jacob Thornton committed
361
    var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
fat's avatar
fat committed
362
    self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
fat's avatar
fat committed
363
  }
364

fat's avatar
fat committed
365
  Tooltip.prototype.destroy = function () {
fat's avatar
nope    
fat committed
366
    clearTimeout(this.timeout)
367
    this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
368
369
370
  }


fat's avatar
fat committed
371
372
  // TOOLTIP PLUGIN DEFINITION
  // =========================
373

374
375
  var old = $.fn.tooltip

fat's avatar
fat committed
376
  $.fn.tooltip = function (option) {
377
    return this.each(function () {
fat's avatar
fat committed
378
      var $this   = $(this)
379
      var data    = $this.data('bs.tooltip')
fat's avatar
fat committed
380
381
      var options = typeof option == 'object' && option

fat's avatar
fat committed
382
      if (!data && option == 'destroy') return
383
      if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
384
385
      if (typeof option == 'string') data[option]()
    })
386
387
  }

388
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
389

390

fat's avatar
fat committed
391
392
  // TOOLTIP NO CONFLICT
  // ===================
393
394
395
396
397
398

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

399
}(jQuery);