4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @extends Roo.util.Observable
16 * @cfg {String} text The button text
17 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
18 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:"x-btn-text-icon")
19 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event)
20 * @cfg {Object} scope The scope of the handler
21 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width)
22 * @cfg {String/Object} tooltip The tooltip for the button - can be a string 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, only
27 applies if enableToggle = true)
28 * @cfg {String/HTMLElement/Element} renderTo The element to append the button to
29 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
30 an {@link Roo.util.ClickRepeater} config object (defaults to false).
33 * @param {Object} config The config object
35 Roo.Button = function(renderTo, config)
39 renderTo = config.renderTo || false;
42 Roo.apply(this, config);
46 * Fires when this button is clicked
47 * @param {Button} this
48 * @param {EventObject} e The click event
53 * Fires when the "pressed" state of this button changes (only if enableToggle = true)
54 * @param {Button} this
55 * @param {Boolean} pressed
60 * Fires when the mouse hovers over the button
61 * @param {Button} this
62 * @param {Event} e The event object
67 * Fires when the mouse exits the button
68 * @param {Button} this
69 * @param {Event} e The event object
74 * Fires when the button is rendered
75 * @param {Button} this
80 this.menu = Roo.menu.MenuMgr.get(this.menu);
82 // register listeners first!! - so render can be captured..
83 Roo.util.Observable.call(this);
85 this.render(renderTo);
91 Roo.extend(Roo.Button, Roo.util.Observable, {
97 * Read-only. True if this button is hidden
102 * Read-only. True if this button is disabled
107 * Read-only. True if this button is pressed (only if enableToggle = true)
113 * @cfg {Number} tabIndex
114 * The DOM tabIndex for this button (defaults to undefined)
116 tabIndex : undefined,
119 * @cfg {Boolean} enableToggle
120 * True to enable pressed/not pressed toggling (defaults to false)
125 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
129 * @cfg {String} menuAlign
130 * The position to align the menu to (see {@link Roo.Element#alignTo} for more details, defaults to 'tl-bl?').
132 menuAlign : "tl-bl?",
135 * @cfg {String} iconCls
136 * A css class which sets a background image to be used as the icon for this button (defaults to undefined).
141 * The button's type, corresponding to the DOM input element type attribute. Either "submit," "reset" or "button" (default).
146 menuClassTarget: 'tr',
149 * @cfg {String} clickEvent
150 * The type of event to map to the button's event handler (defaults to 'click')
152 clickEvent : 'click',
155 * @cfg {Boolean} handleMouseEvents
156 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
158 handleMouseEvents : true,
161 * @cfg {String} tooltipType
162 * The type of tooltip to use. Either "qtip" (default) for QuickTips or "title" for title attribute.
164 tooltipType : 'qtip',
168 * A CSS class to apply to the button's main element.
172 * @cfg {Roo.Template} template (Optional)
173 * An {@link Roo.Template} with which to create the Button's main element. This Template must
174 * contain numeric substitution parameter 0 if it is to display the tRoo property. Changing the template could
175 * require code modifications if required elements (e.g. a button) aren't present.
179 render : function(renderTo){
182 this.parentEl = Roo.get(renderTo);
186 if(!Roo.Button.buttonTemplate){
187 // hideous table template
188 Roo.Button.buttonTemplate = new Roo.Template(
189 '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>',
190 '<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>',
191 "</tr></tbody></table>");
193 this.template = Roo.Button.buttonTemplate;
195 btn = this.template.append(renderTo, [this.text || ' ', this.type], true);
196 var btnEl = btn.child("button:first");
197 btnEl.on('focus', this.onFocus, this);
198 btnEl.on('blur', this.onBlur, this);
200 btn.addClass(this.cls);
203 btnEl.setStyle('background-image', 'url(' +this.icon +')');
206 btnEl.addClass(this.iconCls);
208 btn.addClass(this.text ? 'x-btn-text-icon' : 'x-btn-icon');
211 if(this.tabIndex !== undefined){
212 btnEl.dom.tabIndex = this.tabIndex;
215 if(typeof this.tooltip == 'object'){
216 Roo.QuickTips.tips(Roo.apply({
220 btnEl.dom[this.tooltipType] = this.tooltip;
224 btn = Roo.DomHelper.append(Roo.get(renderTo).dom, this.dhconfig, true);
228 this.el.dom.id = this.el.id = this.id;
231 this.el.child(this.menuClassTarget).addClass("x-btn-with-menu");
232 this.menu.on("show", this.onMenuShow, this);
233 this.menu.on("hide", this.onMenuHide, this);
235 btn.addClass("x-btn");
236 if(Roo.isIE && !Roo.isIE7){
237 this.autoWidth.defer(1, this);
241 if(this.handleMouseEvents){
242 btn.on("mouseover", this.onMouseOver, this);
243 btn.on("mouseout", this.onMouseOut, this);
244 btn.on("mousedown", this.onMouseDown, this);
246 btn.on(this.clickEvent, this.onClick, this);
247 //btn.on("mouseup", this.onMouseUp, this);
254 Roo.ButtonToggleMgr.register(this);
256 this.el.addClass("x-btn-pressed");
259 var repeater = new Roo.util.ClickRepeater(btn,
260 typeof this.repeat == "object" ? this.repeat : {}
262 repeater.on("click", this.onClick, this);
265 this.fireEvent('render', this);
269 * Returns the button's underlying element
270 * @return {Roo.Element} The element
277 * Destroys this Button and removes any listeners.
279 destroy : function(){
280 Roo.ButtonToggleMgr.unregister(this);
281 this.el.removeAllListeners();
282 this.purgeListeners();
287 autoWidth : function(){
289 this.el.setWidth("auto");
290 if(Roo.isIE7 && Roo.isStrict){
291 var ib = this.el.child('button');
292 if(ib && ib.getWidth() > 20){
294 ib.setWidth(Roo.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
299 this.el.beginMeasure();
301 if(this.el.getWidth() < this.minWidth){
302 this.el.setWidth(this.minWidth);
305 this.el.endMeasure();
312 * Assigns this button's click handler
313 * @param {Function} handler The function to call when the button is clicked
314 * @param {Object} scope (optional) Scope for the function passed in
316 setHandler : function(handler, scope){
317 this.handler = handler;
322 * Sets this button's text
323 * @param {String} text The button text
325 setText : function(text){
328 this.el.child("td.x-btn-center button.x-btn-text").update(text);
334 * Gets the text for this button
335 * @return {String} The button text
337 getText : function(){
347 this[this.hideParent? 'parentEl' : 'el'].setStyle("display", "");
357 this[this.hideParent? 'parentEl' : 'el'].setStyle("display", "none");
362 * Convenience function for boolean show/hide
363 * @param {Boolean} visible True to show, false to hide
365 setVisible: function(visible){
374 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
375 * @param {Boolean} state (optional) Force a particular state
377 toggle : function(state){
378 state = state === undefined ? !this.pressed : state;
379 if(state != this.pressed){
381 this.el.addClass("x-btn-pressed");
383 this.fireEvent("toggle", this, true);
385 this.el.removeClass("x-btn-pressed");
386 this.pressed = false;
387 this.fireEvent("toggle", this, false);
389 if(this.toggleHandler){
390 this.toggleHandler.call(this.scope || this, this, state);
399 this.el.child('button:first').focus();
403 * Disable this button
405 disable : function(){
407 this.el.addClass("x-btn-disabled");
409 this.disabled = true;
417 this.el.removeClass("x-btn-disabled");
419 this.disabled = false;
423 * Convenience function for boolean enable/disable
424 * @param {Boolean} enabled True to enable, false to disable
426 setDisabled : function(v){
427 this[v !== true ? "enable" : "disable"]();
431 onClick : function(e){
439 if(this.enableToggle){
442 if(this.menu && !this.menu.isVisible()){
443 this.menu.show(this.el, this.menuAlign);
445 this.fireEvent("click", this, e);
447 this.el.removeClass("x-btn-over");
448 this.handler.call(this.scope || this, this, e);
453 onMouseOver : function(e){
455 this.el.addClass("x-btn-over");
456 this.fireEvent('mouseover', this, e);
460 onMouseOut : function(e){
461 if(!e.within(this.el, true)){
462 this.el.removeClass("x-btn-over");
463 this.fireEvent('mouseout', this, e);
467 onFocus : function(e){
469 this.el.addClass("x-btn-focus");
473 onBlur : function(e){
474 this.el.removeClass("x-btn-focus");
477 onMouseDown : function(e){
478 if(!this.disabled && e.button == 0){
479 this.el.addClass("x-btn-click");
480 Roo.get(document).on('mouseup', this.onMouseUp, this);
484 onMouseUp : function(e){
486 this.el.removeClass("x-btn-click");
487 Roo.get(document).un('mouseup', this.onMouseUp, this);
491 onMenuShow : function(e){
492 this.el.addClass("x-btn-menu-active");
495 onMenuHide : function(e){
496 this.el.removeClass("x-btn-menu-active");
500 // Private utility class used by Button
501 Roo.ButtonToggleMgr = function(){
504 function toggleGroup(btn, state){
506 var g = groups[btn.toggleGroup];
507 for(var i = 0, l = g.length; i < l; i++){
516 register : function(btn){
517 if(!btn.toggleGroup){
520 var g = groups[btn.toggleGroup];
522 g = groups[btn.toggleGroup] = [];
525 btn.on("toggle", toggleGroup);
528 unregister : function(btn){
529 if(!btn.toggleGroup){
532 var g = groups[btn.toggleGroup];
535 btn.un("toggle", toggleGroup);