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 Roo.log('HERE!!!!!!!!!!!!!!!!!!!!!!!!!111');
65 this.el = Roo.get(ct);
67 this.el.addClass(this.cls);
69 // using a table allows for vertical alignment
70 // 100% width is needed by Safari...
71 this.el.update('<div class="x-toolbar x-small-editor"><table cellspacing="0"><tr></tr></table></div>');
72 this.tr = this.el.child("tr", true);
74 this.items = new Roo.util.MixedCollection(false, function(o){
75 return o.id || ("item" + (++autoId));
78 this.add.apply(this, this.buttons);
84 * Adds element(s) to the toolbar -- this function takes a variable number of
85 * arguments of mixed type and adds them to the toolbar.
86 * @param {Mixed} arg1 The following types of arguments are all valid:<br />
88 * <li>{@link Roo.Toolbar.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
89 * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
90 * <li>Field: Any form field (equivalent to {@link #addField})</li>
91 * <li>Item: Any subclass of {@link Roo.Toolbar.Item} (equivalent to {@link #addItem})</li>
92 * <li>String: Any generic string (gets wrapped in a {@link Roo.Toolbar.TextItem}, equivalent to {@link #addText}).
93 * Note that there are a few special strings that are treated differently as explained nRoo.</li>
94 * <li>'separator' or '-': Creates a separator element (equivalent to {@link #addSeparator})</li>
95 * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
96 * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
102 var a = arguments, l = a.length;
103 for(var i = 0; i < l; i++){
108 _add : function(el) {
111 el = Roo.factory(el, typeof(Roo.Toolbar[el.xtype]) == 'undefined' ? Roo.form : Roo.Toolbar);
114 if (el.applyTo){ // some kind of form field
115 return this.addField(el);
117 if (el.render){ // some kind of Toolbar.Item
118 return this.addItem(el);
120 if (typeof el == "string"){ // string
121 if(el == "separator" || el == "-"){
122 return this.addSeparator();
125 return this.addSpacer();
128 return this.addFill();
130 return this.addText(el);
133 if(el.tagName){ // element
134 return this.addElement(el);
136 if(typeof el == "object"){ // must be button config?
137 return this.addButton(el);
145 * Add an Xtype element
146 * @param {Object} xtype Xtype Object
147 * @return {Object} created Object
149 addxtype : function(e){
154 * Returns the Element for this toolbar.
155 * @return {Roo.Element}
163 * @return {Roo.Toolbar.Item} The separator item
165 addSeparator : function(){
166 return this.addItem(new Roo.Toolbar.Separator());
170 * Adds a spacer element
171 * @return {Roo.Toolbar.Spacer} The spacer item
173 addSpacer : function(){
174 return this.addItem(new Roo.Toolbar.Spacer());
178 * Adds a fill element that forces subsequent additions to the right side of the toolbar
179 * @return {Roo.Toolbar.Fill} The fill item
181 addFill : function(){
182 return this.addItem(new Roo.Toolbar.Fill());
186 * Adds any standard HTML element to the toolbar
187 * @param {String/HTMLElement/Element} el The element or id of the element to add
188 * @return {Roo.Toolbar.Item} The element's item
190 addElement : function(el){
191 return this.addItem(new Roo.Toolbar.Item(el));
194 * Collection of items on the toolbar.. (only Toolbar Items, so use fields to retrieve fields)
195 * @type Roo.util.MixedCollection
200 * Adds any Toolbar.Item or subclass
201 * @param {Roo.Toolbar.Item} item
202 * @return {Roo.Toolbar.Item} The item
204 addItem : function(item){
205 var td = this.nextBlock();
207 this.items.add(item);
212 * Adds a button (or buttons). See {@link Roo.Toolbar.Button} for more info on the config.
213 * @param {Object/Array} config A button config or array of configs
214 * @return {Roo.Toolbar.Button/Array}
216 addButton : function(config){
217 if(config instanceof Array){
219 for(var i = 0, len = config.length; i < len; i++) {
220 buttons.push(this.addButton(config[i]));
225 if(!(config instanceof Roo.Toolbar.Button)){
227 new Roo.Toolbar.SplitButton(config) :
228 new Roo.Toolbar.Button(config);
230 var td = this.nextBlock();
237 * Adds text to the toolbar
238 * @param {String} text The text to add
239 * @return {Roo.Toolbar.Item} The element's item
241 addText : function(text){
242 return this.addItem(new Roo.Toolbar.TextItem(text));
246 * Inserts any {@link Roo.Toolbar.Item}/{@link Roo.Toolbar.Button} at the specified index.
247 * @param {Number} index The index where the item is to be inserted
248 * @param {Object/Roo.Toolbar.Item/Roo.Toolbar.Button (may be Array)} item The button, or button config object to be inserted.
249 * @return {Roo.Toolbar.Button/Item}
251 insertButton : function(index, item){
252 if(item instanceof Array){
254 for(var i = 0, len = item.length; i < len; i++) {
255 buttons.push(this.insertButton(index + i, item[i]));
259 if (!(item instanceof Roo.Toolbar.Button)){
260 item = new Roo.Toolbar.Button(item);
262 var td = document.createElement("td");
263 this.tr.insertBefore(td, this.tr.childNodes[index]);
265 this.items.insert(index, item);
270 * Adds a new element to the toolbar from the passed {@link Roo.DomHelper} config.
271 * @param {Object} config
272 * @return {Roo.Toolbar.Item} The element's item
274 addDom : function(config, returnEl){
275 var td = this.nextBlock();
276 Roo.DomHelper.overwrite(td, config);
277 var ti = new Roo.Toolbar.Item(td.firstChild);
284 * Collection of fields on the toolbar.. usefull for quering (value is false if there are no fields)
285 * @type Roo.util.MixedCollection
290 * Adds a dynamically rendered Roo.form field (TextField, ComboBox, etc).
291 * Note: the field should not have been rendered yet. For a field that has already been
292 * rendered, use {@link #addElement}.
293 * @param {Roo.form.Field} field
294 * @return {Roo.ToolbarItem}
298 addField : function(field) {
301 this.fields = new Roo.util.MixedCollection(false, function(o){
302 return o.id || ("item" + (++autoId));
307 var td = this.nextBlock();
309 var ti = new Roo.Toolbar.Item(td.firstChild);
312 this.fields.add(field);
323 this.el.child('div').setVisibilityMode(Roo.Element.DISPLAY);
324 this.el.child('div').hide();
332 this.el.child('div').show();
336 nextBlock : function(){
337 var td = document.createElement("td");
338 this.tr.appendChild(td);
343 destroy : function(){
344 if(this.items){ // rendered?
345 Roo.destroy.apply(Roo, this.items.items);
347 if(this.fields){ // rendered?
348 Roo.destroy.apply(Roo, this.fields.items);
350 Roo.Element.uncache(this.el, this.tr);
355 * @class Roo.Toolbar.Item
356 * The base class that other classes should extend in order to get some basic common toolbar item functionality.
359 * @param {HTMLElement} el
361 Roo.Toolbar.Item = function(el){
362 this.el = Roo.getDom(el);
363 this.id = Roo.id(this.el);
367 Roo.Toolbar.Item.prototype = {
370 * Get this item's HTML Element
371 * @return {HTMLElement}
378 render : function(td){
380 td.appendChild(this.el);
384 * Removes and destroys this item.
386 destroy : function(){
387 this.td.parentNode.removeChild(this.td);
395 this.td.style.display = "";
403 this.td.style.display = "none";
407 * Convenience function for boolean show/hide.
408 * @param {Boolean} visible true to show/false to hide
410 setVisible: function(visible){
419 * Try to focus this item.
422 Roo.fly(this.el).focus();
426 * Disables this item.
428 disable : function(){
429 Roo.fly(this.td).addClass("x-item-disabled");
430 this.disabled = true;
431 this.el.disabled = true;
438 Roo.fly(this.td).removeClass("x-item-disabled");
439 this.disabled = false;
440 this.el.disabled = false;
446 * @class Roo.Toolbar.Separator
447 * @extends Roo.Toolbar.Item
448 * A simple toolbar separator class
450 * Creates a new Separator
452 Roo.Toolbar.Separator = function(){
453 var s = document.createElement("span");
454 s.className = "ytb-sep";
455 Roo.Toolbar.Separator.superclass.constructor.call(this, s);
457 Roo.extend(Roo.Toolbar.Separator, Roo.Toolbar.Item, {
464 * @class Roo.Toolbar.Spacer
465 * @extends Roo.Toolbar.Item
466 * A simple element that adds extra horizontal space to a toolbar.
468 * Creates a new Spacer
470 Roo.Toolbar.Spacer = function(){
471 var s = document.createElement("div");
472 s.className = "ytb-spacer";
473 Roo.Toolbar.Spacer.superclass.constructor.call(this, s);
475 Roo.extend(Roo.Toolbar.Spacer, Roo.Toolbar.Item, {
482 * @class Roo.Toolbar.Fill
483 * @extends Roo.Toolbar.Spacer
484 * A simple element that adds a greedy (100% width) horizontal space to a toolbar.
486 * Creates a new Spacer
488 Roo.Toolbar.Fill = Roo.extend(Roo.Toolbar.Spacer, {
490 render : function(td){
491 td.style.width = '100%';
492 Roo.Toolbar.Fill.superclass.render.call(this, td);
497 * @class Roo.Toolbar.TextItem
498 * @extends Roo.Toolbar.Item
499 * A simple class that renders text directly into a toolbar.
501 * Creates a new TextItem
502 * @param {String} text
504 Roo.Toolbar.TextItem = function(text){
505 if (typeof(text) == 'object') {
508 var s = document.createElement("span");
509 s.className = "ytb-text";
511 Roo.Toolbar.TextItem.superclass.constructor.call(this, s);
513 Roo.extend(Roo.Toolbar.TextItem, Roo.Toolbar.Item, {
520 * @class Roo.Toolbar.Button
521 * @extends Roo.Button
522 * A button that renders into a toolbar.
524 * Creates a new Button
525 * @param {Object} config A standard {@link Roo.Button} config object
527 Roo.Toolbar.Button = function(config){
528 Roo.Toolbar.Button.superclass.constructor.call(this, null, config);
530 Roo.extend(Roo.Toolbar.Button, Roo.Button, {
531 render : function(td){
533 Roo.Toolbar.Button.superclass.render.call(this, td);
537 * Removes and destroys this button
539 destroy : function(){
540 Roo.Toolbar.Button.superclass.destroy.call(this);
541 this.td.parentNode.removeChild(this.td);
549 this.td.style.display = "";
557 this.td.style.display = "none";
563 disable : function(){
564 Roo.fly(this.td).addClass("x-item-disabled");
565 this.disabled = true;
572 Roo.fly(this.td).removeClass("x-item-disabled");
573 this.disabled = false;
577 Roo.ToolbarButton = Roo.Toolbar.Button;
580 * @class Roo.Toolbar.SplitButton
581 * @extends Roo.SplitButton
582 * A menu button that renders into a toolbar.
584 * Creates a new SplitButton
585 * @param {Object} config A standard {@link Roo.SplitButton} config object
587 Roo.Toolbar.SplitButton = function(config){
588 Roo.Toolbar.SplitButton.superclass.constructor.call(this, null, config);
590 Roo.extend(Roo.Toolbar.SplitButton, Roo.SplitButton, {
591 render : function(td){
593 Roo.Toolbar.SplitButton.superclass.render.call(this, td);
597 * Removes and destroys this button
599 destroy : function(){
600 Roo.Toolbar.SplitButton.superclass.destroy.call(this);
601 this.td.parentNode.removeChild(this.td);
609 this.td.style.display = "";
617 this.td.style.display = "none";
622 Roo.Toolbar.MenuButton = Roo.Toolbar.SplitButton;