2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
12 * @extends Ext.Component
\r
13 * Simple Button class
\r
14 * @cfg {String} text The button text
\r
15 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
\r
16 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:"x-btn-text-icon")
\r
17 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event)
\r
18 * @cfg {Object} scope The scope of the handler
\r
19 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width)
\r
20 * @cfg {String/Object} tooltip The tooltip for the button - can be a string or QuickTips config object
\r
21 * @cfg {Boolean} hidden True to start hidden (defaults to false)
\r
22 * @cfg {Boolean} disabled True to start disabled (defaults to false)
\r
23 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
\r
24 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
\r
25 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
\r
26 an {@link Ext.util.ClickRepeater} config object (defaults to false).
\r
28 * Create a new button
\r
29 * @param {Object} config The config object
\r
31 Ext.Button = Ext.extend(Ext.Component, {
\r
33 * Read-only. True if this button is hidden
\r
38 * Read-only. True if this button is disabled
\r
43 * Read-only. True if this button is pressed (only if enableToggle = true)
\r
48 * The Button's owner {@link Ext.Panel} (defaults to undefined, and is set automatically when
\r
49 * the Button is added to a container). Read-only.
\r
55 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
\r
59 * @cfg {Boolean} allowDepress
\r
60 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
\r
64 * @cfg {Boolean} enableToggle
\r
65 * True to enable pressed/not pressed toggling (defaults to false)
\r
67 enableToggle: false,
\r
69 * @cfg {Function} toggleHandler
\r
70 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
\r
71 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
\r
72 * <li><b>state</b> : Boolean<div class="sub-desc">The next state if the Button, true means pressed.</div></li>
\r
77 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
\r
80 * @cfg {String} menuAlign
\r
81 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
\r
83 menuAlign : "tl-bl?",
\r
86 * @cfg {String} iconCls
\r
87 * A css class which sets a background image to be used as the icon for this button
\r
90 * @cfg {String} type
\r
91 * submit, reset or button - defaults to 'button'
\r
96 menuClassTarget: 'tr',
\r
99 * @cfg {String} clickEvent
\r
100 * The type of event to map to the button's event handler (defaults to 'click')
\r
102 clickEvent : 'click',
\r
105 * @cfg {Boolean} handleMouseEvents
\r
106 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
\r
108 handleMouseEvents : true,
\r
111 * @cfg {String} tooltipType
\r
112 * The type of tooltip to use. Either "qtip" (default) for QuickTips or "title" for title attribute.
\r
114 tooltipType : 'qtip',
\r
117 * @cfg {String} buttonSelector
\r
118 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
\r
119 * DOM structure created.</p>
\r
120 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
\r
121 * a focussable element.</p>
\r
122 * <p>Defaults to <b><tt>"button:first-child"</tt></b>.</p>
\r
124 buttonSelector : "button:first-child",
\r
127 * @cfg {Ext.Template} template
\r
128 * (Optional) An {@link Ext.Template} with which to create the Button's main element. This Template must
\r
129 * contain numeric substitution parameter 0 if it is to display the text property. Changing the template could
\r
130 * require code modifications if required elements (e.g. a button) aren't present.
\r
133 * @cfg {String} cls
\r
134 * A CSS class string to apply to the button's main element.
\r
137 initComponent : function(){
\r
138 Ext.Button.superclass.initComponent.call(this);
\r
143 * Fires when this button is clicked
\r
144 * @param {Button} this
\r
145 * @param {EventObject} e The click event
\r
150 * Fires when the "pressed" state of this button changes (only if enableToggle = true)
\r
151 * @param {Button} this
\r
152 * @param {Boolean} pressed
\r
157 * Fires when the mouse hovers over the button
\r
158 * @param {Button} this
\r
159 * @param {Event} e The event object
\r
164 * Fires when the mouse exits the button
\r
165 * @param {Button} this
\r
166 * @param {Event} e The event object
\r
171 * If this button has a menu, this event fires when it is shown
\r
172 * @param {Button} this
\r
173 * @param {Menu} menu
\r
178 * If this button has a menu, this event fires when it is hidden
\r
179 * @param {Button} this
\r
180 * @param {Menu} menu
\r
184 * @event menutriggerover
\r
185 * If this button has a menu, this event fires when the mouse enters the menu triggering element
\r
186 * @param {Button} this
\r
187 * @param {Menu} menu
\r
188 * @param {EventObject} e
\r
192 * @event menutriggerout
\r
193 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
\r
194 * @param {Button} this
\r
195 * @param {Menu} menu
\r
196 * @param {EventObject} e
\r
201 this.menu = Ext.menu.MenuMgr.get(this.menu);
\r
203 if(typeof this.toggleGroup === 'string'){
\r
204 this.enableToggle = true;
\r
209 onRender : function(ct, position){
\r
210 if(!this.template){
\r
211 if(!Ext.Button.buttonTemplate){
\r
212 // hideous table template
\r
213 Ext.Button.buttonTemplate = new Ext.Template(
\r
214 '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>',
\r
215 '<td class="x-btn-left"><i> </i></td><td class="x-btn-center"><em unselectable="on"><button class="x-btn-text" type="{1}">{0}</button></em></td><td class="x-btn-right"><i> </i></td>',
\r
216 "</tr></tbody></table>");
\r
218 this.template = Ext.Button.buttonTemplate;
\r
220 var btn, targs = [this.text || ' ', this.type];
\r
223 btn = this.template.insertBefore(position, targs, true);
\r
225 btn = this.template.append(ct, targs, true);
\r
227 var btnEl = btn.child(this.buttonSelector);
\r
228 btnEl.on('focus', this.onFocus, this);
\r
229 btnEl.on('blur', this.onBlur, this);
\r
231 this.initButtonEl(btn, btnEl);
\r
234 this.el.child(this.menuClassTarget).addClass("x-btn-with-menu");
\r
236 Ext.ButtonToggleMgr.register(this);
\r
240 initButtonEl : function(btn, btnEl){
\r
243 btn.addClass("x-btn");
\r
246 this.el.dom.id = this.el.id = this.id;
\r
249 btnEl.setStyle('background-image', 'url(' +this.icon +')');
\r
252 btnEl.addClass(this.iconCls);
\r
254 btn.addClass(this.text ? 'x-btn-text-icon' : 'x-btn-icon');
\r
257 if(this.tabIndex !== undefined){
\r
258 btnEl.dom.tabIndex = this.tabIndex;
\r
261 if(typeof this.tooltip == 'object'){
\r
262 Ext.QuickTips.register(Ext.apply({
\r
266 btnEl.dom[this.tooltipType] = this.tooltip;
\r
271 this.el.addClass("x-btn-pressed");
\r
274 if(this.handleMouseEvents){
\r
275 btn.on("mouseover", this.onMouseOver, this);
\r
276 // new functionality for monitoring on the document level
\r
277 //btn.on("mouseout", this.onMouseOut, this);
\r
278 btn.on("mousedown", this.onMouseDown, this);
\r
282 this.menu.on("show", this.onMenuShow, this);
\r
283 this.menu.on("hide", this.onMenuHide, this);
\r
287 var repeater = new Ext.util.ClickRepeater(btn,
\r
288 typeof this.repeat == "object" ? this.repeat : {}
\r
290 repeater.on("click", this.onClick, this);
\r
293 btn.on(this.clickEvent, this.onClick, this);
\r
297 afterRender : function(){
\r
298 Ext.Button.superclass.afterRender.call(this);
\r
300 this.autoWidth.defer(1, this);
\r
307 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
\r
308 * the value of the {@link iconCls} config internally.
\r
309 * @param {String} cls The CSS class providing the icon image
\r
311 setIconClass : function(cls){
\r
313 this.el.child(this.buttonSelector).replaceClass(this.iconCls, cls);
\r
315 this.iconCls = cls;
\r
319 beforeDestroy: function(){
\r
321 var btnEl = this.el.child(this.buttonSelector);
\r
324 Ext.QuickTips.unregister(btnEl);
\r
326 btnEl.removeAllListeners();
\r
330 Ext.destroy(this.menu);
\r
335 onDestroy : function(){
\r
337 Ext.ButtonToggleMgr.unregister(this);
\r
342 autoWidth : function(){
\r
344 this.el.setWidth("auto");
\r
345 if(Ext.isIE7 && Ext.isStrict){
\r
346 var ib = this.el.child(this.buttonSelector);
\r
347 if(ib && ib.getWidth() > 20){
\r
349 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
\r
353 if(this.el.getWidth() < this.minWidth){
\r
354 this.el.setWidth(this.minWidth);
\r
361 * Assigns this button's click handler
\r
362 * @param {Function} handler The function to call when the button is clicked
\r
363 * @param {Object} scope (optional) Scope for the function passed in
\r
365 setHandler : function(handler, scope){
\r
366 this.handler = handler;
\r
367 this.scope = scope;
\r
371 * Sets this button's text
\r
372 * @param {String} text The button text
\r
374 setText : function(text){
\r
377 this.el.child("td.x-btn-center " + this.buttonSelector).update(text);
\r
383 * Gets the text for this button
\r
384 * @return {String} The button text
\r
386 getText : function(){
\r
391 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
\r
392 * @param {Boolean} state (optional) Force a particular state
\r
394 toggle : function(state){
\r
395 state = state === undefined ? !this.pressed : state;
\r
396 if(state != this.pressed){
\r
398 this.el.addClass("x-btn-pressed");
\r
399 this.pressed = true;
\r
400 this.fireEvent("toggle", this, true);
\r
402 this.el.removeClass("x-btn-pressed");
\r
403 this.pressed = false;
\r
404 this.fireEvent("toggle", this, false);
\r
406 if(this.toggleHandler){
\r
407 this.toggleHandler.call(this.scope || this, this, state);
\r
415 focus : function(){
\r
416 this.el.child(this.buttonSelector).focus();
\r
420 onDisable : function(){
\r
422 if(!Ext.isIE6 || !this.text){
\r
423 this.el.addClass(this.disabledClass);
\r
425 this.el.dom.disabled = true;
\r
427 this.disabled = true;
\r
431 onEnable : function(){
\r
433 if(!Ext.isIE6 || !this.text){
\r
434 this.el.removeClass(this.disabledClass);
\r
436 this.el.dom.disabled = false;
\r
438 this.disabled = false;
\r
442 * Show this button's menu (if it has one)
\r
444 showMenu : function(){
\r
446 this.menu.show(this.el, this.menuAlign);
\r
452 * Hide this button's menu (if it has one)
\r
454 hideMenu : function(){
\r
462 * Returns true if the button has a menu and it is visible
\r
463 * @return {Boolean}
\r
465 hasVisibleMenu : function(){
\r
466 return this.menu && this.menu.isVisible();
\r
470 onClick : function(e){
\r
472 e.preventDefault();
\r
477 if(!this.disabled){
\r
478 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
\r
481 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
\r
484 this.fireEvent("click", this, e);
\r
486 //this.el.removeClass("x-btn-over");
\r
487 this.handler.call(this.scope || this, this, e);
\r
493 isMenuTriggerOver : function(e, internal){
\r
494 return this.menu && !internal;
\r
498 isMenuTriggerOut : function(e, internal){
\r
499 return this.menu && !internal;
\r
503 onMouseOver : function(e){
\r
504 if(!this.disabled){
\r
505 var internal = e.within(this.el, true);
\r
507 this.el.addClass("x-btn-over");
\r
508 if(!this.monitoringMouseOver){
\r
509 Ext.getDoc().on('mouseover', this.monitorMouseOver, this);
\r
510 this.monitoringMouseOver = true;
\r
512 this.fireEvent('mouseover', this, e);
\r
514 if(this.isMenuTriggerOver(e, internal)){
\r
515 this.fireEvent('menutriggerover', this, this.menu, e);
\r
521 monitorMouseOver : function(e){
\r
522 if(e.target != this.el.dom && !e.within(this.el)){
\r
523 if(this.monitoringMouseOver){
\r
524 Ext.getDoc().un('mouseover', this.monitorMouseOver, this);
\r
525 this.monitoringMouseOver = false;
\r
527 this.onMouseOut(e);
\r
532 onMouseOut : function(e){
\r
533 var internal = e.within(this.el) && e.target != this.el.dom;
\r
534 this.el.removeClass("x-btn-over");
\r
535 this.fireEvent('mouseout', this, e);
\r
536 if(this.isMenuTriggerOut(e, internal)){
\r
537 this.fireEvent('menutriggerout', this, this.menu, e);
\r
541 onFocus : function(e){
\r
542 if(!this.disabled){
\r
543 this.el.addClass("x-btn-focus");
\r
547 onBlur : function(e){
\r
548 this.el.removeClass("x-btn-focus");
\r
552 getClickEl : function(e, isUp){
\r
557 onMouseDown : function(e){
\r
558 if(!this.disabled && e.button == 0){
\r
559 this.getClickEl(e).addClass("x-btn-click");
\r
560 Ext.getDoc().on('mouseup', this.onMouseUp, this);
\r
564 onMouseUp : function(e){
\r
566 this.getClickEl(e, true).removeClass("x-btn-click");
\r
567 Ext.getDoc().un('mouseup', this.onMouseUp, this);
\r
571 onMenuShow : function(e){
\r
572 this.ignoreNextClick = 0;
\r
573 this.el.addClass("x-btn-menu-active");
\r
574 this.fireEvent('menushow', this, this.menu);
\r
577 onMenuHide : function(e){
\r
578 this.el.removeClass("x-btn-menu-active");
\r
579 this.ignoreNextClick = this.restoreClick.defer(250, this);
\r
580 this.fireEvent('menuhide', this, this.menu);
\r
584 restoreClick : function(){
\r
585 this.ignoreNextClick = 0;
\r
591 * @cfg {String} autoEl @hide
\r
594 Ext.reg('button', Ext.Button);
\r
596 // Private utility class used by Button
\r
597 Ext.ButtonToggleMgr = function(){
\r
600 function toggleGroup(btn, state){
\r
602 var g = groups[btn.toggleGroup];
\r
603 for(var i = 0, l = g.length; i < l; i++){
\r
605 g[i].toggle(false);
\r
612 register : function(btn){
\r
613 if(!btn.toggleGroup){
\r
616 var g = groups[btn.toggleGroup];
\r
618 g = groups[btn.toggleGroup] = [];
\r
621 btn.on("toggle", toggleGroup);
\r
624 unregister : function(btn){
\r
625 if(!btn.toggleGroup){
\r
628 var g = groups[btn.toggleGroup];
\r
631 btn.un("toggle", toggleGroup);
\r