3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
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
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
29 * Create a new button
\r
30 * @param {Object} config The config object
\r
33 Ext.Button = Ext.extend(Ext.BoxComponent, {
\r
35 * Read-only. True if this button is hidden
\r
40 * Read-only. True if this button is disabled
\r
45 * Read-only. True if this button is pressed (only if enableToggle = true)
\r
51 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
\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
60 * @cfg {Boolean} enableToggle
\r
61 * True to enable pressed/not pressed toggling (defaults to false)
\r
63 enableToggle : false,
\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
73 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
\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
79 menuAlign : 'tl-bl?',
\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
87 * @cfg {String} iconCls
\r
88 * A css class which sets a background image to be used as the icon for this button
\r
91 * @cfg {String} type
\r
92 * submit, reset or button - defaults to 'button'
\r
97 menuClassTarget : 'tr:nth(2)',
\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
104 clickEvent : 'click',
\r
107 * @cfg {Boolean} handleMouseEvents
\r
108 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
\r
110 handleMouseEvents : true,
\r
113 * @cfg {String} tooltipType
\r
114 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
\r
116 tooltipType : 'qtip',
\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
126 buttonSelector : 'button:first-child',
\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
136 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
\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
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
155 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
\r
157 iconAlign : 'left',
\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
167 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
\r
169 arrowAlign : 'right',
\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
180 * @cfg {String} cls
\r
181 * A CSS class string to apply to the button's main element.
\r
186 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
\r
189 initComponent : function(){
\r
190 Ext.Button.superclass.initComponent.call(this);
\r
195 * Fires when this button is clicked
\r
196 * @param {Button} this
\r
197 * @param {EventObject} e The click event
\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
209 * Fires when the mouse hovers over the button
\r
210 * @param {Button} this
\r
211 * @param {Event} e The event object
\r
216 * Fires when the mouse exits the button
\r
217 * @param {Button} this
\r
218 * @param {Event} e The event object
\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
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
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
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
253 this.menu = Ext.menu.MenuMgr.get(this.menu);
\r
255 if(Ext.isString(this.toggleGroup)){
\r
256 this.enableToggle = true;
\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 <button>'s {@link #type}</li>
\r
268 * <li>A CSS class name applied to the Button's main <tbody> 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 <table> element.</li>
\r
271 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
\r
273 * @return {Array} Substitution data for a Template.
\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
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
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
291 getMenuClass : function(){
\r
292 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
\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> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
\r
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>',
\r
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>',
\r
305 '</tbody></table>');
\r
306 Ext.Button.buttonTemplate.compile();
\r
308 this.template = Ext.Button.buttonTemplate;
\r
311 var btn, targs = this.getTemplateArgs();
\r
314 btn = this.template.insertBefore(position, targs, true);
\r
316 btn = this.template.append(ct, targs, true);
\r
319 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
\r
320 * this references a <tt><button></tt> element. Read only.
\r
321 * @type Ext.Element
\r
324 this.btnEl = btn.child(this.buttonSelector);
\r
325 this.mon(this.btnEl, {
\r
327 focus: this.onFocus,
\r
331 this.initButtonEl(btn, this.btnEl);
\r
333 Ext.ButtonToggleMgr.register(this);
\r
337 initButtonEl : function(btn, btnEl){
\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
346 this.setTooltip(this.tooltip, true);
\r
349 if(this.handleMouseEvents){
\r
352 mouseover: this.onMouseOver,
\r
353 mousedown: this.onMouseDown
\r
356 // new functionality for monitoring on the document level
\r
357 //this.mon(btn, 'mouseout', this.onMouseOut, this);
\r
361 this.mon(this.menu, {
\r
363 show: this.onMenuShow,
\r
364 hide: this.onMenuHide
\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
372 this.mon(btn, this.clickEvent, this.onClick, this);
\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
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
390 setIconClass : function(cls){
\r
391 this.iconCls = cls;
\r
393 this.btnEl.dom.className = '';
\r
394 this.btnEl.addClass(['x-btn-text', cls || '']);
\r
395 this.setButtonClass();
\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
406 * @return {Ext.Button} this
\r
408 setTooltip : function(tooltip, /* private */ initial){
\r
413 if(Ext.isObject(tooltip)){
\r
414 Ext.QuickTips.register(Ext.apply({
\r
415 target: this.btnEl.id
\r
417 this.tooltip = tooltip;
\r
419 this.btnEl.dom[this.tooltipType] = tooltip;
\r
422 this.tooltip = tooltip;
\r
428 clearTip : function(){
\r
429 if(Ext.isObject(this.tooltip)){
\r
430 Ext.QuickTips.unregister(this.btnEl);
\r
435 beforeDestroy : function(){
\r
439 if(this.menu && this.destroyMenu !== false) {
\r
440 Ext.destroy(this.menu);
\r
442 Ext.destroy(this.repeater);
\r
446 onDestroy : function(){
\r
448 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
449 this.doc.un('mouseup', this.onMouseUp, this);
\r
452 Ext.ButtonToggleMgr.unregister(this);
\r
454 Ext.Button.superclass.onDestroy.call(this);
\r
458 doAutoWidth : function(){
\r
459 if(this.el && this.text && this.width === undefined){
\r
460 this.el.setWidth('auto');
\r
461 if(Ext.isIE7 && Ext.isStrict){
\r
462 var ib = this.btnEl;
\r
463 if(ib && ib.getWidth() > 20){
\r
465 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
\r
469 if(this.el.getWidth() < this.minWidth){
\r
470 this.el.setWidth(this.minWidth);
\r
477 * Assigns this Button's click handler
\r
478 * @param {Function} handler The function to call when the button is clicked
\r
479 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
\r
480 * Defaults to this Button.
\r
481 * @return {Ext.Button} this
\r
483 setHandler : function(handler, scope){
\r
484 this.handler = handler;
\r
485 this.scope = scope;
\r
490 * Sets this Button's text
\r
491 * @param {String} text The button text
\r
492 * @return {Ext.Button} this
\r
494 setText : function(text){
\r
497 this.btnEl.update(text || ' ');
\r
498 this.setButtonClass();
\r
500 this.doAutoWidth();
\r
505 * Sets the background image (inline style) of the button. This method also changes
\r
506 * the value of the {@link icon} config internally.
\r
507 * @param {String} icon The path to an image to display in the button
\r
508 * @return {Ext.Button} this
\r
510 setIcon : function(icon){
\r
513 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
\r
514 this.setButtonClass();
\r
520 * Gets the text for this Button
\r
521 * @return {String} The button text
\r
523 getText : function(){
\r
528 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
\r
529 * @param {Boolean} state (optional) Force a particular state
\r
530 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
\r
531 * @return {Ext.Button} this
\r
533 toggle : function(state, suppressEvent){
\r
534 state = state === undefined ? !this.pressed : !!state;
\r
535 if(state != this.pressed){
\r
537 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
\r
539 this.pressed = state;
\r
540 if(!suppressEvent){
\r
541 this.fireEvent('toggle', this, state);
\r
542 if(this.toggleHandler){
\r
543 this.toggleHandler.call(this.scope || this, this, state);
\r
553 focus : function(){
\r
554 this.btnEl.focus();
\r
558 onDisable : function(){
\r
559 this.onDisableChange(true);
\r
563 onEnable : function(){
\r
564 this.onDisableChange(false);
\r
567 onDisableChange : function(disabled){
\r
569 if(!Ext.isIE6 || !this.text){
\r
570 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
\r
572 this.el.dom.disabled = disabled;
\r
574 this.disabled = disabled;
\r
578 * Show this button's menu (if it has one)
\r
580 showMenu : function(){
\r
581 if(this.rendered && this.menu){
\r
583 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
\r
585 if(this.menu.isVisible()){
\r
588 this.menu.ownerCt = this;
\r
589 this.menu.show(this.el, this.menuAlign);
\r
595 * Hide this button's menu (if it has one)
\r
597 hideMenu : function(){
\r
598 if(this.hasVisibleMenu()){
\r
605 * Returns true if the button has a menu and it is visible
\r
606 * @return {Boolean}
\r
608 hasVisibleMenu : function(){
\r
609 return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
\r
613 onClick : function(e){
\r
615 e.preventDefault();
\r
617 if(e.button !== 0){
\r
620 if(!this.disabled){
\r
621 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
\r
624 if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
\r
627 this.fireEvent('click', this, e);
\r
629 //this.el.removeClass('x-btn-over');
\r
630 this.handler.call(this.scope || this, this, e);
\r
636 isMenuTriggerOver : function(e, internal){
\r
637 return this.menu && !internal;
\r
641 isMenuTriggerOut : function(e, internal){
\r
642 return this.menu && !internal;
\r
646 onMouseOver : function(e){
\r
647 if(!this.disabled){
\r
648 var internal = e.within(this.el, true);
\r
650 this.el.addClass('x-btn-over');
\r
651 if(!this.monitoringMouseOver){
\r
652 this.doc.on('mouseover', this.monitorMouseOver, this);
\r
653 this.monitoringMouseOver = true;
\r
655 this.fireEvent('mouseover', this, e);
\r
657 if(this.isMenuTriggerOver(e, internal)){
\r
658 this.fireEvent('menutriggerover', this, this.menu, e);
\r
664 monitorMouseOver : function(e){
\r
665 if(e.target != this.el.dom && !e.within(this.el)){
\r
666 if(this.monitoringMouseOver){
\r
667 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
668 this.monitoringMouseOver = false;
\r
670 this.onMouseOut(e);
\r
675 onMouseOut : function(e){
\r
676 var internal = e.within(this.el) && e.target != this.el.dom;
\r
677 this.el.removeClass('x-btn-over');
\r
678 this.fireEvent('mouseout', this, e);
\r
679 if(this.isMenuTriggerOut(e, internal)){
\r
680 this.fireEvent('menutriggerout', this, this.menu, e);
\r
684 focus : function() {
\r
685 this.btnEl.focus();
\r
688 blur : function() {
\r
693 onFocus : function(e){
\r
694 if(!this.disabled){
\r
695 this.el.addClass('x-btn-focus');
\r
699 onBlur : function(e){
\r
700 this.el.removeClass('x-btn-focus');
\r
704 getClickEl : function(e, isUp){
\r
709 onMouseDown : function(e){
\r
710 if(!this.disabled && e.button === 0){
\r
711 this.getClickEl(e).addClass('x-btn-click');
\r
712 this.doc.on('mouseup', this.onMouseUp, this);
\r
716 onMouseUp : function(e){
\r
717 if(e.button === 0){
\r
718 this.getClickEl(e, true).removeClass('x-btn-click');
\r
719 this.doc.un('mouseup', this.onMouseUp, this);
\r
723 onMenuShow : function(e){
\r
724 if(this.menu.ownerCt == this){
\r
725 this.menu.ownerCt = this;
\r
726 this.ignoreNextClick = 0;
\r
727 this.el.addClass('x-btn-menu-active');
\r
728 this.fireEvent('menushow', this, this.menu);
\r
732 onMenuHide : function(e){
\r
733 if(this.menu.ownerCt == this){
\r
734 this.el.removeClass('x-btn-menu-active');
\r
735 this.ignoreNextClick = this.restoreClick.defer(250, this);
\r
736 this.fireEvent('menuhide', this, this.menu);
\r
737 delete this.menu.ownerCt;
\r
742 restoreClick : function(){
\r
743 this.ignoreNextClick = 0;
\r
747 * @cfg {String} autoEl @hide
\r
750 * @cfg {String/Object} html @hide
\r
753 * @cfg {String} contentEl @hide
\r
756 * @cfg {Mixed} data @hide
\r
759 * @cfg {Mixed} tpl @hide
\r
762 * @cfg {String} tplWriteMode @hide
\r
765 Ext.reg('button', Ext.Button);
\r
767 // Private utility class used by Button
\r
768 Ext.ButtonToggleMgr = function(){
\r
771 function toggleGroup(btn, state){
\r
773 var g = groups[btn.toggleGroup];
\r
774 for(var i = 0, l = g.length; i < l; i++){
\r
776 g[i].toggle(false);
\r
783 register : function(btn){
\r
784 if(!btn.toggleGroup){
\r
787 var g = groups[btn.toggleGroup];
\r
789 g = groups[btn.toggleGroup] = [];
\r
792 btn.on('toggle', toggleGroup);
\r
795 unregister : function(btn){
\r
796 if(!btn.toggleGroup){
\r
799 var g = groups[btn.toggleGroup];
\r
802 btn.un('toggle', toggleGroup);
\r
807 * Gets the pressed button in the passed group or null
\r
808 * @param {String} group
\r
811 getPressed : function(group){
\r
812 var g = groups[group];
\r
814 for(var i = 0, len = g.length; i < len; i++){
\r
815 if(g[i].pressed === true){
\r