- 1 :
/**
- 2 :
* @file button.js
- 3 :
*/
- 4 :
import ClickableComponent from './clickable-component.js';
- 5 :
import Component from './component';
- 6 :
import log from './utils/log.js';
- 7 :
import {assign} from './utils/obj';
- 8 :
import keycode from 'keycode';
- 9 :
import {createEl} from './utils/dom.js';
- 10 :
- 11 :
/**
- 12 :
* Base class for all buttons.
- 13 :
*
- 14 :
* @extends ClickableComponent
- 15 :
*/
- 16 :
class Button extends ClickableComponent {
- 17 :
- 18 :
/**
- 19 :
* Create the `Button`s DOM element.
- 20 :
*
- 21 :
* @param {string} [tag="button"]
- 22 :
* The element's node type. This argument is IGNORED: no matter what
- 23 :
* is passed, it will always create a `button` element.
- 24 :
*
- 25 :
* @param {Object} [props={}]
- 26 :
* An object of properties that should be set on the element.
- 27 :
*
- 28 :
* @param {Object} [attributes={}]
- 29 :
* An object of attributes that should be set on the element.
- 30 :
*
- 31 :
* @return {Element}
- 32 :
* The element that gets created.
- 33 :
*/
- 34 :
createEl(tag, props = {}, attributes = {}) {
- 35 :
tag = 'button';
- 36 :
- 37 :
props = assign({
- 38 :
className: this.buildCSSClass()
- 39 :
}, props);
- 40 :
- 41 :
// Add attributes for button element
- 42 :
attributes = assign({
- 43 :
- 44 :
// Necessary since the default button type is "submit"
- 45 :
type: 'button'
- 46 :
}, attributes);
- 47 :
- 48 :
const el = createEl(tag, props, attributes);
- 49 :
- 50 :
el.appendChild(createEl('span', {
- 51 :
className: 'vjs-icon-placeholder'
- 52 :
}, {
- 53 :
'aria-hidden': true
- 54 :
}));
- 55 :
- 56 :
this.createControlTextEl(el);
- 57 :
- 58 :
return el;
- 59 :
}
- 60 :
- 61 :
/**
- 62 :
* Add a child `Component` inside of this `Button`.
- 63 :
*
- 64 :
* @param {string|Component} child
- 65 :
* The name or instance of a child to add.
- 66 :
*
- 67 :
* @param {Object} [options={}]
- 68 :
* The key/value store of options that will get passed to children of
- 69 :
* the child.
- 70 :
*
- 71 :
* @return {Component}
- 72 :
* The `Component` that gets added as a child. When using a string the
- 73 :
* `Component` will get created by this process.
- 74 :
*
- 75 :
* @deprecated since version 5
- 76 :
*/
- 77 :
addChild(child, options = {}) {
- 78 :
const className = this.constructor.name;
- 79 :
- 80 :
log.warn(`Adding an actionable (user controllable) child to a Button (${className}) is not supported; use a ClickableComponent instead.`);
- 81 :
- 82 :
// Avoid the error message generated by ClickableComponent's addChild method
- 83 :
return Component.prototype.addChild.call(this, child, options);
- 84 :
}
- 85 :
- 86 :
/**
- 87 :
* Enable the `Button` element so that it can be activated or clicked. Use this with
- 88 :
* {@link Button#disable}.
- 89 :
*/
- 90 :
enable() {
- 91 :
super.enable();
- 92 :
this.el_.removeAttribute('disabled');
- 93 :
}
- 94 :
- 95 :
/**
- 96 :
* Disable the `Button` element so that it cannot be activated or clicked. Use this with
- 97 :
* {@link Button#enable}.
- 98 :
*/
- 99 :
disable() {
- 100 :
super.disable();
- 101 :
this.el_.setAttribute('disabled', 'disabled');
- 102 :
}
- 103 :
- 104 :
/**
- 105 :
* This gets called when a `Button` has focus and `keydown` is triggered via a key
- 106 :
* press.
- 107 :
*
- 108 :
* @param {EventTarget~Event} event
- 109 :
* The event that caused this function to get called.
- 110 :
*
- 111 :
* @listens keydown
- 112 :
*/
- 113 :
handleKeyDown(event) {
- 114 :
- 115 :
// Ignore Space or Enter key operation, which is handled by the browser for
- 116 :
// a button - though not for its super class, ClickableComponent. Also,
- 117 :
// prevent the event from propagating through the DOM and triggering Player
- 118 :
// hotkeys. We do not preventDefault here because we _want_ the browser to
- 119 :
// handle it.
- 120 :
if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
- 121 :
event.stopPropagation();
- 122 :
return;
- 123 :
}
- 124 :
- 125 :
// Pass keypress handling up for unsupported keys
- 126 :
super.handleKeyDown(event);
- 127 :
}
- 128 :
}
- 129 :
- 130 :
Component.registerComponent('Button', Button);
- 131 :
export default Button;