Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / SplitButton.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.SplitButton"></div>/**\r
9  * @class Ext.SplitButton\r
10  * @extends Ext.Button\r
11  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default\r
12  * click event of the button.  Typically this would be used to display a dropdown menu that provides additional\r
13  * options to the primary button action, but any custom handler can provide the arrowclick implementation.  Example usage:\r
14  * <pre><code>\r
15 // display a dropdown menu:\r
16 new Ext.SplitButton({\r
17         renderTo: 'button-ct', // the container id\r
18         text: 'Options',\r
19         handler: optionsHandler, // handle a click on the button itself\r
20         menu: new Ext.menu.Menu({\r
21         items: [\r
22                 // these items will render as dropdown menu items when the arrow is clicked:\r
23                 {text: 'Item 1', handler: item1Handler},\r
24                 {text: 'Item 2', handler: item2Handler}\r
25         ]\r
26         })\r
27 });\r
28 \r
29 // Instead of showing a menu, you provide any type of custom\r
30 // functionality you want when the dropdown arrow is clicked:\r
31 new Ext.SplitButton({\r
32         renderTo: 'button-ct',\r
33         text: 'Options',\r
34         handler: optionsHandler,\r
35         arrowHandler: myCustomHandler\r
36 });\r
37 </code></pre>\r
38  * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)\r
39  * @cfg {String} arrowTooltip The title attribute of the arrow\r
40  * @constructor\r
41  * Create a new menu button\r
42  * @param {Object} config The config object\r
43  * @xtype splitbutton\r
44  */\r
45 Ext.SplitButton = Ext.extend(Ext.Button, {\r
46         // private\r
47     arrowSelector : 'em',\r
48     split: true,\r
49 \r
50     // private\r
51     initComponent : function(){\r
52         Ext.SplitButton.superclass.initComponent.call(this);\r
53         /**\r
54          * @event arrowclick\r
55          * Fires when this button's arrow is clicked\r
56          * @param {MenuButton} this\r
57          * @param {EventObject} e The click event\r
58          */\r
59         this.addEvents("arrowclick");\r
60     },\r
61 \r
62     // private\r
63     onRender : function(){\r
64         Ext.SplitButton.superclass.onRender.apply(this, arguments);\r
65         if(this.arrowTooltip){\r
66             this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;\r
67         }\r
68     },\r
69 \r
70     /**\r
71      * Sets this button's arrow click handler.\r
72      * @param {Function} handler The function to call when the arrow is clicked\r
73      * @param {Object} scope (optional) Scope for the function passed above\r
74      */\r
75     setArrowHandler : function(handler, scope){\r
76         this.arrowHandler = handler;\r
77         this.scope = scope;\r
78     },\r
79 \r
80     getMenuClass : function(){\r
81         return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');\r
82     },\r
83 \r
84     isClickOnArrow : function(e){\r
85         return this.arrowAlign != 'bottom' ?\r
86                e.getPageX() > this.el.child(this.buttonSelector).getRegion().right :\r
87                e.getPageY() > this.el.child(this.buttonSelector).getRegion().bottom;\r
88     },\r
89 \r
90     // private\r
91     onClick : function(e, t){\r
92         e.preventDefault();\r
93         if(!this.disabled){\r
94             if(this.isClickOnArrow(e)){\r
95                 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){\r
96                     this.showMenu();\r
97                 }\r
98                 this.fireEvent("arrowclick", this, e);\r
99                 if(this.arrowHandler){\r
100                     this.arrowHandler.call(this.scope || this, this, e);\r
101                 }\r
102             }else{\r
103                 if(this.enableToggle){\r
104                     this.toggle();\r
105                 }\r
106                 this.fireEvent("click", this, e);\r
107                 if(this.handler){\r
108                     this.handler.call(this.scope || this, this, e);\r
109                 }\r
110             }\r
111         }\r
112     },\r
113 \r
114     // private\r
115     isMenuTriggerOver : function(e){\r
116         return this.menu && e.target.tagName == 'em';\r
117     },\r
118 \r
119     // private\r
120     isMenuTriggerOut : function(e, internal){\r
121         return this.menu && e.target.tagName != 'em';\r
122     }\r
123 });\r
124 \r
125 Ext.reg('splitbutton', Ext.SplitButton);</pre>    \r
126 </body>\r
127 </html>