Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / button / Split.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.button.Split
17  * @extends Ext.button.Button
18  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
19  * click event of the button.  Typically this would be used to display a dropdown menu that provides additional
20  * options to the primary button action, but any custom handler can provide the arrowclick implementation.  
21  * {@img Ext.button.Split/Ext.button.Split.png Ext.button.Split component}
22  * Example usage:
23  * <pre><code>
24 // display a dropdown menu:
25     Ext.create('Ext.button.Split', {
26         renderTo: 'button-ct', // the container id
27         text: 'Options',
28         handler: optionsHandler, // handle a click on the button itself
29         menu: new Ext.menu.Menu({
30         items: [
31                 // these items will render as dropdown menu items when the arrow is clicked:
32                 {text: 'Item 1', handler: item1Handler},
33                 {text: 'Item 2', handler: item2Handler}
34         ]
35         })
36     });
37
38 // Instead of showing a menu, you provide any type of custom
39 // functionality you want when the dropdown arrow is clicked:
40     Ext.create('Ext.button.Split', {
41         renderTo: 'button-ct',
42         text: 'Options',
43         handler: optionsHandler,
44         arrowHandler: myCustomHandler
45     });
46 </code></pre>
47  * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
48  * @cfg {String} arrowTooltip The title attribute of the arrow
49  */
50 Ext.define('Ext.button.Split', {
51
52     /* Begin Definitions */
53
54     alias: 'widget.splitbutton',
55
56     extend: 'Ext.button.Button',
57     alternateClassName: 'Ext.SplitButton',
58
59     // private
60     arrowCls      : 'split',
61     split         : true,
62
63     // private
64     initComponent : function(){
65         this.callParent();
66         /**
67          * @event arrowclick
68          * Fires when this button's arrow is clicked
69          * @param {MenuButton} this
70          * @param {EventObject} e The click event
71          */
72         this.addEvents("arrowclick");
73     },
74
75      /**
76      * Sets this button's arrow click handler.
77      * @param {Function} handler The function to call when the arrow is clicked
78      * @param {Object} scope (optional) Scope for the function passed above
79      */
80     setArrowHandler : function(handler, scope){
81         this.arrowHandler = handler;
82         this.scope = scope;
83     },
84
85     // private
86     onClick : function(e, t) {
87         var me = this;
88         
89         e.preventDefault();
90         if (!me.disabled) {
91             if (me.overMenuTrigger) {
92                 if (me.menu && !me.menu.isVisible() && !me.ignoreNextClick) {
93                     me.showMenu();
94                 }
95                 me.fireEvent("arrowclick", me, e);
96                 if (me.arrowHandler) {
97                     me.arrowHandler.call(me.scope || me, me, e);
98                 }
99             } else {
100                 if (me.enableToggle) {
101                     me.toggle();
102                 }
103                 me.fireEvent("click", me, e);
104                 if (me.handler) {
105                     me.handler.call(me.scope || me, me, e);
106                 }
107                 me.onBlur();
108             }
109         }
110     }
111 });