Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / pkgs / pkg-buttons-debug.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
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 of 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 Array 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 &lt;button&gt;'s {@link #type}</li>
268   * <li>A CSS class name applied to the Button's main &lt;tbody&gt; element which determines the button's scale and icon alignment.</li>
269   * <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>
270   * <li>The {@link #cls} CSS class name applied to the button's wrapping &lt;table&gt; element.</li>
271   * <li>The Component id which is applied to the button's wrapping &lt;table&gt; element.</li>
272   * </ul></div>
273   * @return {Array} Substitution data for a Template.
274  */
275     getTemplateArgs : function(){
276         return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
277     },
278
279     // private
280     setButtonClass : function(){
281         if(this.useSetClass){
282             if(!Ext.isEmpty(this.oldCls)){
283                 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
284             }
285             this.oldCls = (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
286             this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
287         }
288     },
289
290     // protected
291     getMenuClass : function(){
292         return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
293     },
294
295     // private
296     onRender : function(ct, position){
297         if(!this.template){
298             if(!Ext.Button.buttonTemplate){
299                 // hideous table template
300                 Ext.Button.buttonTemplate = new Ext.Template(
301                     '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
302                     '<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>',
303                     '<tr><td class="x-btn-ml"><i>&#160;</i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i>&#160;</i></td></tr>',
304                     '<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>',
305                     '</tbody></table>');
306                 Ext.Button.buttonTemplate.compile();
307             }
308             this.template = Ext.Button.buttonTemplate;
309         }
310
311         var btn, targs = this.getTemplateArgs();
312
313         if(position){
314             btn = this.template.insertBefore(position, targs, true);
315         }else{
316             btn = this.template.append(ct, targs, true);
317         }
318         /**
319          * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
320          * this references a <tt>&lt;button&gt;</tt> element. Read only.
321          * @type Ext.Element
322          * @property btnEl
323          */
324         this.btnEl = btn.child(this.buttonSelector);
325         this.mon(this.btnEl, {
326             scope: this,
327             focus: this.onFocus,
328             blur: this.onBlur
329         });
330
331         this.initButtonEl(btn, this.btnEl);
332
333         Ext.ButtonToggleMgr.register(this);
334     },
335
336     // private
337     initButtonEl : function(btn, btnEl){
338         this.el = btn;
339         this.setIcon(this.icon);
340         this.setText(this.text);
341         this.setIconClass(this.iconCls);
342         if(Ext.isDefined(this.tabIndex)){
343             btnEl.dom.tabIndex = this.tabIndex;
344         }
345         if(this.tooltip){
346             this.setTooltip(this.tooltip, true);
347         }
348
349         if(this.handleMouseEvents){
350             this.mon(btn, {
351                 scope: this,
352                 mouseover: this.onMouseOver,
353                 mousedown: this.onMouseDown
354             });
355
356             // new functionality for monitoring on the document level
357             //this.mon(btn, 'mouseout', this.onMouseOut, this);
358         }
359
360         if(this.menu){
361             this.mon(this.menu, {
362                 scope: this,
363                 show: this.onMenuShow,
364                 hide: this.onMenuHide
365             });
366         }
367
368         if(this.repeat){
369             var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
370             this.mon(repeater, 'click', this.onClick, this);
371         }
372         this.mon(btn, this.clickEvent, this.onClick, this);
373     },
374
375     // private
376     afterRender : function(){
377         Ext.Button.superclass.afterRender.call(this);
378         this.useSetClass = true;
379         this.setButtonClass();
380         this.doc = Ext.getDoc();
381         this.doAutoWidth();
382     },
383
384     /**
385      * Sets the CSS class that provides a background image to use as the button's icon.  This method also changes
386      * the value of the {@link iconCls} config internally.
387      * @param {String} cls The CSS class providing the icon image
388      * @return {Ext.Button} this
389      */
390     setIconClass : function(cls){
391         this.iconCls = cls;
392         if(this.el){
393             this.btnEl.dom.className = '';
394             this.btnEl.addClass(['x-btn-text', cls || '']);
395             this.setButtonClass();
396         }
397         return this;
398     },
399
400     /**
401      * Sets the tooltip for this Button.
402      * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
403      * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
404      * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
405      * </ul></div>
406      * @return {Ext.Button} this
407      */
408     setTooltip : function(tooltip, /* private */ initial){
409         if(this.rendered){
410             if(!initial){
411                 this.clearTip();
412             }
413             if(Ext.isObject(tooltip)){
414                 Ext.QuickTips.register(Ext.apply({
415                       target: this.btnEl.id
416                 }, tooltip));
417                 this.tooltip = tooltip;
418             }else{
419                 this.btnEl.dom[this.tooltipType] = tooltip;
420             }
421         }else{
422             this.tooltip = tooltip;
423         }
424         return this;
425     },
426
427     // private
428     clearTip : function(){
429         if(Ext.isObject(this.tooltip)){
430             Ext.QuickTips.unregister(this.btnEl);
431         }
432     },
433
434     // private
435     beforeDestroy : function(){
436         if(this.rendered){
437             this.clearTip();
438         }
439         if(this.menu && this.destroyMenu !== false) {
440             Ext.destroy(this.menu);
441         }
442         Ext.destroy(this.repeater);
443     },
444
445     // private
446     onDestroy : function(){
447         if(this.rendered){
448             this.doc.un('mouseover', this.monitorMouseOver, this);
449             this.doc.un('mouseup', this.onMouseUp, this);
450             delete this.doc;
451             delete this.btnEl;
452             Ext.ButtonToggleMgr.unregister(this);
453         }
454         Ext.Button.superclass.onDestroy.call(this);
455     },
456
457     // private
458     doAutoWidth : function(){
459         if(this.el && this.text && this.width === undefined){
460             this.el.setWidth('auto');
461             if(Ext.isIE7 && Ext.isStrict){
462                 var ib = this.btnEl;
463                 if(ib && ib.getWidth() > 20){
464                     ib.clip();
465                     ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
466                 }
467             }
468             if(this.minWidth){
469                 if(this.el.getWidth() < this.minWidth){
470                     this.el.setWidth(this.minWidth);
471                 }
472             }
473         }
474     },
475
476     /**
477      * Assigns this Button's click handler
478      * @param {Function} handler The function to call when the button is clicked
479      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
480      * Defaults to this Button.
481      * @return {Ext.Button} this
482      */
483     setHandler : function(handler, scope){
484         this.handler = handler;
485         this.scope = scope;
486         return this;
487     },
488
489     /**
490      * Sets this Button's text
491      * @param {String} text The button text
492      * @return {Ext.Button} this
493      */
494     setText : function(text){
495         this.text = text;
496         if(this.el){
497             this.btnEl.update(text || '&#160;');
498             this.setButtonClass();
499         }
500         this.doAutoWidth();
501         return this;
502     },
503
504     /**
505      * Sets the background image (inline style) of the button.  This method also changes
506      * the value of the {@link icon} config internally.
507      * @param {String} icon The path to an image to display in the button
508      * @return {Ext.Button} this
509      */
510     setIcon : function(icon){
511         this.icon = icon;
512         if(this.el){
513             this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
514             this.setButtonClass();
515         }
516         return this;
517     },
518
519     /**
520      * Gets the text for this Button
521      * @return {String} The button text
522      */
523     getText : function(){
524         return this.text;
525     },
526
527     /**
528      * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
529      * @param {Boolean} state (optional) Force a particular state
530      * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
531      * @return {Ext.Button} this
532      */
533     toggle : function(state, suppressEvent){
534         state = state === undefined ? !this.pressed : !!state;
535         if(state != this.pressed){
536             if(this.rendered){
537                 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
538             }
539             this.pressed = state;
540             if(!suppressEvent){
541                 this.fireEvent('toggle', this, state);
542                 if(this.toggleHandler){
543                     this.toggleHandler.call(this.scope || this, this, state);
544                 }
545             }
546         }
547         return this;
548     },
549
550     // private
551     onDisable : function(){
552         this.onDisableChange(true);
553     },
554
555     // private
556     onEnable : function(){
557         this.onDisableChange(false);
558     },
559
560     onDisableChange : function(disabled){
561         if(this.el){
562             if(!Ext.isIE6 || !this.text){
563                 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
564             }
565             this.el.dom.disabled = disabled;
566         }
567         this.disabled = disabled;
568     },
569
570     /**
571      * Show this button's menu (if it has one)
572      */
573     showMenu : function(){
574         if(this.rendered && this.menu){
575             if(this.tooltip){
576                 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
577             }
578             if(this.menu.isVisible()){
579                 this.menu.hide();
580             }
581             this.menu.ownerCt = this;
582             this.menu.show(this.el, this.menuAlign);
583         }
584         return this;
585     },
586
587     /**
588      * Hide this button's menu (if it has one)
589      */
590     hideMenu : function(){
591         if(this.hasVisibleMenu()){
592             this.menu.hide();
593         }
594         return this;
595     },
596
597     /**
598      * Returns true if the button has a menu and it is visible
599      * @return {Boolean}
600      */
601     hasVisibleMenu : function(){
602         return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
603     },
604
605     // private
606     onClick : function(e){
607         if(e){
608             e.preventDefault();
609         }
610         if(e.button !== 0){
611             return;
612         }
613         if(!this.disabled){
614             if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
615                 this.toggle();
616             }
617             if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
618                 this.showMenu();
619             }
620             this.fireEvent('click', this, e);
621             if(this.handler){
622                 //this.el.removeClass('x-btn-over');
623                 this.handler.call(this.scope || this, this, e);
624             }
625         }
626     },
627
628     // private
629     isMenuTriggerOver : function(e, internal){
630         return this.menu && !internal;
631     },
632
633     // private
634     isMenuTriggerOut : function(e, internal){
635         return this.menu && !internal;
636     },
637
638     // private
639     onMouseOver : function(e){
640         if(!this.disabled){
641             var internal = e.within(this.el,  true);
642             if(!internal){
643                 this.el.addClass('x-btn-over');
644                 if(!this.monitoringMouseOver){
645                     this.doc.on('mouseover', this.monitorMouseOver, this);
646                     this.monitoringMouseOver = true;
647                 }
648                 this.fireEvent('mouseover', this, e);
649             }
650             if(this.isMenuTriggerOver(e, internal)){
651                 this.fireEvent('menutriggerover', this, this.menu, e);
652             }
653         }
654     },
655
656     // private
657     monitorMouseOver : function(e){
658         if(e.target != this.el.dom && !e.within(this.el)){
659             if(this.monitoringMouseOver){
660                 this.doc.un('mouseover', this.monitorMouseOver, this);
661                 this.monitoringMouseOver = false;
662             }
663             this.onMouseOut(e);
664         }
665     },
666
667     // private
668     onMouseOut : function(e){
669         var internal = e.within(this.el) && e.target != this.el.dom;
670         this.el.removeClass('x-btn-over');
671         this.fireEvent('mouseout', this, e);
672         if(this.isMenuTriggerOut(e, internal)){
673             this.fireEvent('menutriggerout', this, this.menu, e);
674         }
675     },
676
677     focus : function() {
678         this.btnEl.focus();
679     },
680
681     blur : function() {
682         this.btnEl.blur();
683     },
684
685     // private
686     onFocus : function(e){
687         if(!this.disabled){
688             this.el.addClass('x-btn-focus');
689         }
690     },
691     // private
692     onBlur : function(e){
693         this.el.removeClass('x-btn-focus');
694     },
695
696     // private
697     getClickEl : function(e, isUp){
698        return this.el;
699     },
700
701     // private
702     onMouseDown : function(e){
703         if(!this.disabled && e.button === 0){
704             this.getClickEl(e).addClass('x-btn-click');
705             this.doc.on('mouseup', this.onMouseUp, this);
706         }
707     },
708     // private
709     onMouseUp : function(e){
710         if(e.button === 0){
711             this.getClickEl(e, true).removeClass('x-btn-click');
712             this.doc.un('mouseup', this.onMouseUp, this);
713         }
714     },
715     // private
716     onMenuShow : function(e){
717         if(this.menu.ownerCt == this){
718             this.menu.ownerCt = this;
719             this.ignoreNextClick = 0;
720             this.el.addClass('x-btn-menu-active');
721             this.fireEvent('menushow', this, this.menu);
722         }
723     },
724     // private
725     onMenuHide : function(e){
726         if(this.menu.ownerCt == this){
727             this.el.removeClass('x-btn-menu-active');
728             this.ignoreNextClick = this.restoreClick.defer(250, this);
729             this.fireEvent('menuhide', this, this.menu);
730             delete this.menu.ownerCt;
731         }
732     },
733
734     // private
735     restoreClick : function(){
736         this.ignoreNextClick = 0;
737     }
738
739     /**
740      * @cfg {String} autoEl @hide
741      */
742     /**
743      * @cfg {String/Object} html @hide
744      */
745     /**
746      * @cfg {String} contentEl  @hide
747      */
748     /**
749      * @cfg {Mixed} data  @hide
750      */
751     /**
752      * @cfg {Mixed} tpl  @hide
753      */
754     /**
755      * @cfg {String} tplWriteMode  @hide
756      */
757 });
758 Ext.reg('button', Ext.Button);
759
760 // Private utility class used by Button
761 Ext.ButtonToggleMgr = function(){
762    var groups = {};
763
764    function toggleGroup(btn, state){
765        if(state){
766            var g = groups[btn.toggleGroup];
767            for(var i = 0, l = g.length; i < l; i++){
768                if(g[i] != btn){
769                    g[i].toggle(false);
770                }
771            }
772        }
773    }
774
775    return {
776        register : function(btn){
777            if(!btn.toggleGroup){
778                return;
779            }
780            var g = groups[btn.toggleGroup];
781            if(!g){
782                g = groups[btn.toggleGroup] = [];
783            }
784            g.push(btn);
785            btn.on('toggle', toggleGroup);
786        },
787
788        unregister : function(btn){
789            if(!btn.toggleGroup){
790                return;
791            }
792            var g = groups[btn.toggleGroup];
793            if(g){
794                g.remove(btn);
795                btn.un('toggle', toggleGroup);
796            }
797        },
798
799        /**
800         * Gets the pressed button in the passed group or null
801         * @param {String} group
802         * @return Button
803         */
804        getPressed : function(group){
805            var g = groups[group];
806            if(g){
807                for(var i = 0, len = g.length; i < len; i++){
808                    if(g[i].pressed === true){
809                        return g[i];
810                    }
811                }
812            }
813            return null;
814        }
815    };
816 }();
817 /**
818  * @class Ext.SplitButton
819  * @extends Ext.Button
820  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
821  * click event of the button.  Typically this would be used to display a dropdown menu that provides additional
822  * options to the primary button action, but any custom handler can provide the arrowclick implementation.  Example usage:
823  * <pre><code>
824 // display a dropdown menu:
825 new Ext.SplitButton({
826         renderTo: 'button-ct', // the container id
827         text: 'Options',
828         handler: optionsHandler, // handle a click on the button itself
829         menu: new Ext.menu.Menu({
830         items: [
831                 // these items will render as dropdown menu items when the arrow is clicked:
832                 {text: 'Item 1', handler: item1Handler},
833                 {text: 'Item 2', handler: item2Handler}
834         ]
835         })
836 });
837
838 // Instead of showing a menu, you provide any type of custom
839 // functionality you want when the dropdown arrow is clicked:
840 new Ext.SplitButton({
841         renderTo: 'button-ct',
842         text: 'Options',
843         handler: optionsHandler,
844         arrowHandler: myCustomHandler
845 });
846 </code></pre>
847  * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
848  * @cfg {String} arrowTooltip The title attribute of the arrow
849  * @constructor
850  * Create a new menu button
851  * @param {Object} config The config object
852  * @xtype splitbutton
853  */
854 Ext.SplitButton = Ext.extend(Ext.Button, {
855         // private
856     arrowSelector : 'em',
857     split: true,
858
859     // private
860     initComponent : function(){
861         Ext.SplitButton.superclass.initComponent.call(this);
862         /**
863          * @event arrowclick
864          * Fires when this button's arrow is clicked
865          * @param {MenuButton} this
866          * @param {EventObject} e The click event
867          */
868         this.addEvents("arrowclick");
869     },
870
871     // private
872     onRender : function(){
873         Ext.SplitButton.superclass.onRender.apply(this, arguments);
874         if(this.arrowTooltip){
875             this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;
876         }
877     },
878
879     /**
880      * Sets this button's arrow click handler.
881      * @param {Function} handler The function to call when the arrow is clicked
882      * @param {Object} scope (optional) Scope for the function passed above
883      */
884     setArrowHandler : function(handler, scope){
885         this.arrowHandler = handler;
886         this.scope = scope;
887     },
888
889     getMenuClass : function(){
890         return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');
891     },
892
893     isClickOnArrow : function(e){
894         if (this.arrowAlign != 'bottom') {
895             var visBtn = this.el.child('em.x-btn-split');
896             var right = visBtn.getRegion().right - visBtn.getPadding('r');
897             return e.getPageX() > right;
898         } else {
899             return e.getPageY() > this.btnEl.getRegion().bottom;
900         }
901     },
902
903     // private
904     onClick : function(e, t){
905         e.preventDefault();
906         if(!this.disabled){
907             if(this.isClickOnArrow(e)){
908                 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
909                     this.showMenu();
910                 }
911                 this.fireEvent("arrowclick", this, e);
912                 if(this.arrowHandler){
913                     this.arrowHandler.call(this.scope || this, this, e);
914                 }
915             }else{
916                 if(this.enableToggle){
917                     this.toggle();
918                 }
919                 this.fireEvent("click", this, e);
920                 if(this.handler){
921                     this.handler.call(this.scope || this, this, e);
922                 }
923             }
924         }
925     },
926
927     // private
928     isMenuTriggerOver : function(e){
929         return this.menu && e.target.tagName == this.arrowSelector;
930     },
931
932     // private
933     isMenuTriggerOut : function(e, internal){
934         return this.menu && e.target.tagName != this.arrowSelector;
935     }
936 });
937
938 Ext.reg('splitbutton', Ext.SplitButton);/**
939  * @class Ext.CycleButton
940  * @extends Ext.SplitButton
941  * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements.  The button automatically
942  * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
943  * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
944  * button displays the dropdown menu just like a normal SplitButton.  Example usage:
945  * <pre><code>
946 var btn = new Ext.CycleButton({
947     showText: true,
948     prependText: 'View as ',
949     items: [{
950         text:'text only',
951         iconCls:'view-text',
952         checked:true
953     },{
954         text:'HTML',
955         iconCls:'view-html'
956     }],
957     changeHandler:function(btn, item){
958         Ext.Msg.alert('Change View', item.text);
959     }
960 });
961 </code></pre>
962  * @constructor
963  * Create a new split button
964  * @param {Object} config The config object
965  * @xtype cycle
966  */
967 Ext.CycleButton = Ext.extend(Ext.SplitButton, {
968     /**
969      * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
970      * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
971      */
972     /**
973      * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
974      */
975     /**
976      * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
977      * button's text (only applies when showText = true, defaults to '')
978      */
979     /**
980      * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
981      * item in the button's menu has changed.  If this callback is not supplied, the SplitButton will instead
982      * fire the {@link #change} event on active item change.  The changeHandler function will be called with the
983      * following argument list: (SplitButton this, Ext.menu.CheckItem item)
984      */
985     /**
986      * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button.  This
987      * icon will always be displayed regardless of which item is selected in the dropdown list.  This overrides the 
988      * default behavior of changing the button's icon to match the selected item's icon on change.
989      */
990     /**
991      * @property menu
992      * @type Menu
993      * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
994      */
995
996     // private
997     getItemText : function(item){
998         if(item && this.showText === true){
999             var text = '';
1000             if(this.prependText){
1001                 text += this.prependText;
1002             }
1003             text += item.text;
1004             return text;
1005         }
1006         return undefined;
1007     },
1008
1009     /**
1010      * Sets the button's active menu item.
1011      * @param {Ext.menu.CheckItem} item The item to activate
1012      * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
1013      */
1014     setActiveItem : function(item, suppressEvent){
1015         if(!Ext.isObject(item)){
1016             item = this.menu.getComponent(item);
1017         }
1018         if(item){
1019             if(!this.rendered){
1020                 this.text = this.getItemText(item);
1021                 this.iconCls = item.iconCls;
1022             }else{
1023                 var t = this.getItemText(item);
1024                 if(t){
1025                     this.setText(t);
1026                 }
1027                 this.setIconClass(item.iconCls);
1028             }
1029             this.activeItem = item;
1030             if(!item.checked){
1031                 item.setChecked(true, false);
1032             }
1033             if(this.forceIcon){
1034                 this.setIconClass(this.forceIcon);
1035             }
1036             if(!suppressEvent){
1037                 this.fireEvent('change', this, item);
1038             }
1039         }
1040     },
1041
1042     /**
1043      * Gets the currently active menu item.
1044      * @return {Ext.menu.CheckItem} The active item
1045      */
1046     getActiveItem : function(){
1047         return this.activeItem;
1048     },
1049
1050     // private
1051     initComponent : function(){
1052         this.addEvents(
1053             /**
1054              * @event change
1055              * Fires after the button's active menu item has changed.  Note that if a {@link #changeHandler} function
1056              * is set on this CycleButton, it will be called instead on active item change and this change event will
1057              * not be fired.
1058              * @param {Ext.CycleButton} this
1059              * @param {Ext.menu.CheckItem} item The menu item that was selected
1060              */
1061             "change"
1062         );
1063
1064         if(this.changeHandler){
1065             this.on('change', this.changeHandler, this.scope||this);
1066             delete this.changeHandler;
1067         }
1068
1069         this.itemCount = this.items.length;
1070
1071         this.menu = {cls:'x-cycle-menu', items:[]};
1072         var checked = 0;
1073         Ext.each(this.items, function(item, i){
1074             Ext.apply(item, {
1075                 group: item.group || this.id,
1076                 itemIndex: i,
1077                 checkHandler: this.checkHandler,
1078                 scope: this,
1079                 checked: item.checked || false
1080             });
1081             this.menu.items.push(item);
1082             if(item.checked){
1083                 checked = i;
1084             }
1085         }, this);
1086         Ext.CycleButton.superclass.initComponent.call(this);
1087         this.on('click', this.toggleSelected, this);
1088         this.setActiveItem(checked, true);
1089     },
1090
1091     // private
1092     checkHandler : function(item, pressed){
1093         if(pressed){
1094             this.setActiveItem(item);
1095         }
1096     },
1097
1098     /**
1099      * This is normally called internally on button click, but can be called externally to advance the button's
1100      * active item programmatically to the next one in the menu.  If the current item is the last one in the menu
1101      * the active item will be set to the first item in the menu.
1102      */
1103     toggleSelected : function(){
1104         var m = this.menu;
1105         m.render();
1106         // layout if we haven't before so the items are active
1107         if(!m.hasLayout){
1108             m.doLayout();
1109         }
1110         
1111         var nextIdx, checkItem;
1112         for (var i = 1; i < this.itemCount; i++) {
1113             nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
1114             // check the potential item
1115             checkItem = m.items.itemAt(nextIdx);
1116             // if its not disabled then check it.
1117             if (!checkItem.disabled) {
1118                 checkItem.setChecked(true);
1119                 break;
1120             }
1121         }
1122     }
1123 });
1124 Ext.reg('cycle', Ext.CycleButton);