123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 'use strict';
- exports.__esModule = true;
- var _clickableComponent = require('./clickable-component.js');
- var _clickableComponent2 = _interopRequireDefault(_clickableComponent);
- var _component = require('./component');
- var _component2 = _interopRequireDefault(_component);
- var _log = require('./utils/log.js');
- var _log2 = _interopRequireDefault(_log);
- var _obj = require('./utils/obj');
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
- * @file button.js
- */
- /**
- * Base class for all buttons.
- *
- * @extends ClickableComponent
- */
- var Button = function (_ClickableComponent) {
- _inherits(Button, _ClickableComponent);
- function Button() {
- _classCallCheck(this, Button);
- return _possibleConstructorReturn(this, _ClickableComponent.apply(this, arguments));
- }
- /**
- * Create the `Button`s DOM element.
- *
- * @param {string} [tag=button]
- * Element's node type. e.g. 'button'
- *
- * @param {Object} [props={}]
- * An object of properties that should be set on the element.
- *
- * @param {Object} [attributes={}]
- * An object of attributes that should be set on the element.
- *
- * @return {Element}
- * The element that gets created.
- */
- Button.prototype.createEl = function createEl() {
- var tag = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'button';
- var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- var attributes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
- props = (0, _obj.assign)({
- className: this.buildCSSClass()
- }, props);
- if (tag !== 'button') {
- _log2['default'].warn('Creating a Button with an HTML element of ' + tag + ' is deprecated; use ClickableComponent instead.');
- // Add properties for clickable element which is not a native HTML button
- props = (0, _obj.assign)({
- tabIndex: 0
- }, props);
- // Add ARIA attributes for clickable element which is not a native HTML button
- attributes = (0, _obj.assign)({
- role: 'button'
- }, attributes);
- }
- // Add attributes for button element
- attributes = (0, _obj.assign)({
- // Necessary since the default button type is "submit"
- 'type': 'button',
- // let the screen reader user know that the text of the button may change
- 'aria-live': 'polite'
- }, attributes);
- var el = _component2['default'].prototype.createEl.call(this, tag, props, attributes);
- this.createControlTextEl(el);
- return el;
- };
- /**
- * Add a child `Component` inside of this `Button`.
- *
- * @param {string|Component} child
- * The name or instance of a child to add.
- *
- * @param {Object} [options={}]
- * The key/value store of options that will get passed to children of
- * the child.
- *
- * @return {Component}
- * The `Component` that gets added as a child. When using a string the
- * `Component` will get created by this process.
- *
- * @deprecated since version 5
- */
- Button.prototype.addChild = function addChild(child) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- var className = this.constructor.name;
- _log2['default'].warn('Adding an actionable (user controllable) child to a Button (' + className + ') is not supported; use a ClickableComponent instead.');
- // Avoid the error message generated by ClickableComponent's addChild method
- return _component2['default'].prototype.addChild.call(this, child, options);
- };
- /**
- * Enable the `Button` element so that it can be activated or clicked. Use this with
- * {@link Button#disable}.
- */
- Button.prototype.enable = function enable() {
- _ClickableComponent.prototype.enable.call(this);
- this.el_.removeAttribute('disabled');
- };
- /**
- * Enable the `Button` element so that it cannot be activated or clicked. Use this with
- * {@link Button#enable}.
- */
- Button.prototype.disable = function disable() {
- _ClickableComponent.prototype.disable.call(this);
- this.el_.setAttribute('disabled', 'disabled');
- };
- /**
- * This gets called when a `Button` has focus and `keydown` is triggered via a key
- * press.
- *
- * @param {EventTarget~Event} event
- * The event that caused this function to get called.
- *
- * @listens keydown
- */
- Button.prototype.handleKeyPress = function handleKeyPress(event) {
- // Ignore Space (32) or Enter (13) key operation, which is handled by the browser for a button.
- if (event.which === 32 || event.which === 13) {
- return;
- }
- // Pass keypress handling up for unsupported keys
- _ClickableComponent.prototype.handleKeyPress.call(this, event);
- };
- return Button;
- }(_clickableComponent2['default']);
- _component2['default'].registerComponent('Button', Button);
- exports['default'] = Button;
|