3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('button-group', function(Y) {
10 * A Widget to create groups of buttons
12 * @module button-group
16 var CONTENT_BOX = "contentBox",
17 SELECTOR = "button, input[type=button], input[type=reset], input[type=submit]",
18 CLICK_EVENT = "click",
19 CLASS_NAMES = Y.ButtonCore.CLASS_NAMES;
22 * Creates a ButtonGroup
26 * @param config {Object} Configuration object
29 function ButtonGroup() {
30 ButtonGroup.superclass.constructor.apply(this, arguments);
33 /* ButtonGroup extends Widget */
34 Y.ButtonGroup = Y.extend(ButtonGroup, Y.Widget, {
38 * @description Creates a visual representation of the widget based on existing parameters.
41 renderUI: function() {
42 this.getButtons().plug(Y.Plugin.Button);
47 * @description Hooks up events for the widget
52 cb = group.get(CONTENT_BOX);
54 cb.delegate(CLICK_EVENT, group._handleClick, SELECTOR, group);
59 * @description Returns all buttons inside this this button group
62 getButtons: function() {
63 var cb = this.get(CONTENT_BOX);
65 return cb.all(SELECTOR);
69 * @method getSelectedButtons
70 * @description Returns all Y.Buttons instances that are selected
73 getSelectedButtons: function() {
76 buttons = group.getButtons(),
77 selectedClass = ButtonGroup.CLASS_NAMES.SELECTED;
79 buttons.each(function(node){
80 if (node.hasClass(selectedClass)){
89 * @method getSelectedValues
90 * @description Returns the values of all Y.Button instances that are selected
93 getSelectedValues: function() {
97 selected = group.getSelectedButtons(),
98 selectedClass = ButtonGroup.CLASS_NAMES.SELECTED;
100 Y.Array.each(selected, function(node){
101 if (node.hasClass(selectedClass)){
102 value = node.getContent();
111 * @method _handleClick
112 * @description A delegated click handler for when any button is clicked in the content box
113 * @param e {Object} An event object
116 _handleClick: function(e){
118 clickedNode = e.target,
120 type = group.get('type'),
121 selectedClass = ButtonGroup.CLASS_NAMES.SELECTED,
122 isSelected = clickedNode.hasClass(selectedClass);
124 // TODO: Anything for 'push' groups?
126 if (type === 'checkbox') {
127 clickedNode.toggleClass(selectedClass, !isSelected);
128 group.fire('selectionChange', {originEvent: e});
130 else if (type === 'radio') {
132 buttons = group.getButtons(); // Todo: getSelectedButtons()? Need it to return an arraylist then.
133 buttons.removeClass(selectedClass);
134 clickedNode.addClass(selectedClass);
135 group.fire('selectionChange', {originEvent: e});
141 // Y.ButtonGroup static properties
144 * The identity of the widget.
148 * @default 'buttongroup'
156 * Static property used to define the default attribute configuration of
166 writeOnce: 'initOnly',
172 * List of class names to use for ButtonGroups
174 * @property CLASS_NAMES
178 CLASS_NAMES: CLASS_NAMES
182 }, '3.5.1' ,{requires:['button-plugin', 'cssbutton', 'widget']});