MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / button-core / button-core.js
blobc4c10a0a86d98a4487ca8d5369bbb9ea87eb97af
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('button-core', function(Y) {
9 /**
10 * Provides an interface for working with button-like DOM nodes
12 * @module button-core
13 * @since 3.5.0
16 var getClassName = Y.ClassNameManager.getClassName;
18 /**
19 * Creates a button
21 * @class ButtonCore
22 * @param config {Object} Configuration object
23 * @constructor
25 function Button(config) {
26     this.initializer(config);
29 Button.prototype = {
30     TEMPLATE: '<button/>',
32     constructor: Button,
34     /**
35     * @method initializer
36     * @description Internal init() handler.
37     * @param config {Object} Config object.
38     * @private
39     */
40     initializer: function(config) {
41         this._initNode(config);
42         this._initAttributes(config);
43         this._renderUI(config);
44     },
46     /**
47     * @method _initNode
48     * @description Node initializer
49     * @param config {Object} Config object.
50     * @private
51     */
52     _initNode: function(config) {
53         if (config.host) {
54             this._host = Y.one(config.host);
55         } else {
56             this._host = Y.Node.create(this.TEMPLATE);
57         }
58     },
60     /**
61     * @method _initAttributes
62     * @description  Attribute initializer
63     * @param config {Object} Config object.
64     * @private
65     */
66     _initAttributes: function(config) {
67         var host = this._host,
68             node = host.one('.' + Button.CLASS_NAMES.LABEL) || host;
69             
70         config.label = config.label || this._getLabel(node);
71         Y.AttributeCore.call(this, Button.ATTRS, config);
72     },
74     /**
75     * @method renderUI
76     * @description Renders any UI/DOM elements for Button instances
77     * @param config {Object} Config object.
78     * @private
79     */
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);
86         
87         if (tagName !== 'button' && tagName !== 'input') {
88             node.set('role', 'button');   
89         }
90     },
92     /**
93     * @method enable    
94     * @description Sets the button's `disabled` DOM attribute to false
95     * @public
96     */
97     enable: function() {
98         this.set('disabled', false);
99     },
101     /**
102     * @method disable
103     * @description Sets the button's `disabled` DOM attribute to true
104     * @public
105     */
106     disable: function() {
107         this.set('disabled', true);
108     },
110     /**
111     * @method getNode
112     * @description Gets the host node for this button instance
113     * @public
114     */
115     getNode: function() {
116         return this._host;
117     },
118     
119     /**
120     * @method _getLabel
121     * @description Getter for a button's 'label' ATTR
122     * @private
123     */
124     _getLabel: function () {
125         var node    = this.getNode(),
126             tagName = node.get('tagName').toLowerCase(),
127             label;
129         if (tagName === 'input') {
130             label = node.get('value');
131         }
132         else {
133             label = (node.one('.' + Button.CLASS_NAMES.LABEL) || node).get('text');
134         }
135         
136         return label;
137     },
138     
139     /**
140     * @method _uiSetLabel
141     * @description Setter for a button's 'label' ATTR
142     * @param label {string}
143     * @private
144     */
145     _uiSetLabel: function (label) {
146         var node    = this.getNode(),
147             tagName = node.get('tagName').toLowerCase();
149         if (tagName === 'input') {
150             node.set('value', label);
151         } else {
152             (node.one('.' + Button.CLASS_NAMES.LABEL) || node).set('text', label);
153         }
155         return label;
156     },
158     /**
159     * @method _uiSetDisabled
160     * @description Setter for the 'disabled' ATTR
161     * @param value {boolean}
162     * @private
163     */
164     _uiSetDisabled: function(value) {
165         var node = this.getNode();
166         
167         node.getDOMNode().disabled = value; // avoid rerunning setter when this === node
168         node.toggleClass(Button.CLASS_NAMES.DISABLED, value);
169         
170         return value;
171     }
175 * Attribute configuration.
177 * @property ATTRS
178 * @type {Object}
179 * @private
180 * @static
182 Button.ATTRS = {
183     label: {
184         setter: '_uiSetLabel',
185         getter: '_getLabel',
186         lazyAdd: false
187     },
189     disabled: {
190         value: false,
191         setter: '_uiSetDisabled',
192         lazyAdd: false
193     }
197 * Name of this component.
199 * @property NAME
200 * @type String
201 * @static
203 Button.NAME = "button";
206 * Array of static constants used to identify the classnames applied to DOM nodes
208 * @property CLASS_NAMES
209 * @type {Object}
210 * @public
211 * @static
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
224 * @type {Object}
225 * @private
226 * @static
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
237 * @type {Object}
238 * @private
239 * @static
241 Button.ARIA_ROLES = {
242     BUTTON  : 'button',
243     CHECKBOX: 'checkbox',
244     TOGGLE  : 'toggle'
247 Y.mix(Button.prototype, Y.AttributeCore.prototype);
249 // Export Button
250 Y.ButtonCore = Button;
253 }, '3.5.1' ,{requires:['attribute-core', 'classnamemanager', 'node-base']});