Created
July 16, 2012 11:07
-
-
Save x3ro/3122155 to your computer and use it in GitHub Desktop.
StatefulPopover for Twitter Bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); |
+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
Hi, just saying thanks, this saved me a lot of time. I was able to use this with a couple of tweaks.