Skip to content

Commit

Permalink
fix: prevent dupe events on enabled ClickableComponents (#4316)
Browse files Browse the repository at this point in the history
Prevent ClickableComponents re-adding event listeners each time enabled() is called.

* Keeps track of enabled state (this.enabled_)
* enable() doesn't do anything if the component is enabled, so the event handlers are not re-added

Fixes #4312
  • Loading branch information
mister-ben authored and gkatsev committed May 11, 2017
1 parent f773c47 commit 03bab83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/js/clickable-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,30 @@ class ClickableComponent extends Component {
* Enable this `Component`s element.
*/
enable() {
this.removeClass('vjs-disabled');
this.el_.setAttribute('aria-disabled', 'false');
if (typeof this.tabIndex_ !== 'undefined') {
this.el_.setAttribute('tabIndex', this.tabIndex_);
if (!this.enabled_) {
this.enabled_ = true;
this.removeClass('vjs-disabled');
this.el_.setAttribute('aria-disabled', 'false');
if (typeof this.tabIndex_ !== 'undefined') {
this.el_.setAttribute('tabIndex', this.tabIndex_);
}
this.on(['tap', 'click'], this.handleClick);
this.on('focus', this.handleFocus);
this.on('blur', this.handleBlur);
}
this.on('tap', this.handleClick);
this.on('click', this.handleClick);
this.on('focus', this.handleFocus);
this.on('blur', this.handleBlur);
}

/**
* Disable this `Component`s element.
*/
disable() {
this.enabled_ = false;
this.addClass('vjs-disabled');
this.el_.setAttribute('aria-disabled', 'true');
if (typeof this.tabIndex_ !== 'undefined') {
this.el_.removeAttribute('tabIndex');
}
this.off('tap', this.handleClick);
this.off('click', this.handleClick);
this.off(['tap', 'click'], this.handleClick);
this.off('focus', this.handleFocus);
this.off('blur', this.handleBlur);
}
Expand Down
22 changes: 22 additions & 0 deletions test/unit/clickable-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,25 @@ QUnit.test('handleClick should not be triggered when disabled', function() {
testClickableComponent.dispose();
player.dispose();
});

QUnit.test('handleClick should not be triggered more than once when enabled', function() {
let clicks = 0;

class TestClickableComponent extends ClickableComponent {
handleClick() {
clicks++;
}
}

const player = TestHelpers.makePlayer({});
const testClickableComponent = new TestClickableComponent(player);
const el = testClickableComponent.el();

testClickableComponent.enable();
// Click should still be handled just once
Events.trigger(el, 'click');
QUnit.equal(clicks, 1, 'no additional click handler when already enabled ClickableComponent has been enabled again');

testClickableComponent.dispose();
player.dispose();
});

0 comments on commit 03bab83

Please sign in to comment.