MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / button-group / button-group.js
blobc3187297f9427dea6aece335a554feb60630c25d
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-group', function(Y) {
9 /**
10 * A Widget to create groups of buttons
12 * @module button-group
13 * @since 3.5.0
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;
21 /**
22 * Creates a ButtonGroup
24 * @class ButtonGroup
25 * @extends Widget
26 * @param config {Object} Configuration object
27 * @constructor
29 function ButtonGroup() {
30     ButtonGroup.superclass.constructor.apply(this, arguments);
33 /* ButtonGroup extends Widget */
34 Y.ButtonGroup = Y.extend(ButtonGroup, Y.Widget, {
36     /**
37      * @method renderUI
38      * @description Creates a visual representation of the widget based on existing parameters.
39      * @public
40      */
41     renderUI: function() {
42         this.getButtons().plug(Y.Plugin.Button);
43     },
45     /**
46      * @method bindUI
47      * @description Hooks up events for the widget
48      * @public
49      */
50     bindUI: function() {
51         var group = this,
52             cb = group.get(CONTENT_BOX);
54         cb.delegate(CLICK_EVENT, group._handleClick, SELECTOR, group);
55     },
57     /**
58     * @method getButtons
59     * @description Returns all buttons inside this this button group
60     * @public
61     */
62     getButtons: function() {
63         var cb = this.get(CONTENT_BOX);
65         return cb.all(SELECTOR);
66     },
68     /**
69     * @method getSelectedButtons
70     * @description Returns all Y.Buttons instances that are selected
71     * @public
72     */
73     getSelectedButtons: function() {
74         var group = this,
75             selected = [],
76             buttons = group.getButtons(),
77             selectedClass = ButtonGroup.CLASS_NAMES.SELECTED;
79         buttons.each(function(node){
80             if (node.hasClass(selectedClass)){
81                 selected.push(node);
82             }
83         });
85         return selected;
86     },
88     /**
89     * @method getSelectedValues
90     * @description Returns the values of all Y.Button instances that are selected
91     * @public
92     */
93     getSelectedValues: function() {
94         var group = this,
95             value,
96             values = [],
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();
103                 values.push(value);
104             }
105         });
107         return values;
108     },
110     /**
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
114     * @private
115     */
116     _handleClick: function(e){
117         var buttons,
118             clickedNode = e.target,
119             group = this,
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});
129         }
130         else if (type === 'radio') {
131             if (!isSelected) {
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});
136             }
137         }
138     }
140 }, {
141     // Y.ButtonGroup static properties
143     /**
144      * The identity of the widget.
145      *
146      * @property NAME
147      * @type {String}
148      * @default 'buttongroup'
149      * @readOnly
150      * @protected
151      * @static
152      */
153     NAME: 'buttongroup',
155     /**
156     * Static property used to define the default attribute configuration of
157     * the Widget.
158     *
159     * @property ATTRS
160     * @type {Object}
161     * @protected
162     * @static
163     */
164     ATTRS: {
165         type: {
166             writeOnce: 'initOnly',
167             value: 'radio'
168         }
169     },
171     /**
172      * List of class names to use for ButtonGroups
173      *
174      * @property CLASS_NAMES
175      * @type {Object}
176      * @static
177      */
178     CLASS_NAMES: CLASS_NAMES
182 }, '3.5.1' ,{requires:['button-plugin', 'cssbutton', 'widget']});