3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.toolbar.Toolbar
17 * @extends Ext.container.Container
19 Basic Toolbar class. Although the {@link Ext.container.Container#defaultType defaultType} for Toolbar is {@link Ext.button.Button button}, Toolbar
20 elements (child items for the Toolbar container) may be virtually any type of Component. Toolbar elements can be created explicitly via their
21 constructors, or implicitly via their xtypes, and can be {@link #add}ed dynamically.
23 __Some items have shortcut strings for creation:__
25 | Shortcut | xtype | Class | Description |
26 |:---------|:--------------|:------------------------------|:---------------------------------------------------|
27 | `->` | `tbspacer` | {@link Ext.toolbar.Fill} | begin using the right-justified button container |
28 | `-` | `tbseparator` | {@link Ext.toolbar.Separator} | add a vertical separator bar between toolbar items |
29 | ` ` | `tbspacer` | {@link Ext.toolbar.Spacer} | add horiztonal space between elements |
31 {@img Ext.toolbar.Toolbar/Ext.toolbar.Toolbar1.png Toolbar component}
34 Ext.create('Ext.toolbar.Toolbar', {
35 renderTo: document.body,
39 // xtype: 'button', // default for Toolbars
46 // begin using the right-justified button container
47 '->', // same as {xtype: 'tbfill'}, // Ext.toolbar.Fill
51 emptyText: 'enter search term'
53 // add a vertical separator bar between toolbar items
54 '-', // same as {xtype: 'tbseparator'} to create Ext.toolbar.Separator
55 'text 1', // same as {xtype: 'tbtext', text: 'text1'} to create Ext.toolbar.TextItem
56 {xtype: 'tbspacer'},// same as ' ' to create Ext.toolbar.Spacer
58 {xtype: 'tbspacer', width: 50}, // add a 50px space
63 Toolbars have {@link #enable} and {@link #disable} methods which when called, will enable/disable all items within your toolbar.
65 {@img Ext.toolbar.Toolbar/Ext.toolbar.Toolbar2.png Toolbar component}
68 Ext.create('Ext.toolbar.Toolbar', {
69 renderTo: document.body,
83 emptyText: 'enter search term'
88 {@img Ext.toolbar.Toolbar/Ext.toolbar.Toolbar3.png Toolbar component}
91 var enableBtn = Ext.create('Ext.button.Button', {
92 text : 'Enable All Items',
95 handler : function() {
96 //disable the enable button and enable the disable button
105 var disableBtn = Ext.create('Ext.button.Button', {
106 text : 'Disable All Items',
108 handler : function() {
109 //enable the enable button and disable button
110 disableBtn.disable();
113 //disable the toolbar
118 var toolbar = Ext.create('Ext.toolbar.Toolbar', {
119 renderTo: document.body,
122 items : [enableBtn, disableBtn]
125 Adding items to and removing items from a toolbar is as simple as calling the {@link #add} and {@link #remove} methods. There is also a {@link #removeAll} method
126 which remove all items within the toolbar.
128 {@img Ext.toolbar.Toolbar/Ext.toolbar.Toolbar4.png Toolbar component}
131 var toolbar = Ext.create('Ext.toolbar.Toolbar', {
132 renderTo: document.body,
136 text: 'Example Button'
143 Ext.create('Ext.toolbar.Toolbar', {
144 renderTo: document.body,
149 text : 'Add a button',
151 handler: function() {
152 var text = prompt('Please enter the text for your button:');
153 addedItems.push(toolbar.add({
159 text : 'Add a text item',
161 handler: function() {
162 var text = prompt('Please enter the text for your item:');
163 addedItems.push(toolbar.add(text));
167 text : 'Add a toolbar seperator',
169 handler: function() {
170 addedItems.push(toolbar.add('-'));
174 text : 'Add a toolbar spacer',
176 handler: function() {
177 addedItems.push(toolbar.add('->'));
182 text : 'Remove last inserted item',
184 handler: function() {
185 if (addedItems.length) {
186 toolbar.remove(addedItems.pop());
187 } else if (toolbar.items.length) {
188 toolbar.remove(toolbar.items.last());
190 alert('No items in the toolbar');
195 text : 'Remove all items',
197 handler: function() {
205 * Creates a new Toolbar
206 * @param {Object/Array} config A config object or an array of buttons to <code>{@link #add}</code>
207 * @docauthor Robert Dougan <rob@sencha.com>
210 Ext.define('Ext.toolbar.Toolbar', {
211 extend: 'Ext.container.Container',
214 'Ext.layout.container.HBox',
215 'Ext.layout.container.VBox',
219 'Ext.toolbar.Separator'
221 alias: 'widget.toolbar',
222 alternateClassName: 'Ext.Toolbar',
225 baseCls : Ext.baseCSSPrefix + 'toolbar',
226 ariaRole : 'toolbar',
228 defaultType: 'button',
231 * @cfg {Boolean} vertical
232 * Set to `true` to make the toolbar vertical. The layout will become a `vbox`.
233 * (defaults to `false`)
238 * @cfg {String/Object} layout
239 * This class assigns a default layout (<code>layout:'<b>hbox</b>'</code>).
240 * Developers <i>may</i> override this configuration option if another layout
241 * is required (the constructor must be passed a configuration object in this
242 * case instead of an array).
243 * See {@link Ext.container.Container#layout} for additional information.
247 * @cfg {Boolean} enableOverflow
248 * Defaults to false. Configure <code>true</code> to make the toolbar provide a button
249 * which activates a dropdown Menu to show items which overflow the Toolbar's width.
251 enableOverflow: false,
256 itemCls: Ext.baseCSSPrefix + 'toolbar-item',
258 initComponent: function() {
262 // check for simplified (old-style) overflow config:
263 if (!me.layout && me.enableOverflow) {
264 me.layout = { overflowHandler: 'Menu' };
267 if (me.dock === 'right' || me.dock === 'left') {
271 me.layout = Ext.applyIf(Ext.isString(me.layout) ? {
273 } : me.layout || {}, {
274 type: me.vertical ? 'vbox' : 'hbox',
275 align: me.vertical ? 'stretchmax' : 'middle',
276 clearInnerCtOnLayout: true
280 me.addClsWithUI('vertical');
283 // @TODO: remove this hack and implement a more general solution
284 if (me.ui === 'footer') {
285 me.ignoreBorderManagement = true;
291 * @event overflowchange
292 * Fires after the overflow state has changed.
293 * @param {Object} c The Container
294 * @param {Boolean} lastOverflow overflow state
296 me.addEvents('overflowchange');
298 // Subscribe to Ext.FocusManager for key navigation
299 keys = me.vertical ? ['up', 'down'] : ['left', 'right'];
300 Ext.FocusManager.subscribe(me, {
306 * <p>Adds element(s) to the toolbar -- this function takes a variable number of
307 * arguments of mixed type and adds them to the toolbar.</p>
308 * <br><p><b>Note</b>: See the notes within {@link Ext.container.Container#add}.</p>
309 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
311 * <li>{@link Ext.button.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
312 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
313 * <li>Field: Any form field (equivalent to {@link #addField})</li>
314 * <li>Item: Any subclass of {@link Ext.toolbar.Item} (equivalent to {@link #addItem})</li>
315 * <li>String: Any generic string (gets wrapped in a {@link Ext.toolbar.TextItem}, equivalent to {@link #addText}).
316 * Note that there are a few special strings that are treated differently as explained next.</li>
317 * <li>'-': Creates a separator element (equivalent to {@link #addSeparator})</li>
318 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
319 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
321 * @param {Mixed} arg2
322 * @param {Mixed} etc.
327 lookupComponent: function(c) {
328 if (Ext.isString(c)) {
329 var shortcut = Ext.toolbar.Toolbar.shortcuts[c];
340 this.applyDefaults(c);
342 return this.callParent(arguments);
346 applyDefaults: function(c) {
347 if (!Ext.isString(c)) {
348 c = this.callParent(arguments);
349 var d = this.internalDefaults;
351 Ext.applyIf(c.initialConfig, d);
361 trackMenu: function(item, remove) {
362 if (this.trackMenus && item.menu) {
363 var method = remove ? 'mun' : 'mon',
366 me[method](item, 'menutriggerover', me.onButtonTriggerOver, me);
367 me[method](item, 'menushow', me.onButtonMenuShow, me);
368 me[method](item, 'menuhide', me.onButtonMenuHide, me);
373 constructButton: function(item) {
374 return item.events ? item : this.createComponent(item, item.split ? 'splitbutton' : this.defaultType);
378 onBeforeAdd: function(component) {
379 if (component.is('field') || (component.is('button') && this.ui != 'footer')) {
380 component.ui = component.ui + '-toolbar';
383 // Any separators needs to know if is vertical or not
384 if (component instanceof Ext.toolbar.Separator) {
385 component.setUI((this.vertical) ? 'vertical' : 'horizontal');
388 this.callParent(arguments);
392 onAdd: function(component) {
393 this.callParent(arguments);
395 this.trackMenu(component);
402 onRemove: function(c) {
403 this.callParent(arguments);
404 this.trackMenu(c, true);
408 onButtonTriggerOver: function(btn){
409 if (this.activeMenuBtn && this.activeMenuBtn != btn) {
410 this.activeMenuBtn.hideMenu();
412 this.activeMenuBtn = btn;
417 onButtonMenuShow: function(btn) {
418 this.activeMenuBtn = btn;
422 onButtonMenuHide: function(btn) {
423 delete this.activeMenuBtn;