3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * @extends Ext.BoxComponent
11 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
12 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
13 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
14 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
15 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
16 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
17 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
19 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
20 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
21 * @cfg {String/Object} tooltip The tooltip for the button - can be a string to be used as innerHTML (html tags are accepted) or QuickTips config object
22 * @cfg {Boolean} hidden True to start hidden (defaults to false)
23 * @cfg {Boolean} disabled True to start disabled (defaults to false)
24 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
25 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
26 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
27 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
30 * @param {Object} config The config object
33 Ext.Button = Ext.extend(Ext.BoxComponent, {
35 * Read-only. True if this button is hidden
40 * Read-only. True if this button is disabled
45 * Read-only. True if this button is pressed (only if enableToggle = true)
51 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
55 * @cfg {Boolean} allowDepress
56 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
60 * @cfg {Boolean} enableToggle
61 * True to enable pressed/not pressed toggling (defaults to false)
65 * @cfg {Function} toggleHandler
66 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
67 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
68 * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
73 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
76 * @cfg {String} menuAlign
77 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
82 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
83 * text to be used if this item is shown in the overflow menu. See also
84 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
87 * @cfg {String} iconCls
88 * A css class which sets a background image to be used as the icon for this button
92 * submit, reset or button - defaults to 'button'
97 menuClassTarget : 'tr:nth(2)',
100 * @cfg {String} clickEvent
101 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
102 * Defaults to <tt>'click'</tt>.
104 clickEvent : 'click',
107 * @cfg {Boolean} handleMouseEvents
108 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
110 handleMouseEvents : true,
113 * @cfg {String} tooltipType
114 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
116 tooltipType : 'qtip',
119 * @cfg {String} buttonSelector
120 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
121 * DOM structure created.</p>
122 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
123 * a focussable element.</p>
124 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
126 buttonSelector : 'button:first-child',
129 * @cfg {String} scale
130 * <p>(Optional) The size of the Button. Three values are allowed:</p>
131 * <ul class="mdetail-params">
132 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
133 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
134 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
136 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
141 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
142 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
143 * executed. Defaults to this Button.
147 * @cfg {String} iconAlign
148 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
149 * <ul class="mdetail-params">
150 * <li>'top'<div class="sub-desc"></div></li>
151 * <li>'right'<div class="sub-desc"></div></li>
152 * <li>'bottom'<div class="sub-desc"></div></li>
153 * <li>'left'<div class="sub-desc"></div></li>
155 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
160 * @cfg {String} arrowAlign
161 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
162 * Two values are allowed:</p>
163 * <ul class="mdetail-params">
164 * <li>'right'<div class="sub-desc"></div></li>
165 * <li>'bottom'<div class="sub-desc"></div></li>
167 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
169 arrowAlign : 'right',
172 * @cfg {Ext.Template} template (Optional)
173 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
174 * Instances, or subclasses which need a different DOM structure may provide a different
175 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
181 * A CSS class string to apply to the button's main element.
186 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
189 * @cfg {Boolean} autoWidth
190 * By default, if a width is not specified the button will attempt to stretch horizontally to fit its content.
191 * If the button is being managed by a width sizing layout (hbox, fit, anchor), set this to false to prevent
192 * the button from doing this automatic sizing.
193 * Defaults to <tt>undefined</tt>.
196 initComponent : function(){
197 Ext.Button.superclass.initComponent.call(this);
202 * Fires when this button is clicked
203 * @param {Button} this
204 * @param {EventObject} e The click event
209 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
210 * @param {Button} this
211 * @param {Boolean} pressed
216 * Fires when the mouse hovers over the button
217 * @param {Button} this
218 * @param {Event} e The event object
223 * Fires when the mouse exits the button
224 * @param {Button} this
225 * @param {Event} e The event object
230 * If this button has a menu, this event fires when it is shown
231 * @param {Button} this
237 * If this button has a menu, this event fires when it is hidden
238 * @param {Button} this
243 * @event menutriggerover
244 * If this button has a menu, this event fires when the mouse enters the menu triggering element
245 * @param {Button} this
247 * @param {EventObject} e
251 * @event menutriggerout
252 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
253 * @param {Button} this
255 * @param {EventObject} e
260 this.menu = Ext.menu.MenuMgr.get(this.menu);
262 if(Ext.isString(this.toggleGroup)){
263 this.enableToggle = true;
268 * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used
269 * to create this Button's DOM structure.</p>
270 * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
271 * own implementation of this method.</p>
272 * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
273 * following items:</p><div class="mdetail-params"><ul>
274 * <li>The <button>'s {@link #type}</li>
275 * <li>A CSS class name applied to the Button's main <tbody> element which determines the button's scale and icon alignment.</li>
276 * <li>A CSS class to determine the presence and position of an arrow icon. (<code>'x-btn-arrow'</code> or <code>'x-btn-arrow-bottom'</code> or <code>''</code>)</li>
277 * <li>The {@link #cls} CSS class name applied to the button's wrapping <table> element.</li>
278 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
280 * @return {Array} Substitution data for a Template.
282 getTemplateArgs : function(){
283 return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
287 setButtonClass : function(){
288 if(this.useSetClass){
289 if(!Ext.isEmpty(this.oldCls)){
290 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
292 this.oldCls = (this.iconCls || this.icon) ? (this.text ? 'x-btn-text-icon' : 'x-btn-icon') : 'x-btn-noicon';
293 this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
298 getMenuClass : function(){
299 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
303 onRender : function(ct, position){
305 if(!Ext.Button.buttonTemplate){
306 // hideous table template
307 Ext.Button.buttonTemplate = new Ext.Template(
308 '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
309 '<tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
310 '<tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i> </i></td></tr>',
311 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
313 Ext.Button.buttonTemplate.compile();
315 this.template = Ext.Button.buttonTemplate;
318 var btn, targs = this.getTemplateArgs();
321 btn = this.template.insertBefore(position, targs, true);
323 btn = this.template.append(ct, targs, true);
326 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
327 * this references a <tt><button></tt> element. Read only.
331 this.btnEl = btn.child(this.buttonSelector);
332 this.mon(this.btnEl, {
338 this.initButtonEl(btn, this.btnEl);
340 Ext.ButtonToggleMgr.register(this);
344 initButtonEl : function(btn, btnEl){
346 this.setIcon(this.icon);
347 this.setText(this.text);
348 this.setIconClass(this.iconCls);
349 if(Ext.isDefined(this.tabIndex)){
350 btnEl.dom.tabIndex = this.tabIndex;
353 this.setTooltip(this.tooltip, true);
356 if(this.handleMouseEvents){
359 mouseover: this.onMouseOver,
360 mousedown: this.onMouseDown
363 // new functionality for monitoring on the document level
364 //this.mon(btn, 'mouseout', this.onMouseOut, this);
368 this.mon(this.menu, {
370 show: this.onMenuShow,
371 hide: this.onMenuHide
376 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
377 this.mon(repeater, 'click', this.onRepeatClick, this);
379 this.mon(btn, this.clickEvent, this.onClick, this);
384 afterRender : function(){
385 Ext.Button.superclass.afterRender.call(this);
386 this.useSetClass = true;
387 this.setButtonClass();
388 this.doc = Ext.getDoc();
393 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
394 * the value of the {@link iconCls} config internally.
395 * @param {String} cls The CSS class providing the icon image
396 * @return {Ext.Button} this
398 setIconClass : function(cls){
401 this.btnEl.dom.className = '';
402 this.btnEl.addClass(['x-btn-text', cls || '']);
403 this.setButtonClass();
409 * Sets the tooltip for this Button.
410 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
411 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
412 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
414 * @return {Ext.Button} this
416 setTooltip : function(tooltip, /* private */ initial){
421 if(Ext.isObject(tooltip)){
422 Ext.QuickTips.register(Ext.apply({
423 target: this.btnEl.id
425 this.tooltip = tooltip;
427 this.btnEl.dom[this.tooltipType] = tooltip;
430 this.tooltip = tooltip;
436 clearTip : function(){
437 if(Ext.isObject(this.tooltip)){
438 Ext.QuickTips.unregister(this.btnEl);
443 beforeDestroy : function(){
447 if(this.menu && this.destroyMenu !== false) {
448 Ext.destroy(this.btnEl, this.menu);
450 Ext.destroy(this.repeater);
454 onDestroy : function(){
456 this.doc.un('mouseover', this.monitorMouseOver, this);
457 this.doc.un('mouseup', this.onMouseUp, this);
460 Ext.ButtonToggleMgr.unregister(this);
462 Ext.Button.superclass.onDestroy.call(this);
466 doAutoWidth : function(){
467 if(this.autoWidth !== false && this.el && this.text && this.width === undefined){
468 this.el.setWidth('auto');
469 if(Ext.isIE7 && Ext.isStrict){
471 if(ib && ib.getWidth() > 20){
473 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
477 if(this.el.getWidth() < this.minWidth){
478 this.el.setWidth(this.minWidth);
485 * Assigns this Button's click handler
486 * @param {Function} handler The function to call when the button is clicked
487 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
488 * Defaults to this Button.
489 * @return {Ext.Button} this
491 setHandler : function(handler, scope){
492 this.handler = handler;
498 * Sets this Button's text
499 * @param {String} text The button text
500 * @return {Ext.Button} this
502 setText : function(text){
505 this.btnEl.update(text || ' ');
506 this.setButtonClass();
513 * Sets the background image (inline style) of the button. This method also changes
514 * the value of the {@link icon} config internally.
515 * @param {String} icon The path to an image to display in the button
516 * @return {Ext.Button} this
518 setIcon : function(icon){
521 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
522 this.setButtonClass();
528 * Gets the text for this Button
529 * @return {String} The button text
531 getText : function(){
536 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
537 * @param {Boolean} state (optional) Force a particular state
538 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
539 * @return {Ext.Button} this
541 toggle : function(state, suppressEvent){
542 state = state === undefined ? !this.pressed : !!state;
543 if(state != this.pressed){
545 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
547 this.pressed = state;
549 this.fireEvent('toggle', this, state);
550 if(this.toggleHandler){
551 this.toggleHandler.call(this.scope || this, this, state);
559 onDisable : function(){
560 this.onDisableChange(true);
564 onEnable : function(){
565 this.onDisableChange(false);
568 onDisableChange : function(disabled){
570 if(!Ext.isIE6 || !this.text){
571 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
573 this.el.dom.disabled = disabled;
575 this.disabled = disabled;
579 * Show this button's menu (if it has one)
581 showMenu : function(){
582 if(this.rendered && this.menu){
584 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
586 if(this.menu.isVisible()){
589 this.menu.ownerCt = this;
590 this.menu.show(this.el, this.menuAlign);
596 * Hide this button's menu (if it has one)
598 hideMenu : function(){
599 if(this.hasVisibleMenu()){
606 * Returns true if the button has a menu and it is visible
609 hasVisibleMenu : function(){
610 return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
614 onRepeatClick : function(repeat, e){
619 onClick : function(e){
627 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
630 if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
633 this.fireEvent('click', this, e);
635 //this.el.removeClass('x-btn-over');
636 this.handler.call(this.scope || this, this, e);
642 isMenuTriggerOver : function(e, internal){
643 return this.menu && !internal;
647 isMenuTriggerOut : function(e, internal){
648 return this.menu && !internal;
652 onMouseOver : function(e){
654 var internal = e.within(this.el, true);
656 this.el.addClass('x-btn-over');
657 if(!this.monitoringMouseOver){
658 this.doc.on('mouseover', this.monitorMouseOver, this);
659 this.monitoringMouseOver = true;
661 this.fireEvent('mouseover', this, e);
663 if(this.isMenuTriggerOver(e, internal)){
664 this.fireEvent('menutriggerover', this, this.menu, e);
670 monitorMouseOver : function(e){
671 if(e.target != this.el.dom && !e.within(this.el)){
672 if(this.monitoringMouseOver){
673 this.doc.un('mouseover', this.monitorMouseOver, this);
674 this.monitoringMouseOver = false;
681 onMouseOut : function(e){
682 var internal = e.within(this.el) && e.target != this.el.dom;
683 this.el.removeClass('x-btn-over');
684 this.fireEvent('mouseout', this, e);
685 if(this.isMenuTriggerOut(e, internal)){
686 this.fireEvent('menutriggerout', this, this.menu, e);
699 onFocus : function(e){
701 this.el.addClass('x-btn-focus');
705 onBlur : function(e){
706 this.el.removeClass('x-btn-focus');
710 getClickEl : function(e, isUp){
715 onMouseDown : function(e){
716 if(!this.disabled && e.button === 0){
717 this.getClickEl(e).addClass('x-btn-click');
718 this.doc.on('mouseup', this.onMouseUp, this);
722 onMouseUp : function(e){
724 this.getClickEl(e, true).removeClass('x-btn-click');
725 this.doc.un('mouseup', this.onMouseUp, this);
729 onMenuShow : function(e){
730 if(this.menu.ownerCt == this){
731 this.menu.ownerCt = this;
732 this.ignoreNextClick = 0;
733 this.el.addClass('x-btn-menu-active');
734 this.fireEvent('menushow', this, this.menu);
738 onMenuHide : function(e){
739 if(this.menu.ownerCt == this){
740 this.el.removeClass('x-btn-menu-active');
741 this.ignoreNextClick = this.restoreClick.defer(250, this);
742 this.fireEvent('menuhide', this, this.menu);
743 delete this.menu.ownerCt;
748 restoreClick : function(){
749 this.ignoreNextClick = 0;
753 * @cfg {String} autoEl @hide
756 * @cfg {String/Object} html @hide
759 * @cfg {String} contentEl @hide
762 * @cfg {Mixed} data @hide
765 * @cfg {Mixed} tpl @hide
768 * @cfg {String} tplWriteMode @hide
771 Ext.reg('button', Ext.Button);
773 // Private utility class used by Button
774 Ext.ButtonToggleMgr = function(){
777 function toggleGroup(btn, state){
779 var g = groups[btn.toggleGroup];
780 for(var i = 0, l = g.length; i < l; i++){
789 register : function(btn){
790 if(!btn.toggleGroup){
793 var g = groups[btn.toggleGroup];
795 g = groups[btn.toggleGroup] = [];
798 btn.on('toggle', toggleGroup);
801 unregister : function(btn){
802 if(!btn.toggleGroup){
805 var g = groups[btn.toggleGroup];
808 btn.un('toggle', toggleGroup);
813 * Gets the pressed button in the passed group or null
814 * @param {String} group
817 getPressed : function(group){
818 var g = groups[group];
820 for(var i = 0, len = g.length; i < len; i++){
821 if(g[i].pressed === true){
831 * @class Ext.SplitButton
832 * @extends Ext.Button
833 * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
834 * click event of the button. Typically this would be used to display a dropdown menu that provides additional
835 * options to the primary button action, but any custom handler can provide the arrowclick implementation. Example usage:
837 // display a dropdown menu:
838 new Ext.SplitButton({
839 renderTo: 'button-ct', // the container id
841 handler: optionsHandler, // handle a click on the button itself
842 menu: new Ext.menu.Menu({
844 // these items will render as dropdown menu items when the arrow is clicked:
845 {text: 'Item 1', handler: item1Handler},
846 {text: 'Item 2', handler: item2Handler}
851 // Instead of showing a menu, you provide any type of custom
852 // functionality you want when the dropdown arrow is clicked:
853 new Ext.SplitButton({
854 renderTo: 'button-ct',
856 handler: optionsHandler,
857 arrowHandler: myCustomHandler
860 * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
861 * @cfg {String} arrowTooltip The title attribute of the arrow
863 * Create a new menu button
864 * @param {Object} config The config object
867 Ext.SplitButton = Ext.extend(Ext.Button, {
869 arrowSelector : 'em',
873 initComponent : function(){
874 Ext.SplitButton.superclass.initComponent.call(this);
877 * Fires when this button's arrow is clicked
878 * @param {MenuButton} this
879 * @param {EventObject} e The click event
881 this.addEvents("arrowclick");
885 onRender : function(){
886 Ext.SplitButton.superclass.onRender.apply(this, arguments);
887 if(this.arrowTooltip){
888 this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;
893 * Sets this button's arrow click handler.
894 * @param {Function} handler The function to call when the arrow is clicked
895 * @param {Object} scope (optional) Scope for the function passed above
897 setArrowHandler : function(handler, scope){
898 this.arrowHandler = handler;
902 getMenuClass : function(){
903 return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');
906 isClickOnArrow : function(e){
907 if (this.arrowAlign != 'bottom') {
908 var visBtn = this.el.child('em.x-btn-split');
909 var right = visBtn.getRegion().right - visBtn.getPadding('r');
910 return e.getPageX() > right;
912 return e.getPageY() > this.btnEl.getRegion().bottom;
917 onClick : function(e, t){
920 if(this.isClickOnArrow(e)){
921 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
924 this.fireEvent("arrowclick", this, e);
925 if(this.arrowHandler){
926 this.arrowHandler.call(this.scope || this, this, e);
929 if(this.enableToggle){
932 this.fireEvent("click", this, e);
934 this.handler.call(this.scope || this, this, e);
941 isMenuTriggerOver : function(e){
942 return this.menu && e.target.tagName == this.arrowSelector;
946 isMenuTriggerOut : function(e, internal){
947 return this.menu && e.target.tagName != this.arrowSelector;
951 Ext.reg('splitbutton', Ext.SplitButton);/**
952 * @class Ext.CycleButton
953 * @extends Ext.SplitButton
954 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
955 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
956 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
957 * button displays the dropdown menu just like a normal SplitButton. Example usage:
959 var btn = new Ext.CycleButton({
961 prependText: 'View as ',
970 changeHandler:function(btn, item){
971 Ext.Msg.alert('Change View', item.text);
976 * Create a new split button
977 * @param {Object} config The config object
980 Ext.CycleButton = Ext.extend(Ext.SplitButton, {
982 * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
983 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
986 * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
989 * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
990 * button's text (only applies when showText = true, defaults to '')
993 * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
994 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
995 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
996 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
999 * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
1000 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
1001 * default behavior of changing the button's icon to match the selected item's icon on change.
1006 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
1010 getItemText : function(item){
1011 if(item && this.showText === true){
1013 if(this.prependText){
1014 text += this.prependText;
1023 * Sets the button's active menu item.
1024 * @param {Ext.menu.CheckItem} item The item to activate
1025 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
1027 setActiveItem : function(item, suppressEvent){
1028 if(!Ext.isObject(item)){
1029 item = this.menu.getComponent(item);
1033 this.text = this.getItemText(item);
1034 this.iconCls = item.iconCls;
1036 var t = this.getItemText(item);
1040 this.setIconClass(item.iconCls);
1042 this.activeItem = item;
1044 item.setChecked(true, false);
1047 this.setIconClass(this.forceIcon);
1050 this.fireEvent('change', this, item);
1056 * Gets the currently active menu item.
1057 * @return {Ext.menu.CheckItem} The active item
1059 getActiveItem : function(){
1060 return this.activeItem;
1064 initComponent : function(){
1068 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
1069 * is set on this CycleButton, it will be called instead on active item change and this change event will
1071 * @param {Ext.CycleButton} this
1072 * @param {Ext.menu.CheckItem} item The menu item that was selected
1077 if(this.changeHandler){
1078 this.on('change', this.changeHandler, this.scope||this);
1079 delete this.changeHandler;
1082 this.itemCount = this.items.length;
1084 this.menu = {cls:'x-cycle-menu', items:[]};
1086 Ext.each(this.items, function(item, i){
1088 group: item.group || this.id,
1090 checkHandler: this.checkHandler,
1092 checked: item.checked || false
1094 this.menu.items.push(item);
1099 Ext.CycleButton.superclass.initComponent.call(this);
1100 this.on('click', this.toggleSelected, this);
1101 this.setActiveItem(checked, true);
1105 checkHandler : function(item, pressed){
1107 this.setActiveItem(item);
1112 * This is normally called internally on button click, but can be called externally to advance the button's
1113 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
1114 * the active item will be set to the first item in the menu.
1116 toggleSelected : function(){
1119 // layout if we haven't before so the items are active
1124 var nextIdx, checkItem;
1125 for (var i = 1; i < this.itemCount; i++) {
1126 nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
1127 // check the potential item
1128 checkItem = m.items.itemAt(nextIdx);
1129 // if its not disabled then check it.
1130 if (!checkItem.disabled) {
1131 checkItem.setChecked(true);
1137 Ext.reg('cycle', Ext.CycleButton);