Skip to content

Commit

Permalink
Fix hovering issues on tables actions links
Browse files Browse the repository at this point in the history
Clicking on an action button in a table, a new template for the row
is rendered under current mouse coordinates. This was not making fire
the mouseleave callback for that element.

This commit introduces a MutationObserver that takes the same mouseleave
actions when elements inside that row changes. Also, move the mouseleave
event listener into the mouseenter callback. This way we have the
instance of the MutationObserver and we can disconnect it properly to
avoid aving several mutationobserver attached to the same elements due
to several consecutive mouseenter/mouseleave on that row.
  • Loading branch information
kennyadsl committed Nov 18, 2017
1 parent 87154a0 commit 94781aa
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions backend/app/assets/javascripts/spree/backend/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ Spree.ready(function() {
var tr = $(this).closest('tr');
var klass = 'highlight action-' + $(this).data('action')
tr.addClass(klass)
});
$('table').on("mouseleave", 'td.actions a, td.actions button', function(){
var tr = $(this).closest('tr');
var klass = 'highlight action-' + $(this).data('action')
tr.removeClass(klass)

var observer = new MutationObserver(function(mutations) {
tr.removeClass(klass);
this.disconnect();
});
observer.observe(tr.get(0), { childList: true });

// Using .one() instead of .on() prevents multiple callbacks to be attached
// to this event if mouseentered multiple times.
$(this).one("mouseleave", function() {
tr.removeClass(klass);
observer.disconnect();
});
});
});

Expand Down

0 comments on commit 94781aa

Please sign in to comment.