Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / Button.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.Button\r
9  * @extends Ext.BoxComponent\r
10  * Simple Button class\r
11  * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)\r
12  * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image\r
13  * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')\r
14  * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).\r
15  * The handler is passed the following parameters:<div class="mdetail-params"><ul>\r
16  * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>\r
17  * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>\r
18  * </ul></div>\r
19  * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).\r
20  * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.\r
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\r
22  * @cfg {Boolean} hidden True to start hidden (defaults to false)\r
23  * @cfg {Boolean} disabled True to start disabled (defaults to false)\r
24  * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)\r
25  * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)\r
26  * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be\r
27  * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).\r
28  * @constructor\r
29  * Create a new button\r
30  * @param {Object} config The config object\r
31  * @xtype button\r
32  */\r
33 Ext.Button = Ext.extend(Ext.BoxComponent, {\r
34     /**\r
35      * Read-only. True if this button is hidden\r
36      * @type Boolean\r
37      */\r
38     hidden : false,\r
39     /**\r
40      * Read-only. True if this button is disabled\r
41      * @type Boolean\r
42      */\r
43     disabled : false,\r
44     /**\r
45      * Read-only. True if this button is pressed (only if enableToggle = true)\r
46      * @type Boolean\r
47      */\r
48     pressed : false,\r
49 \r
50     /**\r
51      * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)\r
52      */\r
53 \r
54     /**\r
55      * @cfg {Boolean} allowDepress\r
56      * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.\r
57      */\r
58 \r
59     /**\r
60      * @cfg {Boolean} enableToggle\r
61      * True to enable pressed/not pressed toggling (defaults to false)\r
62      */\r
63     enableToggle : false,\r
64     /**\r
65      * @cfg {Function} toggleHandler\r
66      * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">\r
67      * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>\r
68      * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>\r
69      * </ul>\r
70      */\r
71     /**\r
72      * @cfg {Mixed} menu\r
73      * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).\r
74      */\r
75     /**\r
76      * @cfg {String} menuAlign\r
77      * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').\r
78      */\r
79     menuAlign : 'tl-bl?',\r
80 \r
81     /**\r
82      * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the\r
83      * text to be used if this item is shown in the overflow menu. See also\r
84      * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.\r
85      */\r
86     /**\r
87      * @cfg {String} iconCls\r
88      * A css class which sets a background image to be used as the icon for this button\r
89      */\r
90     /**\r
91      * @cfg {String} type\r
92      * submit, reset or button - defaults to 'button'\r
93      */\r
94     type : 'button',\r
95 \r
96     // private\r
97     menuClassTarget : 'tr:nth(2)',\r
98 \r
99     /**\r
100      * @cfg {String} clickEvent\r
101      * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).\r
102      * Defaults to <tt>'click'</tt>.\r
103      */\r
104     clickEvent : 'click',\r
105 \r
106     /**\r
107      * @cfg {Boolean} handleMouseEvents\r
108      * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)\r
109      */\r
110     handleMouseEvents : true,\r
111 \r
112     /**\r
113      * @cfg {String} tooltipType\r
114      * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.\r
115      */\r
116     tooltipType : 'qtip',\r
117 \r
118     /**\r
119      * @cfg {String} buttonSelector\r
120      * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the\r
121      * DOM structure created.</p>\r
122      * <p>When a custom {@link #template} is used, you  must ensure that this selector results in the selection of\r
123      * a focussable element.</p>\r
124      * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>\r
125      */\r
126     buttonSelector : 'button:first-child',\r
127 \r
128     /**\r
129      * @cfg {String} scale\r
130      * <p>(Optional) The size of the Button. Three values are allowed:</p>\r
131      * <ul class="mdetail-params">\r
132      * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>\r
133      * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>\r
134      * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>\r
135      * </ul>\r
136      * <p>Defaults to <b><tt>'small'</tt></b>.</p>\r
137      */\r
138     scale : 'small',\r
139 \r
140     /**\r
141      * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the\r
142      * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is\r
143      * executed. Defaults to this Button.\r
144      */\r
145 \r
146     /**\r
147      * @cfg {String} iconAlign\r
148      * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>\r
149      * <ul class="mdetail-params">\r
150      * <li>'top'<div class="sub-desc"></div></li>\r
151      * <li>'right'<div class="sub-desc"></div></li>\r
152      * <li>'bottom'<div class="sub-desc"></div></li>\r
153      * <li>'left'<div class="sub-desc"></div></li>\r
154      * </ul>\r
155      * <p>Defaults to <b><tt>'left'</tt></b>.</p>\r
156      */\r
157     iconAlign : 'left',\r
158 \r
159     /**\r
160      * @cfg {String} arrowAlign\r
161      * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.\r
162      * Two values are allowed:</p>\r
163      * <ul class="mdetail-params">\r
164      * <li>'right'<div class="sub-desc"></div></li>\r
165      * <li>'bottom'<div class="sub-desc"></div></li>\r
166      * </ul>\r
167      * <p>Defaults to <b><tt>'right'</tt></b>.</p>\r
168      */\r
169     arrowAlign : 'right',\r
170 \r
171     /**\r
172      * @cfg {Ext.Template} template (Optional)\r
173      * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>\r
174      * Instances, or subclasses which need a different DOM structure may provide a different\r
175      * template layout in conjunction with an implementation of {@link #getTemplateArgs}.\r
176      * @type Ext.Template\r
177      * @property template\r
178      */\r
179     /**\r
180      * @cfg {String} cls\r
181      * A CSS class string to apply to the button's main element.\r
182      */\r
183     /**\r
184      * @property menu\r
185      * @type Menu\r
186      * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.\r
187      */\r
188 \r
189     initComponent : function(){\r
190         Ext.Button.superclass.initComponent.call(this);\r
191 \r
192         this.addEvents(\r
193             /**\r
194              * @event click\r
195              * Fires when this button is clicked\r
196              * @param {Button} this\r
197              * @param {EventObject} e The click event\r
198              */\r
199             'click',\r
200             /**\r
201              * @event toggle\r
202              * Fires when the 'pressed' state of this button changes (only if enableToggle = true)\r
203              * @param {Button} this\r
204              * @param {Boolean} pressed\r
205              */\r
206             'toggle',\r
207             /**\r
208              * @event mouseover\r
209              * Fires when the mouse hovers over the button\r
210              * @param {Button} this\r
211              * @param {Event} e The event object\r
212              */\r
213             'mouseover',\r
214             /**\r
215              * @event mouseout\r
216              * Fires when the mouse exits the button\r
217              * @param {Button} this\r
218              * @param {Event} e The event object\r
219              */\r
220             'mouseout',\r
221             /**\r
222              * @event menushow\r
223              * If this button has a menu, this event fires when it is shown\r
224              * @param {Button} this\r
225              * @param {Menu} menu\r
226              */\r
227             'menushow',\r
228             /**\r
229              * @event menuhide\r
230              * If this button has a menu, this event fires when it is hidden\r
231              * @param {Button} this\r
232              * @param {Menu} menu\r
233              */\r
234             'menuhide',\r
235             /**\r
236              * @event menutriggerover\r
237              * If this button has a menu, this event fires when the mouse enters the menu triggering element\r
238              * @param {Button} this\r
239              * @param {Menu} menu\r
240              * @param {EventObject} e\r
241              */\r
242             'menutriggerover',\r
243             /**\r
244              * @event menutriggerout\r
245              * If this button has a menu, this event fires when the mouse leaves the menu triggering element\r
246              * @param {Button} this\r
247              * @param {Menu} menu\r
248              * @param {EventObject} e\r
249              */\r
250             'menutriggerout'\r
251         );\r
252         if(this.menu){\r
253             this.menu = Ext.menu.MenuMgr.get(this.menu);\r
254         }\r
255         if(Ext.isString(this.toggleGroup)){\r
256             this.enableToggle = true;\r
257         }\r
258     },\r
259 \r
260 /**\r
261   * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used\r
262   * to create this Button's DOM structure.</p>\r
263   * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their\r
264   * own implementation of this method.</p>\r
265   * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the\r
266   * following items:</p><div class="mdetail-params"><ul>\r
267   * <li>The &lt;button&gt;'s {@link #type}</li>\r
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>\r
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>\r
270   * <li>The {@link #cls} CSS class name applied to the button's wrapping &lt;table&gt; element.</li>\r
271   * <li>The Component id which is applied to the button's wrapping &lt;table&gt; element.</li>\r
272   * </ul></div>\r
273   * @return {Array} Substitution data for a Template.\r
274  */\r
275     getTemplateArgs : function(){\r
276         return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];\r
277     },\r
278 \r
279     // private\r
280     setButtonClass : function(){\r
281         if(this.useSetClass){\r
282             if(!Ext.isEmpty(this.oldCls)){\r
283                 this.el.removeClass([this.oldCls, 'x-btn-pressed']);\r
284             }\r
285             this.oldCls = (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';\r
286             this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);\r
287         }\r
288     },\r
289 \r
290     // protected\r
291     getMenuClass : function(){\r
292         return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';\r
293     },\r
294 \r
295     // private\r
296     onRender : function(ct, position){\r
297         if(!this.template){\r
298             if(!Ext.Button.buttonTemplate){\r
299                 // hideous table template\r
300                 Ext.Button.buttonTemplate = new Ext.Template(\r
301                     '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',\r
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>',\r
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>',\r
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>',\r
305                     '</tbody></table>');\r
306                 Ext.Button.buttonTemplate.compile();\r
307             }\r
308             this.template = Ext.Button.buttonTemplate;\r
309         }\r
310 \r
311         var btn, targs = this.getTemplateArgs();\r
312 \r
313         if(position){\r
314             btn = this.template.insertBefore(position, targs, true);\r
315         }else{\r
316             btn = this.template.append(ct, targs, true);\r
317         }\r
318         /**\r
319          * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,\r
320          * this references a <tt>&lt;button&gt;</tt> element. Read only.\r
321          * @type Ext.Element\r
322          * @property btnEl\r
323          */\r
324         this.btnEl = btn.child(this.buttonSelector);\r
325         this.mon(this.btnEl, {\r
326             scope: this,\r
327             focus: this.onFocus,\r
328             blur: this.onBlur\r
329         });\r
330 \r
331         this.initButtonEl(btn, this.btnEl);\r
332 \r
333         Ext.ButtonToggleMgr.register(this);\r
334     },\r
335 \r
336     // private\r
337     initButtonEl : function(btn, btnEl){\r
338         this.el = btn;\r
339         this.setIcon(this.icon);\r
340         this.setText(this.text);\r
341         this.setIconClass(this.iconCls);\r
342         if(Ext.isDefined(this.tabIndex)){\r
343             btnEl.dom.tabIndex = this.tabIndex;\r
344         }\r
345         if(this.tooltip){\r
346             this.setTooltip(this.tooltip, true);\r
347         }\r
348 \r
349         if(this.handleMouseEvents){\r
350             this.mon(btn, {\r
351                 scope: this,\r
352                 mouseover: this.onMouseOver,\r
353                 mousedown: this.onMouseDown\r
354             });\r
355 \r
356             // new functionality for monitoring on the document level\r
357             //this.mon(btn, 'mouseout', this.onMouseOut, this);\r
358         }\r
359 \r
360         if(this.menu){\r
361             this.mon(this.menu, {\r
362                 scope: this,\r
363                 show: this.onMenuShow,\r
364                 hide: this.onMenuHide\r
365             });\r
366         }\r
367 \r
368         if(this.repeat){\r
369             var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});\r
370             this.mon(repeater, 'click', this.onClick, this);\r
371         }\r
372         this.mon(btn, this.clickEvent, this.onClick, this);\r
373     },\r
374 \r
375     // private\r
376     afterRender : function(){\r
377         Ext.Button.superclass.afterRender.call(this);\r
378         this.useSetClass = true;\r
379         this.setButtonClass();\r
380         this.doc = Ext.getDoc();\r
381         this.doAutoWidth();\r
382     },\r
383 \r
384     /**\r
385      * Sets the CSS class that provides a background image to use as the button's icon.  This method also changes\r
386      * the value of the {@link iconCls} config internally.\r
387      * @param {String} cls The CSS class providing the icon image\r
388      * @return {Ext.Button} this\r
389      */\r
390     setIconClass : function(cls){\r
391         this.iconCls = cls;\r
392         if(this.el){\r
393             this.btnEl.dom.className = '';\r
394             this.btnEl.addClass(['x-btn-text', cls || '']);\r
395             this.setButtonClass();\r
396         }\r
397         return this;\r
398     },\r
399 \r
400     /**\r
401      * Sets the tooltip for this Button.\r
402      * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>\r
403      * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>\r
404      * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>\r
405      * </ul></div>\r
406      * @return {Ext.Button} this\r
407      */\r
408     setTooltip : function(tooltip, /* private */ initial){\r
409         if(this.rendered){\r
410             if(!initial){\r
411                 this.clearTip();\r
412             }\r
413             if(Ext.isObject(tooltip)){\r
414                 Ext.QuickTips.register(Ext.apply({\r
415                       target: this.btnEl.id\r
416                 }, tooltip));\r
417                 this.tooltip = tooltip;\r
418             }else{\r
419                 this.btnEl.dom[this.tooltipType] = tooltip;\r
420             }\r
421         }else{\r
422             this.tooltip = tooltip;\r
423         }\r
424         return this;\r
425     },\r
426 \r
427     // private\r
428     clearTip : function(){\r
429         if(Ext.isObject(this.tooltip)){\r
430             Ext.QuickTips.unregister(this.btnEl);\r
431         }\r
432     },\r
433 \r
434     // private\r
435     beforeDestroy : function(){\r
436         if(this.rendered){\r
437             this.clearTip();\r
438         }\r
439         if(this.menu && this.menu.autoDestroy) {\r
440             Ext.destroy(this.menu);\r
441         }\r
442         Ext.destroy(this.repeater);\r
443     },\r
444 \r
445     // private\r
446     onDestroy : function(){\r
447         if(this.rendered){\r
448             this.doc.un('mouseover', this.monitorMouseOver, this);\r
449             this.doc.un('mouseup', this.onMouseUp, this);\r
450             delete this.doc;\r
451             delete this.btnEl;\r
452             Ext.ButtonToggleMgr.unregister(this);\r
453         }\r
454     },\r
455 \r
456     // private\r
457     doAutoWidth : function(){\r
458         if(this.el && this.text && this.width === undefined){\r
459             this.el.setWidth('auto');\r
460             if(Ext.isIE7 && Ext.isStrict){\r
461                 var ib = this.btnEl;\r
462                 if(ib && ib.getWidth() > 20){\r
463                     ib.clip();\r
464                     ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));\r
465                 }\r
466             }\r
467             if(this.minWidth){\r
468                 if(this.el.getWidth() < this.minWidth){\r
469                     this.el.setWidth(this.minWidth);\r
470                 }\r
471             }\r
472         }\r
473     },\r
474 \r
475     /**\r
476      * Assigns this Button's click handler\r
477      * @param {Function} handler The function to call when the button is clicked\r
478      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.\r
479      * Defaults to this Button.\r
480      * @return {Ext.Button} this\r
481      */\r
482     setHandler : function(handler, scope){\r
483         this.handler = handler;\r
484         this.scope = scope;\r
485         return this;\r
486     },\r
487 \r
488     /**\r
489      * Sets this Button's text\r
490      * @param {String} text The button text\r
491      * @return {Ext.Button} this\r
492      */\r
493     setText : function(text){\r
494         this.text = text;\r
495         if(this.el){\r
496             this.btnEl.update(text || '&#160;');\r
497             this.setButtonClass();\r
498         }\r
499         this.doAutoWidth();\r
500         return this;\r
501     },\r
502 \r
503     /**\r
504      * Sets the background image (inline style) of the button.  This method also changes\r
505      * the value of the {@link icon} config internally.\r
506      * @param {String} icon The path to an image to display in the button\r
507      * @return {Ext.Button} this\r
508      */\r
509     setIcon : function(icon){\r
510         this.icon = icon;\r
511         if(this.el){\r
512             this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');\r
513             this.setButtonClass();\r
514         }\r
515         return this;\r
516     },\r
517 \r
518     /**\r
519      * Gets the text for this Button\r
520      * @return {String} The button text\r
521      */\r
522     getText : function(){\r
523         return this.text;\r
524     },\r
525 \r
526     /**\r
527      * If a state it passed, it becomes the pressed state otherwise the current state is toggled.\r
528      * @param {Boolean} state (optional) Force a particular state\r
529      * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.\r
530      * @return {Ext.Button} this\r
531      */\r
532     toggle : function(state, suppressEvent){\r
533         state = state === undefined ? !this.pressed : !!state;\r
534         if(state != this.pressed){\r
535             if(this.rendered){\r
536                 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');\r
537             }\r
538             this.pressed = state;\r
539             if(!suppressEvent){\r
540                 this.fireEvent('toggle', this, state);\r
541                 if(this.toggleHandler){\r
542                     this.toggleHandler.call(this.scope || this, this, state);\r
543                 }\r
544             }\r
545         }\r
546         return this;\r
547     },\r
548 \r
549     /**\r
550      * Focus the button\r
551      */\r
552     focus : function(){\r
553         this.btnEl.focus();\r
554     },\r
555 \r
556     // private\r
557     onDisable : function(){\r
558         this.onDisableChange(true);\r
559     },\r
560 \r
561     // private\r
562     onEnable : function(){\r
563         this.onDisableChange(false);\r
564     },\r
565 \r
566     onDisableChange : function(disabled){\r
567         if(this.el){\r
568             if(!Ext.isIE6 || !this.text){\r
569                 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);\r
570             }\r
571             this.el.dom.disabled = disabled;\r
572         }\r
573         this.disabled = disabled;\r
574     },\r
575 \r
576     /**\r
577      * Show this button's menu (if it has one)\r
578      */\r
579     showMenu : function(){\r
580         if(this.rendered && this.menu){\r
581             if(this.tooltip){\r
582                 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);\r
583             }\r
584             this.menu.show(this.el, this.menuAlign);\r
585         }\r
586         return this;\r
587     },\r
588 \r
589     /**\r
590      * Hide this button's menu (if it has one)\r
591      */\r
592     hideMenu : function(){\r
593         if(this.menu){\r
594             this.menu.hide();\r
595         }\r
596         return this;\r
597     },\r
598 \r
599     /**\r
600      * Returns true if the button has a menu and it is visible\r
601      * @return {Boolean}\r
602      */\r
603     hasVisibleMenu : function(){\r
604         return this.menu && this.menu.isVisible();\r
605     },\r
606 \r
607     // private\r
608     onClick : function(e){\r
609         if(e){\r
610             e.preventDefault();\r
611         }\r
612         if(e.button !== 0){\r
613             return;\r
614         }\r
615         if(!this.disabled){\r
616             if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){\r
617                 this.toggle();\r
618             }\r
619             if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){\r
620                 this.showMenu();\r
621             }\r
622             this.fireEvent('click', this, e);\r
623             if(this.handler){\r
624                 //this.el.removeClass('x-btn-over');\r
625                 this.handler.call(this.scope || this, this, e);\r
626             }\r
627         }\r
628     },\r
629 \r
630     // private\r
631     isMenuTriggerOver : function(e, internal){\r
632         return this.menu && !internal;\r
633     },\r
634 \r
635     // private\r
636     isMenuTriggerOut : function(e, internal){\r
637         return this.menu && !internal;\r
638     },\r
639 \r
640     // private\r
641     onMouseOver : function(e){\r
642         if(!this.disabled){\r
643             var internal = e.within(this.el,  true);\r
644             if(!internal){\r
645                 this.el.addClass('x-btn-over');\r
646                 if(!this.monitoringMouseOver){\r
647                     this.doc.on('mouseover', this.monitorMouseOver, this);\r
648                     this.monitoringMouseOver = true;\r
649                 }\r
650                 this.fireEvent('mouseover', this, e);\r
651             }\r
652             if(this.isMenuTriggerOver(e, internal)){\r
653                 this.fireEvent('menutriggerover', this, this.menu, e);\r
654             }\r
655         }\r
656     },\r
657 \r
658     // private\r
659     monitorMouseOver : function(e){\r
660         if(e.target != this.el.dom && !e.within(this.el)){\r
661             if(this.monitoringMouseOver){\r
662                 this.doc.un('mouseover', this.monitorMouseOver, this);\r
663                 this.monitoringMouseOver = false;\r
664             }\r
665             this.onMouseOut(e);\r
666         }\r
667     },\r
668 \r
669     // private\r
670     onMouseOut : function(e){\r
671         var internal = e.within(this.el) && e.target != this.el.dom;\r
672         this.el.removeClass('x-btn-over');\r
673         this.fireEvent('mouseout', this, e);\r
674         if(this.isMenuTriggerOut(e, internal)){\r
675             this.fireEvent('menutriggerout', this, this.menu, e);\r
676         }\r
677     },\r
678 \r
679     focus : function() {\r
680         this.btnEl.focus();\r
681     },\r
682 \r
683     blur : function() {\r
684         this.btnEl.blur();\r
685     },\r
686 \r
687     // private\r
688     onFocus : function(e){\r
689         if(!this.disabled){\r
690             this.el.addClass('x-btn-focus');\r
691         }\r
692     },\r
693     // private\r
694     onBlur : function(e){\r
695         this.el.removeClass('x-btn-focus');\r
696     },\r
697 \r
698     // private\r
699     getClickEl : function(e, isUp){\r
700        return this.el;\r
701     },\r
702 \r
703     // private\r
704     onMouseDown : function(e){\r
705         if(!this.disabled && e.button === 0){\r
706             this.getClickEl(e).addClass('x-btn-click');\r
707             this.doc.on('mouseup', this.onMouseUp, this);\r
708         }\r
709     },\r
710     // private\r
711     onMouseUp : function(e){\r
712         if(e.button === 0){\r
713             this.getClickEl(e, true).removeClass('x-btn-click');\r
714             this.doc.un('mouseup', this.onMouseUp, this);\r
715         }\r
716     },\r
717     // private\r
718     onMenuShow : function(e){\r
719         this.menu.ownerCt = this;\r
720         this.ignoreNextClick = 0;\r
721         this.el.addClass('x-btn-menu-active');\r
722         this.fireEvent('menushow', this, this.menu);\r
723     },\r
724     // private\r
725     onMenuHide : function(e){\r
726         this.el.removeClass('x-btn-menu-active');\r
727         this.ignoreNextClick = this.restoreClick.defer(250, this);\r
728         this.fireEvent('menuhide', this, this.menu);\r
729         delete this.menu.ownerCt;\r
730     },\r
731 \r
732     // private\r
733     restoreClick : function(){\r
734         this.ignoreNextClick = 0;\r
735     }\r
736 \r
737     /**\r
738      * @cfg {String} autoEl @hide\r
739      */\r
740 });\r
741 Ext.reg('button', Ext.Button);\r
742 \r
743 // Private utility class used by Button\r
744 Ext.ButtonToggleMgr = function(){\r
745    var groups = {};\r
746 \r
747    function toggleGroup(btn, state){\r
748        if(state){\r
749            var g = groups[btn.toggleGroup];\r
750            for(var i = 0, l = g.length; i < l; i++){\r
751                if(g[i] != btn){\r
752                    g[i].toggle(false);\r
753                }\r
754            }\r
755        }\r
756    }\r
757 \r
758    return {\r
759        register : function(btn){\r
760            if(!btn.toggleGroup){\r
761                return;\r
762            }\r
763            var g = groups[btn.toggleGroup];\r
764            if(!g){\r
765                g = groups[btn.toggleGroup] = [];\r
766            }\r
767            g.push(btn);\r
768            btn.on('toggle', toggleGroup);\r
769        },\r
770 \r
771        unregister : function(btn){\r
772            if(!btn.toggleGroup){\r
773                return;\r
774            }\r
775            var g = groups[btn.toggleGroup];\r
776            if(g){\r
777                g.remove(btn);\r
778                btn.un('toggle', toggleGroup);\r
779            }\r
780        },\r
781 \r
782        /**\r
783         * Gets the pressed button in the passed group or null\r
784         * @param {String} group\r
785         * @return Button\r
786         */\r
787        getPressed : function(group){\r
788            var g = groups[group];\r
789            if(g){\r
790                for(var i = 0, len = g.length; i < len; i++){\r
791                    if(g[i].pressed === true){\r
792                        return g[i];\r
793                    }\r
794                }\r
795            }\r
796            return null;\r
797        }\r
798    };\r
799 }();\r