4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-button-Cycle'>/**
19 </span> * @class Ext.button.Cycle
20 * @extends Ext.button.Split
21 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
22 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
23 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
24 * button displays the dropdown menu just like a normal SplitButton.
25 * {@img Ext.button.Cycle/Ext.button.Cycle.png Ext.button.Cycle component}
27 * <pre><code>
28 Ext.create('Ext.button.Cycle', {
30 prependText: 'View as ',
31 renderTo: Ext.getBody(),
43 changeHandler:function(cycleBtn, activeItem){
44 Ext.Msg.alert('Change View', activeItem.text);
47 </code></pre>
49 Ext.define('Ext.button.Cycle', {
51 /* Begin Definitions */
53 alias: 'widget.cycle',
55 extend: 'Ext.button.Split',
56 alternateClassName: 'Ext.CycleButton',
60 <span id='Ext-button-Cycle-cfg-items'> /**
61 </span> * @cfg {Array} items <p>Deprecated as of 4.0. Use the {@link #menu} config instead. All menu items will be created
62 * as {@link Ext.menu.CheckItem CheckItem}s.</p>
63 * <p>An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
64 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
66 <span id='Ext-button-Cycle-cfg-showText'> /**
67 </span> * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false).
68 * The Button will show its configured {@link #text} if this. config is omitted.
70 <span id='Ext-button-Cycle-cfg-prependText'> /**
71 </span> * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
72 * button's text (only applies when showText = true, defaults to '')
74 <span id='Ext-button-Cycle-cfg-changeHandler'> /**
75 </span> * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
76 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
77 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
78 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
80 <span id='Ext-button-Cycle-cfg-forceIcon'> /**
81 </span> * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
82 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
83 * default behavior of changing the button's icon to match the selected item's icon on change.
85 <span id='Ext-button-Cycle-property-menu'> /**
86 </span> * @property menu
88 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
92 getButtonText: function(item) {
96 if (item && me.showText === true) {
98 text += me.prependText;
106 <span id='Ext-button-Cycle-method-setActiveItem'> /**
107 </span> * Sets the button's active menu item.
108 * @param {Ext.menu.CheckItem} item The item to activate
109 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
111 setActiveItem: function(item, suppressEvent) {
114 if (!Ext.isObject(item)) {
115 item = me.menu.getComponent(item);
119 me.text = me.getButtonText(item);
120 me.iconCls = item.iconCls;
122 me.setText(me.getButtonText(item));
123 me.setIconCls(item.iconCls);
125 me.activeItem = item;
127 item.setChecked(true, false);
130 me.setIconCls(me.forceIcon);
132 if (!suppressEvent) {
133 me.fireEvent('change', me, item);
138 <span id='Ext-button-Cycle-method-getActiveItem'> /**
139 </span> * Gets the currently active menu item.
140 * @return {Ext.menu.CheckItem} The active item
142 getActiveItem: function() {
143 return this.activeItem;
147 initComponent: function() {
153 <span id='Ext-button-Cycle-event-change'> /**
154 </span> * @event change
155 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
156 * is set on this CycleButton, it will be called instead on active item change and this change event will
158 * @param {Ext.button.Cycle} this
159 * @param {Ext.menu.CheckItem} item The menu item that was selected
164 if (me.changeHandler) {
165 me.on('change', me.changeHandler, me.scope || me);
166 delete me.changeHandler;
169 // Allow them to specify a menu config which is a standard Button config.
170 // Remove direct use of "items" in 5.0.
171 items = (me.menu.items||[]).concat(me.items||[]);
172 me.menu = Ext.applyIf({
173 cls: Ext.baseCSSPrefix + 'cycle-menu',
177 // Convert all items to CheckItems
178 Ext.each(items, function(item, i) {
182 checkHandler: me.checkHandler,
184 checked: item.checked || false
186 me.menu.items.push(item);
191 me.itemCount = me.menu.items.length;
192 me.callParent(arguments);
193 me.on('click', me.toggleSelected, me);
194 me.setActiveItem(checked, me);
196 // If configured with a fixed width, the cycling will center a different child item's text each click. Prevent this.
197 if (me.width && me.showText) {
198 me.addCls(Ext.baseCSSPrefix + 'cycle-fixed-width');
203 checkHandler: function(item, pressed) {
205 this.setActiveItem(item);
209 <span id='Ext-button-Cycle-method-toggleSelected'> /**
210 </span> * This is normally called internally on button click, but can be called externally to advance the button's
211 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
212 * the active item will be set to the first item in the menu.
214 toggleSelected: function() {
219 checkItem = me.activeItem.next(':not([disabled])') || m.items.getAt(0);
220 checkItem.setChecked(true);