4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * Basic Toolbar class.
16 * Creates a new Toolbar
17 * @param {Object} container The config object
19 Roo.Toolbar = function(container, buttons, config)
21 /// old consturctor format still supported..
22 if(container instanceof Array){ // omit the container for later rendering
27 if (typeof(container) == 'object' && container.xtype) {
29 container = config.container;
30 buttons = config.buttons || []; // not really - use items!!
33 if (config && config.items) {
34 xitems = config.items;
37 Roo.apply(this, config);
38 this.buttons = buttons;
41 this.render(container);
44 Roo.each(xitems, function(b) {
50 Roo.Toolbar.prototype = {
53 * array of button configs or elements to add (will be converted to a MixedCollection)
57 * @cfg {String/HTMLElement/Element} container
58 * The id or element that will contain the toolbar
61 render : function(ct){
62 this.el = Roo.get(ct);
64 this.el.addClass(this.cls);
66 // using a table allows for vertical alignment
67 // 100% width is needed by Safari...
68 this.el.update('<div class="x-toolbar x-small-editor"><table cellspacing="0"><tr></tr></table></div>');
69 this.tr = this.el.child("tr", true);
71 this.items = new Roo.util.MixedCollection(false, function(o){
72 return o.id || ("item" + (++autoId));
75 this.add.apply(this, this.buttons);
81 * Adds element(s) to the toolbar -- this function takes a variable number of
82 * arguments of mixed type and adds them to the toolbar.
83 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
85 * <li>{@link Roo.Toolbar.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
86 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
87 * <li>Field: Any form field (equivalent to {@link #addField})</li>
88 * <li>Item: Any subclass of {@link Roo.Toolbar.Item} (equivalent to {@link #addItem})</li>
89 * <li>String: Any generic string (gets wrapped in a {@link Roo.Toolbar.TextItem}, equivalent to {@link #addText}).
90 * Note that there are a few special strings that are treated differently as explained nRoo.</li>
91 * <li>'separator' or '-': Creates a separator element (equivalent to {@link #addSeparator})</li>
92 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
93 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
99 var a = arguments, l = a.length;
100 for(var i = 0; i < l; i++){
105 _add : function(el) {
108 el = Roo.factory(el, typeof(Roo.Toolbar[el.xtype]) == 'undefined' ? Roo.form : Roo.Toolbar);
111 if (el.applyTo){ // some kind of form field
112 return this.addField(el);
114 if (el.render){ // some kind of Toolbar.Item
115 return this.addItem(el);
117 if (typeof el == "string"){ // string
118 if(el == "separator" || el == "-"){
119 return this.addSeparator();
122 return this.addSpacer();
125 return this.addFill();
127 return this.addText(el);
130 if(el.tagName){ // element
131 return this.addElement(el);
133 if(typeof el == "object"){ // must be button config?
134 return this.addButton(el);
142 * Add an Xtype element
143 * @param {Object} xtype Xtype Object
144 * @return {Object} created Object
146 addxtype : function(e){
151 * Returns the Element for this toolbar.
152 * @return {Roo.Element}
160 * @return {Roo.Toolbar.Item} The separator item
162 addSeparator : function(){
163 return this.addItem(new Roo.Toolbar.Separator());
167 * Adds a spacer element
168 * @return {Roo.Toolbar.Spacer} The spacer item
170 addSpacer : function(){
171 return this.addItem(new Roo.Toolbar.Spacer());
175 * Adds a fill element that forces subsequent additions to the right side of the toolbar
176 * @return {Roo.Toolbar.Fill} The fill item
178 addFill : function(){
179 return this.addItem(new Roo.Toolbar.Fill());
183 * Adds any standard HTML element to the toolbar
184 * @param {String/HTMLElement/Element} el The element or id of the element to add
185 * @return {Roo.Toolbar.Item} The element's item
187 addElement : function(el){
188 return this.addItem(new Roo.Toolbar.Item(el));
191 * Collection of items on the toolbar.. (only Toolbar Items, so use fields to retrieve fields)
192 * @type Roo.util.MixedCollection
197 * Adds any Toolbar.Item or subclass
198 * @param {Roo.Toolbar.Item} item
199 * @return {Roo.Toolbar.Item} The item
201 addItem : function(item){
202 var td = this.nextBlock();
204 this.items.add(item);
209 * Adds a button (or buttons). See {@link Roo.Toolbar.Button} for more info on the config.
210 * @param {Object/Array} config A button config or array of configs
211 * @return {Roo.Toolbar.Button/Array}
213 addButton : function(config){
214 if(config instanceof Array){
216 for(var i = 0, len = config.length; i < len; i++) {
217 buttons.push(this.addButton(config[i]));
222 if(!(config instanceof Roo.Toolbar.Button)){
224 new Roo.Toolbar.SplitButton(config) :
225 new Roo.Toolbar.Button(config);
227 var td = this.nextBlock();
234 * Adds text to the toolbar
235 * @param {String} text The text to add
236 * @return {Roo.Toolbar.Item} The element's item
238 addText : function(text){
239 return this.addItem(new Roo.Toolbar.TextItem(text));
243 * Inserts any {@link Roo.Toolbar.Item}/{@link Roo.Toolbar.Button} at the specified index.
244 * @param {Number} index The index where the item is to be inserted
245 * @param {Object/Roo.Toolbar.Item/Roo.Toolbar.Button (may be Array)} item The button, or button config object to be inserted.
246 * @return {Roo.Toolbar.Button/Item}
248 insertButton : function(index, item){
249 if(item instanceof Array){
251 for(var i = 0, len = item.length; i < len; i++) {
252 buttons.push(this.insertButton(index + i, item[i]));
256 if (!(item instanceof Roo.Toolbar.Button)){
257 item = new Roo.Toolbar.Button(item);
259 var td = document.createElement("td");
260 this.tr.insertBefore(td, this.tr.childNodes[index]);
262 this.items.insert(index, item);
267 * Adds a new element to the toolbar from the passed {@link Roo.DomHelper} config.
268 * @param {Object} config
269 * @return {Roo.Toolbar.Item} The element's item
271 addDom : function(config, returnEl){
272 var td = this.nextBlock();
273 Roo.DomHelper.overwrite(td, config);
274 var ti = new Roo.Toolbar.Item(td.firstChild);
281 * Collection of fields on the toolbar.. usefull for quering (value is false if there are no fields)
282 * @type Roo.util.MixedCollection
287 * Adds a dynamically rendered Roo.form field (TextField, ComboBox, etc).
288 * Note: the field should not have been rendered yet. For a field that has already been
289 * rendered, use {@link #addElement}.
290 * @param {Roo.form.Field} field
291 * @return {Roo.ToolbarItem}
295 addField : function(field) {
298 this.fields = new Roo.util.MixedCollection(false, function(o){
299 return o.id || ("item" + (++autoId));
304 var td = this.nextBlock();
306 var ti = new Roo.Toolbar.Item(td.firstChild);
309 this.fields.add(field);
320 this.el.child('div').setVisibilityMode(Roo.Element.DISPLAY);
321 this.el.child('div').hide();
329 this.el.child('div').show();
333 nextBlock : function(){
334 var td = document.createElement("td");
335 this.tr.appendChild(td);
340 destroy : function(){
341 if(this.items){ // rendered?
342 Roo.destroy.apply(Roo, this.items.items);
344 if(this.fields){ // rendered?
345 Roo.destroy.apply(Roo, this.fields.items);
347 Roo.Element.uncache(this.el, this.tr);
352 * @class Roo.Toolbar.Item
353 * The base class that other classes should extend in order to get some basic common toolbar item functionality.
356 * @param {HTMLElement} el
358 Roo.Toolbar.Item = function(el){
359 this.el = Roo.getDom(el);
360 this.id = Roo.id(this.el);
364 Roo.Toolbar.Item.prototype = {
367 * Get this item's HTML Element
368 * @return {HTMLElement}
375 render : function(td){
377 td.appendChild(this.el);
381 * Removes and destroys this item.
383 destroy : function(){
384 this.td.parentNode.removeChild(this.td);
392 this.td.style.display = "";
400 this.td.style.display = "none";
404 * Convenience function for boolean show/hide.
405 * @param {Boolean} visible true to show/false to hide
407 setVisible: function(visible){
416 * Try to focus this item.
419 Roo.fly(this.el).focus();
423 * Disables this item.
425 disable : function(){
426 Roo.fly(this.td).addClass("x-item-disabled");
427 this.disabled = true;
428 this.el.disabled = true;
435 Roo.fly(this.td).removeClass("x-item-disabled");
436 this.disabled = false;
437 this.el.disabled = false;
443 * @class Roo.Toolbar.Separator
444 * @extends Roo.Toolbar.Item
445 * A simple toolbar separator class
447 * Creates a new Separator
449 Roo.Toolbar.Separator = function(){
450 var s = document.createElement("span");
451 s.className = "ytb-sep";
452 Roo.Toolbar.Separator.superclass.constructor.call(this, s);
454 Roo.extend(Roo.Toolbar.Separator, Roo.Toolbar.Item, {
461 * @class Roo.Toolbar.Spacer
462 * @extends Roo.Toolbar.Item
463 * A simple element that adds extra horizontal space to a toolbar.
465 * Creates a new Spacer
467 Roo.Toolbar.Spacer = function(){
468 var s = document.createElement("div");
469 s.className = "ytb-spacer";
470 Roo.Toolbar.Spacer.superclass.constructor.call(this, s);
472 Roo.extend(Roo.Toolbar.Spacer, Roo.Toolbar.Item, {
479 * @class Roo.Toolbar.Fill
480 * @extends Roo.Toolbar.Spacer
481 * A simple element that adds a greedy (100% width) horizontal space to a toolbar.
483 * Creates a new Spacer
485 Roo.Toolbar.Fill = Roo.extend(Roo.Toolbar.Spacer, {
487 render : function(td){
488 td.style.width = '100%';
489 Roo.Toolbar.Fill.superclass.render.call(this, td);
494 * @class Roo.Toolbar.TextItem
495 * @extends Roo.Toolbar.Item
496 * A simple class that renders text directly into a toolbar.
498 * Creates a new TextItem
499 * @param {String} text
501 Roo.Toolbar.TextItem = function(text){
502 if (typeof(text) == 'object') {
505 var s = document.createElement("span");
506 s.className = "ytb-text";
508 Roo.Toolbar.TextItem.superclass.constructor.call(this, s);
510 Roo.extend(Roo.Toolbar.TextItem, Roo.Toolbar.Item, {
517 * @class Roo.Toolbar.Button
518 * @extends Roo.Button
519 * A button that renders into a toolbar.
521 * Creates a new Button
522 * @param {Object} config A standard {@link Roo.Button} config object
524 Roo.Toolbar.Button = function(config){
525 Roo.Toolbar.Button.superclass.constructor.call(this, null, config);
527 Roo.extend(Roo.Toolbar.Button, Roo.Button, {
528 render : function(td){
530 Roo.Toolbar.Button.superclass.render.call(this, td);
534 * Removes and destroys this button
536 destroy : function(){
537 Roo.Toolbar.Button.superclass.destroy.call(this);
538 this.td.parentNode.removeChild(this.td);
546 this.td.style.display = "";
554 this.td.style.display = "none";
560 disable : function(){
561 Roo.fly(this.td).addClass("x-item-disabled");
562 this.disabled = true;
569 Roo.fly(this.td).removeClass("x-item-disabled");
570 this.disabled = false;
574 Roo.ToolbarButton = Roo.Toolbar.Button;
577 * @class Roo.Toolbar.SplitButton
578 * @extends Roo.SplitButton
579 * A menu button that renders into a toolbar.
581 * Creates a new SplitButton
582 * @param {Object} config A standard {@link Roo.SplitButton} config object
584 Roo.Toolbar.SplitButton = function(config){
585 Roo.Toolbar.SplitButton.superclass.constructor.call(this, null, config);
587 Roo.extend(Roo.Toolbar.SplitButton, Roo.SplitButton, {
588 render : function(td){
590 Roo.Toolbar.SplitButton.superclass.render.call(this, td);
594 * Removes and destroys this button
596 destroy : function(){
597 Roo.Toolbar.SplitButton.superclass.destroy.call(this);
598 this.td.parentNode.removeChild(this.td);
606 this.td.style.display = "";
614 this.td.style.display = "none";
619 Roo.Toolbar.MenuButton = Roo.Toolbar.SplitButton;