3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('button-core', function(Y) {
10 * Provides an interface for working with button-like DOM nodes
16 var getClassName = Y.ClassNameManager.getClassName;
22 * @param config {Object} Configuration object
25 function Button(config) {
26 this.initializer(config);
30 TEMPLATE: '<button/>',
36 * @description Internal init() handler.
37 * @param config {Object} Config object.
40 initializer: function(config) {
41 this._initNode(config);
42 this._initAttributes(config);
43 this._renderUI(config);
48 * @description Node initializer
49 * @param config {Object} Config object.
52 _initNode: function(config) {
54 this._host = Y.one(config.host);
56 this._host = Y.Node.create(this.TEMPLATE);
61 * @method _initAttributes
62 * @description Attribute initializer
63 * @param config {Object} Config object.
66 _initAttributes: function(config) {
67 var host = this._host,
68 node = host.one('.' + Button.CLASS_NAMES.LABEL) || host;
70 config.label = config.label || this._getLabel(node);
71 Y.AttributeCore.call(this, Button.ATTRS, config);
76 * @description Renders any UI/DOM elements for Button instances
77 * @param config {Object} Config object.
80 _renderUI: function(config) {
81 var node = this.getNode(),
82 tagName = node.get('tagName').toLowerCase();
84 // Set some default node attributes
85 node.addClass(Button.CLASS_NAMES.BUTTON);
87 if (tagName !== 'button' && tagName !== 'input') {
88 node.set('role', 'button');
94 * @description Sets the button's `disabled` DOM attribute to false
98 this.set('disabled', false);
103 * @description Sets the button's `disabled` DOM attribute to true
106 disable: function() {
107 this.set('disabled', true);
112 * @description Gets the host node for this button instance
115 getNode: function() {
121 * @description Getter for a button's 'label' ATTR
124 _getLabel: function () {
125 var node = this.getNode(),
126 tagName = node.get('tagName').toLowerCase(),
129 if (tagName === 'input') {
130 label = node.get('value');
133 label = (node.one('.' + Button.CLASS_NAMES.LABEL) || node).get('text');
140 * @method _uiSetLabel
141 * @description Setter for a button's 'label' ATTR
142 * @param label {string}
145 _uiSetLabel: function (label) {
146 var node = this.getNode(),
147 tagName = node.get('tagName').toLowerCase();
149 if (tagName === 'input') {
150 node.set('value', label);
152 (node.one('.' + Button.CLASS_NAMES.LABEL) || node).set('text', label);
159 * @method _uiSetDisabled
160 * @description Setter for the 'disabled' ATTR
161 * @param value {boolean}
164 _uiSetDisabled: function(value) {
165 var node = this.getNode();
167 node.getDOMNode().disabled = value; // avoid rerunning setter when this === node
168 node.toggleClass(Button.CLASS_NAMES.DISABLED, value);
175 * Attribute configuration.
184 setter: '_uiSetLabel',
191 setter: '_uiSetDisabled',
197 * Name of this component.
203 Button.NAME = "button";
206 * Array of static constants used to identify the classnames applied to DOM nodes
208 * @property CLASS_NAMES
213 Button.CLASS_NAMES = {
214 BUTTON : getClassName('button'),
215 DISABLED: getClassName('button', 'disabled'),
216 SELECTED: getClassName('button', 'selected'),
217 LABEL : getClassName('button', 'label')
221 * Array of static constants used to for applying ARIA states
223 * @property CLASS_NAMES
228 Button.ARIA_STATES = {
229 PRESSED : 'aria-pressed',
230 CHECKED : 'aria-checked'
234 * Array of static constants used to for applying ARIA roles
236 * @property CLASS_NAMES
241 Button.ARIA_ROLES = {
243 CHECKBOX: 'checkbox',
247 Y.mix(Button.prototype, Y.AttributeCore.prototype);
250 Y.ButtonCore = Button;
253 }, '3.5.1' ,{requires:['attribute-core', 'classnamemanager', 'node-base']});