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