3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * @extends Ext.BoxComponent
\r
10 * Simple Button class
\r
11 * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
\r
12 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
\r
13 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
\r
14 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
\r
15 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
\r
16 * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
\r
17 * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
\r
19 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
\r
20 * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
\r
21 * @cfg {String/Object} tooltip The tooltip for the button - can be a string to be used as innerHTML (html tags are accepted) or QuickTips config object
\r
22 * @cfg {Boolean} hidden True to start hidden (defaults to false)
\r
23 * @cfg {Boolean} disabled True to start disabled (defaults to false)
\r
24 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
\r
25 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
\r
26 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
\r
27 * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
\r
29 * Create a new button
\r
30 * @param {Object} config The config object
\r
33 Ext.Button = Ext.extend(Ext.BoxComponent, {
\r
35 * Read-only. True if this button is hidden
\r
40 * Read-only. True if this button is disabled
\r
45 * Read-only. True if this button is pressed (only if enableToggle = true)
\r
51 * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
\r
55 * @cfg {Boolean} allowDepress
\r
56 * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
\r
60 * @cfg {Boolean} enableToggle
\r
61 * True to enable pressed/not pressed toggling (defaults to false)
\r
63 enableToggle : false,
\r
65 * @cfg {Function} toggleHandler
\r
66 * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
\r
67 * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
\r
68 * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
\r
73 * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
\r
76 * @cfg {String} menuAlign
\r
77 * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
\r
79 menuAlign : 'tl-bl?',
\r
82 * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
\r
83 * text to be used if this item is shown in the overflow menu. See also
\r
84 * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
\r
87 * @cfg {String} iconCls
\r
88 * A css class which sets a background image to be used as the icon for this button
\r
91 * @cfg {String} type
\r
92 * submit, reset or button - defaults to 'button'
\r
97 menuClassTarget : 'tr:nth(2)',
\r
100 * @cfg {String} clickEvent
\r
101 * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
\r
102 * Defaults to <tt>'click'</tt>.
\r
104 clickEvent : 'click',
\r
107 * @cfg {Boolean} handleMouseEvents
\r
108 * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
\r
110 handleMouseEvents : true,
\r
113 * @cfg {String} tooltipType
\r
114 * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
\r
116 tooltipType : 'qtip',
\r
119 * @cfg {String} buttonSelector
\r
120 * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
\r
121 * DOM structure created.</p>
\r
122 * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
\r
123 * a focussable element.</p>
\r
124 * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
\r
126 buttonSelector : 'button:first-child',
\r
129 * @cfg {String} scale
\r
130 * <p>(Optional) The size of the Button. Three values are allowed:</p>
\r
131 * <ul class="mdetail-params">
\r
132 * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
\r
133 * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
\r
134 * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
\r
136 * <p>Defaults to <b><tt>'small'</tt></b>.</p>
\r
141 * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
\r
142 * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
\r
143 * executed. Defaults to this Button.
\r
147 * @cfg {String} iconAlign
\r
148 * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
\r
149 * <ul class="mdetail-params">
\r
150 * <li>'top'<div class="sub-desc"></div></li>
\r
151 * <li>'right'<div class="sub-desc"></div></li>
\r
152 * <li>'bottom'<div class="sub-desc"></div></li>
\r
153 * <li>'left'<div class="sub-desc"></div></li>
\r
155 * <p>Defaults to <b><tt>'left'</tt></b>.</p>
\r
157 iconAlign : 'left',
\r
160 * @cfg {String} arrowAlign
\r
161 * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
\r
162 * Two values are allowed:</p>
\r
163 * <ul class="mdetail-params">
\r
164 * <li>'right'<div class="sub-desc"></div></li>
\r
165 * <li>'bottom'<div class="sub-desc"></div></li>
\r
167 * <p>Defaults to <b><tt>'right'</tt></b>.</p>
\r
169 arrowAlign : 'right',
\r
172 * @cfg {Ext.Template} template (Optional)
\r
173 * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
\r
174 * Instances, or subclasses which need a different DOM structure may provide a different
\r
175 * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
\r
176 * @type Ext.Template
\r
177 * @property template
\r
180 * @cfg {String} cls
\r
181 * A CSS class string to apply to the button's main element.
\r
186 * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
\r
189 initComponent : function(){
\r
190 Ext.Button.superclass.initComponent.call(this);
\r
195 * Fires when this button is clicked
\r
196 * @param {Button} this
\r
197 * @param {EventObject} e The click event
\r
202 * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
\r
203 * @param {Button} this
\r
204 * @param {Boolean} pressed
\r
209 * Fires when the mouse hovers over the button
\r
210 * @param {Button} this
\r
211 * @param {Event} e The event object
\r
216 * Fires when the mouse exits the button
\r
217 * @param {Button} this
\r
218 * @param {Event} e The event object
\r
223 * If this button has a menu, this event fires when it is shown
\r
224 * @param {Button} this
\r
225 * @param {Menu} menu
\r
230 * If this button has a menu, this event fires when it is hidden
\r
231 * @param {Button} this
\r
232 * @param {Menu} menu
\r
236 * @event menutriggerover
\r
237 * If this button has a menu, this event fires when the mouse enters the menu triggering element
\r
238 * @param {Button} this
\r
239 * @param {Menu} menu
\r
240 * @param {EventObject} e
\r
244 * @event menutriggerout
\r
245 * If this button has a menu, this event fires when the mouse leaves the menu triggering element
\r
246 * @param {Button} this
\r
247 * @param {Menu} menu
\r
248 * @param {EventObject} e
\r
253 this.menu = Ext.menu.MenuMgr.get(this.menu);
\r
255 if(Ext.isString(this.toggleGroup)){
\r
256 this.enableToggle = true;
\r
261 * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used
\r
262 * to create this Button's DOM structure.</p>
\r
263 * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
\r
264 * own implementation of this method.</p>
\r
265 * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
\r
266 * following items:</p><div class="mdetail-params"><ul>
\r
267 * <li>The <button>'s {@link #type}</li>
\r
268 * <li>A CSS class name applied to the Button's main <tbody> element which determines the button's scale and icon alignment.</li>
\r
269 * <li>A CSS class to determine the presence and position of an arrow icon. (<code>'x-btn-arrow'</code> or <code>'x-btn-arrow-bottom'</code> or <code>''</code>)</li>
\r
270 * <li>The {@link #cls} CSS class name applied to the button's wrapping <table> element.</li>
\r
271 * <li>The Component id which is applied to the button's wrapping <table> element.</li>
\r
273 * @return {Array} Substitution data for a Template.
\r
275 getTemplateArgs : function(){
\r
276 return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
\r
280 setButtonClass : function(){
\r
281 if(this.useSetClass){
\r
282 if(!Ext.isEmpty(this.oldCls)){
\r
283 this.el.removeClass([this.oldCls, 'x-btn-pressed']);
\r
285 this.oldCls = (this.iconCls || this.icon) ? (this.text ? ' x-btn-text-icon' : ' x-btn-icon') : ' x-btn-noicon';
\r
286 this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
\r
291 getMenuClass : function(){
\r
292 return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
\r
296 onRender : function(ct, position){
\r
297 if(!this.template){
\r
298 if(!Ext.Button.buttonTemplate){
\r
299 // hideous table template
\r
300 Ext.Button.buttonTemplate = new Ext.Template(
\r
301 '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
\r
302 '<tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr>',
\r
303 '<tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i> </i></td></tr>',
\r
304 '<tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr>',
\r
305 '</tbody></table>');
\r
306 Ext.Button.buttonTemplate.compile();
\r
308 this.template = Ext.Button.buttonTemplate;
\r
311 var btn, targs = this.getTemplateArgs();
\r
314 btn = this.template.insertBefore(position, targs, true);
\r
316 btn = this.template.append(ct, targs, true);
\r
319 * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
\r
320 * this references a <tt><button></tt> element. Read only.
\r
321 * @type Ext.Element
\r
324 this.btnEl = btn.child(this.buttonSelector);
\r
325 this.mon(this.btnEl, {
\r
327 focus: this.onFocus,
\r
331 this.initButtonEl(btn, this.btnEl);
\r
333 Ext.ButtonToggleMgr.register(this);
\r
337 initButtonEl : function(btn, btnEl){
\r
339 this.setIcon(this.icon);
\r
340 this.setText(this.text);
\r
341 this.setIconClass(this.iconCls);
\r
342 if(Ext.isDefined(this.tabIndex)){
\r
343 btnEl.dom.tabIndex = this.tabIndex;
\r
346 this.setTooltip(this.tooltip, true);
\r
349 if(this.handleMouseEvents){
\r
352 mouseover: this.onMouseOver,
\r
353 mousedown: this.onMouseDown
\r
356 // new functionality for monitoring on the document level
\r
357 //this.mon(btn, 'mouseout', this.onMouseOut, this);
\r
361 this.mon(this.menu, {
\r
363 show: this.onMenuShow,
\r
364 hide: this.onMenuHide
\r
369 var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
\r
370 this.mon(repeater, 'click', this.onClick, this);
\r
372 this.mon(btn, this.clickEvent, this.onClick, this);
\r
376 afterRender : function(){
\r
377 Ext.Button.superclass.afterRender.call(this);
\r
378 this.useSetClass = true;
\r
379 this.setButtonClass();
\r
380 this.doc = Ext.getDoc();
\r
381 this.doAutoWidth();
\r
385 * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
\r
386 * the value of the {@link iconCls} config internally.
\r
387 * @param {String} cls The CSS class providing the icon image
\r
388 * @return {Ext.Button} this
\r
390 setIconClass : function(cls){
\r
391 this.iconCls = cls;
\r
393 this.btnEl.dom.className = '';
\r
394 this.btnEl.addClass(['x-btn-text', cls || '']);
\r
395 this.setButtonClass();
\r
401 * Sets the tooltip for this Button.
\r
402 * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
\r
403 * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
\r
404 * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
\r
406 * @return {Ext.Button} this
\r
408 setTooltip : function(tooltip, /* private */ initial){
\r
413 if(Ext.isObject(tooltip)){
\r
414 Ext.QuickTips.register(Ext.apply({
\r
415 target: this.btnEl.id
\r
417 this.tooltip = tooltip;
\r
419 this.btnEl.dom[this.tooltipType] = tooltip;
\r
422 this.tooltip = tooltip;
\r
428 clearTip : function(){
\r
429 if(Ext.isObject(this.tooltip)){
\r
430 Ext.QuickTips.unregister(this.btnEl);
\r
435 beforeDestroy : function(){
\r
439 if(this.menu && this.menu.autoDestroy) {
\r
440 Ext.destroy(this.menu);
\r
442 Ext.destroy(this.repeater);
\r
446 onDestroy : function(){
\r
448 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
449 this.doc.un('mouseup', this.onMouseUp, this);
\r
452 Ext.ButtonToggleMgr.unregister(this);
\r
457 doAutoWidth : function(){
\r
458 if(this.el && this.text && this.width === undefined){
\r
459 this.el.setWidth('auto');
\r
460 if(Ext.isIE7 && Ext.isStrict){
\r
461 var ib = this.btnEl;
\r
462 if(ib && ib.getWidth() > 20){
\r
464 ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
\r
468 if(this.el.getWidth() < this.minWidth){
\r
469 this.el.setWidth(this.minWidth);
\r
476 * Assigns this Button's click handler
\r
477 * @param {Function} handler The function to call when the button is clicked
\r
478 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
\r
479 * Defaults to this Button.
\r
480 * @return {Ext.Button} this
\r
482 setHandler : function(handler, scope){
\r
483 this.handler = handler;
\r
484 this.scope = scope;
\r
489 * Sets this Button's text
\r
490 * @param {String} text The button text
\r
491 * @return {Ext.Button} this
\r
493 setText : function(text){
\r
496 this.btnEl.update(text || ' ');
\r
497 this.setButtonClass();
\r
499 this.doAutoWidth();
\r
504 * Sets the background image (inline style) of the button. This method also changes
\r
505 * the value of the {@link icon} config internally.
\r
506 * @param {String} icon The path to an image to display in the button
\r
507 * @return {Ext.Button} this
\r
509 setIcon : function(icon){
\r
512 this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
\r
513 this.setButtonClass();
\r
519 * Gets the text for this Button
\r
520 * @return {String} The button text
\r
522 getText : function(){
\r
527 * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
\r
528 * @param {Boolean} state (optional) Force a particular state
\r
529 * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
\r
530 * @return {Ext.Button} this
\r
532 toggle : function(state, suppressEvent){
\r
533 state = state === undefined ? !this.pressed : !!state;
\r
534 if(state != this.pressed){
\r
536 this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
\r
538 this.pressed = state;
\r
539 if(!suppressEvent){
\r
540 this.fireEvent('toggle', this, state);
\r
541 if(this.toggleHandler){
\r
542 this.toggleHandler.call(this.scope || this, this, state);
\r
552 focus : function(){
\r
553 this.btnEl.focus();
\r
557 onDisable : function(){
\r
558 this.onDisableChange(true);
\r
562 onEnable : function(){
\r
563 this.onDisableChange(false);
\r
566 onDisableChange : function(disabled){
\r
568 if(!Ext.isIE6 || !this.text){
\r
569 this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
\r
571 this.el.dom.disabled = disabled;
\r
573 this.disabled = disabled;
\r
577 * Show this button's menu (if it has one)
\r
579 showMenu : function(){
\r
580 if(this.rendered && this.menu){
\r
582 Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
\r
584 this.menu.show(this.el, this.menuAlign);
\r
590 * Hide this button's menu (if it has one)
\r
592 hideMenu : function(){
\r
600 * Returns true if the button has a menu and it is visible
\r
601 * @return {Boolean}
\r
603 hasVisibleMenu : function(){
\r
604 return this.menu && this.menu.isVisible();
\r
608 onClick : function(e){
\r
610 e.preventDefault();
\r
612 if(e.button !== 0){
\r
615 if(!this.disabled){
\r
616 if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
\r
619 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
\r
622 this.fireEvent('click', this, e);
\r
624 //this.el.removeClass('x-btn-over');
\r
625 this.handler.call(this.scope || this, this, e);
\r
631 isMenuTriggerOver : function(e, internal){
\r
632 return this.menu && !internal;
\r
636 isMenuTriggerOut : function(e, internal){
\r
637 return this.menu && !internal;
\r
641 onMouseOver : function(e){
\r
642 if(!this.disabled){
\r
643 var internal = e.within(this.el, true);
\r
645 this.el.addClass('x-btn-over');
\r
646 if(!this.monitoringMouseOver){
\r
647 this.doc.on('mouseover', this.monitorMouseOver, this);
\r
648 this.monitoringMouseOver = true;
\r
650 this.fireEvent('mouseover', this, e);
\r
652 if(this.isMenuTriggerOver(e, internal)){
\r
653 this.fireEvent('menutriggerover', this, this.menu, e);
\r
659 monitorMouseOver : function(e){
\r
660 if(e.target != this.el.dom && !e.within(this.el)){
\r
661 if(this.monitoringMouseOver){
\r
662 this.doc.un('mouseover', this.monitorMouseOver, this);
\r
663 this.monitoringMouseOver = false;
\r
665 this.onMouseOut(e);
\r
670 onMouseOut : function(e){
\r
671 var internal = e.within(this.el) && e.target != this.el.dom;
\r
672 this.el.removeClass('x-btn-over');
\r
673 this.fireEvent('mouseout', this, e);
\r
674 if(this.isMenuTriggerOut(e, internal)){
\r
675 this.fireEvent('menutriggerout', this, this.menu, e);
\r
679 focus : function() {
\r
680 this.btnEl.focus();
\r
683 blur : function() {
\r
688 onFocus : function(e){
\r
689 if(!this.disabled){
\r
690 this.el.addClass('x-btn-focus');
\r
694 onBlur : function(e){
\r
695 this.el.removeClass('x-btn-focus');
\r
699 getClickEl : function(e, isUp){
\r
704 onMouseDown : function(e){
\r
705 if(!this.disabled && e.button === 0){
\r
706 this.getClickEl(e).addClass('x-btn-click');
\r
707 this.doc.on('mouseup', this.onMouseUp, this);
\r
711 onMouseUp : function(e){
\r
712 if(e.button === 0){
\r
713 this.getClickEl(e, true).removeClass('x-btn-click');
\r
714 this.doc.un('mouseup', this.onMouseUp, this);
\r
718 onMenuShow : function(e){
\r
719 this.menu.ownerCt = this;
\r
720 this.ignoreNextClick = 0;
\r
721 this.el.addClass('x-btn-menu-active');
\r
722 this.fireEvent('menushow', this, this.menu);
\r
725 onMenuHide : function(e){
\r
726 this.el.removeClass('x-btn-menu-active');
\r
727 this.ignoreNextClick = this.restoreClick.defer(250, this);
\r
728 this.fireEvent('menuhide', this, this.menu);
\r
729 delete this.menu.ownerCt;
\r
733 restoreClick : function(){
\r
734 this.ignoreNextClick = 0;
\r
738 * @cfg {String} autoEl @hide
\r
741 Ext.reg('button', Ext.Button);
\r
743 // Private utility class used by Button
\r
744 Ext.ButtonToggleMgr = function(){
\r
747 function toggleGroup(btn, state){
\r
749 var g = groups[btn.toggleGroup];
\r
750 for(var i = 0, l = g.length; i < l; i++){
\r
752 g[i].toggle(false);
\r
759 register : function(btn){
\r
760 if(!btn.toggleGroup){
\r
763 var g = groups[btn.toggleGroup];
\r
765 g = groups[btn.toggleGroup] = [];
\r
768 btn.on('toggle', toggleGroup);
\r
771 unregister : function(btn){
\r
772 if(!btn.toggleGroup){
\r
775 var g = groups[btn.toggleGroup];
\r
778 btn.un('toggle', toggleGroup);
\r
783 * Gets the pressed button in the passed group or null
\r
784 * @param {String} group
\r
787 getPressed : function(group){
\r
788 var g = groups[group];
\r
790 for(var i = 0, len = g.length; i < len; i++){
\r
791 if(g[i].pressed === true){
\r
801 * @class Ext.SplitButton
\r
802 * @extends Ext.Button
\r
803 * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
\r
804 * click event of the button. Typically this would be used to display a dropdown menu that provides additional
\r
805 * options to the primary button action, but any custom handler can provide the arrowclick implementation. Example usage:
\r
807 // display a dropdown menu:
\r
808 new Ext.SplitButton({
\r
809 renderTo: 'button-ct', // the container id
\r
811 handler: optionsHandler, // handle a click on the button itself
\r
812 menu: new Ext.menu.Menu({
\r
814 // these items will render as dropdown menu items when the arrow is clicked:
\r
815 {text: 'Item 1', handler: item1Handler},
\r
816 {text: 'Item 2', handler: item2Handler}
\r
821 // Instead of showing a menu, you provide any type of custom
\r
822 // functionality you want when the dropdown arrow is clicked:
\r
823 new Ext.SplitButton({
\r
824 renderTo: 'button-ct',
\r
826 handler: optionsHandler,
\r
827 arrowHandler: myCustomHandler
\r
830 * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
\r
831 * @cfg {String} arrowTooltip The title attribute of the arrow
\r
833 * Create a new menu button
\r
834 * @param {Object} config The config object
\r
835 * @xtype splitbutton
\r
837 Ext.SplitButton = Ext.extend(Ext.Button, {
\r
839 arrowSelector : 'em',
\r
843 initComponent : function(){
\r
844 Ext.SplitButton.superclass.initComponent.call(this);
\r
846 * @event arrowclick
\r
847 * Fires when this button's arrow is clicked
\r
848 * @param {MenuButton} this
\r
849 * @param {EventObject} e The click event
\r
851 this.addEvents("arrowclick");
\r
855 onRender : function(){
\r
856 Ext.SplitButton.superclass.onRender.apply(this, arguments);
\r
857 if(this.arrowTooltip){
\r
858 this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;
\r
863 * Sets this button's arrow click handler.
\r
864 * @param {Function} handler The function to call when the arrow is clicked
\r
865 * @param {Object} scope (optional) Scope for the function passed above
\r
867 setArrowHandler : function(handler, scope){
\r
868 this.arrowHandler = handler;
\r
869 this.scope = scope;
\r
872 getMenuClass : function(){
\r
873 return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');
\r
876 isClickOnArrow : function(e){
\r
877 if (this.arrowAlign != 'bottom') {
\r
878 var visBtn = this.el.child('em.x-btn-split');
\r
879 var right = visBtn.getRegion().right - visBtn.getPadding('r');
\r
880 return e.getPageX() > right;
\r
882 return e.getPageY() > this.btnEl.getRegion().bottom;
\r
887 onClick : function(e, t){
\r
888 e.preventDefault();
\r
889 if(!this.disabled){
\r
890 if(this.isClickOnArrow(e)){
\r
891 if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
\r
894 this.fireEvent("arrowclick", this, e);
\r
895 if(this.arrowHandler){
\r
896 this.arrowHandler.call(this.scope || this, this, e);
\r
899 if(this.enableToggle){
\r
902 this.fireEvent("click", this, e);
\r
904 this.handler.call(this.scope || this, this, e);
\r
911 isMenuTriggerOver : function(e){
\r
912 return this.menu && e.target.tagName == this.arrowSelector;
\r
916 isMenuTriggerOut : function(e, internal){
\r
917 return this.menu && e.target.tagName != this.arrowSelector;
\r
921 Ext.reg('splitbutton', Ext.SplitButton);/**
\r
922 * @class Ext.CycleButton
\r
923 * @extends Ext.SplitButton
\r
924 * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
\r
925 * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
\r
926 * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
\r
927 * button displays the dropdown menu just like a normal SplitButton. Example usage:
\r
929 var btn = new Ext.CycleButton({
\r
931 prependText: 'View as ',
\r
934 iconCls:'view-text',
\r
938 iconCls:'view-html'
\r
940 changeHandler:function(btn, item){
\r
941 Ext.Msg.alert('Change View', item.text);
\r
946 * Create a new split button
\r
947 * @param {Object} config The config object
\r
950 Ext.CycleButton = Ext.extend(Ext.SplitButton, {
\r
952 * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
\r
953 * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
\r
956 * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
\r
959 * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
\r
960 * button's text (only applies when showText = true, defaults to '')
\r
963 * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
\r
964 * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
\r
965 * fire the {@link #change} event on active item change. The changeHandler function will be called with the
\r
966 * following argument list: (SplitButton this, Ext.menu.CheckItem item)
\r
969 * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
\r
970 * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
\r
971 * default behavior of changing the button's icon to match the selected item's icon on change.
\r
976 * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
\r
980 getItemText : function(item){
\r
981 if(item && this.showText === true){
\r
983 if(this.prependText){
\r
984 text += this.prependText;
\r
993 * Sets the button's active menu item.
\r
994 * @param {Ext.menu.CheckItem} item The item to activate
\r
995 * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
\r
997 setActiveItem : function(item, suppressEvent){
\r
998 if(!Ext.isObject(item)){
\r
999 item = this.menu.getComponent(item);
\r
1002 if(!this.rendered){
\r
1003 this.text = this.getItemText(item);
\r
1004 this.iconCls = item.iconCls;
\r
1006 var t = this.getItemText(item);
\r
1010 this.setIconClass(item.iconCls);
\r
1012 this.activeItem = item;
\r
1013 if(!item.checked){
\r
1014 item.setChecked(true, true);
\r
1016 if(this.forceIcon){
\r
1017 this.setIconClass(this.forceIcon);
\r
1019 if(!suppressEvent){
\r
1020 this.fireEvent('change', this, item);
\r
1026 * Gets the currently active menu item.
\r
1027 * @return {Ext.menu.CheckItem} The active item
\r
1029 getActiveItem : function(){
\r
1030 return this.activeItem;
\r
1034 initComponent : function(){
\r
1038 * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
\r
1039 * is set on this CycleButton, it will be called instead on active item change and this change event will
\r
1041 * @param {Ext.CycleButton} this
\r
1042 * @param {Ext.menu.CheckItem} item The menu item that was selected
\r
1047 if(this.changeHandler){
\r
1048 this.on('change', this.changeHandler, this.scope||this);
\r
1049 delete this.changeHandler;
\r
1052 this.itemCount = this.items.length;
\r
1054 this.menu = {cls:'x-cycle-menu', items:[]};
\r
1056 Ext.each(this.items, function(item, i){
\r
1058 group: item.group || this.id,
\r
1060 checkHandler: this.checkHandler,
\r
1062 checked: item.checked || false
\r
1064 this.menu.items.push(item);
\r
1069 this.setActiveItem(checked, true);
\r
1070 Ext.CycleButton.superclass.initComponent.call(this);
\r
1072 this.on('click', this.toggleSelected, this);
\r
1076 checkHandler : function(item, pressed){
\r
1078 this.setActiveItem(item);
\r
1083 * This is normally called internally on button click, but can be called externally to advance the button's
\r
1084 * active item programmatically to the next one in the menu. If the current item is the last one in the menu
\r
1085 * the active item will be set to the first item in the menu.
\r
1087 toggleSelected : function(){
\r
1088 var m = this.menu;
\r
1090 // layout if we haven't before so the items are active
\r
1095 var nextIdx, checkItem;
\r
1096 for (var i = 1; i < this.itemCount; i++) {
\r
1097 nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
\r
1098 // check the potential item
\r
1099 checkItem = m.items.itemAt(nextIdx);
\r
1100 // if its not disabled then check it.
\r
1101 if (!checkItem.disabled) {
\r
1102 checkItem.setChecked(true);
\r
1108 Ext.reg('cycle', Ext.CycleButton);