bootstrap-tooltip.js 7.57 KB
Newer Older
1
/* ===========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-tooltip.js v2.2.2
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
41
42
43
44
45
46
      var eventIn
        , eventOut

      this.type = type
      this.$element = $(element)
      this.options = this.getOptions(options)
      this.enabled = true

47
48
49
50
      if (this.options.trigger == 'click') {
        this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
      } else if (this.options.trigger != 'manual') {
        eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
51
        eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
Jon Stevens's avatar
Jon Stevens committed
52
53
        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))
54
55
56
57
58
59
60
      }

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

61
  , getOptions: function (options) {
62
63
64
65
66
67
68
69
70
71
72
73
      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
    }

74
  , enter: function (e) {
75
76
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

77
78
79
80
81
82
83
      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)
84
85
    }

86
  , leave: function (e) {
87
88
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

89
      if (this.timeout) clearTimeout(this.timeout)
90
91
92
93
94
95
      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)
96
97
98
99
    }

  , show: function () {
      var $tip
100
        , inside
101
        , pos
102
103
104
105
106
        , actualWidth
        , actualHeight
        , placement
        , tp

107
      if (this.hasContent() && this.enabled) {
108
        $tip = this.tip()
Jacob Thornton's avatar
Jacob Thornton committed
109
        this.setContent()
110

Jacob Thornton's avatar
Jacob Thornton committed
111
        if (this.options.animation) {
112
113
114
          $tip.addClass('fade')
        }

115
        placement = typeof this.options.placement == 'function' ?
116
          this.options.placement.call(this, $tip[0], this.$element[0]) :
117
118
119
120
          this.options.placement

        inside = /in/.test(placement)

121
        $tip
frntz's avatar
frntz committed
122
          .detach()
123
          .css({ top: 0, left: 0, display: 'block' })
124
          .insertAfter(this.$element)
125

126
        pos = this.getPosition(inside)
127
128
129

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

131
132
        switch (inside ? placement.split(' ')[1] : placement) {
          case 'bottom':
133
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
134
            break
135
          case 'top':
136
            tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
137
138
            break
          case 'left':
139
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
140
141
            break
          case 'right':
142
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
143
144
145
146
            break
        }

        $tip
147
          .offset(tp)
148
          .addClass(placement)
149
          .addClass('in')
150
151
152
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
153
154
  , setContent: function () {
      var $tip = this.tip()
155
156
        , title = this.getTitle()

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

161
  , hide: function () {
162
163
164
      var that = this
        , $tip = this.tip()

165
      $tip.removeClass('in')
166

167
168
      function removeWithAnimation() {
        var timeout = setTimeout(function () {
frntz's avatar
frntz committed
169
          $tip.off($.support.transition.end).detach()
170
171
172
173
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
frntz's avatar
frntz committed
174
          $tip.detach()
175
        })
176
177
      }

178
      $.support.transition && this.$tip.hasClass('fade') ?
179
        removeWithAnimation() :
frntz's avatar
frntz committed
180
        $tip.detach()
Jacob Thornton's avatar
Jacob Thornton committed
181

182
      return this
183
184
    }

185
  , fixTitle: function () {
186
187
188
189
190
191
      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')
      }
    }

192
193
194
195
  , hasContent: function () {
      return this.getTitle()
    }

196
197
198
199
200
201
202
  , getPosition: function (inside) {
      return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
        width: this.$element[0].offsetWidth
      , height: this.$element[0].offsetHeight
      })
    }

203
  , getTitle: function () {
204
205
206
207
      var title
        , $e = this.$element
        , o = this.options

208
209
      title = $e.attr('data-original-title')
        || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
210

211
      return title
212
213
    }

214
  , tip: function () {
215
      return this.$tip = this.$tip || $(this.options.template)
216
217
    }

218
  , validate: function () {
219
220
221
222
223
224
225
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

226
  , enable: function () {
227
228
229
      this.enabled = true
    }

230
  , disable: function () {
231
232
233
      this.enabled = false
    }

234
  , toggleEnabled: function () {
235
236
237
      this.enabled = !this.enabled
    }

238
239
240
  , toggle: function (e) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)
      self[self.tip().hasClass('in') ? 'hide' : 'show']()
241
242
    }

243
  , destroy: function () {
Jon Stevens's avatar
Jon Stevens committed
244
      this.hide().$element.off('.' + this.type).removeData(this.type)
245
246
    }

247
248
249
  }


250
251
 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */
252

253
  $.fn.tooltip = function ( option ) {
254
255
    return this.each(function () {
      var $this = $(this)
256
        , data = $this.data('tooltip')
257
        , options = typeof option == 'object' && option
258
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
259
260
      if (typeof option == 'string') data[option]()
    })
261
262
  }

263
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
264

265
  $.fn.tooltip.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
266
    animation: true
267
  , placement: 'top'
268
269
  , selector: false
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
270
  , trigger: 'hover'
271
  , title: ''
272
  , delay: 0
273
  , html: false
274
275
  }

276
}(window.jQuery);