3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.Button"></div>/**
16 * @extends Ext.BoxComponent
18 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
19 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
20 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
21 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
22 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
23 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
24 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
26 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
27 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
28 * @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
29 * @cfg {Boolean} hidden True to start hidden (defaults to false)
30 * @cfg {Boolean} disabled True to start disabled (defaults to false)
31 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
32 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
33 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
34 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
37 * @param {Object} config The config object
40 Ext.Button = Ext.extend(Ext.BoxComponent, {
42 * Read-only. True if this button is hidden
47 * Read-only. True if this button is disabled
52 * Read-only. True if this button is pressed (only if enableToggle = true)
58 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
62 * @cfg {Boolean} allowDepress
63 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
67 * @cfg {Boolean} enableToggle
68 * True to enable pressed/not pressed toggling (defaults to false)
72 * @cfg {Function} toggleHandler
73 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
74 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
75 * <li><b>state</b> : Boolean<div class="sub-desc">The next state if the Button, true means pressed.</div></li>
80 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
83 * @cfg {String} menuAlign
84 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
89 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
90 * text to be used if this item is shown in the overflow menu. See also
91 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
94 * @cfg {String} iconCls
95 * A css class which sets a background image to be used as the icon for this button
99 * submit, reset or button - defaults to 'button'
104 menuClassTarget : 'tr:nth(2)',
107 * @cfg {String} clickEvent
108 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
109 * Defaults to <tt>'click'</tt>.
111 clickEvent : 'click',
114 * @cfg {Boolean} handleMouseEvents
115 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
117 handleMouseEvents : true,
120 * @cfg {String} tooltipType
121 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
123 tooltipType : 'qtip',
126 * @cfg {String} buttonSelector
127 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
128 * DOM structure created.</p>
129 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
130 * a focussable element.</p>
131 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
133 buttonSelector : 'button:first-child',
136 * @cfg {String} scale
137 * <p>(Optional) The size of the Button. Three values are allowed:</p>
138 * <ul class="mdetail-params">
139 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
140 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
141 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
143 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
148 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
149 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
150 * executed. Defaults to this Button.
154 * @cfg {String} iconAlign
155 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
156 * <ul class="mdetail-params">
157 * <li>'top'<div class="sub-desc"></div></li>
158 * <li>'right'<div class="sub-desc"></div></li>
159 * <li>'bottom'<div class="sub-desc"></div></li>
160 * <li>'left'<div class="sub-desc"></div></li>
162 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
167 * @cfg {String} arrowAlign
168 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
169 * Two values are allowed:</p>
170 * <ul class="mdetail-params">
171 * <li>'right'<div class="sub-desc"></div></li>
172 * <li>'bottom'<div class="sub-desc"></div></li>
174 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
176 arrowAlign : 'right',
179 * @cfg {Ext.Template} template (Optional)
180 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
181 * Instances, or subclasses which need a different DOM structure may provide a different
182 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
188 * A CSS class string to apply to the button's main element.
193 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
196 initComponent : function(){
197 Ext.Button.superclass.initComponent.call(this);
202 * Fires when this button is clicked
203 * @param {Button} this
204 * @param {EventObject} e The click event
209 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
210 * @param {Button} this
211 * @param {Boolean} pressed
216 * Fires when the mouse hovers over the button
217 * @param {Button} this
218 * @param {Event} e The event object
223 * Fires when the mouse exits the button
224 * @param {Button} this
225 * @param {Event} e The event object
230 * If this button has a menu, this event fires when it is shown
231 * @param {Button} this
237 * If this button has a menu, this event fires when it is hidden
238 * @param {Button} this
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
247 * @param {EventObject} e
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
255 * @param {EventObject} e
260 this.menu = Ext.menu.MenuMgr.get(this.menu);
262 if(Ext.isString(this.toggleGroup)){
263 this.enableToggle = true;
268 * <p>This method returns an object 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 Button's {@link #text}</li>
275 * <li>The <button>'s {@link #type}</li>
276 * <li>The {@link iconCls} applied to the <button> {@link #btnEl element}</li>
277 * <li>The {@link #cls} applied to the Button's main {@link #getEl Element}</li>
278 * <li>A CSS class name controlling the Button's {@link #scale} and {@link #iconAlign icon alignment}</li>
279 * <li>A CSS class name which applies an arrow to the Button if configured with a {@link #menu}</li>
281 * @return {Object} Substitution data for a Template.
283 getTemplateArgs : function(){
284 var cls = (this.cls || '');
285 cls += (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
287 cls += ' x-btn-pressed';
289 return [this.text || ' ', this.type, this.iconCls || '', cls, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass()];
293 getMenuClass : function(){
294 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
298 onRender : function(ct, position){
300 if(!Ext.Button.buttonTemplate){
301 // hideous table template
302 Ext.Button.buttonTemplate = new Ext.Template(
303 '<table cellspacing="0" class="x-btn {3}"><tbody class="{4}">',
304 '<tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
305 '<tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="{5}" unselectable="on"><button class="x-btn-text {2}" type="{1}">{0}</button></em></td><td class="x-btn-mr"><i> </i></td></tr>',
306 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
308 Ext.Button.buttonTemplate.compile();
310 this.template = Ext.Button.buttonTemplate;
313 var btn, targs = this.getTemplateArgs();
316 btn = this.template.insertBefore(position, targs, true);
318 btn = this.template.append(ct, targs, true);
321 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
322 * this references a <tt><button></tt> element. Read only.
326 this.btnEl = btn.child(this.buttonSelector);
327 this.mon(this.btnEl, {
333 this.initButtonEl(btn, this.btnEl);
335 Ext.ButtonToggleMgr.register(this);
339 initButtonEl : function(btn, btnEl){
344 c = Ext.Element.cache;
347 d.id = this.el.id = this.id;
351 btnEl.setStyle('background-image', 'url(' +this.icon +')');
353 if(this.tabIndex !== undefined){
354 btnEl.dom.tabIndex = this.tabIndex;
357 this.setTooltip(this.tooltip, true);
360 if(this.handleMouseEvents){
363 mouseover: this.onMouseOver,
364 mousedown: this.onMouseDown
367 // new functionality for monitoring on the document level
368 //this.mon(btn, 'mouseout', this.onMouseOut, this);
372 this.mon(this.menu, {
374 show: this.onMenuShow,
375 hide: this.onMenuHide
380 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
381 this.mon(repeater, 'click', this.onClick, this);
383 this.mon(btn, this.clickEvent, this.onClick, this);
387 afterRender : function(){
388 Ext.Button.superclass.afterRender.call(this);
393 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
394 * the value of the {@link iconCls} config internally.
395 * @param {String} cls The CSS class providing the icon image
396 * @return {Ext.Button} this
398 setIconClass : function(cls){
400 this.btnEl.replaceClass(this.iconCls, cls);
407 * Sets the tooltip for this Button.
408 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
409 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
410 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
412 * @return {Ext.Button} this
414 setTooltip : function(tooltip, /* private */ initial){
419 if(Ext.isObject(tooltip)){
420 Ext.QuickTips.register(Ext.apply({
421 target: this.btnEl.id
423 this.tooltip = tooltip;
425 this.btnEl.dom[this.tooltipType] = tooltip;
428 this.tooltip = tooltip;
434 clearTip : function(){
435 if(Ext.isObject(this.tooltip)){
436 Ext.QuickTips.unregister(this.btnEl);
441 beforeDestroy : function(){
445 Ext.destroy(this.menu, this.repeater);
449 onDestroy : function(){
450 var doc = Ext.getDoc();
451 doc.un('mouseover', this.monitorMouseOver, this);
452 doc.un('mouseup', this.onMouseUp, this);
454 Ext.ButtonToggleMgr.unregister(this);
459 doAutoWidth : function(){
460 if(this.el && this.text && this.width === undefined){
461 this.el.setWidth('auto');
462 if(Ext.isIE7 && Ext.isStrict){
464 if(ib && ib.getWidth() > 20){
466 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
470 if(this.el.getWidth() < this.minWidth){
471 this.el.setWidth(this.minWidth);
478 * Assigns this Button's click handler
479 * @param {Function} handler The function to call when the button is clicked
480 * @param {Object} scope (optional) Scope for the function passed in. 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.el.child('td.x-btn-mc ' + this.buttonSelector).update(text);
504 * Gets the text for this Button
505 * @return {String} The button text
507 getText : function(){
512 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
513 * @param {Boolean} state (optional) Force a particular state
514 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
515 * @return {Ext.Button} this
517 toggle : function(state, suppressEvent){
518 state = state === undefined ? !this.pressed : !!state;
519 if(state != this.pressed){
521 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
523 this.pressed = state;
525 this.fireEvent('toggle', this, state);
526 if(this.toggleHandler){
527 this.toggleHandler.call(this.scope || this, this, state);
542 onDisable : function(){
543 this.onDisableChange(true);
547 onEnable : function(){
548 this.onDisableChange(false);
551 onDisableChange : function(disabled){
553 if(!Ext.isIE6 || !this.text){
554 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
556 this.el.dom.disabled = disabled;
558 this.disabled = disabled;
562 * Show this button's menu (if it has one)
564 showMenu : function(){
565 if(this.rendered && this.menu){
567 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
569 this.menu.show(this.el, this.menuAlign);
575 * Hide this button's menu (if it has one)
577 hideMenu : function(){
585 * Returns true if the button has a menu and it is visible
588 hasVisibleMenu : function(){
589 return this.menu && this.menu.isVisible();
593 onClick : function(e){
601 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
604 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
607 this.fireEvent('click', this, e);
609 //this.el.removeClass('x-btn-over');
610 this.handler.call(this.scope || this, this, e);
616 isMenuTriggerOver : function(e, internal){
617 return this.menu && !internal;
621 isMenuTriggerOut : function(e, internal){
622 return this.menu && !internal;
626 onMouseOver : function(e){
628 var internal = e.within(this.el, true);
630 this.el.addClass('x-btn-over');
631 if(!this.monitoringMouseOver){
632 Ext.getDoc().on('mouseover', this.monitorMouseOver, this);
633 this.monitoringMouseOver = true;
635 this.fireEvent('mouseover', this, e);
637 if(this.isMenuTriggerOver(e, internal)){
638 this.fireEvent('menutriggerover', this, this.menu, e);
644 monitorMouseOver : function(e){
645 if(e.target != this.el.dom && !e.within(this.el)){
646 if(this.monitoringMouseOver){
647 Ext.getDoc().un('mouseover', this.monitorMouseOver, this);
648 this.monitoringMouseOver = false;
655 onMouseOut : function(e){
656 var internal = e.within(this.el) && e.target != this.el.dom;
657 this.el.removeClass('x-btn-over');
658 this.fireEvent('mouseout', this, e);
659 if(this.isMenuTriggerOut(e, internal)){
660 this.fireEvent('menutriggerout', this, this.menu, e);
664 onFocus : function(e){
666 this.el.addClass('x-btn-focus');
670 onBlur : function(e){
671 this.el.removeClass('x-btn-focus');
675 getClickEl : function(e, isUp){
680 onMouseDown : function(e){
681 if(!this.disabled && e.button === 0){
682 this.getClickEl(e).addClass('x-btn-click');
683 Ext.getDoc().on('mouseup', this.onMouseUp, this);
687 onMouseUp : function(e){
689 this.getClickEl(e, true).removeClass('x-btn-click');
690 Ext.getDoc().un('mouseup', this.onMouseUp, this);
694 onMenuShow : function(e){
695 this.ignoreNextClick = 0;
696 this.el.addClass('x-btn-menu-active');
697 this.fireEvent('menushow', this, this.menu);
700 onMenuHide : function(e){
701 this.el.removeClass('x-btn-menu-active');
702 this.ignoreNextClick = this.restoreClick.defer(250, this);
703 this.fireEvent('menuhide', this, this.menu);
707 restoreClick : function(){
708 this.ignoreNextClick = 0;
714 * @cfg {String} autoEl @hide
717 Ext.reg('button', Ext.Button);
719 // Private utility class used by Button
720 Ext.ButtonToggleMgr = function(){
723 function toggleGroup(btn, state){
725 var g = groups[btn.toggleGroup];
726 for(var i = 0, l = g.length; i < l; i++){
735 register : function(btn){
736 if(!btn.toggleGroup){
739 var g = groups[btn.toggleGroup];
741 g = groups[btn.toggleGroup] = [];
744 btn.on('toggle', toggleGroup);
747 unregister : function(btn){
748 if(!btn.toggleGroup){
751 var g = groups[btn.toggleGroup];
754 btn.un('toggle', toggleGroup);
759 * Gets the pressed button in the passed group or null
760 * @param {String} group
763 getPressed : function(group){
764 var g = groups[group];
766 for(var i = 0, len = g.length; i < len; i++){
767 if(g[i].pressed === true){