3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.CycleButton"></div>/**
\r
15 * @class Ext.CycleButton
\r
16 * @extends Ext.SplitButton
\r
17 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
\r
18 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
\r
19 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
\r
20 * button displays the dropdown menu just like a normal SplitButton. Example usage:
\r
22 var btn = new Ext.CycleButton({
\r
24 prependText: 'View as ',
\r
27 iconCls:'view-text',
\r
33 changeHandler:function(btn, item){
\r
34 Ext.Msg.alert('Change View', item.text);
\r
39 * Create a new split button
\r
40 * @param {Object} config The config object
\r
43 Ext.CycleButton = Ext.extend(Ext.SplitButton, {
\r
44 <div id="cfg-Ext.CycleButton-items"></div>/**
\r
45 * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
\r
46 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
\r
48 <div id="cfg-Ext.CycleButton-showText"></div>/**
\r
49 * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
\r
51 <div id="cfg-Ext.CycleButton-prependText"></div>/**
\r
52 * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
\r
53 * button's text (only applies when showText = true, defaults to '')
\r
55 <div id="cfg-Ext.CycleButton-changeHandler"></div>/**
\r
56 * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
\r
57 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
\r
58 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
\r
59 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
\r
61 <div id="cfg-Ext.CycleButton-forceIcon"></div>/**
\r
62 * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
\r
63 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
\r
64 * default behavior of changing the button's icon to match the selected item's icon on change.
\r
66 <div id="prop-Ext.CycleButton-menu"></div>/**
\r
69 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
\r
73 getItemText : function(item){
\r
74 if(item && this.showText === true){
\r
76 if(this.prependText){
\r
77 text += this.prependText;
\r
85 <div id="method-Ext.CycleButton-setActiveItem"></div>/**
\r
86 * Sets the button's active menu item.
\r
87 * @param {Ext.menu.CheckItem} item The item to activate
\r
88 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
\r
90 setActiveItem : function(item, suppressEvent){
\r
91 if(!Ext.isObject(item)){
\r
92 item = this.menu.getComponent(item);
\r
96 this.text = this.getItemText(item);
\r
97 this.iconCls = item.iconCls;
\r
99 var t = this.getItemText(item);
\r
103 this.setIconClass(item.iconCls);
\r
105 this.activeItem = item;
\r
107 item.setChecked(true, true);
\r
109 if(this.forceIcon){
\r
110 this.setIconClass(this.forceIcon);
\r
112 if(!suppressEvent){
\r
113 this.fireEvent('change', this, item);
\r
118 <div id="method-Ext.CycleButton-getActiveItem"></div>/**
\r
119 * Gets the currently active menu item.
\r
120 * @return {Ext.menu.CheckItem} The active item
\r
122 getActiveItem : function(){
\r
123 return this.activeItem;
\r
127 initComponent : function(){
\r
129 <div id="event-Ext.CycleButton-change"></div>/**
\r
131 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
\r
132 * is set on this CycleButton, it will be called instead on active item change and this change event will
\r
134 * @param {Ext.CycleButton} this
\r
135 * @param {Ext.menu.CheckItem} item The menu item that was selected
\r
140 if(this.changeHandler){
\r
141 this.on('change', this.changeHandler, this.scope||this);
\r
142 delete this.changeHandler;
\r
145 this.itemCount = this.items.length;
\r
147 this.menu = {cls:'x-cycle-menu', items:[]};
\r
149 Ext.each(this.items, function(item, i){
\r
151 group: item.group || this.id,
\r
153 checkHandler: this.checkHandler,
\r
155 checked: item.checked || false
\r
157 this.menu.items.push(item);
\r
162 this.setActiveItem(checked, true);
\r
163 Ext.CycleButton.superclass.initComponent.call(this);
\r
165 this.on('click', this.toggleSelected, this);
\r
169 checkHandler : function(item, pressed){
\r
171 this.setActiveItem(item);
\r
175 <div id="method-Ext.CycleButton-toggleSelected"></div>/**
\r
176 * This is normally called internally on button click, but can be called externally to advance the button's
\r
177 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
\r
178 * the active item will be set to the first item in the menu.
\r
180 toggleSelected : function(){
\r
183 // layout if we haven't before so the items are active
\r
188 var nextIdx, checkItem;
\r
189 for (var i = 1; i < this.itemCount; i++) {
\r
190 nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
\r
191 // check the potential item
\r
192 checkItem = m.items.itemAt(nextIdx);
\r
193 // if its not disabled then check it.
\r
194 if (!checkItem.disabled) {
\r
195 checkItem.setChecked(true);
\r
201 Ext.reg('cycle', Ext.CycleButton);</pre>