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