Skip to content

Commit

Permalink
Added $.fn.tipsy.revalidate() to remove dangling tooltips from the DOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Frame committed Jun 24, 2012
1 parent 9fe119e commit 8cd3dbc
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/javascripts/jquery.tipsy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
return (typeof thing == 'function') ? (thing.call(ctx)) : thing;
};

function isElementInDOM(ele) {
while (ele = ele.parentNode) {
if (ele == document) return true;
}
return false;
};

function Tipsy(element, options) {
this.$element = $(element);
this.options = options;
Expand Down Expand Up @@ -104,6 +111,7 @@
tip: function() {
if (!this.$tip) {
this.$tip = $('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>');
this.$tip.data('tipsy-pointee', this.$element[0]);
}
return this.$tip;
},
Expand Down Expand Up @@ -191,6 +199,15 @@
trigger: 'hover'
};

$.fn.tipsy.revalidate = function() {
$('.tipsy').each(function() {
var pointee = $.data(this, 'tipsy-pointee');
if (!pointee || !isElementInDOM(pointee)) {
$(this).remove();
}
});
};

// Overwrite this method to provide options on a per-element basis.
// For example, you could store the gravity in a 'tipsy-gravity' attribute:
// return $.extend({}, options, {gravity: $(ele).attr('tipsy-gravity') || 'n' });
Expand Down

0 comments on commit 8cd3dbc

Please sign in to comment.