update bootstrap to 3.0.0-rc2
[bootswatch] / bower_components / bootstrap / js / tooltip.js
1 /* ========================================================================
2  * Bootstrap: tooltip.js v3.0.0
3  * http://twbs.github.com/bootstrap/javascript.html#tooltip
4  * Inspired by the original jQuery.tipsy by Jason Frame
5  * ========================================================================
6  * Copyright 2012 Twitter, Inc.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ======================================================================== */
20
21
22 +function ($) { "use strict";
23
24   // TOOLTIP PUBLIC CLASS DEFINITION
25   // ===============================
26
27   var Tooltip = function (element, options) {
28     this.type       =
29     this.options    =
30     this.enabled    =
31     this.timeout    =
32     this.hoverState =
33     this.$element   = null
34
35     this.init('tooltip', element, options)
36   }
37
38   Tooltip.DEFAULTS = {
39     animation: true
40   , placement: 'top'
41   , selector: false
42   , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
43   , trigger: 'hover focus'
44   , title: ''
45   , delay: 0
46   , html: false
47   , container: false
48   }
49
50   Tooltip.prototype.init = function (type, element, options) {
51     this.enabled  = true
52     this.type     = type
53     this.$element = $(element)
54     this.options  = this.getOptions(options)
55
56     var triggers = this.options.trigger.split(' ')
57
58     for (var i = triggers.length; i--;) {
59       var trigger = triggers[i]
60
61       if (trigger == 'click') {
62         this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
63       } else if (trigger != 'manual') {
64         var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focus'
65         var eventOut = trigger == 'hover' ? 'mouseleave' : 'blur'
66
67         this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
68         this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
69       }
70     }
71
72     this.options.selector ?
73       (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
74       this.fixTitle()
75   }
76
77   Tooltip.prototype.getDefaults = function () {
78     return Tooltip.DEFAULTS
79   }
80
81   Tooltip.prototype.getOptions = function (options) {
82     options = $.extend({}, this.getDefaults(), this.$element.data(), options)
83
84     if (options.delay && typeof options.delay == 'number') {
85       options.delay = {
86         show: options.delay
87       , hide: options.delay
88       }
89     }
90
91     return options
92   }
93
94   Tooltip.prototype.getDelegateOptions = function () {
95     var options  = {}
96     var defaults = this.getDefaults()
97
98     this._options && $.each(this._options, function (key, value) {
99       if (defaults[key] != value) options[key] = value
100     })
101
102     return options
103   }
104
105   Tooltip.prototype.enter = function (obj) {
106     var self = obj instanceof this.constructor ?
107       obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
108
109     clearTimeout(self.timeout)
110
111     if (!self.options.delay || !self.options.delay.show) return self.show()
112
113     self.hoverState = 'in'
114     self.timeout    = setTimeout(function () {
115       if (self.hoverState == 'in') self.show()
116     }, self.options.delay.show)
117   }
118
119   Tooltip.prototype.leave = function (obj) {
120     var self = obj instanceof this.constructor ?
121       obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
122
123     clearTimeout(self.timeout)
124
125     if (!self.options.delay || !self.options.delay.hide) return self.hide()
126
127     self.hoverState = 'out'
128     self.timeout    = setTimeout(function () {
129       if (self.hoverState == 'out') self.hide()
130     }, self.options.delay.hide)
131   }
132
133   Tooltip.prototype.show = function () {
134     var e = $.Event('show.bs.'+ this.type)
135
136     if (this.hasContent() && this.enabled) {
137       this.$element.trigger(e)
138
139       if (e.isDefaultPrevented()) return
140
141       var $tip = this.tip()
142
143       this.setContent()
144
145       if (this.options.animation) $tip.addClass('fade')
146
147       var placement = typeof this.options.placement == 'function' ?
148         this.options.placement.call(this, $tip[0], this.$element[0]) :
149         this.options.placement
150
151       var autoToken = /\s?auto?\s?/i
152       var autoPlace = autoToken.test(placement)
153       if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
154
155       $tip
156         .detach()
157         .css({ top: 0, left: 0, display: 'block' })
158         .addClass(placement)
159
160       this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
161
162       var pos          = this.getPosition()
163       var actualWidth  = $tip[0].offsetWidth
164       var actualHeight = $tip[0].offsetHeight
165
166       if (autoPlace) {
167         var $parent = this.$element.parent()
168
169         var orgPlacement = placement
170         var docScroll    = document.documentElement.scrollTop || document.body.scrollTop
171         var parentWidth  = this.options.container == 'body' ? window.innerWidth  : $parent.outerWidth()
172         var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
173         var parentLeft   = this.options.container == 'body' ? 0 : $parent.offset().left
174
175         placement = placement == 'bottom' && pos.top   + pos.height  + actualHeight - docScroll > parentHeight  ? 'top'    :
176                     placement == 'top'    && pos.top   - docScroll   - actualHeight < 0                         ? 'bottom' :
177                     placement == 'right'  && pos.right + actualWidth > parentWidth                              ? 'left'   :
178                     placement == 'left'   && pos.left  - actualWidth < parentLeft                               ? 'right'  :
179                     placement
180
181         $tip
182           .removeClass(orgPlacement)
183           .addClass(placement)
184       }
185
186       var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
187
188       this.applyPlacement(calculatedOffset, placement)
189       this.$element.trigger('shown.bs.' + this.type)
190     }
191   }
192
193   Tooltip.prototype.applyPlacement = function(offset, placement) {
194     var replace
195     var $tip   = this.tip()
196     var width  = $tip[0].offsetWidth
197     var height = $tip[0].offsetHeight
198
199     // manually read margins because getBoundingClientRect includes difference
200     var marginTop = parseInt($tip.css('margin-top'), 10)
201     var marginLeft = parseInt($tip.css('margin-left'), 10)
202
203     // we must check for NaN for ie 8/9
204     if (isNaN(marginTop))  marginTop  = 0
205     if (isNaN(marginLeft)) marginLeft = 0
206
207     offset.top  = offset.top  + marginTop
208     offset.left = offset.left + marginLeft
209
210     $tip
211       .offset(offset)
212       .addClass('in')
213
214     // check to see if placing tip in new offset caused the tip to resize itself
215     var actualWidth  = $tip[0].offsetWidth
216     var actualHeight = $tip[0].offsetHeight
217
218     if (placement == 'top' && actualHeight != height) {
219       replace = true
220       offset.top = offset.top + height - actualHeight
221     }
222
223     if (/bottom|top/.test(placement)) {
224       var delta = 0
225
226       if (offset.left < 0) {
227         delta       = offset.left * -2
228         offset.left = 0
229
230         $tip.offset(offset)
231
232         actualWidth  = $tip[0].offsetWidth
233         actualHeight = $tip[0].offsetHeight
234       }
235
236       this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
237     } else {
238       this.replaceArrow(actualHeight - height, actualHeight, 'top')
239     }
240
241     if (replace) $tip.offset(offset)
242   }
243
244   Tooltip.prototype.replaceArrow = function(delta, dimension, position) {
245     this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
246   }
247
248   Tooltip.prototype.setContent = function () {
249     var $tip  = this.tip()
250     var title = this.getTitle()
251
252     $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
253     $tip.removeClass('fade in top bottom left right')
254   }
255
256   Tooltip.prototype.hide = function () {
257     var that = this
258     var $tip = this.tip()
259     var e    = $.Event('hide.bs.' + this.type)
260
261     function complete() { $tip.detach() }
262
263     this.$element.trigger(e)
264
265     if (e.isDefaultPrevented()) return
266
267     $tip.removeClass('in')
268
269     $.support.transition && this.$tip.hasClass('fade') ?
270       $tip
271         .one($.support.transition.end, complete)
272         .emulateTransitionEnd(150) :
273       complete()
274
275     this.$element.trigger('hidden.bs.' + this.type)
276
277     return this
278   }
279
280   Tooltip.prototype.fixTitle = function () {
281     var $e = this.$element
282     if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
283       $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
284     }
285   }
286
287   Tooltip.prototype.hasContent = function () {
288     return this.getTitle()
289   }
290
291   Tooltip.prototype.getPosition = function () {
292     var el = this.$element[0]
293     return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
294       width: el.offsetWidth
295     , height: el.offsetHeight
296     }, this.$element.offset())
297   }
298
299   Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
300     return placement == 'bottom' ? { top: pos.top + pos.height,   left: pos.left + pos.width / 2 - actualWidth / 2  } :
301            placement == 'top'    ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2  } :
302            placement == 'left'   ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
303         /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width   }
304   }
305
306   Tooltip.prototype.getTitle = function () {
307     var title
308     var $e = this.$element
309     var o  = this.options
310
311     title = $e.attr('data-original-title')
312       || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
313
314     return title
315   }
316
317   Tooltip.prototype.tip = function () {
318     return this.$tip = this.$tip || $(this.options.template)
319   }
320
321   Tooltip.prototype.arrow = function () {
322     return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
323   }
324
325   Tooltip.prototype.validate = function () {
326     if (!this.$element[0].parentNode) {
327       this.hide()
328       this.$element = null
329       this.options  = null
330     }
331   }
332
333   Tooltip.prototype.enable = function () {
334     this.enabled = true
335   }
336
337   Tooltip.prototype.disable = function () {
338     this.enabled = false
339   }
340
341   Tooltip.prototype.toggleEnabled = function () {
342     this.enabled = !this.enabled
343   }
344
345   Tooltip.prototype.toggle = function (e) {
346     var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
347     self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
348   }
349
350   Tooltip.prototype.destroy = function () {
351     this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
352   }
353
354
355   // TOOLTIP PLUGIN DEFINITION
356   // =========================
357
358   var old = $.fn.tooltip
359
360   $.fn.tooltip = function (option) {
361     return this.each(function () {
362       var $this   = $(this)
363       var data    = $this.data('bs.tooltip')
364       var options = typeof option == 'object' && option
365
366       if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
367       if (typeof option == 'string') data[option]()
368     })
369   }
370
371   $.fn.tooltip.Constructor = Tooltip
372
373
374   // TOOLTIP NO CONFLICT
375   // ===================
376
377   $.fn.tooltip.noConflict = function () {
378     $.fn.tooltip = old
379     return this
380   }
381
382 }(window.jQuery);