allow string based values for comboboxarray
[roojs1] / Roo / SplitButton.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.SplitButton
14  * @extends Roo.Button
15  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
16  * click event of the button.  Typically this would be used to display a dropdown menu that provides additional
17  * options to the primary button action, but any custom handler can provide the arrowclick implementation.
18  * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
19  * @cfg {String} arrowTooltip The title attribute of the arrow
20  * @constructor
21  * Create a new menu button
22  * @param {String/HTMLElement/Element} renderTo The element to append the button to
23  * @param {Object} config The config object
24  */
25 Roo.SplitButton = function(renderTo, config){
26     Roo.SplitButton.superclass.constructor.call(this, renderTo, config);
27     /**
28      * @event arrowclick
29      * Fires when this button's arrow is clicked
30      * @param {SplitButton} this
31      * @param {EventObject} e The click event
32      */
33     this.addEvents({"arrowclick":true});
34 };
35
36 Roo.extend(Roo.SplitButton, Roo.Button, {
37     render : function(renderTo){
38         // this is one sweet looking template!
39         var tpl = new Roo.Template(
40             '<table cellspacing="0" class="x-btn-menu-wrap x-btn"><tr><td>',
41             '<table cellspacing="0" class="x-btn-wrap x-btn-menu-text-wrap"><tbody>',
42             '<tr><td class="x-btn-left"><i>&#160;</i></td><td class="x-btn-center"><button class="x-btn-text" type="{1}">{0}</button></td></tr>',
43             "</tbody></table></td><td>",
44             '<table cellspacing="0" class="x-btn-wrap x-btn-menu-arrow-wrap"><tbody>',
45             '<tr><td class="x-btn-center"><button class="x-btn-menu-arrow-el" type="button">&#160;</button></td><td class="x-btn-right"><i>&#160;</i></td></tr>',
46             "</tbody></table></td></tr></table>"
47         );
48         var btn = tpl.append(renderTo, [this.text, this.type], true);
49         var btnEl = btn.child("button");
50         if(this.cls){
51             btn.addClass(this.cls);
52         }
53         if(this.icon){
54             btnEl.setStyle('background-image', 'url(' +this.icon +')');
55         }
56         if(this.iconCls){
57             btnEl.addClass(this.iconCls);
58             if(!this.cls){
59                 btn.addClass(this.text ? 'x-btn-text-icon' : 'x-btn-icon');
60             }
61         }
62         this.el = btn;
63         if(this.handleMouseEvents){
64             btn.on("mouseover", this.onMouseOver, this);
65             btn.on("mouseout", this.onMouseOut, this);
66             btn.on("mousedown", this.onMouseDown, this);
67             btn.on("mouseup", this.onMouseUp, this);
68         }
69         btn.on(this.clickEvent, this.onClick, this);
70         if(this.tooltip){
71             if(typeof this.tooltip == 'object'){
72                 Roo.QuickTips.tips(Roo.apply({
73                       target: btnEl.id
74                 }, this.tooltip));
75             } else {
76                 btnEl.dom[this.tooltipType] = this.tooltip;
77             }
78         }
79         if(this.arrowTooltip){
80             btn.child("button:nth(2)").dom[this.tooltipType] = this.arrowTooltip;
81         }
82         if(this.hidden){
83             this.hide();
84         }
85         if(this.disabled){
86             this.disable();
87         }
88         if(this.pressed){
89             this.el.addClass("x-btn-pressed");
90         }
91         if(Roo.isIE && !Roo.isIE7){
92             this.autoWidth.defer(1, this);
93         }else{
94             this.autoWidth();
95         }
96         if(this.menu){
97             this.menu.on("show", this.onMenuShow, this);
98             this.menu.on("hide", this.onMenuHide, this);
99         }
100         this.fireEvent('render', this);
101     },
102
103     // private
104     autoWidth : function(){
105         if(this.el){
106             var tbl = this.el.child("table:first");
107             var tbl2 = this.el.child("table:last");
108             this.el.setWidth("auto");
109             tbl.setWidth("auto");
110             if(Roo.isIE7 && Roo.isStrict){
111                 var ib = this.el.child('button:first');
112                 if(ib && ib.getWidth() > 20){
113                     ib.clip();
114                     ib.setWidth(Roo.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
115                 }
116             }
117             if(this.minWidth){
118                 if(this.hidden){
119                     this.el.beginMeasure();
120                 }
121                 if((tbl.getWidth()+tbl2.getWidth()) < this.minWidth){
122                     tbl.setWidth(this.minWidth-tbl2.getWidth());
123                 }
124                 if(this.hidden){
125                     this.el.endMeasure();
126                 }
127             }
128             this.el.setWidth(tbl.getWidth()+tbl2.getWidth());
129         } 
130     },
131     /**
132      * Sets this button's click handler
133      * @param {Function} handler The function to call when the button is clicked
134      * @param {Object} scope (optional) Scope for the function passed above
135      */
136     setHandler : function(handler, scope){
137         this.handler = handler;
138         this.scope = scope;  
139     },
140     
141     /**
142      * Sets this button's arrow click handler
143      * @param {Function} handler The function to call when the arrow is clicked
144      * @param {Object} scope (optional) Scope for the function passed above
145      */
146     setArrowHandler : function(handler, scope){
147         this.arrowHandler = handler;
148         this.scope = scope;  
149     },
150     
151     /**
152      * Focus the button
153      */
154     focus : function(){
155         if(this.el){
156             this.el.child("button:first").focus();
157         }
158     },
159
160     // private
161     onClick : function(e){
162         e.preventDefault();
163         if(!this.disabled){
164             if(e.getTarget(".x-btn-menu-arrow-wrap")){
165                 if(this.menu && !this.menu.isVisible()){
166                     this.menu.show(this.el, this.menuAlign);
167                 }
168                 this.fireEvent("arrowclick", this, e);
169                 if(this.arrowHandler){
170                     this.arrowHandler.call(this.scope || this, this, e);
171                 }
172             }else{
173                 this.fireEvent("click", this, e);
174                 if(this.handler){
175                     this.handler.call(this.scope || this, this, e);
176                 }
177             }
178         }
179     },
180     // private
181     onMouseDown : function(e){
182         if(!this.disabled){
183             Roo.fly(e.getTarget("table")).addClass("x-btn-click");
184         }
185     },
186     // private
187     onMouseUp : function(e){
188         Roo.fly(e.getTarget("table")).removeClass("x-btn-click");
189     }   
190 });
191
192
193 // backwards compat
194 Roo.MenuButton = Roo.SplitButton;