public
Last active

Dynamically add a class to Bootstrap's 'popover' container

  • Download Gist
bootstrap-tooltip.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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
/* ===========================================================
* bootstrap-tooltip.js v2.1.0
* http://twitter.github.com/bootstrap/javascript.html#tooltips
* Inspired by the original jQuery.tipsy by Jason Frame
* ===========================================================
* Copyright 2012 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.
* ========================================================== */
 
 
!function ($) {
 
"use strict"; // jshint ;_;
 
 
/* TOOLTIP PUBLIC CLASS DEFINITION
* =============================== */
 
var Tooltip = function (element, options) {
this.init('tooltip', element, options)
}
 
Tooltip.prototype = {
 
constructor: Tooltip
 
, 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 == 'click') {
this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
} else if (this.options.trigger != 'manual') {
eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
this.$element.on(eventOut + '.' + this.type, 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) return self.show()
 
clearTimeout(this.timeout)
self.hoverState = 'in'
this.timeout = 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 (this.timeout) clearTimeout(this.timeout)
if (!self.options.delay || !self.options.delay.hide) return self.hide()
 
self.hoverState = 'out'
this.timeout = setTimeout(function() {
if (self.hoverState == 'out') self.hide()
}, self.options.delay.hide)
}
 
, show: function () {
var $tip
, inside
, pos
, actualWidth
, actualHeight
, placement
, tp
 
if (this.hasContent() && this.enabled) {
$tip = this.tip()
this.setContent()
 
if (this.options.animation) {
$tip.addClass('fade')
}
 
placement = typeof this.options.placement == 'function' ?
this.options.placement.call(this, $tip[0], this.$element[0]) :
this.options.placement
 
inside = /in/.test(placement)
 
$tip
.remove()
.css({ top: 0, left: 0, display: 'block' })
.appendTo(inside ? this.$element : document.body)
 
pos = this.getPosition(inside)
 
actualWidth = $tip[0].offsetWidth
actualHeight = $tip[0].offsetHeight
 
switch (inside ? placement.split(' ')[1] : placement) {
case 'bottom':
tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
break
case 'top':
tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
break
case 'left':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
break
case 'right':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
break
}
 
$tip
.css(tp)
.addClass(placement)
.addClass('in')
.addClass(this.$element.attr("data-class")) // this adds the data-class attribute to the tooltip body
}
}
 
, setContent: function () {
var $tip = this.tip()
, title = this.getTitle()
 
$tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
$tip.removeClass('fade in top bottom left right')
}
 
, hide: function () {
var that = this
, $tip = this.tip()
 
$tip.removeClass('in')
 
function removeWithAnimation() {
var timeout = setTimeout(function () {
$tip.off($.support.transition.end).remove()
}, 500)
 
$tip.one($.support.transition.end, function () {
clearTimeout(timeout)
$tip.remove()
})
}
 
$.support.transition && this.$tip.hasClass('fade') ?
removeWithAnimation() :
$tip.remove()
 
return this
}
 
, 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')
}
}
 
, hasContent: function () {
return this.getTitle()
}
, getPosition: function (inside) {
return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
width: this.$element[0].offsetWidth
, height: this.$element[0].offsetHeight
})
}
 
, getTitle: function () {
var title
, $e = this.$element
, o = this.options
 
title = $e.attr('data-original-title')
|| (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
 
return title
}
 
, tip: function () {
return this.$tip = this.$tip || $(this.options.template)
}
 
, 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
}
 
, toggle: function () {
this[this.tip().hasClass('in') ? 'hide' : 'show']()
}
 
, destroy: function () {
this.hide().$element.off('.' + this.type).removeData(this.type)
}
 
}
 
 
/* TOOLTIP PLUGIN DEFINITION
* ========================= */
 
$.fn.tooltip = function ( option ) {
return this.each(function () {
var $this = $(this)
, data = $this.data('tooltip')
, options = typeof option == 'object' && option
if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
if (typeof option == 'string') data[option]()
})
}
 
$.fn.tooltip.Constructor = Tooltip
 
$.fn.tooltip.defaults = {
animation: true
, placement: 'top'
, selector: false
, template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
, trigger: 'hover'
, title: ''
, delay: 0
, html: true
}
 
}(window.jQuery);
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap-tooltip.js"></script>
<script type="text/javascript" src="bootstrap-popover.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('a[rel=popover]').popover({
placement : 'bottom',
trigger : 'hover'
}).click(function(e) {
e.preventDefault()
});
});
</script>
</head>
<body>
<a href="#" rel="popover" data-class="dynamic-class" title="Title goes here" data-content="Content goes here">
foo
</a>
</body>
</html>

.addClass(this.$element.data("class"))

probably it is more elegant than

.addClass(this.$element.attr("data-class"))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.