3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.CycleButton"></div>/**
\r
10 * @class Ext.CycleButton
\r
11 * @extends Ext.SplitButton
\r
12 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
\r
13 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
\r
14 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
\r
15 * button displays the dropdown menu just like a normal SplitButton. Example usage:
\r
17 var btn = new Ext.CycleButton({
\r
19 prependText: 'View as ',
\r
22 iconCls:'view-text',
\r
28 changeHandler:function(btn, item){
\r
29 Ext.Msg.alert('Change View', item.text);
\r
34 * Create a new split button
\r
35 * @param {Object} config The config object
\r
38 Ext.CycleButton = Ext.extend(Ext.SplitButton, {
\r
39 <div id="cfg-Ext.CycleButton-items"></div>/**
\r
40 * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
\r
41 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
\r
43 <div id="cfg-Ext.CycleButton-showText"></div>/**
\r
44 * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
\r
46 <div id="cfg-Ext.CycleButton-prependText"></div>/**
\r
47 * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
\r
48 * button's text (only applies when showText = true, defaults to '')
\r
50 <div id="cfg-Ext.CycleButton-changeHandler"></div>/**
\r
51 * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
\r
52 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
\r
53 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
\r
54 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
\r
56 <div id="cfg-Ext.CycleButton-forceIcon"></div>/**
\r
57 * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
\r
58 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
\r
59 * default behavior of changing the button's icon to match the selected item's icon on change.
\r
61 <div id="prop-Ext.CycleButton-menu"></div>/**
\r
64 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
\r
68 getItemText : function(item){
\r
69 if(item && this.showText === true){
\r
71 if(this.prependText){
\r
72 text += this.prependText;
\r
80 <div id="method-Ext.CycleButton-setActiveItem"></div>/**
\r
81 * Sets the button's active menu item.
\r
82 * @param {Ext.menu.CheckItem} item The item to activate
\r
83 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
\r
85 setActiveItem : function(item, suppressEvent){
\r
86 if(!Ext.isObject(item)){
\r
87 item = this.menu.getComponent(item);
\r
91 this.text = this.getItemText(item);
\r
92 this.iconCls = item.iconCls;
\r
94 var t = this.getItemText(item);
\r
98 this.setIconClass(item.iconCls);
\r
100 this.activeItem = item;
\r
102 item.setChecked(true, true);
\r
104 if(this.forceIcon){
\r
105 this.setIconClass(this.forceIcon);
\r
107 if(!suppressEvent){
\r
108 this.fireEvent('change', this, item);
\r
113 <div id="method-Ext.CycleButton-getActiveItem"></div>/**
\r
114 * Gets the currently active menu item.
\r
115 * @return {Ext.menu.CheckItem} The active item
\r
117 getActiveItem : function(){
\r
118 return this.activeItem;
\r
122 initComponent : function(){
\r
124 <div id="event-Ext.CycleButton-change"></div>/**
\r
126 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
\r
127 * is set on this CycleButton, it will be called instead on active item change and this change event will
\r
129 * @param {Ext.CycleButton} this
\r
130 * @param {Ext.menu.CheckItem} item The menu item that was selected
\r
135 if(this.changeHandler){
\r
136 this.on('change', this.changeHandler, this.scope||this);
\r
137 delete this.changeHandler;
\r
140 this.itemCount = this.items.length;
\r
142 this.menu = {cls:'x-cycle-menu', items:[]};
\r
144 Ext.each(this.items, function(item, i){
\r
146 group: item.group || this.id,
\r
148 checkHandler: this.checkHandler,
\r
150 checked: item.checked || false
\r
152 this.menu.items.push(item);
\r
157 this.setActiveItem(checked, true);
\r
158 Ext.CycleButton.superclass.initComponent.call(this);
\r
160 this.on('click', this.toggleSelected, this);
\r
164 checkHandler : function(item, pressed){
\r
166 this.setActiveItem(item);
\r
170 <div id="method-Ext.CycleButton-toggleSelected"></div>/**
\r
171 * This is normally called internally on button click, but can be called externally to advance the button's
\r
172 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
\r
173 * the active item will be set to the first item in the menu.
\r
175 toggleSelected : function(){
\r
178 // layout if we haven't before so the items are active
\r
183 var nextIdx, checkItem;
\r
184 for (var i = 1; i < this.itemCount; i++) {
\r
185 nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
\r
186 // check the potential item
\r
187 checkItem = m.items.itemAt(nextIdx);
\r
188 // if its not disabled then check it.
\r
189 if (!checkItem.disabled) {
\r
190 checkItem.setChecked(true);
\r
196 Ext.reg('cycle', Ext.CycleButton);</pre>
\r