3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * @extends Ext.Container
10 * <p>Basic Toolbar class. Although the <tt>{@link Ext.Container#defaultType defaultType}</tt> for Toolbar
11 * is <tt>{@link Ext.Button button}</tt>, Toolbar elements (child items for the Toolbar container) may
12 * be virtually any type of Component. Toolbar elements can be created explicitly via their constructors,
13 * or implicitly via their xtypes, and can be <tt>{@link #add}</tt>ed dynamically.</p>
14 * <p>Some items have shortcut strings for creation:</p>
16 <u>Shortcut</u> <u>xtype</u> <u>Class</u> <u>Description</u>
17 '->' 'tbfill' {@link Ext.Toolbar.Fill} begin using the right-justified button container
18 '-' 'tbseparator' {@link Ext.Toolbar.Separator} add a vertical separator bar between toolbar items
19 ' ' 'tbspacer' {@link Ext.Toolbar.Spacer} add horiztonal space between elements
22 * Example usage of various elements:
24 var tb = new Ext.Toolbar({
25 renderTo: document.body,
30 // xtype: 'button', // default for Toolbars, same as 'tbbutton'
34 xtype: 'splitbutton', // same as 'tbsplitbutton'
37 // begin using the right-justified button container
38 '->', // same as {xtype: 'tbfill'}, // Ext.Toolbar.Fill
42 emptyText: 'enter search term'
44 // add a vertical separator bar between toolbar items
45 '-', // same as {xtype: 'tbseparator'} to create Ext.Toolbar.Separator
46 'text 1', // same as {xtype: 'tbtext', text: 'text1'} to create Ext.Toolbar.TextItem
47 {xtype: 'tbspacer'},// same as ' ' to create Ext.Toolbar.Spacer
49 {xtype: 'tbspacer', width: 50}, // add a 50px space
54 * Example adding a ComboBox within a menu of a button:
57 var combo = new Ext.form.ComboBox({
58 store: new Ext.data.ArrayStore({
60 fields: ['initials', 'fullname'],
62 ['FF', 'Fred Flintstone'],
63 ['BR', 'Barney Rubble']
66 displayField: 'fullname',
71 emptyText: 'Select a name...',
74 getListParent: function() {
75 return this.el.up('.x-menu');
77 iconCls: 'no-icon' //use iconCls if placing within menu to shift to right side of menu
80 // put ComboBox in a Menu
81 var menu = new Ext.menu.Menu({
84 combo // A Field in a Menu
88 // add a Button with the menu
90 text:'Button w/ Menu',
91 menu: menu // assign menu by instance
96 * Creates a new Toolbar
97 * @param {Object/Array} config A config object or an array of buttons to <tt>{@link #add}</tt>
100 Ext.Toolbar = function(config){
101 if(Ext.isArray(config)){
102 config = {items: config, layout: 'toolbar'};
108 config.items = config.buttons;
111 Ext.Toolbar.superclass.constructor.call(this, config);
118 Ext.extend(T, Ext.Container, {
120 defaultType: 'button',
123 * @cfg {String/Object} layout
124 * This class assigns a default layout (<code>layout:'<b>toolbar</b>'</code>).
125 * Developers <i>may</i> override this configuration option if another layout
126 * is required (the constructor must be passed a configuration object in this
127 * case instead of an array).
128 * See {@link Ext.Container#layout} for additional information.
131 enableOverflow : false,
134 * @cfg {Boolean} enableOverflow
135 * Defaults to false. Configure <code>true<code> to make the toolbar provide a button
136 * which activates a dropdown Menu to show items which overflow the Toolbar's width.
139 * @cfg {String} buttonAlign
140 * <p>The default position at which to align child items. Defaults to <code>"left"</code></p>
141 * <p>May be specified as <code>"center"</code> to cause items added before a Fill (A <code>"->"</code>) item
142 * to be centered in the Toolbar. Items added after a Fill are still right-aligned.</p>
143 * <p>Specify as <code>"right"</code> to right align all child items.</p>
147 internalDefaults: {removeMode: 'container', hideParent: true},
148 toolbarCls: 'x-toolbar',
150 initComponent : function(){
151 T.superclass.initComponent.call(this);
154 * @event overflowchange
155 * Fires after the overflow state has changed.
156 * @param {Object} c The Container
157 * @param {Boolean} lastOverflow overflow state
159 this.addEvents('overflowchange');
163 onRender : function(ct, position){
165 if(!this.autoCreate){
167 cls: this.toolbarCls + ' x-small-editor'
170 this.el = ct.createChild(Ext.apply({ id: this.id },this.autoCreate), position);
171 Ext.Toolbar.superclass.onRender.apply(this, arguments);
176 * <p>Adds element(s) to the toolbar -- this function takes a variable number of
177 * arguments of mixed type and adds them to the toolbar.</p>
178 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
179 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
181 * <li>{@link Ext.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
182 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
183 * <li>Field: Any form field (equivalent to {@link #addField})</li>
184 * <li>Item: Any subclass of {@link Ext.Toolbar.Item} (equivalent to {@link #addItem})</li>
185 * <li>String: Any generic string (gets wrapped in a {@link Ext.Toolbar.TextItem}, equivalent to {@link #addText}).
186 * Note that there are a few special strings that are treated differently as explained next.</li>
187 * <li>'-': Creates a separator element (equivalent to {@link #addSeparator})</li>
188 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
189 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
191 * @param {Mixed} arg2
192 * @param {Mixed} etc.
197 lookupComponent : function(c){
200 c = new T.Separator();
206 c = new T.TextItem(c);
208 this.applyDefaults(c);
210 if(c.isFormField || c.render){ // some kind of form field, some kind of Toolbar.Item
211 c = this.createComponent(c);
212 }else if(c.tag){ // DomHelper spec
213 c = new T.Item({autoEl: c});
214 }else if(c.tagName){ // element
215 c = new T.Item({el:c});
216 }else if(Ext.isObject(c)){ // must be button config?
217 c = c.xtype ? this.createComponent(c) : this.constructButton(c);
224 applyDefaults : function(c){
225 if(!Ext.isString(c)){
226 c = Ext.Toolbar.superclass.applyDefaults.call(this, c);
227 var d = this.internalDefaults;
229 Ext.applyIf(c.initialConfig, d);
240 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
241 * @return {Ext.Toolbar.Item} The separator {@link Ext.Toolbar.Item item}
243 addSeparator : function(){
244 return this.add(new T.Separator());
248 * Adds a spacer element
249 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
250 * @return {Ext.Toolbar.Spacer} The spacer item
252 addSpacer : function(){
253 return this.add(new T.Spacer());
257 * Forces subsequent additions into the float:right toolbar
258 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
260 addFill : function(){
261 this.add(new T.Fill());
265 * Adds any standard HTML element to the toolbar
266 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
267 * @param {Mixed} el The element or id of the element to add
268 * @return {Ext.Toolbar.Item} The element's item
270 addElement : function(el){
271 return this.addItem(new T.Item({el:el}));
275 * Adds any Toolbar.Item or subclass
276 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
277 * @param {Ext.Toolbar.Item} item
278 * @return {Ext.Toolbar.Item} The item
280 addItem : function(item){
281 return this.add.apply(this, arguments);
285 * Adds a button (or buttons). See {@link Ext.Button} for more info on the config.
286 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
287 * @param {Object/Array} config A button config or array of configs
288 * @return {Ext.Button/Array}
290 addButton : function(config){
291 if(Ext.isArray(config)){
293 for(var i = 0, len = config.length; i < len; i++) {
294 buttons.push(this.addButton(config[i]));
298 return this.add(this.constructButton(config));
302 * Adds text to the toolbar
303 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
304 * @param {String} text The text to add
305 * @return {Ext.Toolbar.Item} The element's item
307 addText : function(text){
308 return this.addItem(new T.TextItem(text));
312 * Adds a new element to the toolbar from the passed {@link Ext.DomHelper} config
313 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
314 * @param {Object} config
315 * @return {Ext.Toolbar.Item} The element's item
317 addDom : function(config){
318 return this.add(new T.Item({autoEl: config}));
322 * Adds a dynamically rendered Ext.form field (TextField, ComboBox, etc). Note: the field should not have
323 * been rendered yet. For a field that has already been rendered, use {@link #addElement}.
324 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
325 * @param {Ext.form.Field} field
326 * @return {Ext.Toolbar.Item}
328 addField : function(field){
329 return this.add(field);
333 * Inserts any {@link Ext.Toolbar.Item}/{@link Ext.Button} at the specified index.
334 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
335 * @param {Number} index The index where the item is to be inserted
336 * @param {Object/Ext.Toolbar.Item/Ext.Button/Array} item The button, or button config object to be
337 * inserted, or an array of buttons/configs.
338 * @return {Ext.Button/Item}
340 insertButton : function(index, item){
341 if(Ext.isArray(item)){
343 for(var i = 0, len = item.length; i < len; i++) {
344 buttons.push(this.insertButton(index + i, item[i]));
348 return Ext.Toolbar.superclass.insert.call(this, index, item);
352 trackMenu : function(item, remove){
353 if(this.trackMenus && item.menu){
354 var method = remove ? 'mun' : 'mon';
355 this[method](item, 'menutriggerover', this.onButtonTriggerOver, this);
356 this[method](item, 'menushow', this.onButtonMenuShow, this);
357 this[method](item, 'menuhide', this.onButtonMenuHide, this);
362 constructButton : function(item){
363 var b = item.events ? item : this.createComponent(item, item.split ? 'splitbutton' : this.defaultType);
369 Ext.Toolbar.superclass.onAdd.call(this);
377 onRemove : function(c){
378 Ext.Toolbar.superclass.onRemove.call(this);
379 this.trackMenu(c, true);
383 onDisable : function(){
384 this.items.each(function(item){
392 onEnable : function(){
393 this.items.each(function(item){
401 onButtonTriggerOver : function(btn){
402 if(this.activeMenuBtn && this.activeMenuBtn != btn){
403 this.activeMenuBtn.hideMenu();
405 this.activeMenuBtn = btn;
410 onButtonMenuShow : function(btn){
411 this.activeMenuBtn = btn;
415 onButtonMenuHide : function(btn){
416 delete this.activeMenuBtn;
419 Ext.reg('toolbar', Ext.Toolbar);
422 * @class Ext.Toolbar.Item
423 * @extends Ext.BoxComponent
424 * The base class that other non-interacting Toolbar Item classes should extend in order to
425 * get some basic common toolbar item functionality.
428 * @param {HTMLElement} el
431 T.Item = Ext.extend(Ext.BoxComponent, {
432 hideParent: true, // Hiding a Toolbar.Item hides its containing TD
437 * @cfg {String} overflowText Text to be used for the menu if the item is overflowed.
440 Ext.reg('tbitem', T.Item);
443 * @class Ext.Toolbar.Separator
444 * @extends Ext.Toolbar.Item
445 * A simple class that adds a vertical separator bar between toolbar items
446 * (css class:<tt>'xtb-sep'</tt>). Example usage:
451 {xtype: 'tbseparator'}, // or '-'
457 * Creates a new Separator
460 T.Separator = Ext.extend(T.Item, {
461 onRender : function(ct, position){
462 this.el = ct.createChild({tag:'span', cls:'xtb-sep'}, position);
465 Ext.reg('tbseparator', T.Separator);
468 * @class Ext.Toolbar.Spacer
469 * @extends Ext.Toolbar.Item
470 * A simple element that adds extra horizontal space between items in a toolbar.
471 * By default a 2px wide space is added via css specification:<pre><code>
472 .x-toolbar .xtb-spacer {
476 * <p>Example usage:</p>
481 {xtype: 'tbspacer'}, // or ' '
483 // space width is also configurable via javascript
484 {xtype: 'tbspacer', width: 50}, // add a 50px space
490 * Creates a new Spacer
493 T.Spacer = Ext.extend(T.Item, {
495 * @cfg {Number} width
496 * The width of the spacer in pixels (defaults to 2px via css style <tt>.x-toolbar .xtb-spacer</tt>).
499 onRender : function(ct, position){
500 this.el = ct.createChild({tag:'div', cls:'xtb-spacer', style: this.width?'width:'+this.width+'px':''}, position);
503 Ext.reg('tbspacer', T.Spacer);
506 * @class Ext.Toolbar.Fill
507 * @extends Ext.Toolbar.Spacer
508 * A non-rendering placeholder item which instructs the Toolbar's Layout to begin using
509 * the right-justified button container.
514 {xtype: 'tbfill'}, // or '->'
523 T.Fill = Ext.extend(T.Item, {
525 render : Ext.emptyFn,
528 Ext.reg('tbfill', T.Fill);
531 * @class Ext.Toolbar.TextItem
532 * @extends Ext.Toolbar.Item
533 * A simple class that renders text directly into a toolbar
534 * (with css class:<tt>'xtb-text'</tt>). Example usage:
538 {xtype: 'tbtext', text: 'Item 1'} // or simply 'Item 1'
543 * Creates a new TextItem
544 * @param {String/Object} text A text string, or a config object containing a <tt>text</tt> property
547 T.TextItem = Ext.extend(T.Item, {
549 * @cfg {String} text The text to be used as innerHTML (html tags are accepted)
552 constructor: function(config){
553 T.TextItem.superclass.constructor.call(this, Ext.isString(config) ? {text: config} : config);
557 onRender : function(ct, position) {
558 this.autoEl = {cls: 'xtb-text', html: this.text || ''};
559 T.TextItem.superclass.onRender.call(this, ct, position);
563 * Updates this item's text, setting the text to be used as innerHTML.
564 * @param {String} t The text to display (html accepted).
566 setText : function(t) {
574 Ext.reg('tbtext', T.TextItem);
577 T.Button = Ext.extend(Ext.Button, {});
578 T.SplitButton = Ext.extend(Ext.SplitButton, {});
579 Ext.reg('tbbutton', T.Button);
580 Ext.reg('tbsplit', T.SplitButton);