Skip to content

Commit

Permalink
feat: allow a click handler to be specified in clickable component's …
Browse files Browse the repository at this point in the history
…options (#6140)
  • Loading branch information
mister-ben authored and gkatsev committed Aug 29, 2019
1 parent 22782b8 commit f7185ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 6 additions & 2 deletions docs/guides/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ Creation:
```js
// adding a button to the player
var player = videojs('some-video-id');
var Component = videojs.getComponent('Component');
var button = new Component(player);
var Button = videojs.getComponent('Button');
var button = new Button(player, {
clickHandler: function(event) {
videojs.log('Clicked');
}
});

console.log(button.el());
```
Expand Down
9 changes: 8 additions & 1 deletion src/js/clickable-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ClickableComponent extends Component {
*
* @param {Object} [options]
* The key/value store of player options.
*
* @param {function} [options.clickHandler]
* The function to call when the button is clicked / activated
*/
constructor(player, options) {
super(player, options);
Expand Down Expand Up @@ -185,7 +188,11 @@ class ClickableComponent extends Component {
* @listens click
* @abstract
*/
handleClick(event) {}
handleClick(event) {
if (this.options_.clickHandler) {
this.options_.clickHandler.call(this, arguments);
}
}

/**
* Event handler that is called when a `ClickableComponent` receives a
Expand Down
18 changes: 18 additions & 0 deletions test/unit/clickable-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ QUnit.test('handleClick should not be triggered more than once when enabled', fu
testClickableComponent.dispose();
player.dispose();
});

QUnit.test('handleClick should use handler from options', function(assert) {
let clicks = 0;

const player = TestHelpers.makePlayer({});
const testClickableComponent = new ClickableComponent(player, {
clickHandler() {
clicks++;
}
});
const el = testClickableComponent.el();

Events.trigger(el, 'click');
assert.equal(clicks, 1, 'options handler was called');

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

0 comments on commit f7185ba

Please sign in to comment.