3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.Button"></div>/**
\r
11 * @extends Ext.BoxComponent
\r
12 * Simple Button class
\r
13 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
\r
14 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
\r
15 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
\r
16 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
\r
17 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
\r
18 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
\r
19 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
\r
21 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
\r
22 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
\r
23 * @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
24 * @cfg {Boolean} hidden True to start hidden (defaults to false)
\r
25 * @cfg {Boolean} disabled True to start disabled (defaults to false)
\r
26 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
\r
27 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
\r
28 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
\r
29 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
\r
31 * Create a new button
\r
32 * @param {Object} config The config object
\r
35 Ext.Button = Ext.extend(Ext.BoxComponent, {
\r
37 * Read-only. True if this button is hidden
\r
42 * Read-only. True if this button is disabled
\r
47 * Read-only. True if this button is pressed (only if enableToggle = true)
\r
53 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
\r
57 * @cfg {Boolean} allowDepress
\r
58 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
\r
62 * @cfg {Boolean} enableToggle
\r
63 * True to enable pressed/not pressed toggling (defaults to false)
\r
65 enableToggle : false,
\r
67 * @cfg {Function} toggleHandler
\r
68 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
\r
69 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
\r
70 * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
\r
75 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
\r
78 * @cfg {String} menuAlign
\r
79 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
\r
81 menuAlign : 'tl-bl?',
\r
84 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
\r
85 * text to be used if this item is shown in the overflow menu. See also
\r
86 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
\r
89 * @cfg {String} iconCls
\r
90 * A css class which sets a background image to be used as the icon for this button
\r
93 * @cfg {String} type
\r
94 * submit, reset or button - defaults to 'button'
\r
99 menuClassTarget : 'tr:nth(2)',
\r
102 * @cfg {String} clickEvent
\r
103 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
\r
104 * Defaults to <tt>'click'</tt>.
\r
106 clickEvent : 'click',
\r
109 * @cfg {Boolean} handleMouseEvents
\r
110 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
\r
112 handleMouseEvents : true,
\r
115 * @cfg {String} tooltipType
\r
116 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
\r
118 tooltipType : 'qtip',
\r
121 * @cfg {String} buttonSelector
\r
122 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
\r
123 * DOM structure created.</p>
\r
124 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
\r
125 * a focussable element.</p>
\r
126 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
\r
128 buttonSelector : 'button:first-child',
\r
131 * @cfg {String} scale
\r
132 * <p>(Optional) The size of the Button. Three values are allowed:</p>
\r
133 * <ul class="mdetail-params">
\r
134 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
\r
135 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
\r
136 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
\r
138 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
\r
143 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
\r
144 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
\r
145 * executed. Defaults to this Button.
\r
149 * @cfg {String} iconAlign
\r
150 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
\r
151 * <ul class="mdetail-params">
\r
152 * <li>'top'<div class="sub-desc"></div></li>
\r
153 * <li>'right'<div class="sub-desc"></div></li>
\r
154 * <li>'bottom'<div class="sub-desc"></div></li>
\r
155 * <li>'left'<div class="sub-desc"></div></li>
\r
157 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
\r
159 iconAlign : 'left',
\r
162 * @cfg {String} arrowAlign
\r
163 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
\r
164 * Two values are allowed:</p>
\r
165 * <ul class="mdetail-params">
\r
166 * <li>'right'<div class="sub-desc"></div></li>
\r
167 * <li>'bottom'<div class="sub-desc"></div></li>
\r
169 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
\r
171 arrowAlign : 'right',
\r
174 * @cfg {Ext.Template} template (Optional)
\r
175 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
\r
176 * Instances, or subclasses which need a different DOM structure may provide a different
\r
177 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
\r
178 * @type Ext.Template
\r
179 * @property template
\r
182 * @cfg {String} cls
\r
183 * A CSS class string to apply to the button's main element.
\r
188 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
\r
191 initComponent : function(){
\r
192 Ext.Button.superclass.initComponent.call(this);
\r
197 * Fires when this button is clicked
\r
198 * @param {Button} this
\r
199 * @param {EventObject} e The click event
\r
204 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
\r
205 * @param {Button} this
\r
206 * @param {Boolean} pressed
\r
211 * Fires when the mouse hovers over the button
\r
212 * @param {Button} this
\r
213 * @param {Event} e The event object
\r
218 * Fires when the mouse exits the button
\r
219 * @param {Button} this
\r
220 * @param {Event} e The event object
\r
225 * If this button has a menu, this event fires when it is shown
\r
226 * @param {Button} this
\r
227 * @param {Menu} menu
\r
232 * If this button has a menu, this event fires when it is hidden
\r
233 * @param {Button} this
\r
234 * @param {Menu} menu
\r
238 * @event menutriggerover
\r
239 * If this button has a menu, this event fires when the mouse enters the menu triggering element
\r
240 * @param {Button} this
\r
241 * @param {Menu} menu
\r
242 * @param {EventObject} e
\r
246 * @event menutriggerout
\r
247 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
\r
248 * @param {Button} this
\r
249 * @param {Menu} menu
\r
250 * @param {EventObject} e
\r
255 this.menu = Ext.menu.MenuMgr.get(this.menu);
\r
257 if(Ext.isString(this.toggleGroup)){
\r
258 this.enableToggle = true;
\r
263 * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used
\r
264 * to create this Button's DOM structure.</p>
\r
265 * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
\r
266 * own implementation of this method.</p>
\r
267 * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
\r
268 * following items:</p><div class="mdetail-params"><ul>
\r
269 * <li>The <button>'s {@link #type}</li>
\r
270 * <li>A CSS class name applied to the Button's main <tbody> element which determines the button's scale and icon alignment.</li>
\r
271 * <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
272 * <li>The {@link #cls} CSS class name applied to the button's wrapping <table> element.</li>
\r
273 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
\r
275 * @return {Array} Substitution data for a Template.
\r
277 getTemplateArgs : function(){
\r
278 return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
\r
282 setButtonClass : function(){
\r
283 if(this.useSetClass){
\r
284 if(!Ext.isEmpty(this.oldCls)){
\r
285 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
\r
287 this.oldCls = (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
\r
288 this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
\r
293 getMenuClass : function(){
\r
294 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
\r
298 onRender : function(ct, position){
\r
299 if(!this.template){
\r
300 if(!Ext.Button.buttonTemplate){
\r
301 // hideous table template
\r
302 Ext.Button.buttonTemplate = new Ext.Template(
\r
303 '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
\r
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>',
\r
305 '<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
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>',
\r
307 '</tbody></table>');
\r
308 Ext.Button.buttonTemplate.compile();
\r
310 this.template = Ext.Button.buttonTemplate;
\r
313 var btn, targs = this.getTemplateArgs();
\r
316 btn = this.template.insertBefore(position, targs, true);
\r
318 btn = this.template.append(ct, targs, true);
\r
321 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
\r
322 * this references a <tt><button></tt> element. Read only.
\r
323 * @type Ext.Element
\r
326 this.btnEl = btn.child(this.buttonSelector);
\r
327 this.mon(this.btnEl, {
\r
329 focus: this.onFocus,
\r
333 this.initButtonEl(btn, this.btnEl);
\r
335 Ext.ButtonToggleMgr.register(this);
\r
339 initButtonEl : function(btn, btnEl){
\r
341 this.setIcon(this.icon);
\r
342 this.setText(this.text);
\r
343 this.setIconClass(this.iconCls);
\r
344 if(Ext.isDefined(this.tabIndex)){
\r
345 btnEl.dom.tabIndex = this.tabIndex;
\r
348 this.setTooltip(this.tooltip, true);
\r
351 if(this.handleMouseEvents){
\r
354 mouseover: this.onMouseOver,
\r
355 mousedown: this.onMouseDown
\r
358 // new functionality for monitoring on the document level
\r
359 //this.mon(btn, 'mouseout', this.onMouseOut, this);
\r
363 this.mon(this.menu, {
\r
365 show: this.onMenuShow,
\r
366 hide: this.onMenuHide
\r
371 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
\r
372 this.mon(repeater, 'click', this.onClick, this);
\r
374 this.mon(btn, this.clickEvent, this.onClick, this);
\r
378 afterRender : function(){
\r
379 Ext.Button.superclass.afterRender.call(this);
\r
380 this.useSetClass = true;
\r
381 this.setButtonClass();
\r
382 this.doc = Ext.getDoc();
\r
383 this.doAutoWidth();
\r
387 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
\r
388 * the value of the {@link iconCls} config internally.
\r
389 * @param {String} cls The CSS class providing the icon image
\r
390 * @return {Ext.Button} this
\r
392 setIconClass : function(cls){
\r
393 this.iconCls = cls;
\r
395 this.btnEl.dom.className = '';
\r
396 this.btnEl.addClass(['x-btn-text', cls || '']);
\r
397 this.setButtonClass();
\r
403 * Sets the tooltip for this Button.
\r
404 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
\r
405 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
\r
406 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
\r
408 * @return {Ext.Button} this
\r
410 setTooltip : function(tooltip, /* private */ initial){
\r
415 if(Ext.isObject(tooltip)){
\r
416 Ext.QuickTips.register(Ext.apply({
\r
417 target: this.btnEl.id
\r
419 this.tooltip = tooltip;
\r
421 this.btnEl.dom[this.tooltipType] = tooltip;
\r
424 this.tooltip = tooltip;
\r
430 clearTip : function(){
\r
431 if(Ext.isObject(this.tooltip)){
\r
432 Ext.QuickTips.unregister(this.btnEl);
\r
437 beforeDestroy : function(){
\r
441 if(this.menu && this.destroyMenu !== false) {
\r
442 Ext.destroy(this.menu);
\r
444 Ext.destroy(this.repeater);
\r
448 onDestroy : function(){
\r
450 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
451 this.doc.un('mouseup', this.onMouseUp, this);
\r
454 Ext.ButtonToggleMgr.unregister(this);
\r
456 Ext.Button.superclass.onDestroy.call(this);
\r
460 doAutoWidth : function(){
\r
461 if(this.el && this.text && this.width === undefined){
\r
462 this.el.setWidth('auto');
\r
463 if(Ext.isIE7 && Ext.isStrict){
\r
464 var ib = this.btnEl;
\r
465 if(ib && ib.getWidth() > 20){
\r
467 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
\r
471 if(this.el.getWidth() < this.minWidth){
\r
472 this.el.setWidth(this.minWidth);
\r
479 * Assigns this Button's click handler
\r
480 * @param {Function} handler The function to call when the button is clicked
\r
481 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
\r
482 * Defaults to this Button.
\r
483 * @return {Ext.Button} this
\r
485 setHandler : function(handler, scope){
\r
486 this.handler = handler;
\r
487 this.scope = scope;
\r
492 * Sets this Button's text
\r
493 * @param {String} text The button text
\r
494 * @return {Ext.Button} this
\r
496 setText : function(text){
\r
499 this.btnEl.update(text || ' ');
\r
500 this.setButtonClass();
\r
502 this.doAutoWidth();
\r
507 * Sets the background image (inline style) of the button. This method also changes
\r
508 * the value of the {@link icon} config internally.
\r
509 * @param {String} icon The path to an image to display in the button
\r
510 * @return {Ext.Button} this
\r
512 setIcon : function(icon){
\r
515 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
\r
516 this.setButtonClass();
\r
522 * Gets the text for this Button
\r
523 * @return {String} The button text
\r
525 getText : function(){
\r
530 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
\r
531 * @param {Boolean} state (optional) Force a particular state
\r
532 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
\r
533 * @return {Ext.Button} this
\r
535 toggle : function(state, suppressEvent){
\r
536 state = state === undefined ? !this.pressed : !!state;
\r
537 if(state != this.pressed){
\r
539 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
\r
541 this.pressed = state;
\r
542 if(!suppressEvent){
\r
543 this.fireEvent('toggle', this, state);
\r
544 if(this.toggleHandler){
\r
545 this.toggleHandler.call(this.scope || this, this, state);
\r
555 focus : function(){
\r
556 this.btnEl.focus();
\r
560 onDisable : function(){
\r
561 this.onDisableChange(true);
\r
565 onEnable : function(){
\r
566 this.onDisableChange(false);
\r
569 onDisableChange : function(disabled){
\r
571 if(!Ext.isIE6 || !this.text){
\r
572 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
\r
574 this.el.dom.disabled = disabled;
\r
576 this.disabled = disabled;
\r
580 * Show this button's menu (if it has one)
\r
582 showMenu : function(){
\r
583 if(this.rendered && this.menu){
\r
585 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
\r
587 if(this.menu.isVisible()){
\r
590 this.menu.ownerCt = this;
\r
591 this.menu.show(this.el, this.menuAlign);
\r
597 * Hide this button's menu (if it has one)
\r
599 hideMenu : function(){
\r
600 if(this.hasVisibleMenu()){
\r
607 * Returns true if the button has a menu and it is visible
\r
608 * @return {Boolean}
\r
610 hasVisibleMenu : function(){
\r
611 return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
\r
615 onClick : function(e){
\r
617 e.preventDefault();
\r
619 if(e.button !== 0){
\r
622 if(!this.disabled){
\r
623 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
\r
626 if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
\r
629 this.fireEvent('click', this, e);
\r
631 //this.el.removeClass('x-btn-over');
\r
632 this.handler.call(this.scope || this, this, e);
\r
638 isMenuTriggerOver : function(e, internal){
\r
639 return this.menu && !internal;
\r
643 isMenuTriggerOut : function(e, internal){
\r
644 return this.menu && !internal;
\r
648 onMouseOver : function(e){
\r
649 if(!this.disabled){
\r
650 var internal = e.within(this.el, true);
\r
652 this.el.addClass('x-btn-over');
\r
653 if(!this.monitoringMouseOver){
\r
654 this.doc.on('mouseover', this.monitorMouseOver, this);
\r
655 this.monitoringMouseOver = true;
\r
657 this.fireEvent('mouseover', this, e);
\r
659 if(this.isMenuTriggerOver(e, internal)){
\r
660 this.fireEvent('menutriggerover', this, this.menu, e);
\r
666 monitorMouseOver : function(e){
\r
667 if(e.target != this.el.dom && !e.within(this.el)){
\r
668 if(this.monitoringMouseOver){
\r
669 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
670 this.monitoringMouseOver = false;
\r
672 this.onMouseOut(e);
\r
677 onMouseOut : function(e){
\r
678 var internal = e.within(this.el) && e.target != this.el.dom;
\r
679 this.el.removeClass('x-btn-over');
\r
680 this.fireEvent('mouseout', this, e);
\r
681 if(this.isMenuTriggerOut(e, internal)){
\r
682 this.fireEvent('menutriggerout', this, this.menu, e);
\r
686 focus : function() {
\r
687 this.btnEl.focus();
\r
690 blur : function() {
\r
695 onFocus : function(e){
\r
696 if(!this.disabled){
\r
697 this.el.addClass('x-btn-focus');
\r
701 onBlur : function(e){
\r
702 this.el.removeClass('x-btn-focus');
\r
706 getClickEl : function(e, isUp){
\r
711 onMouseDown : function(e){
\r
712 if(!this.disabled && e.button === 0){
\r
713 this.getClickEl(e).addClass('x-btn-click');
\r
714 this.doc.on('mouseup', this.onMouseUp, this);
\r
718 onMouseUp : function(e){
\r
719 if(e.button === 0){
\r
720 this.getClickEl(e, true).removeClass('x-btn-click');
\r
721 this.doc.un('mouseup', this.onMouseUp, this);
\r
725 onMenuShow : function(e){
\r
726 if(this.menu.ownerCt == this){
\r
727 this.menu.ownerCt = this;
\r
728 this.ignoreNextClick = 0;
\r
729 this.el.addClass('x-btn-menu-active');
\r
730 this.fireEvent('menushow', this, this.menu);
\r
734 onMenuHide : function(e){
\r
735 if(this.menu.ownerCt == this){
\r
736 this.el.removeClass('x-btn-menu-active');
\r
737 this.ignoreNextClick = this.restoreClick.defer(250, this);
\r
738 this.fireEvent('menuhide', this, this.menu);
\r
739 delete this.menu.ownerCt;
\r
744 restoreClick : function(){
\r
745 this.ignoreNextClick = 0;
\r
749 * @cfg {String} autoEl @hide
\r
752 * @cfg {String/Object} html @hide
\r
755 * @cfg {String} contentEl @hide
\r
758 * @cfg {Mixed} data @hide
\r
761 * @cfg {Mixed} tpl @hide
\r
764 * @cfg {String} tplWriteMode @hide
\r
767 Ext.reg('button', Ext.Button);
\r
769 // Private utility class used by Button
\r
770 Ext.ButtonToggleMgr = function(){
\r
773 function toggleGroup(btn, state){
\r
775 var g = groups[btn.toggleGroup];
\r
776 for(var i = 0, l = g.length; i < l; i++){
\r
778 g[i].toggle(false);
\r
785 register : function(btn){
\r
786 if(!btn.toggleGroup){
\r
789 var g = groups[btn.toggleGroup];
\r
791 g = groups[btn.toggleGroup] = [];
\r
794 btn.on('toggle', toggleGroup);
\r
797 unregister : function(btn){
\r
798 if(!btn.toggleGroup){
\r
801 var g = groups[btn.toggleGroup];
\r
804 btn.un('toggle', toggleGroup);
\r
809 * Gets the pressed button in the passed group or null
\r
810 * @param {String} group
\r
813 getPressed : function(group){
\r
814 var g = groups[group];
\r
816 for(var i = 0, len = g.length; i < len; i++){
\r
817 if(g[i].pressed === true){
\r