3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * @extends Ext.BoxComponent
11 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
12 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
13 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
14 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
15 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
16 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
17 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
19 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
20 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
21 * @cfg {String/Object} tooltip The tooltip for the button - can be a string to be used as innerHTML (html tags are accepted) or QuickTips config object
22 * @cfg {Boolean} hidden True to start hidden (defaults to false)
23 * @cfg {Boolean} disabled True to start disabled (defaults to false)
24 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
25 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
26 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
27 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
30 * @param {Object} config The config object
33 Ext.Button = Ext.extend(Ext.BoxComponent, {
35 * Read-only. True if this button is hidden
40 * Read-only. True if this button is disabled
45 * Read-only. True if this button is pressed (only if enableToggle = true)
51 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
55 * @cfg {Boolean} allowDepress
56 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
60 * @cfg {Boolean} enableToggle
61 * True to enable pressed/not pressed toggling (defaults to false)
65 * @cfg {Function} toggleHandler
66 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
67 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
68 * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
73 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
76 * @cfg {String} menuAlign
77 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
82 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
83 * text to be used if this item is shown in the overflow menu. See also
84 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
87 * @cfg {String} iconCls
88 * A css class which sets a background image to be used as the icon for this button
92 * submit, reset or button - defaults to 'button'
97 menuClassTarget : 'tr:nth(2)',
100 * @cfg {String} clickEvent
101 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
102 * Defaults to <tt>'click'</tt>.
104 clickEvent : 'click',
107 * @cfg {Boolean} handleMouseEvents
108 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
110 handleMouseEvents : true,
113 * @cfg {String} tooltipType
114 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
116 tooltipType : 'qtip',
119 * @cfg {String} buttonSelector
120 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
121 * DOM structure created.</p>
122 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
123 * a focussable element.</p>
124 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
126 buttonSelector : 'button:first-child',
129 * @cfg {String} scale
130 * <p>(Optional) The size of the Button. Three values are allowed:</p>
131 * <ul class="mdetail-params">
132 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
133 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
134 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
136 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
141 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
142 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
143 * executed. Defaults to this Button.
147 * @cfg {String} iconAlign
148 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
149 * <ul class="mdetail-params">
150 * <li>'top'<div class="sub-desc"></div></li>
151 * <li>'right'<div class="sub-desc"></div></li>
152 * <li>'bottom'<div class="sub-desc"></div></li>
153 * <li>'left'<div class="sub-desc"></div></li>
155 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
160 * @cfg {String} arrowAlign
161 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
162 * Two values are allowed:</p>
163 * <ul class="mdetail-params">
164 * <li>'right'<div class="sub-desc"></div></li>
165 * <li>'bottom'<div class="sub-desc"></div></li>
167 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
169 arrowAlign : 'right',
172 * @cfg {Ext.Template} template (Optional)
173 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
174 * Instances, or subclasses which need a different DOM structure may provide a different
175 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
181 * A CSS class string to apply to the button's main element.
186 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
189 initComponent : function(){
190 Ext.Button.superclass.initComponent.call(this);
195 * Fires when this button is clicked
196 * @param {Button} this
197 * @param {EventObject} e The click event
202 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
203 * @param {Button} this
204 * @param {Boolean} pressed
209 * Fires when the mouse hovers over the button
210 * @param {Button} this
211 * @param {Event} e The event object
216 * Fires when the mouse exits the button
217 * @param {Button} this
218 * @param {Event} e The event object
223 * If this button has a menu, this event fires when it is shown
224 * @param {Button} this
230 * If this button has a menu, this event fires when it is hidden
231 * @param {Button} this
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
240 * @param {EventObject} e
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
248 * @param {EventObject} e
253 this.menu = Ext.menu.MenuMgr.get(this.menu);
255 if(Ext.isString(this.toggleGroup)){
256 this.enableToggle = true;
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 <button>'s {@link #type}</li>
268 * <li>A CSS class name applied to the Button's main <tbody> 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 <table> element.</li>
271 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
273 * @return {Array} Substitution data for a Template.
275 getTemplateArgs : function(){
276 return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
280 setButtonClass : function(){
281 if(this.useSetClass){
282 if(!Ext.isEmpty(this.oldCls)){
283 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
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]);
291 getMenuClass : function(){
292 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
296 onRender : function(ct, position){
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> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
303 '<tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i> </i></td></tr>',
304 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
306 Ext.Button.buttonTemplate.compile();
308 this.template = Ext.Button.buttonTemplate;
311 var btn, targs = this.getTemplateArgs();
314 btn = this.template.insertBefore(position, targs, true);
316 btn = this.template.append(ct, targs, true);
319 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
320 * this references a <tt><button></tt> element. Read only.
324 this.btnEl = btn.child(this.buttonSelector);
325 this.mon(this.btnEl, {
331 this.initButtonEl(btn, this.btnEl);
333 Ext.ButtonToggleMgr.register(this);
337 initButtonEl : function(btn, btnEl){
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;
346 this.setTooltip(this.tooltip, true);
349 if(this.handleMouseEvents){
352 mouseover: this.onMouseOver,
353 mousedown: this.onMouseDown
356 // new functionality for monitoring on the document level
357 //this.mon(btn, 'mouseout', this.onMouseOut, this);
361 this.mon(this.menu, {
363 show: this.onMenuShow,
364 hide: this.onMenuHide
369 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
370 this.mon(repeater, 'click', this.onClick, this);
372 this.mon(btn, this.clickEvent, this.onClick, this);
376 afterRender : function(){
377 Ext.Button.superclass.afterRender.call(this);
378 this.useSetClass = true;
379 this.setButtonClass();
380 this.doc = Ext.getDoc();
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
390 setIconClass : function(cls){
393 this.btnEl.dom.className = '';
394 this.btnEl.addClass(['x-btn-text', cls || '']);
395 this.setButtonClass();
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>
406 * @return {Ext.Button} this
408 setTooltip : function(tooltip, /* private */ initial){
413 if(Ext.isObject(tooltip)){
414 Ext.QuickTips.register(Ext.apply({
415 target: this.btnEl.id
417 this.tooltip = tooltip;
419 this.btnEl.dom[this.tooltipType] = tooltip;
422 this.tooltip = tooltip;
428 clearTip : function(){
429 if(Ext.isObject(this.tooltip)){
430 Ext.QuickTips.unregister(this.btnEl);
435 beforeDestroy : function(){
439 if(this.menu && this.destroyMenu !== false) {
440 Ext.destroy(this.menu);
442 Ext.destroy(this.repeater);
446 onDestroy : function(){
448 this.doc.un('mouseover', this.monitorMouseOver, this);
449 this.doc.un('mouseup', this.onMouseUp, this);
452 Ext.ButtonToggleMgr.unregister(this);
454 Ext.Button.superclass.onDestroy.call(this);
458 doAutoWidth : function(){
459 if(this.el && this.text && this.width === undefined){
460 this.el.setWidth('auto');
461 if(Ext.isIE7 && Ext.isStrict){
463 if(ib && ib.getWidth() > 20){
465 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
469 if(this.el.getWidth() < this.minWidth){
470 this.el.setWidth(this.minWidth);
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
483 setHandler : function(handler, scope){
484 this.handler = handler;
490 * Sets this Button's text
491 * @param {String} text The button text
492 * @return {Ext.Button} this
494 setText : function(text){
497 this.btnEl.update(text || ' ');
498 this.setButtonClass();
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
510 setIcon : function(icon){
513 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
514 this.setButtonClass();
520 * Gets the text for this Button
521 * @return {String} The button text
523 getText : function(){
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
533 toggle : function(state, suppressEvent){
534 state = state === undefined ? !this.pressed : !!state;
535 if(state != this.pressed){
537 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
539 this.pressed = state;
541 this.fireEvent('toggle', this, state);
542 if(this.toggleHandler){
543 this.toggleHandler.call(this.scope || this, this, state);
551 onDisable : function(){
552 this.onDisableChange(true);
556 onEnable : function(){
557 this.onDisableChange(false);
560 onDisableChange : function(disabled){
562 if(!Ext.isIE6 || !this.text){
563 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
565 this.el.dom.disabled = disabled;
567 this.disabled = disabled;
571 * Show this button's menu (if it has one)
573 showMenu : function(){
574 if(this.rendered && this.menu){
576 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
578 if(this.menu.isVisible()){
581 this.menu.ownerCt = this;
582 this.menu.show(this.el, this.menuAlign);
588 * Hide this button's menu (if it has one)
590 hideMenu : function(){
591 if(this.hasVisibleMenu()){
598 * Returns true if the button has a menu and it is visible
601 hasVisibleMenu : function(){
602 return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
606 onClick : function(e){
614 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
617 if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
620 this.fireEvent('click', this, e);
622 //this.el.removeClass('x-btn-over');
623 this.handler.call(this.scope || this, this, e);
629 isMenuTriggerOver : function(e, internal){
630 return this.menu && !internal;
634 isMenuTriggerOut : function(e, internal){
635 return this.menu && !internal;
639 onMouseOver : function(e){
641 var internal = e.within(this.el, true);
643 this.el.addClass('x-btn-over');
644 if(!this.monitoringMouseOver){
645 this.doc.on('mouseover', this.monitorMouseOver, this);
646 this.monitoringMouseOver = true;
648 this.fireEvent('mouseover', this, e);
650 if(this.isMenuTriggerOver(e, internal)){
651 this.fireEvent('menutriggerover', this, this.menu, e);
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;
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);
686 onFocus : function(e){
688 this.el.addClass('x-btn-focus');
692 onBlur : function(e){
693 this.el.removeClass('x-btn-focus');
697 getClickEl : function(e, isUp){
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);
709 onMouseUp : function(e){
711 this.getClickEl(e, true).removeClass('x-btn-click');
712 this.doc.un('mouseup', this.onMouseUp, this);
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);
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;
735 restoreClick : function(){
736 this.ignoreNextClick = 0;
740 * @cfg {String} autoEl @hide
743 * @cfg {String/Object} html @hide
746 * @cfg {String} contentEl @hide
749 * @cfg {Mixed} data @hide
752 * @cfg {Mixed} tpl @hide
755 * @cfg {String} tplWriteMode @hide
758 Ext.reg('button', Ext.Button);
760 // Private utility class used by Button
761 Ext.ButtonToggleMgr = function(){
764 function toggleGroup(btn, state){
766 var g = groups[btn.toggleGroup];
767 for(var i = 0, l = g.length; i < l; i++){
776 register : function(btn){
777 if(!btn.toggleGroup){
780 var g = groups[btn.toggleGroup];
782 g = groups[btn.toggleGroup] = [];
785 btn.on('toggle', toggleGroup);
788 unregister : function(btn){
789 if(!btn.toggleGroup){
792 var g = groups[btn.toggleGroup];
795 btn.un('toggle', toggleGroup);
800 * Gets the pressed button in the passed group or null
801 * @param {String} group
804 getPressed : function(group){
805 var g = groups[group];
807 for(var i = 0, len = g.length; i < len; i++){
808 if(g[i].pressed === true){