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 Roo.util.Observable.call(this);
84 this.render(renderTo);
90 Roo.extend(Roo.Button, Roo.util.Observable, {
96 * Read-only. True if this button is hidden
101 * Read-only. True if this button is disabled
106 * Read-only. True if this button is pressed (only if enableToggle = true)
112 * @cfg {Number} tabIndex
113 * The DOM tabIndex for this button (defaults to undefined)
115 tabIndex : undefined,
118 * @cfg {Boolean} enableToggle
119 * True to enable pressed/not pressed toggling (defaults to false)
124 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
128 * @cfg {String} menuAlign
129 * The position to align the menu to (see {@link Roo.Element#alignTo} for more details, defaults to 'tl-bl?').
131 menuAlign : "tl-bl?",
134 * @cfg {String} iconCls
135 * A css class which sets a background image to be used as the icon for this button (defaults to undefined).
140 * The button's type, corresponding to the DOM input element type attribute. Either "submit," "reset" or "button" (default).
145 menuClassTarget: 'tr',
148 * @cfg {String} clickEvent
149 * The type of event to map to the button's event handler (defaults to 'click')
151 clickEvent : 'click',
154 * @cfg {Boolean} handleMouseEvents
155 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
157 handleMouseEvents : true,
160 * @cfg {String} tooltipType
161 * The type of tooltip to use. Either "qtip" (default) for QuickTips or "title" for title attribute.
163 tooltipType : 'qtip',
167 * A CSS class to apply to the button's main element.
171 * @cfg {Roo.Template} template (Optional)
172 * An {@link Roo.Template} with which to create the Button's main element. This Template must
173 * contain numeric substitution parameter 0 if it is to display the tRoo property. Changing the template could
174 * require code modifications if required elements (e.g. a button) aren't present.
178 render : function(renderTo){
181 this.parentEl = Roo.get(renderTo);
185 if(!Roo.Button.buttonTemplate){
186 // hideous table template
187 Roo.Button.buttonTemplate = new Roo.Template(
188 '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>',
189 '<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>',
190 "</tr></tbody></table>");
192 this.template = Roo.Button.buttonTemplate;
194 btn = this.template.append(renderTo, [this.text || ' ', this.type], true);
195 var btnEl = btn.child("button:first");
196 btnEl.on('focus', this.onFocus, this);
197 btnEl.on('blur', this.onBlur, this);
199 btn.addClass(this.cls);
202 btnEl.setStyle('background-image', 'url(' +this.icon +')');
205 btnEl.addClass(this.iconCls);
207 btn.addClass(this.text ? 'x-btn-text-icon' : 'x-btn-icon');
210 if(this.tabIndex !== undefined){
211 btnEl.dom.tabIndex = this.tabIndex;
214 if(typeof this.tooltip == 'object'){
215 Roo.QuickTips.tips(Roo.apply({
219 btnEl.dom[this.tooltipType] = this.tooltip;
223 btn = Roo.DomHelper.append(Roo.get(renderTo).dom, this.dhconfig, true);
227 this.el.dom.id = this.el.id = this.id;
230 this.el.child(this.menuClassTarget).addClass("x-btn-with-menu");
231 this.menu.on("show", this.onMenuShow, this);
232 this.menu.on("hide", this.onMenuHide, this);
234 btn.addClass("x-btn");
235 if(Roo.isIE && !Roo.isIE7){
236 this.autoWidth.defer(1, this);
240 if(this.handleMouseEvents){
241 btn.on("mouseover", this.onMouseOver, this);
242 btn.on("mouseout", this.onMouseOut, this);
243 btn.on("mousedown", this.onMouseDown, this);
245 btn.on(this.clickEvent, this.onClick, this);
246 //btn.on("mouseup", this.onMouseUp, this);
253 Roo.ButtonToggleMgr.register(this);
255 this.el.addClass("x-btn-pressed");
258 var repeater = new Roo.util.ClickRepeater(btn,
259 typeof this.repeat == "object" ? this.repeat : {}
261 repeater.on("click", this.onClick, this);
263 Roo.log("FIRING RENDER EVENT");
264 this.fireEvent('render', this);
268 * Returns the button's underlying element
269 * @return {Roo.Element} The element
276 * Destroys this Button and removes any listeners.
278 destroy : function(){
279 Roo.ButtonToggleMgr.unregister(this);
280 this.el.removeAllListeners();
281 this.purgeListeners();
286 autoWidth : function(){
288 this.el.setWidth("auto");
289 if(Roo.isIE7 && Roo.isStrict){
290 var ib = this.el.child('button');
291 if(ib && ib.getWidth() > 20){
293 ib.setWidth(Roo.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
298 this.el.beginMeasure();
300 if(this.el.getWidth() < this.minWidth){
301 this.el.setWidth(this.minWidth);
304 this.el.endMeasure();
311 * Assigns this button's click handler
312 * @param {Function} handler The function to call when the button is clicked
313 * @param {Object} scope (optional) Scope for the function passed in
315 setHandler : function(handler, scope){
316 this.handler = handler;
321 * Sets this button's text
322 * @param {String} text The button text
324 setText : function(text){
327 this.el.child("td.x-btn-center button.x-btn-text").update(text);
333 * Gets the text for this button
334 * @return {String} The button text
336 getText : function(){
346 this[this.hideParent? 'parentEl' : 'el'].setStyle("display", "");
356 this[this.hideParent? 'parentEl' : 'el'].setStyle("display", "none");
361 * Convenience function for boolean show/hide
362 * @param {Boolean} visible True to show, false to hide
364 setVisible: function(visible){
373 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
374 * @param {Boolean} state (optional) Force a particular state
376 toggle : function(state){
377 state = state === undefined ? !this.pressed : state;
378 if(state != this.pressed){
380 this.el.addClass("x-btn-pressed");
382 this.fireEvent("toggle", this, true);
384 this.el.removeClass("x-btn-pressed");
385 this.pressed = false;
386 this.fireEvent("toggle", this, false);
388 if(this.toggleHandler){
389 this.toggleHandler.call(this.scope || this, this, state);
398 this.el.child('button:first').focus();
402 * Disable this button
404 disable : function(){
406 this.el.addClass("x-btn-disabled");
408 this.disabled = true;
416 this.el.removeClass("x-btn-disabled");
418 this.disabled = false;
422 * Convenience function for boolean enable/disable
423 * @param {Boolean} enabled True to enable, false to disable
425 setDisabled : function(v){
426 this[v !== true ? "enable" : "disable"]();
430 onClick : function(e){
438 if(this.enableToggle){
441 if(this.menu && !this.menu.isVisible()){
442 this.menu.show(this.el, this.menuAlign);
444 this.fireEvent("click", this, e);
446 this.el.removeClass("x-btn-over");
447 this.handler.call(this.scope || this, this, e);
452 onMouseOver : function(e){
454 this.el.addClass("x-btn-over");
455 this.fireEvent('mouseover', this, e);
459 onMouseOut : function(e){
460 if(!e.within(this.el, true)){
461 this.el.removeClass("x-btn-over");
462 this.fireEvent('mouseout', this, e);
466 onFocus : function(e){
468 this.el.addClass("x-btn-focus");
472 onBlur : function(e){
473 this.el.removeClass("x-btn-focus");
476 onMouseDown : function(e){
477 if(!this.disabled && e.button == 0){
478 this.el.addClass("x-btn-click");
479 Roo.get(document).on('mouseup', this.onMouseUp, this);
483 onMouseUp : function(e){
485 this.el.removeClass("x-btn-click");
486 Roo.get(document).un('mouseup', this.onMouseUp, this);
490 onMenuShow : function(e){
491 this.el.addClass("x-btn-menu-active");
494 onMenuHide : function(e){
495 this.el.removeClass("x-btn-menu-active");
499 // Private utility class used by Button
500 Roo.ButtonToggleMgr = function(){
503 function toggleGroup(btn, state){
505 var g = groups[btn.toggleGroup];
506 for(var i = 0, l = g.length; i < l; i++){
515 register : function(btn){
516 if(!btn.toggleGroup){
519 var g = groups[btn.toggleGroup];
521 g = groups[btn.toggleGroup] = [];
524 btn.on("toggle", toggleGroup);
527 unregister : function(btn){
528 if(!btn.toggleGroup){
531 var g = groups[btn.toggleGroup];
534 btn.un("toggle", toggleGroup);