bootstrap-tooltip.js 8.19 KB
Newer Older
1
/* ===========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-tooltip.js v2.3.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#tooltips
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
19
20
 *
 * 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 ;_;
25

26

27
28
 /* TOOLTIP PUBLIC CLASS DEFINITION
  * =============================== */
29

30
  var Tooltip = function (element, options) {
31
    this.init('tooltip', element, options)
32
33
  }

34
  Tooltip.prototype = {
35

36
    constructor: Tooltip
37

38
  , init: function (type, element, options) {
39
40
      var eventIn
        , eventOut
41
42
43
        , triggers
        , trigger
        , i
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))
        }
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
71
72
73
74
75
76
77
78
79
80
81
      options = $.extend({}, $.fn[this.type].defaults, options, this.$element.data())

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

      return options
    }

82
  , enter: function (e) {
83
84
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

85
86
87
88
89
90
91
      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)
92
93
    }

94
  , leave: function (e) {
95
96
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

97
      if (this.timeout) clearTimeout(this.timeout)
98
99
100
101
102
103
      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)
104
105
106
107
108
    }

  , show: function () {
      var $tip
        , pos
109
110
111
112
        , actualWidth
        , actualHeight
        , placement
        , tp
113
        , e = $.Event('show')
114

115
      if (this.hasContent() && this.enabled) {
116
        this.$element.trigger(e)
117
        if (e.isDefaultPrevented()) return
118
        $tip = this.tip()
Jacob Thornton's avatar
Jacob Thornton committed
119
        this.setContent()
120

Jacob Thornton's avatar
Jacob Thornton committed
121
        if (this.options.animation) {
122
123
124
          $tip.addClass('fade')
        }

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

129
        $tip
frntz's avatar
frntz committed
130
          .detach()
131
          .css({ top: 0, left: 0, display: 'block' })
Yohn's avatar
Yohn committed
132

Yohn's avatar
Yohn committed
133
        this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
134

135
        pos = this.getPosition()
136
137
138

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

140
        switch (placement) {
141
          case 'bottom':
142
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
143
            break
144
          case 'top':
145
            tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
146
147
            break
          case 'left':
148
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
149
150
            break
          case 'right':
151
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
152
153
154
155
            break
        }

        $tip
156
          .offset(tp)
157
          .addClass(placement)
158
          .addClass('in')
159
160

        this.$element.trigger('shown')
161
162
163
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
164
165
  , setContent: function () {
      var $tip = this.tip()
166
167
        , title = this.getTitle()

168
      $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
169
      $tip.removeClass('fade in top bottom left right')
Jacob Thornton's avatar
Jacob Thornton committed
170
171
    }

172
  , hide: function () {
173
174
      var that = this
        , $tip = this.tip()
175
        , e = $.Event('hide')
176

177
      this.$element.trigger(e)
178
      if (e.isDefaultPrevented()) return
179

180
      $tip.removeClass('in')
181

182
183
      function removeWithAnimation() {
        var timeout = setTimeout(function () {
frntz's avatar
frntz committed
184
          $tip.off($.support.transition.end).detach()
185
186
187
188
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
frntz's avatar
frntz committed
189
          $tip.detach()
190
        })
191
192
      }

193
      $.support.transition && this.$tip.hasClass('fade') ?
194
        removeWithAnimation() :
frntz's avatar
frntz committed
195
        $tip.detach()
Jacob Thornton's avatar
Jacob Thornton committed
196

197
198
      this.$element.trigger('hidden')

199
      return this
200
201
    }

202
  , fixTitle: function () {
203
204
205
206
207
208
      var $e = this.$element
      if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
        $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
      }
    }

209
210
211
212
  , hasContent: function () {
      return this.getTitle()
    }

213
214
  , getPosition: function () {
      var el = this.$element[0]
fat's avatar
fat committed
215
      return $.extend({}, el.getBoundingClientRect ? el.getBoundingClientRect() : {
216
217
218
        width: el.offsetWidth
      , height: el.offsetHeight
      }, this.$element.offset())
219
220
    }

221
  , getTitle: function () {
222
223
224
225
      var title
        , $e = this.$element
        , o = this.options

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

229
      return title
230
231
    }

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

236
  , validate: function () {
237
238
239
240
241
242
243
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

244
  , enable: function () {
245
246
247
      this.enabled = true
    }

248
  , disable: function () {
249
250
251
      this.enabled = false
    }

252
  , toggleEnabled: function () {
253
254
255
      this.enabled = !this.enabled
    }

256
  , toggle: function (e) {
Yohn's avatar
Yohn committed
257
258
      var self = e ? $(e.currentTarget)[this.type](this._options).data(this.type) : this
      self.tip().hasClass('in') ? self.hide() : self.show()
259
260
    }

261
  , destroy: function () {
Jon Stevens's avatar
Jon Stevens committed
262
      this.hide().$element.off('.' + this.type).removeData(this.type)
263
264
    }

265
266
267
  }


268
269
 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */
270

271
272
  var old = $.fn.tooltip

273
  $.fn.tooltip = function ( option ) {
274
275
    return this.each(function () {
      var $this = $(this)
276
        , data = $this.data('tooltip')
277
        , options = typeof option == 'object' && option
278
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
279
280
      if (typeof option == 'string') data[option]()
    })
281
282
  }

283
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
284

285
  $.fn.tooltip.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
286
    animation: true
287
  , placement: 'top'
288
289
  , selector: false
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
290
  , trigger: 'hover focus'
291
  , title: ''
292
  , delay: 0
293
  , html: false
Yohn's avatar
Yohn committed
294
  , container: false
295
296
  }

297
298
299
300
301
302
303
304
305

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

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

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