3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.Button"></div>/**
17 * @extends Ext.BoxComponent
19 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
20 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
21 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
22 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
23 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
24 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
25 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
27 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
28 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
29 * @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
30 * @cfg {Boolean} hidden True to start hidden (defaults to false)
31 * @cfg {Boolean} disabled True to start disabled (defaults to false)
32 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
33 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
34 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
35 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
38 * @param {Object} config The config object
41 Ext.Button = Ext.extend(Ext.BoxComponent, {
43 * Read-only. True if this button is hidden
48 * Read-only. True if this button is disabled
53 * Read-only. True if this button is pressed (only if enableToggle = true)
59 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
63 * @cfg {Boolean} allowDepress
64 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
68 * @cfg {Boolean} enableToggle
69 * True to enable pressed/not pressed toggling (defaults to false)
73 * @cfg {Function} toggleHandler
74 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
75 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
76 * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
81 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
84 * @cfg {String} menuAlign
85 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
90 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
91 * text to be used if this item is shown in the overflow menu. See also
92 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
95 * @cfg {String} iconCls
96 * A css class which sets a background image to be used as the icon for this button
100 * submit, reset or button - defaults to 'button'
105 menuClassTarget : 'tr:nth(2)',
108 * @cfg {String} clickEvent
109 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
110 * Defaults to <tt>'click'</tt>.
112 clickEvent : 'click',
115 * @cfg {Boolean} handleMouseEvents
116 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
118 handleMouseEvents : true,
121 * @cfg {String} tooltipType
122 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
124 tooltipType : 'qtip',
127 * @cfg {String} buttonSelector
128 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
129 * DOM structure created.</p>
130 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
131 * a focussable element.</p>
132 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
134 buttonSelector : 'button:first-child',
137 * @cfg {String} scale
138 * <p>(Optional) The size of the Button. Three values are allowed:</p>
139 * <ul class="mdetail-params">
140 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
141 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
142 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
144 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
149 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
150 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
151 * executed. Defaults to this Button.
155 * @cfg {String} iconAlign
156 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
157 * <ul class="mdetail-params">
158 * <li>'top'<div class="sub-desc"></div></li>
159 * <li>'right'<div class="sub-desc"></div></li>
160 * <li>'bottom'<div class="sub-desc"></div></li>
161 * <li>'left'<div class="sub-desc"></div></li>
163 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
168 * @cfg {String} arrowAlign
169 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
170 * Two values are allowed:</p>
171 * <ul class="mdetail-params">
172 * <li>'right'<div class="sub-desc"></div></li>
173 * <li>'bottom'<div class="sub-desc"></div></li>
175 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
177 arrowAlign : 'right',
180 * @cfg {Ext.Template} template (Optional)
181 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
182 * Instances, or subclasses which need a different DOM structure may provide a different
183 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
189 * A CSS class string to apply to the button's main element.
194 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
197 * @cfg {Boolean} autoWidth
198 * By default, if a width is not specified the button will attempt to stretch horizontally to fit its content.
199 * If the button is being managed by a width sizing layout (hbox, fit, anchor), set this to false to prevent
200 * the button from doing this automatic sizing.
201 * Defaults to <tt>undefined</tt>.
204 initComponent : function(){
206 this.menu = Ext.menu.MenuMgr.get(this.menu);
207 this.menu.ownerCt = this;
210 Ext.Button.superclass.initComponent.call(this);
215 * Fires when this button is clicked
216 * @param {Button} this
217 * @param {EventObject} e The click event
222 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
223 * @param {Button} this
224 * @param {Boolean} pressed
229 * Fires when the mouse hovers over the button
230 * @param {Button} this
231 * @param {Event} e The event object
236 * Fires when the mouse exits the button
237 * @param {Button} this
238 * @param {Event} e The event object
243 * If this button has a menu, this event fires when it is shown
244 * @param {Button} this
250 * If this button has a menu, this event fires when it is hidden
251 * @param {Button} this
256 * @event menutriggerover
257 * If this button has a menu, this event fires when the mouse enters the menu triggering element
258 * @param {Button} this
260 * @param {EventObject} e
264 * @event menutriggerout
265 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
266 * @param {Button} this
268 * @param {EventObject} e
274 this.menu.ownerCt = undefined;
276 if(Ext.isString(this.toggleGroup)){
277 this.enableToggle = true;
282 * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used
283 * to create this Button's DOM structure.</p>
284 * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
285 * own implementation of this method.</p>
286 * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
287 * following items:</p><div class="mdetail-params"><ul>
288 * <li>The <button>'s {@link #type}</li>
289 * <li>A CSS class name applied to the Button's main <tbody> element which determines the button's scale and icon alignment.</li>
290 * <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>
291 * <li>The {@link #cls} CSS class name applied to the button's wrapping <table> element.</li>
292 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
294 * @return {Array} Substitution data for a Template.
296 getTemplateArgs : function(){
297 return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
301 setButtonClass : function(){
302 if(this.useSetClass){
303 if(!Ext.isEmpty(this.oldCls)){
304 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
306 this.oldCls = (this.iconCls || this.icon) ? (this.text ? 'x-btn-text-icon' : 'x-btn-icon') : 'x-btn-noicon';
307 this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
312 getMenuClass : function(){
313 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
317 onRender : function(ct, position){
319 if(!Ext.Button.buttonTemplate){
320 // hideous table template
321 Ext.Button.buttonTemplate = new Ext.Template(
322 '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
323 '<tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
324 '<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>',
325 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
327 Ext.Button.buttonTemplate.compile();
329 this.template = Ext.Button.buttonTemplate;
332 var btn, targs = this.getTemplateArgs();
335 btn = this.template.insertBefore(position, targs, true);
337 btn = this.template.append(ct, targs, true);
340 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
341 * this references a <tt><button></tt> element. Read only.
345 this.btnEl = btn.child(this.buttonSelector);
346 this.mon(this.btnEl, {
352 this.initButtonEl(btn, this.btnEl);
354 Ext.ButtonToggleMgr.register(this);
358 initButtonEl : function(btn, btnEl){
360 this.setIcon(this.icon);
361 this.setText(this.text);
362 this.setIconClass(this.iconCls);
363 if(Ext.isDefined(this.tabIndex)){
364 btnEl.dom.tabIndex = this.tabIndex;
367 this.setTooltip(this.tooltip, true);
370 if(this.handleMouseEvents){
373 mouseover: this.onMouseOver,
374 mousedown: this.onMouseDown
377 // new functionality for monitoring on the document level
378 //this.mon(btn, 'mouseout', this.onMouseOut, this);
382 this.mon(this.menu, {
384 show: this.onMenuShow,
385 hide: this.onMenuHide
390 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
391 this.mon(repeater, 'click', this.onRepeatClick, this);
393 this.mon(btn, this.clickEvent, this.onClick, this);
398 afterRender : function(){
399 Ext.Button.superclass.afterRender.call(this);
400 this.useSetClass = true;
401 this.setButtonClass();
402 this.doc = Ext.getDoc();
407 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
408 * the value of the {@link iconCls} config internally.
409 * @param {String} cls The CSS class providing the icon image
410 * @return {Ext.Button} this
412 setIconClass : function(cls){
415 this.btnEl.dom.className = '';
416 this.btnEl.addClass(['x-btn-text', cls || '']);
417 this.setButtonClass();
423 * Sets the tooltip for this Button.
424 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
425 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
426 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
428 * @return {Ext.Button} this
430 setTooltip : function(tooltip, /* private */ initial){
435 if(Ext.isObject(tooltip)){
436 Ext.QuickTips.register(Ext.apply({
437 target: this.btnEl.id
439 this.tooltip = tooltip;
441 this.btnEl.dom[this.tooltipType] = tooltip;
444 this.tooltip = tooltip;
450 clearTip : function(){
451 if(Ext.isObject(this.tooltip)){
452 Ext.QuickTips.unregister(this.btnEl);
457 beforeDestroy : function(){
461 if(this.menu && this.destroyMenu !== false) {
462 Ext.destroy(this.btnEl, this.menu);
464 Ext.destroy(this.repeater);
468 onDestroy : function(){
470 this.doc.un('mouseover', this.monitorMouseOver, this);
471 this.doc.un('mouseup', this.onMouseUp, this);
474 Ext.ButtonToggleMgr.unregister(this);
476 Ext.Button.superclass.onDestroy.call(this);
480 doAutoWidth : function(){
481 if(this.autoWidth !== false && this.el && this.text && this.width === undefined){
482 this.el.setWidth('auto');
483 if(Ext.isIE7 && Ext.isStrict){
485 if(ib && ib.getWidth() > 20){
487 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
491 if(this.el.getWidth() < this.minWidth){
492 this.el.setWidth(this.minWidth);
499 * Assigns this Button's click handler
500 * @param {Function} handler The function to call when the button is clicked
501 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
502 * Defaults to this Button.
503 * @return {Ext.Button} this
505 setHandler : function(handler, scope){
506 this.handler = handler;
512 * Sets this Button's text
513 * @param {String} text The button text
514 * @return {Ext.Button} this
516 setText : function(text){
519 this.btnEl.update(text || ' ');
520 this.setButtonClass();
527 * Sets the background image (inline style) of the button. This method also changes
528 * the value of the {@link icon} config internally.
529 * @param {String} icon The path to an image to display in the button
530 * @return {Ext.Button} this
532 setIcon : function(icon){
535 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
536 this.setButtonClass();
542 * Gets the text for this Button
543 * @return {String} The button text
545 getText : function(){
550 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
551 * @param {Boolean} state (optional) Force a particular state
552 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
553 * @return {Ext.Button} this
555 toggle : function(state, suppressEvent){
556 state = state === undefined ? !this.pressed : !!state;
557 if(state != this.pressed){
559 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
561 this.pressed = state;
563 this.fireEvent('toggle', this, state);
564 if(this.toggleHandler){
565 this.toggleHandler.call(this.scope || this, this, state);
573 onDisable : function(){
574 this.onDisableChange(true);
578 onEnable : function(){
579 this.onDisableChange(false);
582 onDisableChange : function(disabled){
584 if(!Ext.isIE6 || !this.text){
585 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
587 this.el.dom.disabled = disabled;
589 this.disabled = disabled;
593 * Show this button's menu (if it has one)
595 showMenu : function(){
596 if(this.rendered && this.menu){
598 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
600 if(this.menu.isVisible()){
603 this.menu.ownerCt = this;
604 this.menu.show(this.el, this.menuAlign);
610 * Hide this button's menu (if it has one)
612 hideMenu : function(){
613 if(this.hasVisibleMenu()){
620 * Returns true if the button has a menu and it is visible
623 hasVisibleMenu : function(){
624 return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
628 onRepeatClick : function(repeat, e){
633 onClick : function(e){
642 if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
645 this.fireEvent('click', this, e);
647 //this.el.removeClass('x-btn-over');
648 this.handler.call(this.scope || this, this, e);
654 doToggle: function(){
655 if (this.enableToggle && (this.allowDepress !== false || !this.pressed)) {
661 isMenuTriggerOver : function(e, internal){
662 return this.menu && !internal;
666 isMenuTriggerOut : function(e, internal){
667 return this.menu && !internal;
671 onMouseOver : function(e){
673 var internal = e.within(this.el, true);
675 this.el.addClass('x-btn-over');
676 if(!this.monitoringMouseOver){
677 this.doc.on('mouseover', this.monitorMouseOver, this);
678 this.monitoringMouseOver = true;
680 this.fireEvent('mouseover', this, e);
682 if(this.isMenuTriggerOver(e, internal)){
683 this.fireEvent('menutriggerover', this, this.menu, e);
689 monitorMouseOver : function(e){
690 if(e.target != this.el.dom && !e.within(this.el)){
691 if(this.monitoringMouseOver){
692 this.doc.un('mouseover', this.monitorMouseOver, this);
693 this.monitoringMouseOver = false;
700 onMouseOut : function(e){
701 var internal = e.within(this.el) && e.target != this.el.dom;
702 this.el.removeClass('x-btn-over');
703 this.fireEvent('mouseout', this, e);
704 if(this.isMenuTriggerOut(e, internal)){
705 this.fireEvent('menutriggerout', this, this.menu, e);
718 onFocus : function(e){
720 this.el.addClass('x-btn-focus');
724 onBlur : function(e){
725 this.el.removeClass('x-btn-focus');
729 getClickEl : function(e, isUp){
734 onMouseDown : function(e){
735 if(!this.disabled && e.button === 0){
736 this.getClickEl(e).addClass('x-btn-click');
737 this.doc.on('mouseup', this.onMouseUp, this);
741 onMouseUp : function(e){
743 this.getClickEl(e, true).removeClass('x-btn-click');
744 this.doc.un('mouseup', this.onMouseUp, this);
748 onMenuShow : function(e){
749 if(this.menu.ownerCt == this){
750 this.menu.ownerCt = this;
751 this.ignoreNextClick = 0;
752 this.el.addClass('x-btn-menu-active');
753 this.fireEvent('menushow', this, this.menu);
757 onMenuHide : function(e){
758 if(this.menu.ownerCt == this){
759 this.el.removeClass('x-btn-menu-active');
760 this.ignoreNextClick = this.restoreClick.defer(250, this);
761 this.fireEvent('menuhide', this, this.menu);
762 delete this.menu.ownerCt;
767 restoreClick : function(){
768 this.ignoreNextClick = 0;
772 * @cfg {String} autoEl @hide
775 * @cfg {String/Object} html @hide
778 * @cfg {String} contentEl @hide
781 * @cfg {Mixed} data @hide
784 * @cfg {Mixed} tpl @hide
787 * @cfg {String} tplWriteMode @hide
790 Ext.reg('button', Ext.Button);
792 // Private utility class used by Button
793 Ext.ButtonToggleMgr = function(){
796 function toggleGroup(btn, state){
798 var g = groups[btn.toggleGroup];
799 for(var i = 0, l = g.length; i < l; i++){
808 register : function(btn){
809 if(!btn.toggleGroup){
812 var g = groups[btn.toggleGroup];
814 g = groups[btn.toggleGroup] = [];
817 btn.on('toggle', toggleGroup);
820 unregister : function(btn){
821 if(!btn.toggleGroup){
824 var g = groups[btn.toggleGroup];
827 btn.un('toggle', toggleGroup);
832 * Gets the pressed button in the passed group or null
833 * @param {String} group
836 getPressed : function(group){
837 var g = groups[group];
839 for(var i = 0, len = g.length; i < len; i++){
840 if(g[i].pressed === true){