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 Roo.log('HERE!!!!!!!!!!!!!!!!!!!!!!!!');
25 /// old consturctor format still supported..
26 if(container instanceof Array){ // omit the container for later rendering
31 if (typeof(container) == 'object' && container.xtype) {
33 container = config.container;
34 buttons = config.buttons || []; // not really - use items!!
37 if (config && config.items) {
38 xitems = config.items;
41 Roo.apply(this, config);
42 this.buttons = buttons;
45 this.render(container);
48 Roo.each(xitems, function(b) {
54 Roo.Toolbar.prototype = {
57 * array of button configs or elements to add (will be converted to a MixedCollection)
61 * @cfg {String/HTMLElement/Element} container
62 * The id or element that will contain the toolbar
65 render : function(ct){
66 this.el = Roo.get(ct);
68 this.el.addClass(this.cls);
70 // using a table allows for vertical alignment
71 // 100% width is needed by Safari...
72 this.el.update('<div class="x-toolbar x-small-editor"><table cellspacing="0"><tr></tr></table></div>');
73 this.tr = this.el.child("tr", true);
75 this.items = new Roo.util.MixedCollection(false, function(o){
76 return o.id || ("item" + (++autoId));
79 this.add.apply(this, this.buttons);
85 * Adds element(s) to the toolbar -- this function takes a variable number of
86 * arguments of mixed type and adds them to the toolbar.
87 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
89 * <li>{@link Roo.Toolbar.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
90 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
91 * <li>Field: Any form field (equivalent to {@link #addField})</li>
92 * <li>Item: Any subclass of {@link Roo.Toolbar.Item} (equivalent to {@link #addItem})</li>
93 * <li>String: Any generic string (gets wrapped in a {@link Roo.Toolbar.TextItem}, equivalent to {@link #addText}).
94 * Note that there are a few special strings that are treated differently as explained nRoo.</li>
95 * <li>'separator' or '-': Creates a separator element (equivalent to {@link #addSeparator})</li>
96 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
97 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
100 * @param {Mixed} etc.
103 var a = arguments, l = a.length;
104 for(var i = 0; i < l; i++){
109 _add : function(el) {
112 el = Roo.factory(el, typeof(Roo.Toolbar[el.xtype]) == 'undefined' ? Roo.form : Roo.Toolbar);
115 if (el.applyTo){ // some kind of form field
116 return this.addField(el);
118 if (el.render){ // some kind of Toolbar.Item
119 return this.addItem(el);
121 if (typeof el == "string"){ // string
122 if(el == "separator" || el == "-"){
123 return this.addSeparator();
126 return this.addSpacer();
129 return this.addFill();
131 return this.addText(el);
134 if(el.tagName){ // element
135 return this.addElement(el);
137 if(typeof el == "object"){ // must be button config?
138 return this.addButton(el);
146 * Add an Xtype element
147 * @param {Object} xtype Xtype Object
148 * @return {Object} created Object
150 addxtype : function(e){
155 * Returns the Element for this toolbar.
156 * @return {Roo.Element}
164 * @return {Roo.Toolbar.Item} The separator item
166 addSeparator : function(){
167 return this.addItem(new Roo.Toolbar.Separator());
171 * Adds a spacer element
172 * @return {Roo.Toolbar.Spacer} The spacer item
174 addSpacer : function(){
175 return this.addItem(new Roo.Toolbar.Spacer());
179 * Adds a fill element that forces subsequent additions to the right side of the toolbar
180 * @return {Roo.Toolbar.Fill} The fill item
182 addFill : function(){
183 return this.addItem(new Roo.Toolbar.Fill());
187 * Adds any standard HTML element to the toolbar
188 * @param {String/HTMLElement/Element} el The element or id of the element to add
189 * @return {Roo.Toolbar.Item} The element's item
191 addElement : function(el){
192 return this.addItem(new Roo.Toolbar.Item(el));
195 * Collection of items on the toolbar.. (only Toolbar Items, so use fields to retrieve fields)
196 * @type Roo.util.MixedCollection
201 * Adds any Toolbar.Item or subclass
202 * @param {Roo.Toolbar.Item} item
203 * @return {Roo.Toolbar.Item} The item
205 addItem : function(item){
206 var td = this.nextBlock();
208 this.items.add(item);
213 * Adds a button (or buttons). See {@link Roo.Toolbar.Button} for more info on the config.
214 * @param {Object/Array} config A button config or array of configs
215 * @return {Roo.Toolbar.Button/Array}
217 addButton : function(config){
218 if(config instanceof Array){
220 for(var i = 0, len = config.length; i < len; i++) {
221 buttons.push(this.addButton(config[i]));
226 if(!(config instanceof Roo.Toolbar.Button)){
228 new Roo.Toolbar.SplitButton(config) :
229 new Roo.Toolbar.Button(config);
231 var td = this.nextBlock();
238 * Adds text to the toolbar
239 * @param {String} text The text to add
240 * @return {Roo.Toolbar.Item} The element's item
242 addText : function(text){
243 return this.addItem(new Roo.Toolbar.TextItem(text));
247 * Inserts any {@link Roo.Toolbar.Item}/{@link Roo.Toolbar.Button} at the specified index.
248 * @param {Number} index The index where the item is to be inserted
249 * @param {Object/Roo.Toolbar.Item/Roo.Toolbar.Button (may be Array)} item The button, or button config object to be inserted.
250 * @return {Roo.Toolbar.Button/Item}
252 insertButton : function(index, item){
253 if(item instanceof Array){
255 for(var i = 0, len = item.length; i < len; i++) {
256 buttons.push(this.insertButton(index + i, item[i]));
260 if (!(item instanceof Roo.Toolbar.Button)){
261 item = new Roo.Toolbar.Button(item);
263 var td = document.createElement("td");
264 this.tr.insertBefore(td, this.tr.childNodes[index]);
266 this.items.insert(index, item);
271 * Adds a new element to the toolbar from the passed {@link Roo.DomHelper} config.
272 * @param {Object} config
273 * @return {Roo.Toolbar.Item} The element's item
275 addDom : function(config, returnEl){
276 var td = this.nextBlock();
277 Roo.DomHelper.overwrite(td, config);
278 var ti = new Roo.Toolbar.Item(td.firstChild);
285 * Collection of fields on the toolbar.. usefull for quering (value is false if there are no fields)
286 * @type Roo.util.MixedCollection
291 * Adds a dynamically rendered Roo.form field (TextField, ComboBox, etc).
292 * Note: the field should not have been rendered yet. For a field that has already been
293 * rendered, use {@link #addElement}.
294 * @param {Roo.form.Field} field
295 * @return {Roo.ToolbarItem}
299 addField : function(field) {
302 this.fields = new Roo.util.MixedCollection(false, function(o){
303 return o.id || ("item" + (++autoId));
308 var td = this.nextBlock();
310 var ti = new Roo.Toolbar.Item(td.firstChild);
313 this.fields.add(field);
324 this.el.child('div').setVisibilityMode(Roo.Element.DISPLAY);
325 this.el.child('div').hide();
333 this.el.child('div').show();
337 nextBlock : function(){
338 var td = document.createElement("td");
339 this.tr.appendChild(td);
344 destroy : function(){
345 if(this.items){ // rendered?
346 Roo.destroy.apply(Roo, this.items.items);
348 if(this.fields){ // rendered?
349 Roo.destroy.apply(Roo, this.fields.items);
351 Roo.Element.uncache(this.el, this.tr);
356 * @class Roo.Toolbar.Item
357 * The base class that other classes should extend in order to get some basic common toolbar item functionality.
360 * @param {HTMLElement} el
362 Roo.Toolbar.Item = function(el){
363 this.el = Roo.getDom(el);
364 this.id = Roo.id(this.el);
368 Roo.Toolbar.Item.prototype = {
371 * Get this item's HTML Element
372 * @return {HTMLElement}
379 render : function(td){
381 td.appendChild(this.el);
385 * Removes and destroys this item.
387 destroy : function(){
388 this.td.parentNode.removeChild(this.td);
396 this.td.style.display = "";
404 this.td.style.display = "none";
408 * Convenience function for boolean show/hide.
409 * @param {Boolean} visible true to show/false to hide
411 setVisible: function(visible){
420 * Try to focus this item.
423 Roo.fly(this.el).focus();
427 * Disables this item.
429 disable : function(){
430 Roo.fly(this.td).addClass("x-item-disabled");
431 this.disabled = true;
432 this.el.disabled = true;
439 Roo.fly(this.td).removeClass("x-item-disabled");
440 this.disabled = false;
441 this.el.disabled = false;
447 * @class Roo.Toolbar.Separator
448 * @extends Roo.Toolbar.Item
449 * A simple toolbar separator class
451 * Creates a new Separator
453 Roo.Toolbar.Separator = function(){
454 var s = document.createElement("span");
455 s.className = "ytb-sep";
456 Roo.Toolbar.Separator.superclass.constructor.call(this, s);
458 Roo.extend(Roo.Toolbar.Separator, Roo.Toolbar.Item, {
465 * @class Roo.Toolbar.Spacer
466 * @extends Roo.Toolbar.Item
467 * A simple element that adds extra horizontal space to a toolbar.
469 * Creates a new Spacer
471 Roo.Toolbar.Spacer = function(){
472 var s = document.createElement("div");
473 s.className = "ytb-spacer";
474 Roo.Toolbar.Spacer.superclass.constructor.call(this, s);
476 Roo.extend(Roo.Toolbar.Spacer, Roo.Toolbar.Item, {
483 * @class Roo.Toolbar.Fill
484 * @extends Roo.Toolbar.Spacer
485 * A simple element that adds a greedy (100% width) horizontal space to a toolbar.
487 * Creates a new Spacer
489 Roo.Toolbar.Fill = Roo.extend(Roo.Toolbar.Spacer, {
491 render : function(td){
492 td.style.width = '100%';
493 Roo.Toolbar.Fill.superclass.render.call(this, td);
498 * @class Roo.Toolbar.TextItem
499 * @extends Roo.Toolbar.Item
500 * A simple class that renders text directly into a toolbar.
502 * Creates a new TextItem
503 * @param {String} text
505 Roo.Toolbar.TextItem = function(text){
506 if (typeof(text) == 'object') {
509 var s = document.createElement("span");
510 s.className = "ytb-text";
512 Roo.Toolbar.TextItem.superclass.constructor.call(this, s);
514 Roo.extend(Roo.Toolbar.TextItem, Roo.Toolbar.Item, {
521 * @class Roo.Toolbar.Button
522 * @extends Roo.Button
523 * A button that renders into a toolbar.
525 * Creates a new Button
526 * @param {Object} config A standard {@link Roo.Button} config object
528 Roo.Toolbar.Button = function(config){
529 Roo.Toolbar.Button.superclass.constructor.call(this, null, config);
531 Roo.extend(Roo.Toolbar.Button, Roo.Button, {
532 render : function(td){
534 Roo.Toolbar.Button.superclass.render.call(this, td);
538 * Removes and destroys this button
540 destroy : function(){
541 Roo.Toolbar.Button.superclass.destroy.call(this);
542 this.td.parentNode.removeChild(this.td);
550 this.td.style.display = "";
558 this.td.style.display = "none";
564 disable : function(){
565 Roo.fly(this.td).addClass("x-item-disabled");
566 this.disabled = true;
573 Roo.fly(this.td).removeClass("x-item-disabled");
574 this.disabled = false;
578 Roo.ToolbarButton = Roo.Toolbar.Button;
581 * @class Roo.Toolbar.SplitButton
582 * @extends Roo.SplitButton
583 * A menu button that renders into a toolbar.
585 * Creates a new SplitButton
586 * @param {Object} config A standard {@link Roo.SplitButton} config object
588 Roo.Toolbar.SplitButton = function(config){
589 Roo.Toolbar.SplitButton.superclass.constructor.call(this, null, config);
591 Roo.extend(Roo.Toolbar.SplitButton, Roo.SplitButton, {
592 render : function(td){
594 Roo.Toolbar.SplitButton.superclass.render.call(this, td);
598 * Removes and destroys this button
600 destroy : function(){
601 Roo.Toolbar.SplitButton.superclass.destroy.call(this);
602 this.td.parentNode.removeChild(this.td);
610 this.td.style.display = "";
618 this.td.style.display = "none";
623 Roo.Toolbar.MenuButton = Roo.Toolbar.SplitButton;