bootstrap-tooltip.js 7.24 KB
Newer Older
1
/* ===========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-tooltip.js v2.0.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
!function( $ ) {
22

23
24
  "use strict"

25
26
 /* TOOLTIP PUBLIC CLASS DEFINITION
  * =============================== */
27

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

32
  Tooltip.prototype = {
33

34
    constructor: Tooltip
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  , init: function ( type, element, options ) {
      var eventIn
        , eventOut

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

      if (this.options.trigger != 'manual') {
        eventIn  = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
        eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
        this.$element.on(eventIn, this.options.selector, $.proxy(this.enter, this))
        this.$element.on(eventOut, this.options.selector, $.proxy(this.leave, this))
      }

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

  , getOptions: function ( options ) {
      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
    }

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

      if (!self.options.delay || !self.options.delay.show) {
        self.show()
      } else {
76
        clearTimeout(this.timeout)
77
        self.hoverState = 'in'
78
        this.timeout = setTimeout(function() {
79
80
81
82
83
84
85
86
87
88
89
90
91
          if (self.hoverState == 'in') {
            self.show()
          }
        }, self.options.delay.show)
      }
    }

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

      if (!self.options.delay || !self.options.delay.hide) {
        self.hide()
      } else {
92
        clearTimeout(this.timeout)
93
        self.hoverState = 'out'
94
        this.timeout = setTimeout(function() {
95
96
97
98
99
100
101
102
103
          if (self.hoverState == 'out') {
            self.hide()
          }
        }, self.options.delay.hide)
      }
    }

  , show: function () {
      var $tip
104
        , inside
105
        , pos
106
107
108
109
110
        , actualWidth
        , actualHeight
        , placement
        , tp

111
      if (this.hasContent() && this.enabled) {
112
        $tip = this.tip()
Jacob Thornton's avatar
Jacob Thornton committed
113
        this.setContent()
114

Jacob Thornton's avatar
Jacob Thornton committed
115
        if (this.options.animation) {
116
117
118
          $tip.addClass('fade')
        }

119
        placement = typeof this.options.placement == 'function' ?
120
          this.options.placement.call(this, $tip[0], this.$element[0]) :
121
122
123
124
          this.options.placement

        inside = /in/.test(placement)

125
126
127
        $tip
          .remove()
          .css({ top: 0, left: 0, display: 'block' })
Jacob Thornton's avatar
Jacob Thornton committed
128
          .appendTo(inside ? this.$element : document.body)
129

130
        pos = this.getPosition(inside)
131
132
133

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

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

        $tip
          .css(tp)
          .addClass(placement)
153
          .addClass('in')
154
155
156
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
157
158
  , setContent: function () {
      var $tip = this.tip()
159
      $tip.find('.tooltip-inner').html(this.getTitle())
160
      $tip.removeClass('fade in top bottom left right')
Jacob Thornton's avatar
Jacob Thornton committed
161
162
    }

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

167
      $tip.removeClass('in')
168

169
170
171
172
173
174
175
176
177
      function removeWithAnimation() {
        var timeout = setTimeout(function () {
          $tip.off($.support.transition.end).remove()
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
          $tip.remove()
        })
178
179
      }

180
      $.support.transition && this.$tip.hasClass('fade') ?
181
182
        removeWithAnimation() :
        $tip.remove()
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
241
  , toggle: function () {
      this[this.tip().hasClass('in') ? 'hide' : 'show']()
    }

242
243
244
  }


245
246
 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */
247

248
  $.fn.tooltip = function ( option ) {
249
250
    return this.each(function () {
      var $this = $(this)
251
        , data = $this.data('tooltip')
252
        , options = typeof option == 'object' && option
253
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
254
255
      if (typeof option == 'string') data[option]()
    })
256
257
  }

258
  $.fn.tooltip.Constructor = Tooltip
Jacob Thornton's avatar
Jacob Thornton committed
259

260
  $.fn.tooltip.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
261
    animation: true
262
  , delay: 0
263
  , selector: false
264
  , placement: 'top'
265
  , trigger: 'hover'
266
  , title: ''
267
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
268
269
  }

270
}( window.jQuery );