core.js revision 787aaacd9cf857e016e3bf7b0ddfff8634cdbe7d
258N/A/**
943N/A* Provides an interface for working with button-like DOM nodes
258N/A*
258N/A* @module button-core
919N/A* @since 3.5.0
919N/A*/
919N/A
919N/Avar getClassName = Y.ClassNameManager.getClassName;
919N/A
258N/A/**
919N/A* Creates a button
919N/A*
919N/A* @class Button
258N/A* @param config {Object} Configuration object
919N/A* @constructor
919N/A*/
919N/Afunction Button(config) {
919N/A this.initializer(config);
919N/A}
919N/A
919N/AButton.prototype = {
258N/A TEMPLATE: '<button/>',
258N/A
258N/A constructor: Button,
258N/A
258N/A /**
258N/A * @method initializer
258N/A * @description Internal init() handler.
258N/A * @param config {Object} Config object.
258N/A * @private
258N/A */
258N/A initializer: function(config) {
258N/A this._initNode(config);
258N/A this._initAttributes(config);
258N/A this._renderUI(config);
258N/A },
258N/A
258N/A /**
258N/A * @method _initNode
258N/A * @description
258N/A * @param config {Object} Config object.
258N/A * @private
258N/A */
258N/A _initNode: function(config) {
258N/A if (config.host) {
258N/A this._host = Y.one(config.host);
258N/A } else {
258N/A this._host = Y.Node.create(this.TEMPLATE);
258N/A }
258N/A },
258N/A
258N/A /**
258N/A * @method _initAttributes
258N/A * @description
258N/A * @param config {Object} Config object.
258N/A * @private
258N/A */
258N/A _initAttributes: function(config) {
258N/A var host = this._host,
258N/A node = node.one('.' + Button.CLASS_NAMES.LABEL) || node;
258N/A
258N/A config.label = config.label || this._getLabel(node);
258N/A Y.AttributeCore.call(this, Button.ATTRS, config);
258N/A },
258N/A
258N/A /**
258N/A * @method renderUI
258N/A * @description Renders any UI/DOM elements for Button instances
258N/A * @private
258N/A */
258N/A _renderUI: function(config) {
258N/A var node = this.getNode(),
258N/A tagName = node.get('tagName').toLowerCase();
258N/A
258N/A // Set some default node attributes
258N/A node.addClass(Button.CLASS_NAMES.BUTTON);
258N/A
258N/A if (tagName !== 'button' && tagName !== 'input') {
258N/A node.set('role', 'button');
258N/A }
258N/A },
/**
* @method enable
* @description
* @public
*/
enable: function() {
this.set('disabled', false);
},
/**
* @method disable
* @public
*/
disable: function() {
this.set('disabled', true);
},
/**
* @method getNode
* @description Gets the host node
* @public
*/
getNode: function() {
return this._host;
},
/**
* @method _getLabel
* @description Getter for a button's 'label' ATTR
* @private
*/
_getLabel: function () {
var node = this.getNode(),
tagName = node.get('tagName').toLowerCase(),
label;
if (tagName === 'input') {
label = node.get('value');
}
else {
label = (node.one('.' + Button.CLASS_NAMES.LABEL) || node).get('text');
}
return label;
},
/**
* @method _uiSetLabel
* @description Setter for a button's 'label' ATTR
* @private
*/
_setLabel: function (label) {
var node = this.getNode(),
tagName = node.get('tagName').toLowerCase();
if (tagName === 'input') {
node.set('value', label);
} else {
(node.one('.' + Button.CLASS_NAMES.LABEL) || node).set('text', label);
}
return label;
},
/**
* @method _setDisabled
* @description Setter for the 'disabled' ATTR
* @private
*/
_setDisabled: function(value) {
var node = this.getNode();
node.getDOMNode().disabled = value; // avoid rerunning setter when this === node
node.toggleClass(Button.CLASS_NAMES.DISABLED, value);
return value;
}
};
/**
* Attribute configuration.
*
* @property ATTRS
* @type {Object}
* @private
* @static
*/
Button.ATTRS = {
label: {
setter: '_setLabel',
getter: '_getLabel',
lazyAdd: false
},
disabled: {
value: false,
setter: '_setDisabled',
lazyAdd: false
}
};
/**
* Name of this component.
*
* @property NAME
* @type String
* @static
*/
Button.NAME = "button";
/**
* Array of static constants used to identify the classnames applied to the Button DOM objects
*
* @property CLASS_NAMES
* @type {Array}
* @static
*/
Button.CLASS_NAMES = {
BUTTON : getClassName('button'),
DISABLED: getClassName('button', 'disabled'),
SELECTED: getClassName('button', 'selected'),
LABEL: getClassName('button', 'label')
};
Y.mix(Button.prototype, Y.AttributeCore.prototype);
// Export Button
Y.ButtonCore = Button;