3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.button.Cycle
17 * @extends Ext.button.Split
18 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
19 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
20 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
21 * button displays the dropdown menu just like a normal SplitButton.
22 * {@img Ext.button.Cycle/Ext.button.Cycle.png Ext.button.Cycle component}
25 Ext.create('Ext.button.Cycle', {
27 prependText: 'View as ',
28 renderTo: Ext.getBody(),
40 changeHandler:function(cycleBtn, activeItem){
41 Ext.Msg.alert('Change View', activeItem.text);
46 Ext.define('Ext.button.Cycle', {
48 /* Begin Definitions */
50 alias: 'widget.cycle',
52 extend: 'Ext.button.Split',
53 alternateClassName: 'Ext.CycleButton',
58 * @cfg {Array} items <p>Deprecated as of 4.0. Use the {@link #menu} config instead. All menu items will be created
59 * as {@link Ext.menu.CheckItem CheckItem}s.</p>
60 * <p>An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
61 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
64 * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false).
65 * The Button will show its configured {@link #text} if this. config is omitted.
68 * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
69 * button's text (only applies when showText = true, defaults to '')
72 * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
73 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
74 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
75 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
78 * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
79 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
80 * default behavior of changing the button's icon to match the selected item's icon on change.
85 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
89 getButtonText: function(item) {
93 if (item && me.showText === true) {
95 text += me.prependText;
104 * Sets the button's active menu item.
105 * @param {Ext.menu.CheckItem} item The item to activate
106 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
108 setActiveItem: function(item, suppressEvent) {
111 if (!Ext.isObject(item)) {
112 item = me.menu.getComponent(item);
116 me.text = me.getButtonText(item);
117 me.iconCls = item.iconCls;
119 me.setText(me.getButtonText(item));
120 me.setIconCls(item.iconCls);
122 me.activeItem = item;
124 item.setChecked(true, false);
127 me.setIconCls(me.forceIcon);
129 if (!suppressEvent) {
130 me.fireEvent('change', me, item);
136 * Gets the currently active menu item.
137 * @return {Ext.menu.CheckItem} The active item
139 getActiveItem: function() {
140 return this.activeItem;
144 initComponent: function() {
152 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
153 * is set on this CycleButton, it will be called instead on active item change and this change event will
155 * @param {Ext.button.Cycle} this
156 * @param {Ext.menu.CheckItem} item The menu item that was selected
161 if (me.changeHandler) {
162 me.on('change', me.changeHandler, me.scope || me);
163 delete me.changeHandler;
166 // Allow them to specify a menu config which is a standard Button config.
167 // Remove direct use of "items" in 5.0.
168 items = (me.menu.items||[]).concat(me.items||[]);
169 me.menu = Ext.applyIf({
170 cls: Ext.baseCSSPrefix + 'cycle-menu',
174 // Convert all items to CheckItems
175 Ext.each(items, function(item, i) {
179 checkHandler: me.checkHandler,
181 checked: item.checked || false
183 me.menu.items.push(item);
188 me.itemCount = me.menu.items.length;
189 me.callParent(arguments);
190 me.on('click', me.toggleSelected, me);
191 me.setActiveItem(checked, me);
193 // If configured with a fixed width, the cycling will center a different child item's text each click. Prevent this.
194 if (me.width && me.showText) {
195 me.addCls(Ext.baseCSSPrefix + 'cycle-fixed-width');
200 checkHandler: function(item, pressed) {
202 this.setActiveItem(item);
207 * This is normally called internally on button click, but can be called externally to advance the button's
208 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
209 * the active item will be set to the first item in the menu.
211 toggleSelected: function() {
216 checkItem = me.activeItem.next(':not([disabled])') || m.items.getAt(0);
217 checkItem.setChecked(true);