bootstrap-twipsy.js 6.85 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
2
 * bootstrap-twipsy.js v2.0.0
Jacob Thornton's avatar
Jacob Thornton committed
3
 * http://twitter.github.com/bootstrap/javascript.html#twipsy
4
 * Inspired by the original jQuery.tipsy by Jason Frame
Jacob Thornton's avatar
Jacob Thornton committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * ==========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * 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
27
28
 /* TWIPSY PUBLIC CLASS DEFINITION
  * ============================== */

  var Twipsy = function ( element, options ) {
29
    this.init('twipsy', element, options)
30
31
32
33
  }

  Twipsy.prototype = {

34
35
    constructor: Twipsy

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  , 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 {
        self.hoverState = 'in'
        setTimeout(function() {
          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 {
        setTimeout(function() {
          self.hoverState = 'out'
          if (self.hoverState == 'out') {
            self.hide()
          }
        }, self.options.delay.hide)
      }
    }

  , show: function () {
      var $tip
        , pos
103
104
105
106
107
        , actualWidth
        , actualHeight
        , placement
        , tp

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

        if (this.options.animate) {
          $tip.addClass('fade')
        }

116
117
118
119
120
121
122
123
124
125
126
127
        $tip
          .remove()
          .css({ top: 0, left: 0, display: 'block' })
          .prependTo(document.body)

        pos = $.extend({}, this.$element.offset(), {
          width: this.$element[0].offsetWidth
        , height: this.$element[0].offsetHeight
        })

        actualWidth = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight
128
129

        placement = maybeCall(this.options.placement, this, [ $tip[0], this.$element[0] ])
130
131
132

        switch (placement) {
          case 'below':
133
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
134
135
            break
          case 'above':
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
147
148
            break
        }

        $tip
          .css(tp)
          .addClass(placement)
149
          .addClass('in')
150
151
152
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
153
154
  , setContent: function () {
      var $tip = this.tip()
155
      $tip.find('.twipsy-inner').html(this.getTitle())
Jacob Thornton's avatar
Jacob Thornton committed
156
157
158
      $tip[0].className = 'twipsy'
    }

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

163
      $tip.removeClass('in')
164
165
166
167
168

      function removeElement () {
        $tip.remove()
      }

169
      $.support.transition && this.$tip.hasClass('fade') ?
170
        $tip.bind( $.support.transition.end, removeElement) :
171
172
173
        removeElement()
    }

174
  , fixTitle: function () {
175
176
177
178
179
180
      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')
      }
    }

181
182
183
184
  , hasContent: function () {
      return this.getTitle()
    }

185
  , getTitle: function () {
186
187
188
189
      var title
        , $e = this.$element
        , o = this.options

190
191
      title = $e.attr('data-original-title')
        || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
192

193
      title = title.toString().replace(/(^\s*|\s*$)/, "")
194

195
      return title
196
197
    }

198
  , tip: function () {
199
      return this.$tip = this.$tip || $('<div class="twipsy" />').html(this.options.template)
200
201
    }

202
  , validate: function () {
203
204
205
206
207
208
209
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

210
  , enable: function () {
211
212
213
      this.enabled = true
    }

214
  , disable: function () {
215
216
217
      this.enabled = false
    }

218
  , toggleEnabled: function () {
219
220
221
      this.enabled = !this.enabled
    }

222
223
224
225
  , toggle: function () {
      this[this.tip().hasClass('in') ? 'hide' : 'show']()
    }

226
227
228
229
230
231
  }


 /* TWIPSY PRIVATE METHODS
  * ====================== */

232
233
   function maybeCall ( thing, ctx, args ) {
     return typeof thing == 'function' ? thing.apply(ctx, args) : thing
234
235
   }

236

Jacob Thornton's avatar
Jacob Thornton committed
237
238
 /* TWIPSY PLUGIN DEFINITION
  * ======================== */
239

240
241
242
243
244
245
246
247
  $.fn.twipsy = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('twipsy')
        , options = typeof option == 'object' && option
      if (!data) $this.data('twipsy', (data = new Twipsy(this, options)))
      if (typeof option == 'string') data[option]()
    })
248
249
  }

Jacob Thornton's avatar
Jacob Thornton committed
250
251
  $.fn.twipsy.Twipsy = Twipsy

252
  $.fn.twipsy.defaults = {
253
    animate: true
254
  , delay: 0
255
  , selector: false
256
257
  , placement: 'above'
  , trigger: 'hover'
258
  , title: ''
259
  , template: '<div class="twipsy-arrow"></div><div class="twipsy-inner"></div>'
260
261
  }

262
}( window.jQuery )