3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('button', function(Y) {
16
var CLASS_NAMES = Y.ButtonCore.CLASS_NAMES,
17
ARIA_STATES = Y.ButtonCore.ARIA_STATES,
18
ARIA_ROLES = Y.ButtonCore.ARIA_ROLES;
25
* @param config {Object} Configuration object
28
function Button(config) {
29
Button.superclass.constructor.apply(this, arguments);
32
/* Button extends Widget */
33
Y.extend(Button, Y.Widget, {
35
BOUNDING_TEMPLATE: Y.ButtonCore.prototype.TEMPLATE,
37
CONTENT_TEMPLATE: null,
41
* @description Internal init() handler.
42
* @param config {Object} Config object.
45
initializer: function(config) {
46
this._host = this.get('boundingBox');
50
* bindUI implementation
52
* @description Hooks up events for the widget
57
button.after('labelChange', button._afterLabelChange);
58
button.after('disabledChange', button._afterDisabledChange);
63
* @description Updates button attributes
67
button._uiSetLabel(button.get('label'));
68
button._uiSetDisabled(button.get('disabled'));
72
* @method _afterLabelChange
75
_afterLabelChange: function(e) {
76
this._uiSetLabel(e.newVal);
80
* @method _afterDisabledChange
83
_afterDisabledChange: function(e) {
84
this._uiSetDisabled(e.newVal);
88
// Y.Button static properties
91
* The identity of the widget.
103
* Static property used to define the default attribute configuration of
113
value: Y.ButtonCore.ATTRS.label.value
122
* @property HTML_PARSER
128
label: function(node) {
129
this._host = node; // TODO: remove
130
return this._getLabel();
133
disabled: function(node) {
134
return node.getDOMNode().disabled;
139
* List of class names used in the ButtonGroup's DOM
141
* @property CLASS_NAMES
145
CLASS_NAMES: CLASS_NAMES
148
Y.mix(Button.prototype, Y.ButtonCore.prototype);
151
* Creates a ToggleButton
153
* @class ToggleButton
155
* @param config {Object} Configuration object
158
function ToggleButton(config) {
159
Button.superclass.constructor.apply(this, arguments);
162
// TODO: move to ButtonCore subclass to enable toggle plugin, widget, etc.
163
/* ToggleButton extends Button */
164
Y.extend(ToggleButton, Button, {
167
selectedAttrName: '',
169
initializer: function (config) {
171
type = button.get('type'),
172
selectedAttrName = (type === "checkbox" ? 'checked' : 'pressed'),
173
selectedState = config[selectedAttrName] || false;
175
// Create the checked/pressed attribute
176
button.addAttr(selectedAttrName, {
180
button.selectedAttrName = selectedAttrName;
183
destructor: function () {
184
delete this.selectedAttrName;
189
* @description Hooks up events for the widget
193
cb = button.get('contentBox');
195
ToggleButton.superclass.bindUI.call(button);
197
cb.on(button.trigger, button.toggle, button);
198
button.after(button.selectedAttrName + 'Change', button._afterSelectedChange);
203
* @description Syncs the UI for the widget
207
cb = button.get('contentBox'),
208
type = button.get('type'),
209
ROLES = ToggleButton.ARIA_ROLES,
210
role = (type === 'checkbox' ? ROLES.CHECKBOX : ROLES.TOGGLE),
211
selectedAttrName = button.selectedAttrName;
213
ToggleButton.superclass.syncUI.call(button);
215
cb.set('role', role);
216
button._uiSetSelected(button.get(selectedAttrName));
219
_afterSelectedChange: function(e){
220
this._uiSetSelected(e.newVal);
224
* @method _uiSetSelected
227
_uiSetSelected: function(value) {
229
cb = button.get('contentBox'),
230
STATES = ToggleButton.ARIA_STATES,
231
type = button.get('type'),
232
ariaState = (type === 'checkbox' ? STATES.CHECKED : STATES.PRESSED);
234
cb.toggleClass(Button.CLASS_NAMES.SELECTED, value);
235
cb.set(ariaState, value);
240
* @description Toggles the selected/pressed/checked state of a ToggleButton
245
button._set(button.selectedAttrName, !button.get(button.selectedAttrName));
251
* The identity of the widget.
255
* @default 'buttongroup'
260
NAME: 'toggleButton',
263
* Static property used to define the default attribute configuration of
274
writeOnce: 'initOnly'
279
* @property HTML_PARSER
285
checked: function(node) {
286
return node.hasClass(CLASS_NAMES.SELECTED);
288
pressed: function(node) {
289
return node.hasClass(CLASS_NAMES.SELECTED);
294
* @property ARIA_STATES
299
ARIA_STATES: ARIA_STATES,
302
* @property ARIA_ROLES
307
ARIA_ROLES: ARIA_ROLES,
310
* Array of static constants used to identify the classnames applied to DOM nodes
312
* @property CLASS_NAMES
316
CLASS_NAMES: CLASS_NAMES
322
Y.ToggleButton = ToggleButton;
325
}, '3.5.1' ,{requires:['button-core', 'cssbutton', 'widget']});