Skip to content

Instantly share code, notes, and snippets.

@x3ro
Created July 16, 2012 11:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save x3ro/3122155 to your computer and use it in GitHub Desktop.
Save x3ro/3122155 to your computer and use it in GitHub Desktop.
StatefulPopover for Twitter Bootstrap
!function ($) {
"use strict"; // jshint ;_;
/* POPOVER PUBLIC CLASS DEFINITION
* =============================== */
var StatefulPopover = function ( element, options ) {
this.contentWasSet = false
this.init('statefulpopover', element, options)
}
/* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
========================================== */
StatefulPopover.prototype = $.extend({}, $.fn.popover.Constructor.prototype, {
constructor: StatefulPopover
, setContent: function () {
var $tip = this.tip()
, title = this.getTitle()
, content = this.getContent()
if(this.contentWasSet !== false) {
return;
}
this.contentWasSet = true;
$tip.find('.popover-title')[this.isHTML(title) ? 'html' : 'text'](title)
$tip.find('.popover-content > *')[this.isHTML(content) ? 'html' : 'text'](content)
$tip.removeClass('fade top bottom left right in')
}
})
/* POPOVER PLUGIN DEFINITION
* ======================= */
$.fn.statefulpopover = function (option) {
return this.each(function () {
var $this = $(this)
, data = $this.data('statefulpopover')
, options = typeof option == 'object' && option
if (!data) $this.data('statefulpopover', (data = new StatefulPopover(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.statefulpopover.Constructor = StatefulPopover
$.fn.statefulpopover.defaults = $.extend({} , $.fn.popover.defaults, {
placement: 'right'
, content: ''
, template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'
})
}(window.jQuery);
@sgulics
Copy link

sgulics commented May 28, 2013

Hi, just saying thanks, this saved me a lot of time. I was able to use this with a couple of tweaks.

@charleshimmer
Copy link

+1. Thanks! This is what I was looking for as well. I'm using a later version of bootstrap (v2.0.0) so I had to replace lines 33 and 34 from

$tip.find('.popover-title')[this.isHTML(title) ? 'html' : 'text'](title)
$tip.find('.popover-content > *')[this.isHTML(content) ? 'html' : 'text'](content)

to

$tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
$tip.find('.popover-content')[this.options.html ? 'html' : 'text'](content)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment