3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.Button"></div>/**
10 * @extends Ext.BoxComponent
12 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
13 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
14 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
15 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
16 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
17 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
18 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
20 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
21 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
22 * @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
23 * @cfg {Boolean} hidden True to start hidden (defaults to false)
24 * @cfg {Boolean} disabled True to start disabled (defaults to false)
25 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
26 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
27 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
28 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
31 * @param {Object} config The config object
34 Ext.Button = Ext.extend(Ext.BoxComponent, {
36 * Read-only. True if this button is hidden
41 * Read-only. True if this button is disabled
46 * Read-only. True if this button is pressed (only if enableToggle = true)
51 * The Button's owner {@link Ext.Panel} (defaults to undefined, and is set automatically when
52 * the Button is added to a container). Read-only.
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 type of event to map to the button's event handler (defaults to 'click')
110 clickEvent : 'click',
113 * @cfg {Boolean} handleMouseEvents
114 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
116 handleMouseEvents : true,
119 * @cfg {String} tooltipType
120 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
122 tooltipType : 'qtip',
125 * @cfg {String} buttonSelector
126 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
127 * DOM structure created.</p>
128 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
129 * a focussable element.</p>
130 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
132 buttonSelector : 'button:first-child',
135 * @cfg {String} scale
136 * <p>(Optional) The size of the Button. Three values are allowed:</p>
137 * <ul class="mdetail-params">
138 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
139 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
140 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
142 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
147 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
148 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
149 * executed. Defaults to this Button.
153 * @cfg {String} iconAlign
154 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
155 * <ul class="mdetail-params">
156 * <li>'top'<div class="sub-desc"></div></li>
157 * <li>'right'<div class="sub-desc"></div></li>
158 * <li>'bottom'<div class="sub-desc"></div></li>
159 * <li>'left'<div class="sub-desc"></div></li>
161 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
166 * @cfg {String} arrowAlign
167 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
168 * Two values are allowed:</p>
169 * <ul class="mdetail-params">
170 * <li>'right'<div class="sub-desc"></div></li>
171 * <li>'bottom'<div class="sub-desc"></div></li>
173 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
175 arrowAlign : 'right',
178 * @cfg {Ext.Template} template (Optional)
179 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
180 * Instances, or subclasses which need a different DOM structure may provide a different
181 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
187 * A CSS class string to apply to the button's main element.
192 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
195 initComponent : function(){
196 Ext.Button.superclass.initComponent.call(this);
201 * Fires when this button is clicked
202 * @param {Button} this
203 * @param {EventObject} e The click event
208 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
209 * @param {Button} this
210 * @param {Boolean} pressed
215 * Fires when the mouse hovers over the button
216 * @param {Button} this
217 * @param {Event} e The event object
222 * Fires when the mouse exits the button
223 * @param {Button} this
224 * @param {Event} e The event object
229 * If this button has a menu, this event fires when it is shown
230 * @param {Button} this
236 * If this button has a menu, this event fires when it is hidden
237 * @param {Button} this
242 * @event menutriggerover
243 * If this button has a menu, this event fires when the mouse enters the menu triggering element
244 * @param {Button} this
246 * @param {EventObject} e
250 * @event menutriggerout
251 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
252 * @param {Button} this
254 * @param {EventObject} e
259 this.menu = Ext.menu.MenuMgr.get(this.menu);
261 if(Ext.isString(this.toggleGroup)){
262 this.enableToggle = true;
267 * <p>This method returns an object which provides substitution parameters for the {@link #template Template} used
268 * to create this Button's DOM structure.</p>
269 * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
270 * own implementation of this method.</p>
271 * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
272 * following items:</p><div class="mdetail-params"><ul>
273 * <li>The Button's {@link #text}</li>
274 * <li>The <button>'s {@link #type}</li>
275 * <li>The {@link iconCls} applied to the <button> {@link #btnEl element}</li>
276 * <li>The {@link #cls} applied to the Button's main {@link #getEl Element}</li>
277 * <li>A CSS class name controlling the Button's {@link #scale} and {@link #iconAlign icon alignment}</li>
278 * <li>A CSS class name which applies an arrow to the Button if configured with a {@link #menu}</li>
280 * @return {Object} Substitution data for a Template.
282 getTemplateArgs : function(){
283 var cls = (this.cls || '');
284 cls += (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
286 cls += ' x-btn-pressed';
288 return [this.text || ' ', this.type, this.iconCls || '', cls, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass()];
292 getMenuClass : function(){
293 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
297 onRender : function(ct, position){
299 if(!Ext.Button.buttonTemplate){
300 // hideous table template
301 Ext.Button.buttonTemplate = new Ext.Template(
302 '<table cellspacing="0" class="x-btn {3}"><tbody class="{4}">',
303 '<tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
304 '<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>',
305 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
307 Ext.Button.buttonTemplate.compile();
309 this.template = Ext.Button.buttonTemplate;
312 var btn, targs = this.getTemplateArgs();
315 btn = this.template.insertBefore(position, targs, true);
317 btn = this.template.append(ct, targs, true);
320 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
321 * this references a <tt><button></tt> element. Read only.
325 this.btnEl = btn.child(this.buttonSelector);
326 this.mon(this.btnEl, {
332 this.initButtonEl(btn, this.btnEl);
334 Ext.ButtonToggleMgr.register(this);
338 initButtonEl : function(btn, btnEl){
342 this.el.dom.id = this.el.id = this.id;
345 btnEl.setStyle('background-image', 'url(' +this.icon +')');
347 if(this.tabIndex !== undefined){
348 btnEl.dom.tabIndex = this.tabIndex;
351 this.setTooltip(this.tooltip, true);
354 if(this.handleMouseEvents){
357 mouseover: this.onMouseOver,
358 mousedown: this.onMouseDown
361 // new functionality for monitoring on the document level
362 //this.mon(btn, 'mouseout', this.onMouseOut, this);
366 this.mon(this.menu, {
368 show: this.onMenuShow,
369 hide: this.onMenuHide
374 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
375 this.mon(repeater, 'click', this.onClick, this);
378 this.mon(btn, this.clickEvent, this.onClick, this);
382 afterRender : function(){
383 Ext.Button.superclass.afterRender.call(this);
388 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
389 * the value of the {@link iconCls} config internally.
390 * @param {String} cls The CSS class providing the icon image
391 * @return {Ext.Button} this
393 setIconClass : function(cls){
395 this.btnEl.replaceClass(this.iconCls, cls);
402 * Sets the tooltip for this Button.
403 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
404 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
405 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
407 * @return {Ext.Button} this
409 setTooltip : function(tooltip, /* private */ initial){
414 if(Ext.isObject(tooltip)){
415 Ext.QuickTips.register(Ext.apply({
416 target: this.btnEl.id
418 this.tooltip = tooltip;
420 this.btnEl.dom[this.tooltipType] = tooltip;
423 this.tooltip = tooltip;
429 clearTip : function(){
430 if(Ext.isObject(this.tooltip)){
431 Ext.QuickTips.unregister(this.btnEl);
436 beforeDestroy : function(){
440 Ext.destroy(this.menu, this.repeater);
444 onDestroy : function(){
445 var doc = Ext.getDoc();
446 doc.un('mouseover', this.monitorMouseOver, this);
447 doc.un('mouseup', this.onMouseUp, this);
449 Ext.ButtonToggleMgr.unregister(this);
454 doAutoWidth : function(){
455 if(this.el && this.text && this.width === undefined){
456 this.el.setWidth('auto');
457 if(Ext.isIE7 && Ext.isStrict){
459 if(ib && ib.getWidth() > 20){
461 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
465 if(this.el.getWidth() < this.minWidth){
466 this.el.setWidth(this.minWidth);
473 * Assigns this Button's click handler
474 * @param {Function} handler The function to call when the button is clicked
475 * @param {Object} scope (optional) Scope for the function passed in. Defaults to this Button.
476 * @return {Ext.Button} this
478 setHandler : function(handler, scope){
479 this.handler = handler;
485 * Sets this Button's text
486 * @param {String} text The button text
487 * @return {Ext.Button} this
489 setText : function(text){
492 this.el.child('td.x-btn-mc ' + this.buttonSelector).update(text);
499 * Gets the text for this Button
500 * @return {String} The button text
502 getText : function(){
507 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
508 * @param {Boolean} state (optional) Force a particular state
509 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
510 * @return {Ext.Button} this
512 toggle : function(state, suppressEvent){
513 state = state === undefined ? !this.pressed : !!state;
514 if(state != this.pressed){
516 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
518 this.pressed = state;
520 this.fireEvent('toggle', this, state);
521 if(this.toggleHandler){
522 this.toggleHandler.call(this.scope || this, this, state);
537 onDisable : function(){
538 this.onDisableChange(true);
542 onEnable : function(){
543 this.onDisableChange(false);
546 onDisableChange : function(disabled){
548 if(!Ext.isIE6 || !this.text){
549 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
551 this.el.dom.disabled = disabled;
553 this.disabled = disabled;
557 * Show this button's menu (if it has one)
559 showMenu : function(){
560 if(this.rendered && this.menu){
562 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
564 this.menu.show(this.el, this.menuAlign);
570 * Hide this button's menu (if it has one)
572 hideMenu : function(){
580 * Returns true if the button has a menu and it is visible
583 hasVisibleMenu : function(){
584 return this.menu && this.menu.isVisible();
588 onClick : function(e){
596 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
599 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
602 this.fireEvent('click', this, e);
604 //this.el.removeClass('x-btn-over');
605 this.handler.call(this.scope || this, this, e);
611 isMenuTriggerOver : function(e, internal){
612 return this.menu && !internal;
616 isMenuTriggerOut : function(e, internal){
617 return this.menu && !internal;
621 onMouseOver : function(e){
623 var internal = e.within(this.el, true);
625 this.el.addClass('x-btn-over');
626 if(!this.monitoringMouseOver){
627 Ext.getDoc().on('mouseover', this.monitorMouseOver, this);
628 this.monitoringMouseOver = true;
630 this.fireEvent('mouseover', this, e);
632 if(this.isMenuTriggerOver(e, internal)){
633 this.fireEvent('menutriggerover', this, this.menu, e);
639 monitorMouseOver : function(e){
640 if(e.target != this.el.dom && !e.within(this.el)){
641 if(this.monitoringMouseOver){
642 Ext.getDoc().un('mouseover', this.monitorMouseOver, this);
643 this.monitoringMouseOver = false;
650 onMouseOut : function(e){
651 var internal = e.within(this.el) && e.target != this.el.dom;
652 this.el.removeClass('x-btn-over');
653 this.fireEvent('mouseout', this, e);
654 if(this.isMenuTriggerOut(e, internal)){
655 this.fireEvent('menutriggerout', this, this.menu, e);
659 onFocus : function(e){
661 this.el.addClass('x-btn-focus');
665 onBlur : function(e){
666 this.el.removeClass('x-btn-focus');
670 getClickEl : function(e, isUp){
675 onMouseDown : function(e){
676 if(!this.disabled && e.button === 0){
677 this.getClickEl(e).addClass('x-btn-click');
678 Ext.getDoc().on('mouseup', this.onMouseUp, this);
682 onMouseUp : function(e){
684 this.getClickEl(e, true).removeClass('x-btn-click');
685 Ext.getDoc().un('mouseup', this.onMouseUp, this);
689 onMenuShow : function(e){
690 this.ignoreNextClick = 0;
691 this.el.addClass('x-btn-menu-active');
692 this.fireEvent('menushow', this, this.menu);
695 onMenuHide : function(e){
696 this.el.removeClass('x-btn-menu-active');
697 this.ignoreNextClick = this.restoreClick.defer(250, this);
698 this.fireEvent('menuhide', this, this.menu);
702 restoreClick : function(){
703 this.ignoreNextClick = 0;
709 * @cfg {String} autoEl @hide
712 Ext.reg('button', Ext.Button);
714 // Private utility class used by Button
715 Ext.ButtonToggleMgr = function(){
718 function toggleGroup(btn, state){
720 var g = groups[btn.toggleGroup];
721 for(var i = 0, l = g.length; i < l; i++){
730 register : function(btn){
731 if(!btn.toggleGroup){
734 var g = groups[btn.toggleGroup];
736 g = groups[btn.toggleGroup] = [];
739 btn.on('toggle', toggleGroup);
742 unregister : function(btn){
743 if(!btn.toggleGroup){
746 var g = groups[btn.toggleGroup];
749 btn.un('toggle', toggleGroup);
754 * Gets the pressed button in the passed group or null
755 * @param {String} group
758 getPressed : function(group){
759 var g = groups[group];
761 for(var i = 0, len = g.length; i < len; i++){
762 if(g[i].pressed === true){