Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / docs / source / CycleButton.html
1 <html>\r
2 <head>\r
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
7 </head>\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
16  * <pre><code>\r
17 var btn = new Ext.CycleButton({\r
18     showText: true,\r
19     prependText: 'View as ',\r
20     items: [{\r
21         text:'text only',\r
22         iconCls:'view-text',\r
23         checked:true\r
24     },{\r
25         text:'HTML',\r
26         iconCls:'view-html'\r
27     }],\r
28     changeHandler:function(btn, item){\r
29         Ext.Msg.alert('Change View', item.text);\r
30     }\r
31 });\r
32 </code></pre>\r
33  * @constructor\r
34  * Create a new split button\r
35  * @param {Object} config The config object\r
36  * @xtype cycle\r
37  */\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
42      */\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
45      */\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
49      */\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
55      */\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
60      */\r
61     <div id="prop-Ext.CycleButton-menu"></div>/**\r
62      * @property menu\r
63      * @type Menu\r
64      * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.\r
65      */\r
66 \r
67     // private\r
68     getItemText : function(item){\r
69         if(item && this.showText === true){\r
70             var text = '';\r
71             if(this.prependText){\r
72                 text += this.prependText;\r
73             }\r
74             text += item.text;\r
75             return text;\r
76         }\r
77         return undefined;\r
78     },\r
79 \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
84      */\r
85     setActiveItem : function(item, suppressEvent){\r
86         if(!Ext.isObject(item)){\r
87             item = this.menu.getComponent(item);\r
88         }\r
89         if(item){\r
90             if(!this.rendered){\r
91                 this.text = this.getItemText(item);\r
92                 this.iconCls = item.iconCls;\r
93             }else{\r
94                 var t = this.getItemText(item);\r
95                 if(t){\r
96                     this.setText(t);\r
97                 }\r
98                 this.setIconClass(item.iconCls);\r
99             }\r
100             this.activeItem = item;\r
101             if(!item.checked){\r
102                 item.setChecked(true, true);\r
103             }\r
104             if(this.forceIcon){\r
105                 this.setIconClass(this.forceIcon);\r
106             }\r
107             if(!suppressEvent){\r
108                 this.fireEvent('change', this, item);\r
109             }\r
110         }\r
111     },\r
112 \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
116      */\r
117     getActiveItem : function(){\r
118         return this.activeItem;\r
119     },\r
120 \r
121     // private\r
122     initComponent : function(){\r
123         this.addEvents(\r
124             <div id="event-Ext.CycleButton-change"></div>/**\r
125              * @event change\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
128              * not be fired.\r
129              * @param {Ext.CycleButton} this\r
130              * @param {Ext.menu.CheckItem} item The menu item that was selected\r
131              */\r
132             "change"\r
133         );\r
134 \r
135         if(this.changeHandler){\r
136             this.on('change', this.changeHandler, this.scope||this);\r
137             delete this.changeHandler;\r
138         }\r
139 \r
140         this.itemCount = this.items.length;\r
141 \r
142         this.menu = {cls:'x-cycle-menu', items:[]};\r
143         var checked;\r
144         Ext.each(this.items, function(item, i){\r
145             Ext.apply(item, {\r
146                 group: item.group || this.id,\r
147                 itemIndex: i,\r
148                 checkHandler: this.checkHandler,\r
149                 scope: this,\r
150                 checked: item.checked || false\r
151             });\r
152             this.menu.items.push(item);\r
153             if(item.checked){\r
154                 checked = item;\r
155             }\r
156         }, this);\r
157         this.setActiveItem(checked, true);\r
158         Ext.CycleButton.superclass.initComponent.call(this);\r
159 \r
160         this.on('click', this.toggleSelected, this);\r
161     },\r
162 \r
163     // private\r
164     checkHandler : function(item, pressed){\r
165         if(pressed){\r
166             this.setActiveItem(item);\r
167         }\r
168     },\r
169 \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
174      */\r
175     toggleSelected : function(){\r
176         var m = this.menu;\r
177         m.render();\r
178         // layout if we haven't before so the items are active\r
179         if(!m.hasLayout){\r
180             m.doLayout();\r
181         }\r
182         \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
191                 break;\r
192             }\r
193         }\r
194     }\r
195 });\r
196 Ext.reg('cycle', Ext.CycleButton);</pre>    \r
197 </body>\r
198 </html>