3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.layout.ToolbarLayout
9 * @extends Ext.layout.ContainerLayout
10 * Layout manager implicitly used by Ext.Toolbar.
12 Ext.layout.ToolbarLayout = Ext.extend(Ext.layout.ContainerLayout, {
18 noItemsMenuText : '<div class="x-toolbar-no-items">(None)</div>',
20 onLayout : function(ct, target){
22 var align = ct.buttonAlign == 'center' ? 'center' : 'left';
23 target.addClass('x-toolbar-layout-ct');
24 target.insertHtml('beforeEnd',
25 '<table cellspacing="0" class="x-toolbar-ct"><tbody><tr><td class="x-toolbar-left" align="' + align + '"><table cellspacing="0"><tbody><tr class="x-toolbar-left-row"></tr></tbody></table></td><td class="x-toolbar-right" align="right"><table cellspacing="0" class="x-toolbar-right-ct"><tbody><tr><td><table cellspacing="0"><tbody><tr class="x-toolbar-right-row"></tr></tbody></table></td><td><table cellspacing="0"><tbody><tr class="x-toolbar-extras-row"></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table>');
26 this.leftTr = target.child('tr.x-toolbar-left-row', true);
27 this.rightTr = target.child('tr.x-toolbar-right-row', true);
28 this.extrasTr = target.child('tr.x-toolbar-extras-row', true);
30 var side = ct.buttonAlign == 'right' ? this.rightTr : this.leftTr,
32 items = ct.items.items;
34 for(var i = 0, len = items.length, c; i < len; i++, pos++) {
39 }else if(!c.rendered){
40 c.render(this.insertCell(c, side, pos));
42 if(!c.xtbHidden && !this.isValidParent(c, side.childNodes[pos])){
43 var td = this.insertCell(c, side, pos);
44 td.appendChild(c.getPositionEl().dom);
45 c.container = Ext.get(td);
49 //strip extra empty cells
50 this.cleanup(this.leftTr);
51 this.cleanup(this.rightTr);
52 this.cleanup(this.extrasTr);
53 this.fitToSize(target);
56 cleanup : function(row){
57 var cn = row.childNodes;
58 for(var i = cn.length-1, c; i >= 0 && (c = cn[i]); i--){
65 insertCell : function(c, side, pos){
66 var td = document.createElement('td');
67 td.className='x-toolbar-cell';
68 side.insertBefore(td, side.childNodes[pos]||null);
72 hideItem : function(item){
73 var h = (this.hiddens = this.hiddens || []);
75 item.xtbHidden = true;
76 item.xtbWidth = item.getPositionEl().dom.parentNode.offsetWidth;
80 unhideItem : function(item){
82 item.xtbHidden = false;
83 this.hiddens.remove(item);
84 if(this.hiddens.length < 1){
89 getItemWidth : function(c){
90 return c.hidden ? (c.xtbWidth || 0) : c.getPositionEl().dom.parentNode.offsetWidth;
93 fitToSize : function(t){
94 if(this.container.enableOverflow === false){
97 var w = t.dom.clientWidth,
98 lw = this.lastWidth || 0,
99 iw = t.dom.firstChild.offsetWidth,
100 clipWidth = w - this.triggerWidth,
105 if(iw > w || (this.hiddens && w >= lw)){
106 var i, items = this.container.items.items,
107 len = items.length, c,
110 for(i = 0; i < len; i++) {
113 loopWidth += this.getItemWidth(c);
114 if(loopWidth > clipWidth){
115 if(!(c.hidden || c.xtbHidden)){
118 }else if(c.xtbHidden){
126 if(!this.lastOverflow){
127 this.container.fireEvent('overflowchange', this.container, true);
128 this.lastOverflow = true;
134 if(this.lastOverflow){
135 this.container.fireEvent('overflowchange', this.container, false);
136 this.lastOverflow = false;
141 createMenuConfig : function(c, hideOnClick){
142 var cfg = Ext.apply({}, c.initialConfig),
143 group = c.toggleGroup;
146 text: c.overflowText || c.text,
150 disabled: c.disabled,
154 hideOnClick: hideOnClick
156 if(group || c.enableToggle){
161 checkchange: function(item, checked){
174 addComponentToMenu : function(m, c){
175 if(c instanceof Ext.Toolbar.Separator){
177 }else if(Ext.isFunction(c.isXType)){
178 if(c.isXType('splitbutton')){
179 m.add(this.createMenuConfig(c, true));
180 }else if(c.isXType('button')){
181 m.add(this.createMenuConfig(c, !c.menu));
182 }else if(c.isXType('buttongroup')){
183 c.items.each(function(item){
184 this.addComponentToMenu(m, item);
190 clearMenu : function(){
191 var m = this.moreMenu;
193 m.items.each(function(item){
200 beforeMoreShow : function(m){
201 var h = this.container.items.items,
205 needsSep = function(group, item){
206 return group.isXType('buttongroup') && !(item instanceof Ext.Toolbar.Separator);
211 for(var i = 0; i < len; i++){
214 if(prev && (needsSep(c, prev) || needsSep(prev, c))){
217 this.addComponentToMenu(m, c);
221 // put something so the menu isn't empty
222 // if no compatible items found
223 if(m.items.length < 1){
224 m.add(this.noItemsMenuText);
228 initMore : function(){
230 this.moreMenu = new Ext.menu.Menu({
232 beforeshow: this.beforeMoreShow,
236 this.moreMenu.ownerCt = this.container;
237 this.more = new Ext.Button({
238 iconCls: 'x-toolbar-more-icon',
239 cls: 'x-toolbar-more',
242 var td = this.insertCell(this.more, this.extrasTr, 100);
243 this.more.render(td);
247 onRemove : function(c){
250 delete this.extrasTr;
251 Ext.layout.ToolbarLayout.superclass.onRemove.call(this, c);
254 destroy : function(){
255 Ext.destroy(this.more, this.moreMenu);
258 delete this.extrasTr;
259 Ext.layout.ToolbarLayout.superclass.destroy.call(this);
262 * @property activeItem
267 Ext.Container.LAYOUTS.toolbar = Ext.layout.ToolbarLayout;
271 * @extends Ext.Container
272 * <p>Basic Toolbar class. Although the <tt>{@link Ext.Container#defaultType defaultType}</tt> for Toolbar
273 * is <tt>{@link Ext.Button button}</tt>, Toolbar elements (child items for the Toolbar container) may
274 * be virtually any type of Component. Toolbar elements can be created explicitly via their constructors,
275 * or implicitly via their xtypes, and can be <tt>{@link #add}</tt>ed dynamically.</p>
276 * <p>Some items have shortcut strings for creation:</p>
278 <u>Shortcut</u> <u>xtype</u> <u>Class</u> <u>Description</u>
279 '->' 'tbfill' {@link Ext.Toolbar.Fill} begin using the right-justified button container
280 '-' 'tbseparator' {@link Ext.Toolbar.Separator} add a vertical separator bar between toolbar items
281 ' ' 'tbspacer' {@link Ext.Toolbar.Spacer} add horiztonal space between elements
284 * Example usage of various elements:
286 var tb = new Ext.Toolbar({
287 renderTo: document.body,
292 // xtype: 'button', // default for Toolbars, same as 'tbbutton'
296 xtype: 'splitbutton', // same as 'tbsplitbutton'
299 // begin using the right-justified button container
300 '->', // same as {xtype: 'tbfill'}, // Ext.Toolbar.Fill
304 emptyText: 'enter search term'
306 // add a vertical separator bar between toolbar items
307 '-', // same as {xtype: 'tbseparator'} to create Ext.Toolbar.Separator
308 'text 1', // same as {xtype: 'tbtext', text: 'text1'} to create Ext.Toolbar.TextItem
309 {xtype: 'tbspacer'},// same as ' ' to create Ext.Toolbar.Spacer
311 {xtype: 'tbspacer', width: 50}, // add a 50px space
316 * Example adding a ComboBox within a menu of a button:
319 var combo = new Ext.form.ComboBox({
320 store: new Ext.data.ArrayStore({
322 fields: ['initials', 'fullname'],
324 ['FF', 'Fred Flintstone'],
325 ['BR', 'Barney Rubble']
328 displayField: 'fullname',
331 forceSelection: true,
332 triggerAction: 'all',
333 emptyText: 'Select a name...',
336 getListParent: function() {
337 return this.el.up('.x-menu');
339 iconCls: 'no-icon' //use iconCls if placing within menu to shift to right side of menu
342 // put ComboBox in a Menu
343 var menu = new Ext.menu.Menu({
346 combo // A Field in a Menu
350 // add a Button with the menu
352 text:'Button w/ Menu',
353 menu: menu // assign menu by instance
358 * Creates a new Toolbar
359 * @param {Object/Array} config A config object or an array of buttons to <tt>{@link #add}</tt>
362 Ext.Toolbar = function(config){
363 if(Ext.isArray(config)){
364 config = {items: config, layout: 'toolbar'};
370 config.items = config.buttons;
373 Ext.Toolbar.superclass.constructor.call(this, config);
380 Ext.extend(T, Ext.Container, {
382 defaultType: 'button',
385 * @cfg {String/Object} layout
386 * This class assigns a default layout (<code>layout:'<b>toolbar</b>'</code>).
387 * Developers <i>may</i> override this configuration option if another layout
388 * is required (the constructor must be passed a configuration object in this
389 * case instead of an array).
390 * See {@link Ext.Container#layout} for additional information.
393 * @cfg {Boolean} enableOverflow
394 * Defaults to false. Configure <code>true<code> to make the toolbar provide a button
395 * which activates a dropdown Menu to show items which overflow the Toolbar's width.
399 internalDefaults: {removeMode: 'container', hideParent: true},
400 toolbarCls: 'x-toolbar',
402 initComponent : function(){
403 T.superclass.initComponent.call(this);
406 * @event overflowchange
407 * Fires after the overflow state has changed.
408 * @param {Object} c The Container
409 * @param {Boolean} lastOverflow overflow state
411 this.addEvents('overflowchange');
415 onRender : function(ct, position){
417 if(!this.autoCreate){
419 cls: this.toolbarCls + ' x-small-editor'
422 this.el = ct.createChild(Ext.apply({ id: this.id },this.autoCreate), position);
423 Ext.Toolbar.superclass.onRender.apply(this, arguments);
428 * <p>Adds element(s) to the toolbar -- this function takes a variable number of
429 * arguments of mixed type and adds them to the toolbar.</p>
430 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
431 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
433 * <li>{@link Ext.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
434 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
435 * <li>Field: Any form field (equivalent to {@link #addField})</li>
436 * <li>Item: Any subclass of {@link Ext.Toolbar.Item} (equivalent to {@link #addItem})</li>
437 * <li>String: Any generic string (gets wrapped in a {@link Ext.Toolbar.TextItem}, equivalent to {@link #addText}).
438 * Note that there are a few special strings that are treated differently as explained next.</li>
439 * <li>'-': Creates a separator element (equivalent to {@link #addSeparator})</li>
440 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
441 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
443 * @param {Mixed} arg2
444 * @param {Mixed} etc.
449 lookupComponent : function(c){
452 c = new T.Separator();
458 c = new T.TextItem(c);
460 this.applyDefaults(c);
462 if(c.isFormField || c.render){ // some kind of form field, some kind of Toolbar.Item
463 c = this.createComponent(c);
464 }else if(c.tag){ // DomHelper spec
465 c = new T.Item({autoEl: c});
466 }else if(c.tagName){ // element
467 c = new T.Item({el:c});
468 }else if(Ext.isObject(c)){ // must be button config?
469 c = c.xtype ? this.createComponent(c) : this.constructButton(c);
476 applyDefaults : function(c){
477 if(!Ext.isString(c)){
478 c = Ext.Toolbar.superclass.applyDefaults.call(this, c);
479 var d = this.internalDefaults;
481 Ext.applyIf(c.initialConfig, d);
492 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
493 * @return {Ext.Toolbar.Item} The separator {@link Ext.Toolbar.Item item}
495 addSeparator : function(){
496 return this.add(new T.Separator());
500 * Adds a spacer element
501 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
502 * @return {Ext.Toolbar.Spacer} The spacer item
504 addSpacer : function(){
505 return this.add(new T.Spacer());
509 * Forces subsequent additions into the float:right toolbar
510 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
512 addFill : function(){
513 this.add(new T.Fill());
517 * Adds any standard HTML element to the toolbar
518 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
519 * @param {Mixed} el The element or id of the element to add
520 * @return {Ext.Toolbar.Item} The element's item
522 addElement : function(el){
523 return this.addItem(new T.Item({el:el}));
527 * Adds any Toolbar.Item or subclass
528 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
529 * @param {Ext.Toolbar.Item} item
530 * @return {Ext.Toolbar.Item} The item
532 addItem : function(item){
533 return this.add.apply(this, arguments);
537 * Adds a button (or buttons). See {@link Ext.Button} for more info on the config.
538 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
539 * @param {Object/Array} config A button config or array of configs
540 * @return {Ext.Button/Array}
542 addButton : function(config){
543 if(Ext.isArray(config)){
545 for(var i = 0, len = config.length; i < len; i++) {
546 buttons.push(this.addButton(config[i]));
550 return this.add(this.constructButton(config));
554 * Adds text to the toolbar
555 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
556 * @param {String} text The text to add
557 * @return {Ext.Toolbar.Item} The element's item
559 addText : function(text){
560 return this.addItem(new T.TextItem(text));
564 * Adds a new element to the toolbar from the passed {@link Ext.DomHelper} config
565 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
566 * @param {Object} config
567 * @return {Ext.Toolbar.Item} The element's item
569 addDom : function(config){
570 return this.add(new T.Item({autoEl: config}));
574 * Adds a dynamically rendered Ext.form field (TextField, ComboBox, etc). Note: the field should not have
575 * been rendered yet. For a field that has already been rendered, use {@link #addElement}.
576 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
577 * @param {Ext.form.Field} field
578 * @return {Ext.Toolbar.Item}
580 addField : function(field){
581 return this.add(field);
585 * Inserts any {@link Ext.Toolbar.Item}/{@link Ext.Button} at the specified index.
586 * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
587 * @param {Number} index The index where the item is to be inserted
588 * @param {Object/Ext.Toolbar.Item/Ext.Button/Array} item The button, or button config object to be
589 * inserted, or an array of buttons/configs.
590 * @return {Ext.Button/Item}
592 insertButton : function(index, item){
593 if(Ext.isArray(item)){
595 for(var i = 0, len = item.length; i < len; i++) {
596 buttons.push(this.insertButton(index + i, item[i]));
600 return Ext.Toolbar.superclass.insert.call(this, index, item);
604 trackMenu : function(item, remove){
605 if(this.trackMenus && item.menu){
606 var method = remove ? 'mun' : 'mon';
607 this[method](item, 'menutriggerover', this.onButtonTriggerOver, this);
608 this[method](item, 'menushow', this.onButtonMenuShow, this);
609 this[method](item, 'menuhide', this.onButtonMenuHide, this);
614 constructButton : function(item){
615 var b = item.events ? item : this.createComponent(item, item.split ? 'splitbutton' : this.defaultType);
621 Ext.Toolbar.superclass.onAdd.call(this);
626 onRemove : function(c){
627 Ext.Toolbar.superclass.onRemove.call(this);
628 this.trackMenu(c, true);
632 onDisable : function(){
633 this.items.each(function(item){
641 onEnable : function(){
642 this.items.each(function(item){
650 onButtonTriggerOver : function(btn){
651 if(this.activeMenuBtn && this.activeMenuBtn != btn){
652 this.activeMenuBtn.hideMenu();
654 this.activeMenuBtn = btn;
659 onButtonMenuShow : function(btn){
660 this.activeMenuBtn = btn;
664 onButtonMenuHide : function(btn){
665 delete this.activeMenuBtn;
668 Ext.reg('toolbar', Ext.Toolbar);
671 * @class Ext.Toolbar.Item
672 * @extends Ext.BoxComponent
673 * The base class that other non-interacting Toolbar Item classes should extend in order to
674 * get some basic common toolbar item functionality.
677 * @param {HTMLElement} el
680 T.Item = Ext.extend(Ext.BoxComponent, {
681 hideParent: true, // Hiding a Toolbar.Item hides its containing TD
686 * @cfg {String} overflowText Text to be used for the menu if the item is overflowed.
689 Ext.reg('tbitem', T.Item);
692 * @class Ext.Toolbar.Separator
693 * @extends Ext.Toolbar.Item
694 * A simple class that adds a vertical separator bar between toolbar items
695 * (css class:<tt>'xtb-sep'</tt>). Example usage:
700 {xtype: 'tbseparator'}, // or '-'
706 * Creates a new Separator
709 T.Separator = Ext.extend(T.Item, {
710 onRender : function(ct, position){
711 this.el = ct.createChild({tag:'span', cls:'xtb-sep'}, position);
714 Ext.reg('tbseparator', T.Separator);
717 * @class Ext.Toolbar.Spacer
718 * @extends Ext.Toolbar.Item
719 * A simple element that adds extra horizontal space between items in a toolbar.
720 * By default a 2px wide space is added via css specification:<pre><code>
721 .x-toolbar .xtb-spacer {
725 * <p>Example usage:</p>
730 {xtype: 'tbspacer'}, // or ' '
732 // space width is also configurable via javascript
733 {xtype: 'tbspacer', width: 50}, // add a 50px space
739 * Creates a new Spacer
742 T.Spacer = Ext.extend(T.Item, {
744 * @cfg {Number} width
745 * The width of the spacer in pixels (defaults to 2px via css style <tt>.x-toolbar .xtb-spacer</tt>).
748 onRender : function(ct, position){
749 this.el = ct.createChild({tag:'div', cls:'xtb-spacer', style: this.width?'width:'+this.width+'px':''}, position);
752 Ext.reg('tbspacer', T.Spacer);
755 * @class Ext.Toolbar.Fill
756 * @extends Ext.Toolbar.Spacer
757 * A non-rendering placeholder item which instructs the Toolbar's Layout to begin using
758 * the right-justified button container.
763 {xtype: 'tbfill'}, // or '->'
772 T.Fill = Ext.extend(T.Item, {
774 render : Ext.emptyFn,
777 Ext.reg('tbfill', T.Fill);
780 * @class Ext.Toolbar.TextItem
781 * @extends Ext.Toolbar.Item
782 * A simple class that renders text directly into a toolbar
783 * (with css class:<tt>'xtb-text'</tt>). Example usage:
787 {xtype: 'tbtext', text: 'Item 1'} // or simply 'Item 1'
792 * Creates a new TextItem
793 * @param {String/Object} text A text string, or a config object containing a <tt>text</tt> property
796 T.TextItem = Ext.extend(T.Item, {
798 * @cfg {String} text The text to be used as innerHTML (html tags are accepted)
801 constructor: function(config){
802 T.TextItem.superclass.constructor.call(this, Ext.isString(config) ? {text: config} : config);
806 onRender : function(ct, position) {
807 this.autoEl = {cls: 'xtb-text', html: this.text || ''};
808 T.TextItem.superclass.onRender.call(this, ct, position);
812 * Updates this item's text, setting the text to be used as innerHTML.
813 * @param {String} t The text to display (html accepted).
815 setText : function(t) {
823 Ext.reg('tbtext', T.TextItem);
826 T.Button = Ext.extend(Ext.Button, {});
827 T.SplitButton = Ext.extend(Ext.SplitButton, {});
828 Ext.reg('tbbutton', T.Button);
829 Ext.reg('tbsplit', T.SplitButton);