3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.layout.ToolbarLayout
9 * @extends Ext.layout.ContainerLayout
10 * Layout manager used by Ext.Toolbar. This is highly specialised for use by Toolbars and would not
11 * usually be used by any other class.
13 Ext.layout.ToolbarLayout = Ext.extend(Ext.layout.ContainerLayout, {
19 * @property triggerWidth
21 * The width allocated for the menu trigger at the extreme right end of the Toolbar
26 * @property noItemsMenuText
28 * HTML fragment to render into the toolbar overflow menu if there are no items to display
30 noItemsMenuText : '<div class="x-toolbar-no-items">(None)</div>',
34 * @property lastOverflow
36 * Used internally to record whether the last layout caused an overflow or not
44 * String used to build the HTML injected to support the Toolbar's layout. The align property is
45 * injected into this string inside the td.x-toolbar-left element during onLayout.
48 '<table cellspacing="0" class="x-toolbar-ct">',
51 '<td class="x-toolbar-left" align="{0}">',
52 '<table cellspacing="0">',
54 '<tr class="x-toolbar-left-row"></tr>',
58 '<td class="x-toolbar-right" align="right">',
59 '<table cellspacing="0" class="x-toolbar-right-ct">',
63 '<table cellspacing="0">',
65 '<tr class="x-toolbar-right-row"></tr>',
70 '<table cellspacing="0">',
72 '<tr class="x-toolbar-extras-row"></tr>',
87 * Create the wrapping Toolbar HTML and render/move all the items into the correct places
89 onLayout : function(ct, target) {
90 //render the Toolbar <table> HTML if it's not already present
92 var align = ct.buttonAlign == 'center' ? 'center' : 'left';
94 target.addClass('x-toolbar-layout-ct');
95 target.insertHtml('beforeEnd', String.format(this.tableHTML, align));
97 this.leftTr = target.child('tr.x-toolbar-left-row', true);
98 this.rightTr = target.child('tr.x-toolbar-right-row', true);
99 this.extrasTr = target.child('tr.x-toolbar-extras-row', true);
101 if (this.hiddenItem == undefined) {
103 * @property hiddenItems
105 * Holds all items that are currently hidden due to there not being enough space to render them
106 * These items will appear on the expand menu.
108 this.hiddenItems = [];
112 var side = ct.buttonAlign == 'right' ? this.rightTr : this.leftTr,
113 items = ct.items.items,
116 //render each item if not already rendered, place it into the correct (left or right) target
117 for (var i = 0, len = items.length, c; i < len; i++, position++) {
123 } else if (!c.rendered) {
124 c.render(this.insertCell(c, side, position));
125 this.configureItem(c);
127 if (!c.xtbHidden && !this.isValidParent(c, side.childNodes[position])) {
128 var td = this.insertCell(c, side, position);
129 td.appendChild(c.getPositionEl().dom);
130 c.container = Ext.get(td);
135 //strip extra empty cells
136 this.cleanup(this.leftTr);
137 this.cleanup(this.rightTr);
138 this.cleanup(this.extrasTr);
139 this.fitToSize(target);
144 * Removes any empty nodes from the given element
145 * @param {Ext.Element} el The element to clean up
147 cleanup : function(el) {
148 var cn = el.childNodes, i, c;
150 for (i = cn.length-1; i >= 0 && (c = cn[i]); i--) {
159 * Inserts the given Toolbar item into the given element
160 * @param {Ext.Component} c The component to add
161 * @param {Ext.Element} target The target to add the component to
162 * @param {Number} position The position to add the component at
164 insertCell : function(c, target, position) {
165 var td = document.createElement('td');
166 td.className = 'x-toolbar-cell';
168 target.insertBefore(td, target.childNodes[position] || null);
175 * Hides an item because it will not fit in the available width. The item will be unhidden again
176 * if the Toolbar is resized to be large enough to show it
177 * @param {Ext.Component} item The item to hide
179 hideItem : function(item) {
180 this.hiddenItems.push(item);
182 item.xtbHidden = true;
183 item.xtbWidth = item.getPositionEl().dom.parentNode.offsetWidth;
189 * Unhides an item that was previously hidden due to there not being enough space left on the Toolbar
190 * @param {Ext.Component} item The item to show
192 unhideItem : function(item) {
194 item.xtbHidden = false;
195 this.hiddenItems.remove(item);
200 * Returns the width of the given toolbar item. If the item is currently hidden because there
201 * is not enough room to render it, its previous width is returned
202 * @param {Ext.Component} c The component to measure
203 * @return {Number} The width of the item
205 getItemWidth : function(c) {
206 return c.hidden ? (c.xtbWidth || 0) : c.getPositionEl().dom.parentNode.offsetWidth;
211 * Called at the end of onLayout. At this point the Toolbar has already been resized, so we need
212 * to fit the items into the available width. We add up the width required by all of the items in
213 * the toolbar - if we don't have enough space we hide the extra items and render the expand menu
215 * @param {Ext.Element} target The Element the Toolbar is currently laid out within
217 fitToSize : function(target) {
218 if (this.container.enableOverflow === false) {
222 var width = target.dom.clientWidth,
223 tableWidth = target.dom.firstChild.offsetWidth,
224 clipWidth = width - this.triggerWidth,
225 lastWidth = this.lastWidth || 0,
227 hiddenItems = this.hiddenItems,
228 hasHiddens = hiddenItems.length != 0,
229 isLarger = width >= lastWidth;
231 this.lastWidth = width;
233 if (tableWidth > width || (hasHiddens && isLarger)) {
234 var items = this.container.items.items,
239 for (var i = 0; i < len; i++) {
243 loopWidth += this.getItemWidth(item);
244 if (loopWidth > clipWidth) {
245 if (!(item.hidden || item.xtbHidden)) {
248 } else if (item.xtbHidden) {
249 this.unhideItem(item);
255 //test for number of hidden items again here because they may have changed above
256 hasHiddens = hiddenItems.length != 0;
261 if (!this.lastOverflow) {
262 this.container.fireEvent('overflowchange', this.container, true);
263 this.lastOverflow = true;
265 } else if (this.more) {
270 if (this.lastOverflow) {
271 this.container.fireEvent('overflowchange', this.container, false);
272 this.lastOverflow = false;
279 * Returns a menu config for a given component. This config is used to create a menu item
280 * to be added to the expander menu
281 * @param {Ext.Component} component The component to create the config for
282 * @param {Boolean} hideOnClick Passed through to the menu item
284 createMenuConfig : function(component, hideOnClick){
285 var config = Ext.apply({}, component.initialConfig),
286 group = component.toggleGroup;
288 Ext.copyTo(config, component, [
289 'iconCls', 'icon', 'itemId', 'disabled', 'handler', 'scope', 'menu'
293 text : component.overflowText || component.text,
294 hideOnClick: hideOnClick
297 if (group || component.enableToggle) {
300 checked: component.pressed,
302 checkchange: function(item, checked){
303 component.toggle(checked);
309 delete config.ownerCt;
318 * Adds the given Toolbar item to the given menu. Buttons inside a buttongroup are added individually.
319 * @param {Ext.menu.Menu} menu The menu to add to
320 * @param {Ext.Component} component The component to add
322 addComponentToMenu : function(menu, component) {
323 if (component instanceof Ext.Toolbar.Separator) {
326 } else if (Ext.isFunction(component.isXType)) {
327 if (component.isXType('splitbutton')) {
328 menu.add(this.createMenuConfig(component, true));
330 } else if (component.isXType('button')) {
331 menu.add(this.createMenuConfig(component, !component.menu));
333 } else if (component.isXType('buttongroup')) {
334 component.items.each(function(item){
335 this.addComponentToMenu(menu, item);
343 * Deletes the sub-menu of each item in the expander menu. Submenus are created for items such as
344 * splitbuttons and buttongroups, where the Toolbar item cannot be represented by a single menu item
346 clearMenu : function(){
347 var menu = this.moreMenu;
348 if (menu && menu.items) {
349 menu.items.each(function(item){
357 * Called before the expand menu is shown, this rebuilds the menu since it was last shown because
358 * it is possible that the items hidden due to space limitations on the Toolbar have changed since.
359 * @param {Ext.menu.Menu} m The menu
361 beforeMoreShow : function(menu) {
362 var items = this.container.items.items,
367 var needsSep = function(group, item){
368 return group.isXType('buttongroup') && !(item instanceof Ext.Toolbar.Separator);
373 for (var i = 0; i < len; i++) {
375 if (item.xtbHidden) {
376 if (prev && (needsSep(item, prev) || needsSep(prev, item))) {
379 this.addComponentToMenu(menu, item);
384 // put something so the menu isn't empty if no compatible items found
385 if (menu.items.length < 1) {
386 menu.add(this.noItemsMenuText);
392 * Creates the expand trigger and menu, adding them to the <tr> at the extreme right of the
395 initMore : function(){
400 * @type Ext.menu.Menu
401 * The expand menu - holds items for every Toolbar item that cannot be shown
402 * because the Toolbar is currently not wide enough.
404 this.moreMenu = new Ext.menu.Menu({
405 ownerCt : this.container,
407 beforeshow: this.beforeMoreShow,
416 * The expand button which triggers the overflow menu to be shown
418 this.more = new Ext.Button({
419 iconCls: 'x-toolbar-more-icon',
420 cls : 'x-toolbar-more',
421 menu : this.moreMenu,
422 ownerCt: this.container
425 var td = this.insertCell(this.more, this.extrasTr, 100);
426 this.more.render(td);
430 destroy : function(){
431 Ext.destroy(this.more, this.moreMenu);
434 delete this.extrasTr;
435 Ext.layout.ToolbarLayout.superclass.destroy.call(this);
439 Ext.Container.LAYOUTS.toolbar = Ext.layout.ToolbarLayout;