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