bootstrap-twipsy.js 7.7 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
2
 * bootstrap-twipsy.js v1.3.0
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * http://twitter.github.com/bootstrap/javascript.html#twipsy
 * Adapted from the original jQuery.tipsy by Jason Frame
 * ==========================================================
 * 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

22
!function( $ ) {
23

Jacob Thornton's avatar
Jacob Thornton committed
24
25
 /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  * ======================================================= */
26
27
28

  var transitionEnd

29
  $(document).ready(function () {
Jacob Thornton's avatar
Jacob Thornton committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    $.support.transition = (function () {
      var thisBody = document.body || document.documentElement
        , thisStyle = thisBody.style
        , support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined
      return support
    })()

    // set CSS transition event type
    if ( $.support.transition ) {
      transitionEnd = "TransitionEnd"
      if ( $.browser.webkit ) {
      	transitionEnd = "webkitTransitionEnd"
      } else if ( $.browser.mozilla ) {
      	transitionEnd = "transitionend"
      } else if ( $.browser.opera ) {
      	transitionEnd = "oTransitionEnd"
      }
48
    }
Jacob Thornton's avatar
Jacob Thornton committed
49
50

  })
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


 /* TWIPSY PUBLIC CLASS DEFINITION
  * ============================== */

  var Twipsy = function ( element, options ) {
    this.$element = $(element)
    this.options = options
    this.enabled = true
    this.fixTitle()
  }

  Twipsy.prototype = {

    show: function() {
Jacob Thornton's avatar
Jacob Thornton committed
66
      var pos
67
68
69
70
71
72
        , actualWidth
        , actualHeight
        , placement
        , $tip
        , tp

Jacob Thornton's avatar
Jacob Thornton committed
73
      if (this.getTitle() && this.enabled) {
74
        $tip = this.tip()
Jacob Thornton's avatar
Jacob Thornton committed
75
        this.setContent()
76
77
78
79
80

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

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        $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
        placement = _.maybeCall(this.options.placement, this.$element[0])

        switch (placement) {
          case 'below':
            tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'above':
            tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'left':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset}
            break
          case 'right':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset}
            break
        }

        $tip
          .css(tp)
          .addClass(placement)
113
          .addClass('in')
114
115
116
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
117
118
119
120
121
122
  , setContent: function () {
      var $tip = this.tip()
      $tip.find('.twipsy-inner')[this.options.html ? 'html' : 'text'](this.getTitle())
      $tip[0].className = 'twipsy'
    }

123
124
125
126
  , hide: function() {
      var that = this
        , $tip = this.tip()

127
      $tip.removeClass('in')
128
129
130
131
132

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

133
      $.support.transition && this.$tip.hasClass('fade') ?
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        $tip.bind(transitionEnd, removeElement) :
        removeElement()
    }

  , 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') || '').removeAttr('title')
      }
    }

  , getTitle: function() {
      var title
        , $e = this.$element
        , o = this.options

        this.fixTitle()

        if (typeof o.title == 'string') {
          title = $e.attr(o.title == 'title' ? 'data-original-title' : o.title)
        } else if (typeof o.title == 'function') {
          title = o.title.call($e[0])
        }

        title = ('' + title).replace(/(^\s*|\s*$)/, "")

        return title || o.fallback
    }

  , tip: function() {
      if (!this.$tip) {
        this.$tip = $('<div class="twipsy" />').html('<div class="twipsy-arrow"></div><div class="twipsy-inner"></div>')
      }
      return this.$tip
    }

  , validate: function() {
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

  , enable: function() {
      this.enabled = true
    }

  , disable: function() {
      this.enabled = false
    }

  , toggleEnabled: function() {
      this.enabled = !this.enabled
    }

  }


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

   var _ = {

     maybeCall: function ( thing, ctx ) {
       return (typeof thing == 'function') ? (thing.call(ctx)) : thing
     }

   }


Jacob Thornton's avatar
Jacob Thornton committed
205
206
 /* TWIPSY PLUGIN DEFINITION
  * ======================== */
207

Jacob Thornton's avatar
Jacob Thornton committed
208
  $.fn.twipsy = function (options) {
209
    $.fn.twipsy.initWith.call(this, options, Twipsy, 'twipsy')
Jacob Thornton's avatar
Jacob Thornton committed
210
    return this
Jacob Thornton's avatar
Jacob Thornton committed
211
212
  }

213
  $.fn.twipsy.initWith = function (options, Constructor, name) {
214
215
216
217
218
219
    var twipsy
      , binder
      , eventIn
      , eventOut

    if (options === true) {
220
      return this.data(name)
221
    } else if (typeof options == 'string') {
222
      twipsy = this.data(name)
223
224
225
226
227
228
      if (twipsy) {
        twipsy[options]()
      }
      return this
    }

229
    options = $.extend({}, $.fn[name].defaults, options)
230
231

    function get(ele) {
232
      var twipsy = $.data(ele, name)
233
234

      if (!twipsy) {
Jacob Thornton's avatar
Jacob Thornton committed
235
        twipsy = new Constructor(ele, $.fn.twipsy.elementOptions(ele, options))
236
        $.data(ele, name, twipsy)
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
      }

      return twipsy
    }

    function enter() {
      var twipsy = get(this)
      twipsy.hoverState = 'in'

      if (options.delayIn == 0) {
        twipsy.show()
      } else {
        twipsy.fixTitle()
        setTimeout(function() {
          if (twipsy.hoverState == 'in') {
            twipsy.show()
          }
        }, options.delayIn)
      }
    }

    function leave() {
      var twipsy = get(this)
      twipsy.hoverState = 'out'
      if (options.delayOut == 0) {
        twipsy.hide()
      } else {
        setTimeout(function() {
          if (twipsy.hoverState == 'out') {
            twipsy.hide()
          }
        }, options.delayOut)
      }
    }

    if (!options.live) {
      this.each(function() {
        get(this)
      })
    }

    if (options.trigger != 'manual') {
      binder   = options.live ? 'live' : 'bind'
      eventIn  = options.trigger == 'hover' ? 'mouseenter' : 'focus'
      eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur'
      this[binder](eventIn, enter)[binder](eventOut, leave)
    }

    return this
  }

Jacob Thornton's avatar
Jacob Thornton committed
288
289
  $.fn.twipsy.Twipsy = Twipsy

290
  $.fn.twipsy.defaults = {
291
292
    animate: true
  , delayIn: 0
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  , delayOut: 0
  , fallback: ''
  , placement: 'above'
  , html: false
  , live: false
  , offset: 0
  , title: 'title'
  , trigger: 'hover'
  }

  $.fn.twipsy.elementOptions = function(ele, options) {
    return $.metadata ? $.extend({}, options, $(ele).metadata()) : options
  }

307
}( window.jQuery || window.ender );