unignore bower_components
[bootswatch] / bower_components / bootstrap / js / tooltip.js
1 /* ========================================================================
2  * Bootstrap: tooltip.js v3.0.0
3  * http://twbs.github.com/bootstrap/javascript.html#affix
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.enter = function (obj) {
95     var defaults = this.getDefaults()
96     var options  = {}
97
98     this._options && $.each(this._options, function (key, value) {
99       if (defaults[key] != value) options[key] = value
100     })
101
102     var self = obj instanceof this.constructor ?
103       obj : $(obj.currentTarget)[this.type](options).data('bs.' + this.type)
104
105     clearTimeout(self.timeout)
106
107     if (!self.options.delay || !self.options.delay.show) return self.show()
108
109     self.hoverState = 'in'
110     self.timeout    = setTimeout(function () {
111       if (self.hoverState == 'in') self.show()
112     }, self.options.delay.show)
113   }
114
115   Tooltip.prototype.leave = function (obj) {
116     var self = obj instanceof this.constructor ?
117       obj : $(obj.currentTarget)[this.type](this._options).data('bs.' + this.type)
118
119     clearTimeout(self.timeout)
120
121     if (!self.options.delay || !self.options.delay.hide) return self.hide()
122
123     self.hoverState = 'out'
124     self.timeout    = setTimeout(function () {
125       if (self.hoverState == 'out') self.hide()
126     }, self.options.delay.hide)
127   }
128
129   Tooltip.prototype.show = function () {
130     var e = $.Event('show.bs.'+ this.type)
131
132     if (this.hasContent() && this.enabled) {
133       this.$element.trigger(e)
134
135       if (e.isDefaultPrevented()) return
136
137       var $tip = this.tip()
138
139       this.setContent()
140
141       if (this.options.animation) $tip.addClass('fade')
142
143       var placement = typeof this.options.placement == 'function' ?
144         this.options.placement.call(this, $tip[0], this.$element[0]) :
145         this.options.placement
146
147       var autoToken = /\s?auto?\s?/i
148       var autoPlace = autoToken.test(placement)
149       if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
150
151       $tip
152         .detach()
153         .css({ top: 0, left: 0, display: 'block' })
154         .addClass(placement)
155
156       this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
157
158       var pos          = this.getPosition()
159       var actualWidth  = $tip[0].offsetWidth
160       var actualHeight = $tip[0].offsetHeight
161
162       if (autoPlace) {
163         var $parent = this.$element.parent()
164
165         var orgPlacement = placement
166         var docScroll    = document.documentElement.scrollTop || document.body.scrollTop
167         var parentWidth  = this.options.container == 'body' ? window.innerWidth  : $parent.outerWidth()
168         var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
169         var parentLeft   = this.options.container == 'body' ? 0 : $parent.offset().left
170
171         placement = placement == 'bottom' && pos.top   + pos.height  + actualHeight - docScroll > parentHeight  ? 'top'    :
172                     placement == 'top'    && pos.top   - docScroll   - actualHeight < 0                         ? 'bottom' :
173                     placement == 'right'  && pos.right + actualWidth > parentWidth                              ? 'left'   :
174                     placement == 'left'   && pos.left  - actualWidth < parentLeft                               ? 'right'  :
175                     placement
176
177         $tip
178           .removeClass(orgPlacement)
179           .addClass(placement)
180       }
181
182       var tp = placement == 'bottom' ? { top: pos.top + pos.height,   left: pos.left + pos.width / 2 - actualWidth / 2  } :
183                placement == 'top'    ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2  } :
184                placement == 'left'   ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
185             /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width   }
186
187       this.applyPlacement(tp, placement)
188       this.$element.trigger('shown.bs.' + this.type)
189     }
190   }
191
192   Tooltip.prototype.applyPlacement = function(offset, placement) {
193     var replace
194     var $tip   = this.tip()
195     var width  = $tip[0].offsetWidth
196     var height = $tip[0].offsetHeight
197
198     // manually read margins because getBoundingClientRect includes difference
199     offset.top  = offset.top  + parseInt($tip.css('margin-top'), 10)
200     offset.left = offset.left + parseInt($tip.css('margin-left'), 10)
201
202     $tip
203       .offset(offset)
204       .addClass('in')
205
206     var actualWidth  = $tip[0].offsetWidth
207     var actualHeight = $tip[0].offsetHeight
208
209     if (placement == 'top' && actualHeight != height) {
210       replace = true
211       offset.top  = offset.top + height - actualHeight
212     }
213
214     if (placement == 'bottom' || placement == 'top') {
215       var delta = 0
216
217       if (offset.left < 0){
218         delta       = offset.left * -2
219         offset.left = 0
220
221         $tip.offset(offset)
222
223         actualWidth  = $tip[0].offsetWidth
224         actualHeight = $tip[0].offsetHeight
225       }
226
227       this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
228     } else {
229       this.replaceArrow(actualHeight - height, actualHeight, 'top')
230     }
231
232     if (replace) $tip.offset(offset)
233   }
234
235   Tooltip.prototype.replaceArrow = function(delta, dimension, position) {
236     this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
237   }
238
239   Tooltip.prototype.setContent = function () {
240     var $tip  = this.tip()
241     var title = this.getTitle()
242
243     $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
244     $tip.removeClass('fade in top bottom left right')
245   }
246
247   Tooltip.prototype.hide = function () {
248     var that = this
249     var $tip = this.tip()
250     var e    = $.Event('hide.bs.' + this.type)
251
252     this.$element.trigger(e)
253
254     if (e.isDefaultPrevented()) return
255
256     $tip.removeClass('in')
257
258     $.support.transition && this.$tip.hasClass('fade') ?
259       $tip
260         .one($.support.transition.end, $tip.detach)
261         .emulateTransitionEnd(150) :
262       $tip.detach()
263
264     this.$element.trigger('hidden.bs.' + this.type)
265
266     return this
267   }
268
269   Tooltip.prototype.fixTitle = function () {
270     var $e = this.$element
271     if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
272       $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
273     }
274   }
275
276   Tooltip.prototype.hasContent = function () {
277     return this.getTitle()
278   }
279
280   Tooltip.prototype.getPosition = function () {
281     var el = this.$element[0]
282     return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
283       width: el.offsetWidth
284     , height: el.offsetHeight
285     }, this.$element.offset())
286   }
287
288   Tooltip.prototype.getTitle = function () {
289     var title
290     var $e = this.$element
291     var o  = this.options
292
293     title = $e.attr('data-original-title')
294       || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
295
296     return title
297   }
298
299   Tooltip.prototype.tip = function () {
300     return this.$tip = this.$tip || $(this.options.template)
301   }
302
303   Tooltip.prototype.arrow =function(){
304     return this.$arrow = this.$arrow || this.tip().find(".tooltip-arrow")
305   }
306
307   Tooltip.prototype.validate = function () {
308     if (!this.$element[0].parentNode) {
309       this.hide()
310       this.$element = null
311       this.options  = null
312     }
313   }
314
315   Tooltip.prototype.enable = function () {
316     this.enabled = true
317   }
318
319   Tooltip.prototype.disable = function () {
320     this.enabled = false
321   }
322
323   Tooltip.prototype.toggleEnabled = function () {
324     this.enabled = !this.enabled
325   }
326
327   Tooltip.prototype.toggle = function (e) {
328     var self = e ? $(e.currentTarget)[this.type](this._options).data('bs.' + this.type) : this
329     self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
330   }
331
332   Tooltip.prototype.destroy = function () {
333     this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
334   }
335
336
337   // TOOLTIP PLUGIN DEFINITION
338   // =========================
339
340   var old = $.fn.tooltip
341
342   $.fn.tooltip = function (option) {
343     return this.each(function () {
344       var $this   = $(this)
345       var data    = $this.data('bs.tooltip')
346       var options = typeof option == 'object' && option
347
348       if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
349       if (typeof option == 'string') data[option]()
350     })
351   }
352
353   $.fn.tooltip.Constructor = Tooltip
354
355
356   // TOOLTIP NO CONFLICT
357   // ===================
358
359   $.fn.tooltip.noConflict = function () {
360     $.fn.tooltip = old
361     return this
362   }
363
364 }(window.jQuery);