Upgrade to ExtJS 4.0.7 - Released 10/19/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  * A split button that provides a built-in dropdown arrow that can fire an event separately from the default click event
17  * of the button. Typically this would be used to display a dropdown menu that provides additional options to the
18  * primary button action, but any custom handler can provide the arrowclick implementation.  Example usage:
19  *
20  *     @example
21  *     // display a dropdown menu:
22  *     Ext.create('Ext.button.Split', {
23  *         renderTo: Ext.getBody(),
24  *         text: 'Options',
25  *         // handle a click on the button itself
26  *         handler: function() {
27  *             alert("The button was clicked");
28  *         },
29  *         menu: new Ext.menu.Menu({
30  *             items: [
31  *                 // these will render as dropdown menu items when the arrow is clicked:
32  *                 {text: 'Item 1', handler: function(){ alert("Item 1 clicked"); }},
33  *                 {text: 'Item 2', handler: function(){ alert("Item 2 clicked"); }}
34  *             ]
35  *         })
36  *     });
37  *
38  * Instead of showing a menu, you can provide any type of custom functionality you want when the dropdown
39  * arrow is clicked:
40  *
41  *     Ext.create('Ext.button.Split', {
42  *         renderTo: 'button-ct',
43  *         text: 'Options',
44  *         handler: optionsHandler,
45  *         arrowHandler: myCustomHandler
46  *     });
47  *
48  */
49 Ext.define('Ext.button.Split', {
50
51     /* Begin Definitions */
52     alias: 'widget.splitbutton',
53
54     extend: 'Ext.button.Button',
55     alternateClassName: 'Ext.SplitButton',
56     /* End Definitions */
57     
58     /**
59      * @cfg {Function} arrowHandler
60      * A function called when the arrow button is clicked (can be used instead of click event)
61      */
62     /**
63      * @cfg {String} arrowTooltip
64      * The title attribute of the arrow
65      */
66
67     // private
68     arrowCls      : 'split',
69     split         : true,
70
71     // private
72     initComponent : function(){
73         this.callParent();
74         /**
75          * @event arrowclick
76          * Fires when this button's arrow is clicked.
77          * @param {Ext.button.Split} this
78          * @param {Event} e The click event
79          */
80         this.addEvents("arrowclick");
81     },
82
83     /**
84      * Sets this button's arrow click handler.
85      * @param {Function} handler The function to call when the arrow is clicked
86      * @param {Object} scope (optional) Scope for the function passed above
87      */
88     setArrowHandler : function(handler, scope){
89         this.arrowHandler = handler;
90         this.scope = scope;
91     },
92
93     // private
94     onClick : function(e, t) {
95         var me = this;
96         
97         e.preventDefault();
98         if (!me.disabled) {
99             if (me.overMenuTrigger) {
100                 me.maybeShowMenu();
101                 me.fireEvent("arrowclick", me, e);
102                 if (me.arrowHandler) {
103                     me.arrowHandler.call(me.scope || me, me, e);
104                 }
105             } else {
106                 me.doToggle();
107                 me.fireHandler();
108             }
109         }
110     }
111 });