Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / pkgs / pkg-buttons-debug.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Button
9  * @extends Ext.BoxComponent
10  * Simple Button class
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>
18  * </ul></div>
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).
28  * @constructor
29  * Create a new button
30  * @param {Object} config The config object
31  * @xtype button
32  */
33 Ext.Button = Ext.extend(Ext.BoxComponent, {
34     /**
35      * Read-only. True if this button is hidden
36      * @type Boolean
37      */
38     hidden : false,
39     /**
40      * Read-only. True if this button is disabled
41      * @type Boolean
42      */
43     disabled : false,
44     /**
45      * Read-only. True if this button is pressed (only if enableToggle = true)
46      * @type Boolean
47      */
48     pressed : false,
49
50     /**
51      * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
52      */
53
54     /**
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.
57      */
58
59     /**
60      * @cfg {Boolean} enableToggle
61      * True to enable pressed/not pressed toggling (defaults to false)
62      */
63     enableToggle : false,
64     /**
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 if the Button, true means pressed.</div></li>
69      * </ul>
70      */
71     /**
72      * @cfg {Mixed} menu
73      * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
74      */
75     /**
76      * @cfg {String} menuAlign
77      * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
78      */
79     menuAlign : 'tl-bl?',
80
81     /**
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>.
85      */
86     /**
87      * @cfg {String} iconCls
88      * A css class which sets a background image to be used as the icon for this button
89      */
90     /**
91      * @cfg {String} type
92      * submit, reset or button - defaults to 'button'
93      */
94     type : 'button',
95
96     // private
97     menuClassTarget : 'tr:nth(2)',
98
99     /**
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>.
103      */
104     clickEvent : 'click',
105
106     /**
107      * @cfg {Boolean} handleMouseEvents
108      * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
109      */
110     handleMouseEvents : true,
111
112     /**
113      * @cfg {String} tooltipType
114      * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
115      */
116     tooltipType : 'qtip',
117
118     /**
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>
125      */
126     buttonSelector : 'button:first-child',
127
128     /**
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>
135      * </ul>
136      * <p>Defaults to <b><tt>'small'</tt></b>.</p>
137      */
138     scale : 'small',
139
140     /**
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.
144      */
145
146     /**
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>
154      * </ul>
155      * <p>Defaults to <b><tt>'left'</tt></b>.</p>
156      */
157     iconAlign : 'left',
158
159     /**
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>
166      * </ul>
167      * <p>Defaults to <b><tt>'right'</tt></b>.</p>
168      */
169     arrowAlign : 'right',
170
171     /**
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}.
176      * @type Ext.Template
177      * @property template
178      */
179     /**
180      * @cfg {String} cls
181      * A CSS class string to apply to the button's main element.
182      */
183     /**
184      * @property menu
185      * @type Menu
186      * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
187      */
188
189     initComponent : function(){
190         Ext.Button.superclass.initComponent.call(this);
191
192         this.addEvents(
193             /**
194              * @event click
195              * Fires when this button is clicked
196              * @param {Button} this
197              * @param {EventObject} e The click event
198              */
199             'click',
200             /**
201              * @event toggle
202              * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
203              * @param {Button} this
204              * @param {Boolean} pressed
205              */
206             'toggle',
207             /**
208              * @event mouseover
209              * Fires when the mouse hovers over the button
210              * @param {Button} this
211              * @param {Event} e The event object
212              */
213             'mouseover',
214             /**
215              * @event mouseout
216              * Fires when the mouse exits the button
217              * @param {Button} this
218              * @param {Event} e The event object
219              */
220             'mouseout',
221             /**
222              * @event menushow
223              * If this button has a menu, this event fires when it is shown
224              * @param {Button} this
225              * @param {Menu} menu
226              */
227             'menushow',
228             /**
229              * @event menuhide
230              * If this button has a menu, this event fires when it is hidden
231              * @param {Button} this
232              * @param {Menu} menu
233              */
234             'menuhide',
235             /**
236              * @event menutriggerover
237              * If this button has a menu, this event fires when the mouse enters the menu triggering element
238              * @param {Button} this
239              * @param {Menu} menu
240              * @param {EventObject} e
241              */
242             'menutriggerover',
243             /**
244              * @event menutriggerout
245              * If this button has a menu, this event fires when the mouse leaves the menu triggering element
246              * @param {Button} this
247              * @param {Menu} menu
248              * @param {EventObject} e
249              */
250             'menutriggerout'
251         );
252         if(this.menu){
253             this.menu = Ext.menu.MenuMgr.get(this.menu);
254         }
255         if(Ext.isString(this.toggleGroup)){
256             this.enableToggle = true;
257         }
258     },
259
260 /**
261   * <p>This method returns an object which provides substitution parameters for the {@link #template Template} used
262   * to create this Button's DOM structure.</p>
263   * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
264   * own implementation of this method.</p>
265   * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
266   * following items:</p><div class="mdetail-params"><ul>
267   * <li>The Button's {@link #text}</li>
268   * <li>The &lt;button&gt;'s {@link #type}</li>
269   * <li>The {@link iconCls} applied to the &lt;button&gt; {@link #btnEl element}</li>
270   * <li>The {@link #cls} applied to the Button's main {@link #getEl Element}</li>
271   * <li>A CSS class name controlling the Button's {@link #scale} and {@link #iconAlign icon alignment}</li>
272   * <li>A CSS class name which applies an arrow to the Button if configured with a {@link #menu}</li>
273   * </ul></div>
274   * @return {Object} Substitution data for a Template.
275  */
276     getTemplateArgs : function(){
277         var cls = (this.cls || '');
278         cls += (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
279         if(this.pressed){
280             cls += ' x-btn-pressed';
281         }
282         return [this.text || '&#160;', this.type, this.iconCls || '', cls, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass()];
283     },
284
285     // protected
286     getMenuClass : function(){
287         return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
288     },
289
290     // private
291     onRender : function(ct, position){
292         if(!this.template){
293             if(!Ext.Button.buttonTemplate){
294                 // hideous table template
295                 Ext.Button.buttonTemplate = new Ext.Template(
296                     '<table cellspacing="0" class="x-btn {3}"><tbody class="{4}">',
297                     '<tr><td class="x-btn-tl"><i>&#160;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&#160;</i></td></tr>',
298                     '<tr><td class="x-btn-ml"><i>&#160;</i></td><td class="x-btn-mc"><em class="{5}" unselectable="on"><button class="x-btn-text {2}" type="{1}">{0}</button></em></td><td class="x-btn-mr"><i>&#160;</i></td></tr>',
299                     '<tr><td class="x-btn-bl"><i>&#160;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&#160;</i></td></tr>',
300                     "</tbody></table>");
301                 Ext.Button.buttonTemplate.compile();
302             }
303             this.template = Ext.Button.buttonTemplate;
304         }
305
306         var btn, targs = this.getTemplateArgs();
307
308         if(position){
309             btn = this.template.insertBefore(position, targs, true);
310         }else{
311             btn = this.template.append(ct, targs, true);
312         }
313         /**
314          * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
315          * this references a <tt>&lt;button&gt;</tt> element. Read only.
316          * @type Ext.Element
317          * @property btnEl
318          */
319         this.btnEl = btn.child(this.buttonSelector);
320         this.mon(this.btnEl, {
321             scope: this,
322             focus: this.onFocus,
323             blur: this.onBlur
324         });
325
326         this.initButtonEl(btn, this.btnEl);
327
328         Ext.ButtonToggleMgr.register(this);
329     },
330
331     // private
332     initButtonEl : function(btn, btnEl){
333         this.el = btn;
334
335         if(this.id){
336             var d = this.el.dom,
337                 c = Ext.Element.cache;
338                 
339             delete c[d.id];
340             d.id = this.el.id = this.id;
341             c[d.id] = this.el;
342         }
343         if(this.icon){
344             btnEl.setStyle('background-image', 'url(' +this.icon +')');
345         }
346         if(this.tabIndex !== undefined){
347             btnEl.dom.tabIndex = this.tabIndex;
348         }
349         if(this.tooltip){
350             this.setTooltip(this.tooltip, true);
351         }
352
353         if(this.handleMouseEvents){
354             this.mon(btn, {
355                 scope: this,
356                 mouseover: this.onMouseOver,
357                 mousedown: this.onMouseDown
358             });
359
360             // new functionality for monitoring on the document level
361             //this.mon(btn, 'mouseout', this.onMouseOut, this);
362         }
363
364         if(this.menu){
365             this.mon(this.menu, {
366                 scope: this,
367                 show: this.onMenuShow,
368                 hide: this.onMenuHide
369             });
370         }
371
372         if(this.repeat){
373             var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
374             this.mon(repeater, 'click', this.onClick, this);
375         }
376         this.mon(btn, this.clickEvent, this.onClick, this);
377     },
378
379     // private
380     afterRender : function(){
381         Ext.Button.superclass.afterRender.call(this);
382         this.doAutoWidth();
383     },
384
385     /**
386      * Sets the CSS class that provides a background image to use as the button's icon.  This method also changes
387      * the value of the {@link iconCls} config internally.
388      * @param {String} cls The CSS class providing the icon image
389      * @return {Ext.Button} this
390      */
391     setIconClass : function(cls){
392         if(this.el){
393             this.btnEl.replaceClass(this.iconCls, cls);
394         }
395         this.iconCls = cls;
396         return this;
397     },
398
399     /**
400      * Sets the tooltip for this Button.
401      * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
402      * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
403      * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
404      * </ul></div>
405      * @return {Ext.Button} this
406      */
407     setTooltip : function(tooltip, /* private */ initial){
408         if(this.rendered){
409             if(!initial){
410                 this.clearTip();
411             }
412             if(Ext.isObject(tooltip)){
413                 Ext.QuickTips.register(Ext.apply({
414                       target: this.btnEl.id
415                 }, tooltip));
416                 this.tooltip = tooltip;
417             }else{
418                 this.btnEl.dom[this.tooltipType] = tooltip;
419             }
420         }else{
421             this.tooltip = tooltip;
422         }
423         return this;
424     },
425
426     // private
427     clearTip : function(){
428         if(Ext.isObject(this.tooltip)){
429             Ext.QuickTips.unregister(this.btnEl);
430         }
431     },
432
433     // private
434     beforeDestroy : function(){
435         if(this.rendered){
436             this.clearTip();
437         }
438         Ext.destroy(this.menu, this.repeater);
439     },
440
441     // private
442     onDestroy : function(){
443         var doc = Ext.getDoc();
444         doc.un('mouseover', this.monitorMouseOver, this);
445         doc.un('mouseup', this.onMouseUp, this);
446         if(this.rendered){
447             Ext.ButtonToggleMgr.unregister(this);
448         }
449     },
450
451     // private
452     doAutoWidth : function(){
453         if(this.el && this.text && this.width === undefined){
454             this.el.setWidth('auto');
455             if(Ext.isIE7 && Ext.isStrict){
456                 var ib = this.btnEl;
457                 if(ib && ib.getWidth() > 20){
458                     ib.clip();
459                     ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
460                 }
461             }
462             if(this.minWidth){
463                 if(this.el.getWidth() < this.minWidth){
464                     this.el.setWidth(this.minWidth);
465                 }
466             }
467         }
468     },
469
470     /**
471      * Assigns this Button's click handler
472      * @param {Function} handler The function to call when the button is clicked
473      * @param {Object} scope (optional) Scope for the function passed in. Defaults to this Button.
474      * @return {Ext.Button} this
475      */
476     setHandler : function(handler, scope){
477         this.handler = handler;
478         this.scope = scope;
479         return this;
480     },
481
482     /**
483      * Sets this Button's text
484      * @param {String} text The button text
485      * @return {Ext.Button} this
486      */
487     setText : function(text){
488         this.text = text;
489         if(this.el){
490             this.el.child('td.x-btn-mc ' + this.buttonSelector).update(text);
491         }
492         this.doAutoWidth();
493         return this;
494     },
495
496     /**
497      * Gets the text for this Button
498      * @return {String} The button text
499      */
500     getText : function(){
501         return this.text;
502     },
503
504     /**
505      * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
506      * @param {Boolean} state (optional) Force a particular state
507      * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
508      * @return {Ext.Button} this
509      */
510     toggle : function(state, suppressEvent){
511         state = state === undefined ? !this.pressed : !!state;
512         if(state != this.pressed){
513             if(this.rendered){
514                 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
515             }
516             this.pressed = state;
517             if(!suppressEvent){
518                 this.fireEvent('toggle', this, state);
519                 if(this.toggleHandler){
520                     this.toggleHandler.call(this.scope || this, this, state);
521                 }
522             }
523         }
524         return this;
525     },
526
527     /**
528      * Focus the button
529      */
530     focus : function(){
531         this.btnEl.focus();
532     },
533
534     // private
535     onDisable : function(){
536         this.onDisableChange(true);
537     },
538
539     // private
540     onEnable : function(){
541         this.onDisableChange(false);
542     },
543
544     onDisableChange : function(disabled){
545         if(this.el){
546             if(!Ext.isIE6 || !this.text){
547                 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
548             }
549             this.el.dom.disabled = disabled;
550         }
551         this.disabled = disabled;
552     },
553
554     /**
555      * Show this button's menu (if it has one)
556      */
557     showMenu : function(){
558         if(this.rendered && this.menu){
559             if(this.tooltip){
560                 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
561             }
562             this.menu.show(this.el, this.menuAlign);
563         }
564         return this;
565     },
566
567     /**
568      * Hide this button's menu (if it has one)
569      */
570     hideMenu : function(){
571         if(this.menu){
572             this.menu.hide();
573         }
574         return this;
575     },
576
577     /**
578      * Returns true if the button has a menu and it is visible
579      * @return {Boolean}
580      */
581     hasVisibleMenu : function(){
582         return this.menu && this.menu.isVisible();
583     },
584
585     // private
586     onClick : function(e){
587         if(e){
588             e.preventDefault();
589         }
590         if(e.button !== 0){
591             return;
592         }
593         if(!this.disabled){
594             if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
595                 this.toggle();
596             }
597             if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
598                 this.showMenu();
599             }
600             this.fireEvent('click', this, e);
601             if(this.handler){
602                 //this.el.removeClass('x-btn-over');
603                 this.handler.call(this.scope || this, this, e);
604             }
605         }
606     },
607
608     // private
609     isMenuTriggerOver : function(e, internal){
610         return this.menu && !internal;
611     },
612
613     // private
614     isMenuTriggerOut : function(e, internal){
615         return this.menu && !internal;
616     },
617
618     // private
619     onMouseOver : function(e){
620         if(!this.disabled){
621             var internal = e.within(this.el,  true);
622             if(!internal){
623                 this.el.addClass('x-btn-over');
624                 if(!this.monitoringMouseOver){
625                     Ext.getDoc().on('mouseover', this.monitorMouseOver, this);
626                     this.monitoringMouseOver = true;
627                 }
628                 this.fireEvent('mouseover', this, e);
629             }
630             if(this.isMenuTriggerOver(e, internal)){
631                 this.fireEvent('menutriggerover', this, this.menu, e);
632             }
633         }
634     },
635
636     // private
637     monitorMouseOver : function(e){
638         if(e.target != this.el.dom && !e.within(this.el)){
639             if(this.monitoringMouseOver){
640                 Ext.getDoc().un('mouseover', this.monitorMouseOver, this);
641                 this.monitoringMouseOver = false;
642             }
643             this.onMouseOut(e);
644         }
645     },
646
647     // private
648     onMouseOut : function(e){
649         var internal = e.within(this.el) && e.target != this.el.dom;
650         this.el.removeClass('x-btn-over');
651         this.fireEvent('mouseout', this, e);
652         if(this.isMenuTriggerOut(e, internal)){
653             this.fireEvent('menutriggerout', this, this.menu, e);
654         }
655     },
656     // private
657     onFocus : function(e){
658         if(!this.disabled){
659             this.el.addClass('x-btn-focus');
660         }
661     },
662     // private
663     onBlur : function(e){
664         this.el.removeClass('x-btn-focus');
665     },
666
667     // private
668     getClickEl : function(e, isUp){
669        return this.el;
670     },
671
672     // private
673     onMouseDown : function(e){
674         if(!this.disabled && e.button === 0){
675             this.getClickEl(e).addClass('x-btn-click');
676             Ext.getDoc().on('mouseup', this.onMouseUp, this);
677         }
678     },
679     // private
680     onMouseUp : function(e){
681         if(e.button === 0){
682             this.getClickEl(e, true).removeClass('x-btn-click');
683             Ext.getDoc().un('mouseup', this.onMouseUp, this);
684         }
685     },
686     // private
687     onMenuShow : function(e){
688         this.ignoreNextClick = 0;
689         this.el.addClass('x-btn-menu-active');
690         this.fireEvent('menushow', this, this.menu);
691     },
692     // private
693     onMenuHide : function(e){
694         this.el.removeClass('x-btn-menu-active');
695         this.ignoreNextClick = this.restoreClick.defer(250, this);
696         this.fireEvent('menuhide', this, this.menu);
697     },
698
699     // private
700     restoreClick : function(){
701         this.ignoreNextClick = 0;
702     }
703
704
705
706     /**
707      * @cfg {String} autoEl @hide
708      */
709 });
710 Ext.reg('button', Ext.Button);
711
712 // Private utility class used by Button
713 Ext.ButtonToggleMgr = function(){
714    var groups = {};
715
716    function toggleGroup(btn, state){
717        if(state){
718            var g = groups[btn.toggleGroup];
719            for(var i = 0, l = g.length; i < l; i++){
720                if(g[i] != btn){
721                    g[i].toggle(false);
722                }
723            }
724        }
725    }
726
727    return {
728        register : function(btn){
729            if(!btn.toggleGroup){
730                return;
731            }
732            var g = groups[btn.toggleGroup];
733            if(!g){
734                g = groups[btn.toggleGroup] = [];
735            }
736            g.push(btn);
737            btn.on('toggle', toggleGroup);
738        },
739
740        unregister : function(btn){
741            if(!btn.toggleGroup){
742                return;
743            }
744            var g = groups[btn.toggleGroup];
745            if(g){
746                g.remove(btn);
747                btn.un('toggle', toggleGroup);
748            }
749        },
750
751        /**
752         * Gets the pressed button in the passed group or null
753         * @param {String} group
754         * @return Button
755         */
756        getPressed : function(group){
757            var g = groups[group];
758            if(g){
759                for(var i = 0, len = g.length; i < len; i++){
760                    if(g[i].pressed === true){
761                        return g[i];
762                    }
763                }
764            }
765            return null;
766        }
767    };
768 }();/**\r
769  * @class Ext.SplitButton\r
770  * @extends Ext.Button\r
771  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default\r
772  * click event of the button.  Typically this would be used to display a dropdown menu that provides additional\r
773  * options to the primary button action, but any custom handler can provide the arrowclick implementation.  Example usage:\r
774  * <pre><code>\r
775 // display a dropdown menu:\r
776 new Ext.SplitButton({\r
777         renderTo: 'button-ct', // the container id\r
778         text: 'Options',\r
779         handler: optionsHandler, // handle a click on the button itself\r
780         menu: new Ext.menu.Menu({\r
781         items: [\r
782                 // these items will render as dropdown menu items when the arrow is clicked:\r
783                 {text: 'Item 1', handler: item1Handler},\r
784                 {text: 'Item 2', handler: item2Handler}\r
785         ]\r
786         })\r
787 });\r
788 \r
789 // Instead of showing a menu, you provide any type of custom\r
790 // functionality you want when the dropdown arrow is clicked:\r
791 new Ext.SplitButton({\r
792         renderTo: 'button-ct',\r
793         text: 'Options',\r
794         handler: optionsHandler,\r
795         arrowHandler: myCustomHandler\r
796 });\r
797 </code></pre>\r
798  * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)\r
799  * @cfg {String} arrowTooltip The title attribute of the arrow\r
800  * @constructor\r
801  * Create a new menu button\r
802  * @param {Object} config The config object\r
803  * @xtype splitbutton\r
804  */\r
805 Ext.SplitButton = Ext.extend(Ext.Button, {\r
806         // private\r
807     arrowSelector : 'em',\r
808     split: true,\r
809 \r
810     // private\r
811     initComponent : function(){\r
812         Ext.SplitButton.superclass.initComponent.call(this);\r
813         /**\r
814          * @event arrowclick\r
815          * Fires when this button's arrow is clicked\r
816          * @param {MenuButton} this\r
817          * @param {EventObject} e The click event\r
818          */\r
819         this.addEvents("arrowclick");\r
820     },\r
821 \r
822     // private\r
823     onRender : function(){\r
824         Ext.SplitButton.superclass.onRender.apply(this, arguments);\r
825         if(this.arrowTooltip){\r
826             this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;\r
827         }\r
828     },\r
829 \r
830     /**\r
831      * Sets this button's arrow click handler.\r
832      * @param {Function} handler The function to call when the arrow is clicked\r
833      * @param {Object} scope (optional) Scope for the function passed above\r
834      */\r
835     setArrowHandler : function(handler, scope){\r
836         this.arrowHandler = handler;\r
837         this.scope = scope;\r
838     },\r
839 \r
840     getMenuClass : function(){\r
841         return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');\r
842     },\r
843 \r
844     isClickOnArrow : function(e){\r
845         return this.arrowAlign != 'bottom' ?\r
846                e.getPageX() > this.el.child(this.buttonSelector).getRegion().right :\r
847                e.getPageY() > this.el.child(this.buttonSelector).getRegion().bottom;\r
848     },\r
849 \r
850     // private\r
851     onClick : function(e, t){\r
852         e.preventDefault();\r
853         if(!this.disabled){\r
854             if(this.isClickOnArrow(e)){\r
855                 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){\r
856                     this.showMenu();\r
857                 }\r
858                 this.fireEvent("arrowclick", this, e);\r
859                 if(this.arrowHandler){\r
860                     this.arrowHandler.call(this.scope || this, this, e);\r
861                 }\r
862             }else{\r
863                 if(this.enableToggle){\r
864                     this.toggle();\r
865                 }\r
866                 this.fireEvent("click", this, e);\r
867                 if(this.handler){\r
868                     this.handler.call(this.scope || this, this, e);\r
869                 }\r
870             }\r
871         }\r
872     },\r
873 \r
874     // private\r
875     isMenuTriggerOver : function(e){\r
876         return this.menu && e.target.tagName == 'em';\r
877     },\r
878 \r
879     // private\r
880     isMenuTriggerOut : function(e, internal){\r
881         return this.menu && e.target.tagName != 'em';\r
882     }\r
883 });\r
884 \r
885 Ext.reg('splitbutton', Ext.SplitButton);/**\r
886  * @class Ext.CycleButton\r
887  * @extends Ext.SplitButton\r
888  * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements.  The button automatically\r
889  * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's\r
890  * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the\r
891  * button displays the dropdown menu just like a normal SplitButton.  Example usage:\r
892  * <pre><code>\r
893 var btn = new Ext.CycleButton({\r
894     showText: true,\r
895     prependText: 'View as ',\r
896     items: [{\r
897         text:'text only',\r
898         iconCls:'view-text',\r
899         checked:true\r
900     },{\r
901         text:'HTML',\r
902         iconCls:'view-html'\r
903     }],\r
904     changeHandler:function(btn, item){\r
905         Ext.Msg.alert('Change View', item.text);\r
906     }\r
907 });\r
908 </code></pre>\r
909  * @constructor\r
910  * Create a new split button\r
911  * @param {Object} config The config object\r
912  * @xtype cycle\r
913  */\r
914 Ext.CycleButton = Ext.extend(Ext.SplitButton, {\r
915     /**\r
916      * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the\r
917      * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})\r
918      */\r
919     /**\r
920      * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)\r
921      */\r
922     /**\r
923      * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the\r
924      * button's text (only applies when showText = true, defaults to '')\r
925      */\r
926     /**\r
927      * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu\r
928      * item in the button's menu has changed.  If this callback is not supplied, the SplitButton will instead\r
929      * fire the {@link #change} event on active item change.  The changeHandler function will be called with the\r
930      * following argument list: (SplitButton this, Ext.menu.CheckItem item)\r
931      */\r
932     /**\r
933      * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button.  This\r
934      * icon will always be displayed regardless of which item is selected in the dropdown list.  This overrides the \r
935      * default behavior of changing the button's icon to match the selected item's icon on change.\r
936      */\r
937     /**\r
938      * @property menu\r
939      * @type Menu\r
940      * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.\r
941      */\r
942 \r
943     // private\r
944     getItemText : function(item){\r
945         if(item && this.showText === true){\r
946             var text = '';\r
947             if(this.prependText){\r
948                 text += this.prependText;\r
949             }\r
950             text += item.text;\r
951             return text;\r
952         }\r
953         return undefined;\r
954     },\r
955 \r
956     /**\r
957      * Sets the button's active menu item.\r
958      * @param {Ext.menu.CheckItem} item The item to activate\r
959      * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)\r
960      */\r
961     setActiveItem : function(item, suppressEvent){\r
962         if(!Ext.isObject(item)){\r
963             item = this.menu.getComponent(item);\r
964         }\r
965         if(item){\r
966             if(!this.rendered){\r
967                 this.text = this.getItemText(item);\r
968                 this.iconCls = item.iconCls;\r
969             }else{\r
970                 var t = this.getItemText(item);\r
971                 if(t){\r
972                     this.setText(t);\r
973                 }\r
974                 this.setIconClass(item.iconCls);\r
975             }\r
976             this.activeItem = item;\r
977             if(!item.checked){\r
978                 item.setChecked(true, true);\r
979             }\r
980             if(this.forceIcon){\r
981                 this.setIconClass(this.forceIcon);\r
982             }\r
983             if(!suppressEvent){\r
984                 this.fireEvent('change', this, item);\r
985             }\r
986         }\r
987     },\r
988 \r
989     /**\r
990      * Gets the currently active menu item.\r
991      * @return {Ext.menu.CheckItem} The active item\r
992      */\r
993     getActiveItem : function(){\r
994         return this.activeItem;\r
995     },\r
996 \r
997     // private\r
998     initComponent : function(){\r
999         this.addEvents(\r
1000             /**\r
1001              * @event change\r
1002              * Fires after the button's active menu item has changed.  Note that if a {@link #changeHandler} function\r
1003              * is set on this CycleButton, it will be called instead on active item change and this change event will\r
1004              * not be fired.\r
1005              * @param {Ext.CycleButton} this\r
1006              * @param {Ext.menu.CheckItem} item The menu item that was selected\r
1007              */\r
1008             "change"\r
1009         );\r
1010 \r
1011         if(this.changeHandler){\r
1012             this.on('change', this.changeHandler, this.scope||this);\r
1013             delete this.changeHandler;\r
1014         }\r
1015 \r
1016         this.itemCount = this.items.length;\r
1017 \r
1018         this.menu = {cls:'x-cycle-menu', items:[]};\r
1019         var checked;\r
1020         Ext.each(this.items, function(item, i){\r
1021             Ext.apply(item, {\r
1022                 group: item.group || this.id,\r
1023                 itemIndex: i,\r
1024                 checkHandler: this.checkHandler,\r
1025                 scope: this,\r
1026                 checked: item.checked || false\r
1027             });\r
1028             this.menu.items.push(item);\r
1029             if(item.checked){\r
1030                 checked = item;\r
1031             }\r
1032         }, this);\r
1033         this.setActiveItem(checked, true);\r
1034         Ext.CycleButton.superclass.initComponent.call(this);\r
1035 \r
1036         this.on('click', this.toggleSelected, this);\r
1037     },\r
1038 \r
1039     // private\r
1040     checkHandler : function(item, pressed){\r
1041         if(pressed){\r
1042             this.setActiveItem(item);\r
1043         }\r
1044     },\r
1045 \r
1046     /**\r
1047      * This is normally called internally on button click, but can be called externally to advance the button's\r
1048      * active item programmatically to the next one in the menu.  If the current item is the last one in the menu\r
1049      * the active item will be set to the first item in the menu.\r
1050      */\r
1051     toggleSelected : function(){\r
1052         var m = this.menu;\r
1053         m.render();\r
1054         // layout if we haven't before so the items are active\r
1055         if(!m.hasLayout){\r
1056             m.doLayout();\r
1057         }\r
1058         \r
1059         var nextIdx, checkItem;\r
1060         for (var i = 1; i < this.itemCount; i++) {\r
1061             nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;\r
1062             // check the potential item\r
1063             checkItem = m.items.itemAt(nextIdx);\r
1064             // if its not disabled then check it.\r
1065             if (!checkItem.disabled) {\r
1066                 checkItem.setChecked(true);\r
1067                 break;\r
1068             }\r
1069         }\r
1070     }\r
1071 });\r
1072 Ext.reg('cycle', Ext.CycleButton);