Skip to content

Commit

Permalink
Revert "Remove zombie tooltips with MutationObserver"
Browse files Browse the repository at this point in the history
This reverts commit 1df1f3b.
  • Loading branch information
JDutil committed Oct 3, 2018
1 parent 3010496 commit 8621da5
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions backend/app/assets/javascripts/spree/backend/components/tooltips.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@ Spree.ready(function(){

$('body').tooltip({selector: '.with-tip'});

/*
* Poll tooltips to hide them if they are no longer being hovered.
*
* This is necessary to fix tooltips hanging around after their attached
* element has been removed from the DOM (and will therefore receive no
* mouseleave event). This may be unnecessary in a future version of
* bootstrap, which intends to solve this using MutationObserver.
*/
var removeDesyncedTooltip = function(tooltip) {
var interval = setInterval(function(){
if(!$(tooltip.element).is(":hover")) {
tooltip.hide();
clearInterval(interval);
}
}, 200);
$(tooltip.element).on('hidden.bs.tooltip', function(){
clearInterval(interval);
});
};

$('body').on('inserted.bs.tooltip', function(e){
var $target = $(e.target);
var tooltip = $target.data('bs.tooltip');

/*
* Observe target changes to understand if we need to remove tooltips.
*
* This is necessary to fix tooltips hanging around after their attached
* element has been removed from the DOM (and will therefore receive no
* mouseleave event).
*/
var observer = new MutationObserver(function(mutations) {
// disconnect itself when content is changed, a new observer will
// be attached to this element when the new tooltip is created.
this.disconnect();

tooltip.hide();
});
observer.observe($target.get(0), { attributes: true });

removeDesyncedTooltip(tooltip);
var $tooltip = $("#" + $target.attr("aria-describedby"));
$tooltip.addClass("action-" + $target.data("action"));
});
Expand Down

0 comments on commit 8621da5

Please sign in to comment.