1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-button.Cycle-method-constructor'><span id='Ext-button.Cycle'>/**
2 </span></span> * @class Ext.button.Cycle
3 * @extends Ext.button.Split
4 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
5 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
6 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
7 * button displays the dropdown menu just like a normal SplitButton.
8 * {@img Ext.button.Cycle/Ext.button.Cycle.png Ext.button.Cycle component}
10 * <pre><code>
11 Ext.create('Ext.button.Cycle', {
13 prependText: 'View as ',
14 renderTo: Ext.getBody(),
26 changeHandler:function(cycleBtn, activeItem){
27 Ext.Msg.alert('Change View', activeItem.text);
30 </code></pre>
32 * Create a new split button
33 * @param {Object} config The config object
37 Ext.define('Ext.button.Cycle', {
39 /* Begin Definitions */
41 alias: 'widget.cycle',
43 extend: 'Ext.button.Split',
44 alternateClassName: 'Ext.CycleButton',
48 <span id='Ext-button.Cycle-cfg-items'> /**
49 </span> * @cfg {Array} items <p>Deprecated as of 4.0. Use the {@link #menu} config instead. All menu items will be created
50 * as {@link Ext.menu.CheckItem CheckItem}s.</p>
51 * <p>An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
52 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
54 <span id='Ext-button.Cycle-cfg-showText'> /**
55 </span> * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false).
56 * The Button will show its configured {@link #text} if this. config is omitted.
58 <span id='Ext-button.Cycle-cfg-prependText'> /**
59 </span> * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
60 * button's text (only applies when showText = true, defaults to '')
62 <span id='Ext-button.Cycle-cfg-changeHandler'> /**
63 </span> * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
64 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
65 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
66 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
68 <span id='Ext-button.Cycle-cfg-forceIcon'> /**
69 </span> * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
70 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
71 * default behavior of changing the button's icon to match the selected item's icon on change.
73 <span id='Ext-button.Cycle-property-menu'> /**
74 </span> * @property menu
76 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
80 getButtonText: function(item) {
84 if (item && me.showText === true) {
86 text += me.prependText;
94 <span id='Ext-button.Cycle-method-setActiveItem'> /**
95 </span> * Sets the button's active menu item.
96 * @param {Ext.menu.CheckItem} item The item to activate
97 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
99 setActiveItem: function(item, suppressEvent) {
102 if (!Ext.isObject(item)) {
103 item = me.menu.getComponent(item);
107 me.text = me.getButtonText(item);
108 me.iconCls = item.iconCls;
110 me.setText(me.getButtonText(item));
111 me.setIconCls(item.iconCls);
113 me.activeItem = item;
115 item.setChecked(true, false);
118 me.setIconCls(me.forceIcon);
120 if (!suppressEvent) {
121 me.fireEvent('change', me, item);
126 <span id='Ext-button.Cycle-method-getActiveItem'> /**
127 </span> * Gets the currently active menu item.
128 * @return {Ext.menu.CheckItem} The active item
130 getActiveItem: function() {
131 return this.activeItem;
135 initComponent: function() {
141 <span id='Ext-button.Cycle-event-change'> /**
142 </span> * @event change
143 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
144 * is set on this CycleButton, it will be called instead on active item change and this change event will
146 * @param {Ext.button.Cycle} this
147 * @param {Ext.menu.CheckItem} item The menu item that was selected
152 if (me.changeHandler) {
153 me.on('change', me.changeHandler, me.scope || me);
154 delete me.changeHandler;
157 // Allow them to specify a menu config which is a standard Button config.
158 // Remove direct use of "items" in 5.0.
159 items = (me.menu.items||[]).concat(me.items||[]);
160 me.menu = Ext.applyIf({
161 cls: Ext.baseCSSPrefix + 'cycle-menu',
165 // Convert all items to CheckItems
166 Ext.each(items, function(item, i) {
170 checkHandler: me.checkHandler,
172 checked: item.checked || false
174 me.menu.items.push(item);
179 me.itemCount = me.menu.items.length;
180 me.callParent(arguments);
181 me.on('click', me.toggleSelected, me);
182 me.setActiveItem(checked, me);
184 // If configured with a fixed width, the cycling will center a different child item's text each click. Prevent this.
185 if (me.width && me.showText) {
186 me.addCls(Ext.baseCSSPrefix + 'cycle-fixed-width');
191 checkHandler: function(item, pressed) {
193 this.setActiveItem(item);
197 <span id='Ext-button.Cycle-method-toggleSelected'> /**
198 </span> * This is normally called internally on button click, but can be called externally to advance the button's
199 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
200 * the active item will be set to the first item in the menu.
202 toggleSelected: function() {
207 checkItem = me.activeItem.next(':not([disabled])') || m.items.getAt(0);
208 checkItem.setChecked(true);
210 });</pre></pre></body></html>