source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/Toolbar.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 16.2 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.Toolbar
11 * @extends Ext.BoxComponent
12 * Basic Toolbar class. Toolbar elements can be created explicitly via their constructors, or implicitly
13 * via their xtypes.  Some items also have shortcut strings for creation. 
14 * @constructor
15 * Creates a new Toolbar
16 * @param {Object/Array} config A config object or an array of buttons to add
17 */ 
18 Ext.Toolbar = function(config){
19    if(Ext.isArray(config)){
20        config = {buttons:config};
21    }
22    Ext.Toolbar.superclass.constructor.call(this, config);
23};
24
25(function(){
26
27var T = Ext.Toolbar;
28
29Ext.extend(T, Ext.BoxComponent, {
30
31    trackMenus : true,
32
33    // private
34    initComponent : function(){
35        T.superclass.initComponent.call(this);
36
37        if(this.items){
38            this.buttons = this.items;
39        }
40        /**
41         * A MixedCollection of this Toolbar's items
42         * @property items
43         * @type Ext.util.MixedCollection
44         */
45        this.items = new Ext.util.MixedCollection(false, function(o){
46            return o.itemId || o.id || Ext.id();
47        });
48    },
49
50    // private
51    autoCreate: {
52        cls:'x-toolbar x-small-editor',
53        html:'<table cellspacing="0"><tr></tr></table>'
54    },
55
56    // private
57    onRender : function(ct, position){
58        this.el = ct.createChild(Ext.apply({ id: this.id },this.autoCreate), position);
59        this.tr = this.el.child("tr", true);
60    },
61
62    // private
63    afterRender : function(){
64        T.superclass.afterRender.call(this);
65        if(this.buttons){
66            this.add.apply(this, this.buttons);
67            delete this.buttons;
68        }
69    },
70
71    /**
72     * Adds element(s) to the toolbar -- this function takes a variable number of
73     * arguments of mixed type and adds them to the toolbar.
74     * @param {Mixed} arg1 The following types of arguments are all valid:<br />
75     * <ul>
76     * <li>{@link Ext.Toolbar.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
77     * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
78     * <li>Field: Any form field (equivalent to {@link #addField})</li>
79     * <li>Item: Any subclass of {@link Ext.Toolbar.Item} (equivalent to {@link #addItem})</li>
80     * <li>String: Any generic string (gets wrapped in a {@link Ext.Toolbar.TextItem}, equivalent to {@link #addText}).
81     * Note that there are a few special strings that are treated differently as explained next.</li>
82     * <li>'separator' or '-': Creates a separator element (equivalent to {@link #addSeparator})</li>
83     * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
84     * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
85     * </ul>
86     * @param {Mixed} arg2
87     * @param {Mixed} etc.
88     */
89    add : function(){
90        var a = arguments, l = a.length;
91        for(var i = 0; i < l; i++){
92            var el = a[i];
93            if(el.isFormField){ // some kind of form field
94                this.addField(el);
95            }else if(el.render){ // some kind of Toolbar.Item
96                this.addItem(el);
97            }else if(typeof el == "string"){ // string
98                if(el == "separator" || el == "-"){
99                    this.addSeparator();
100                }else if(el == " "){
101                    this.addSpacer();
102                }else if(el == "->"){
103                    this.addFill();
104                }else{
105                    this.addText(el);
106                }
107            }else if(el.tagName){ // element
108                this.addElement(el);
109            }else if(typeof el == "object"){ // must be button config?
110                if(el.xtype){
111                    this.addField(Ext.ComponentMgr.create(el, 'button'));
112                }else{
113                    this.addButton(el);
114                }
115            }
116        }
117    },
118   
119    /**
120     * Adds a separator
121     * @return {Ext.Toolbar.Item} The separator item
122     */
123    addSeparator : function(){
124        return this.addItem(new T.Separator());
125    },
126
127    /**
128     * Adds a spacer element
129     * @return {Ext.Toolbar.Spacer} The spacer item
130     */
131    addSpacer : function(){
132        return this.addItem(new T.Spacer());
133    },
134
135    /**
136     * Adds a fill element that forces subsequent additions to the right side of the toolbar
137     * @return {Ext.Toolbar.Fill} The fill item
138     */
139    addFill : function(){
140        return this.addItem(new T.Fill());
141    },
142
143    /**
144     * Adds any standard HTML element to the toolbar
145     * @param {Mixed} el The element or id of the element to add
146     * @return {Ext.Toolbar.Item} The element's item
147     */
148    addElement : function(el){
149        return this.addItem(new T.Item(el));
150    },
151   
152    /**
153     * Adds any Toolbar.Item or subclass
154     * @param {Ext.Toolbar.Item} item
155     * @return {Ext.Toolbar.Item} The item
156     */
157    addItem : function(item){
158        var td = this.nextBlock();
159        this.initMenuTracking(item);
160        item.render(td);
161        this.items.add(item);
162        return item;
163    },
164   
165    /**
166     * Adds a button (or buttons). See {@link Ext.Toolbar.Button} for more info on the config.
167     * @param {Object/Array} config A button config or array of configs
168     * @return {Ext.Toolbar.Button/Array}
169     */
170    addButton : function(config){
171        if(Ext.isArray(config)){
172            var buttons = [];
173            for(var i = 0, len = config.length; i < len; i++) {
174                buttons.push(this.addButton(config[i]));
175            }
176            return buttons;
177        }
178        var b = config;
179        if(!(config instanceof T.Button)){
180            b = config.split ? 
181                new T.SplitButton(config) :
182                new T.Button(config);
183        }
184        var td = this.nextBlock();
185        this.initMenuTracking(b);
186        b.render(td);
187        this.items.add(b);
188        return b;
189    },
190
191    // private
192    initMenuTracking : function(item){
193        if(this.trackMenus && item.menu){
194            item.on({
195                'menutriggerover' : this.onButtonTriggerOver,
196                'menushow' : this.onButtonMenuShow,
197                'menuhide' : this.onButtonMenuHide,
198                scope: this
199            })
200        }
201    },
202
203    /**
204     * Adds text to the toolbar
205     * @param {String} text The text to add
206     * @return {Ext.Toolbar.Item} The element's item
207     */
208    addText : function(text){
209        return this.addItem(new T.TextItem(text));
210    },
211   
212    /**
213     * Inserts any {@link Ext.Toolbar.Item}/{@link Ext.Toolbar.Button} at the specified index.
214     * @param {Number} index The index where the item is to be inserted
215     * @param {Object/Ext.Toolbar.Item/Ext.Toolbar.Button/Array} item The button, or button config object to be
216     * inserted, or an array of buttons/configs.
217     * @return {Ext.Toolbar.Button/Item}
218     */
219    insertButton : function(index, item){
220        if(Ext.isArray(item)){
221            var buttons = [];
222            for(var i = 0, len = item.length; i < len; i++) {
223               buttons.push(this.insertButton(index + i, item[i]));
224            }
225            return buttons;
226        }
227        if (!(item instanceof T.Button)){
228           item = new T.Button(item);
229        }
230        var td = document.createElement("td");
231        this.tr.insertBefore(td, this.tr.childNodes[index]);
232        this.initMenuTracking(item);
233        item.render(td);
234        this.items.insert(index, item);
235        return item;
236    },
237   
238    /**
239     * Adds a new element to the toolbar from the passed {@link Ext.DomHelper} config
240     * @param {Object} config
241     * @return {Ext.Toolbar.Item} The element's item
242     */
243    addDom : function(config, returnEl){
244        var td = this.nextBlock();
245        Ext.DomHelper.overwrite(td, config);
246        var ti = new T.Item(td.firstChild);
247        ti.render(td);
248        this.items.add(ti);
249        return ti;
250    },
251
252    /**
253     * Adds a dynamically rendered Ext.form field (TextField, ComboBox, etc). Note: the field should not have
254     * been rendered yet. For a field that has already been rendered, use {@link #addElement}.
255     * @param {Ext.form.Field} field
256     * @return {Ext.Toolbar.Item}
257     */
258    addField : function(field){
259        var td = this.nextBlock();
260        field.render(td);
261        var ti = new T.Item(td.firstChild);
262        ti.render(td);
263        this.items.add(field);
264        return ti;
265    },
266
267    // private
268    nextBlock : function(){
269        var td = document.createElement("td");
270        this.tr.appendChild(td);
271        return td;
272    },
273
274    // private
275    onDestroy : function(){
276        Ext.Toolbar.superclass.onDestroy.call(this);
277        if(this.rendered){
278            if(this.items){ // rendered?
279                Ext.destroy.apply(Ext, this.items.items);
280            }
281            Ext.Element.uncache(this.tr);
282        }
283    },
284
285    // private
286    onDisable : function(){
287        this.items.each(function(item){
288             if(item.disable){
289                 item.disable();
290             }
291        });
292    },
293
294    // private
295    onEnable : function(){
296        this.items.each(function(item){
297             if(item.enable){
298                 item.enable();
299             }
300        });
301    },
302
303    // private
304    onButtonTriggerOver : function(btn){
305        if(this.activeMenuBtn && this.activeMenuBtn != btn){
306            this.activeMenuBtn.hideMenu();
307            btn.showMenu();
308            this.activeMenuBtn = btn;
309        }
310    },
311
312    // private
313    onButtonMenuShow : function(btn){
314        this.activeMenuBtn = btn;
315    },
316
317    // private
318    onButtonMenuHide : function(btn){
319        delete this.activeMenuBtn;
320    }
321
322    /**
323     * @cfg {String} autoEl @hide
324     */
325});
326Ext.reg('toolbar', Ext.Toolbar);
327
328/**
329 * @class Ext.Toolbar.Item
330 * The base class that other classes should extend in order to get some basic common toolbar item functionality.
331 * @constructor
332 * Creates a new Item
333 * @param {HTMLElement} el
334 */
335T.Item = function(el){
336    this.el = Ext.getDom(el);
337    this.id = Ext.id(this.el);
338    this.hidden = false;
339};
340
341T.Item.prototype = {
342   
343    /**
344     * Get this item's HTML Element
345     * @return {HTMLElement}
346     */
347    getEl : function(){
348       return this.el; 
349    },
350
351    // private
352    render : function(td){
353        this.td = td;
354        td.appendChild(this.el);
355    },
356   
357    /**
358     * Removes and destroys this item.
359     */
360    destroy : function(){
361        if(this.el){
362            var el = Ext.get(this.el);
363            Ext.destroy(el);
364        }
365        Ext.removeNode(this.td);
366    },
367   
368    /**
369     * Shows this item.
370     */
371    show: function(){
372        this.hidden = false;
373        this.td.style.display = "";
374    },
375   
376    /**
377     * Hides this item.
378     */
379    hide: function(){
380        this.hidden = true;
381        this.td.style.display = "none";
382    },
383   
384    /**
385     * Convenience function for boolean show/hide.
386     * @param {Boolean} visible true to show/false to hide
387     */
388    setVisible: function(visible){
389        if(visible) {
390            this.show();
391        }else{
392            this.hide();
393        }
394    },
395   
396    /**
397     * Try to focus this item
398     */
399    focus : function(){
400        Ext.fly(this.el).focus();
401    },
402   
403    /**
404     * Disables this item.
405     */
406    disable : function(){
407        Ext.fly(this.td).addClass("x-item-disabled");
408        this.disabled = true;
409        this.el.disabled = true;
410    },
411   
412    /**
413     * Enables this item.
414     */
415    enable : function(){
416        Ext.fly(this.td).removeClass("x-item-disabled");
417        this.disabled = false;
418        this.el.disabled = false;
419    }
420};
421Ext.reg('tbitem', T.Item);
422
423
424/**
425 * @class Ext.Toolbar.Separator
426 * @extends Ext.Toolbar.Item
427 * A simple class that adds a vertical separator bar between toolbar items.  Example usage:
428 * <pre><code>
429new Ext.Panel({
430        tbar : [
431                'Item 1',
432                {xtype: 'tbseparator'}, // or '-'
433                'Item 2'
434        ]
435});
436</code></pre>
437 * @constructor
438 * Creates a new Separator
439 */
440T.Separator = function(){
441    var s = document.createElement("span");
442    s.className = "ytb-sep";
443    T.Separator.superclass.constructor.call(this, s);
444};
445Ext.extend(T.Separator, T.Item, {
446    enable:Ext.emptyFn,
447    disable:Ext.emptyFn,
448    focus:Ext.emptyFn
449});
450Ext.reg('tbseparator', T.Separator);
451
452/**
453 * @class Ext.Toolbar.Spacer
454 * @extends Ext.Toolbar.Item
455 * A simple element that adds extra horizontal space between items in a toolbar.
456 * <pre><code>
457new Ext.Panel({
458        tbar : [
459                'Item 1',
460                {xtype: 'tbspacer'}, // or ' '
461                'Item 2'
462        ]
463});
464</code></pre>
465 * @constructor
466 * Creates a new Spacer
467 */
468T.Spacer = function(){
469    var s = document.createElement("div");
470    s.className = "ytb-spacer";
471    T.Spacer.superclass.constructor.call(this, s);
472};
473Ext.extend(T.Spacer, T.Item, {
474    enable:Ext.emptyFn,
475    disable:Ext.emptyFn,
476    focus:Ext.emptyFn
477});
478
479Ext.reg('tbspacer', T.Spacer);
480
481/**
482 * @class Ext.Toolbar.Fill
483 * @extends Ext.Toolbar.Spacer
484 * A simple element that adds a greedy (100% width) horizontal space between items in a toolbar.
485 * <pre><code>
486new Ext.Panel({
487        tbar : [
488                'Item 1',
489                {xtype: 'tbfill'}, // or '->'
490                'Item 2'
491        ]
492});
493</code></pre>
494 * @constructor
495 * Creates a new Spacer
496 */
497T.Fill = Ext.extend(T.Spacer, {
498    // private
499    render : function(td){
500        td.style.width = '100%';
501        T.Fill.superclass.render.call(this, td);
502    }
503});
504Ext.reg('tbfill', T.Fill);
505
506/**
507 * @class Ext.Toolbar.TextItem
508 * @extends Ext.Toolbar.Item
509 * A simple class that renders text directly into a toolbar.
510 * <pre><code>
511new Ext.Panel({
512        tbar : [
513                {xtype: 'tbtext', text: 'Item 1'} // or simply 'Item 1'
514        ]
515});
516</code></pre>
517 * @constructor
518 * Creates a new TextItem
519 * @param {String/Object} text A text string, or a config object containing a <tt>text</tt> property
520 */
521T.TextItem = function(t){
522    var s = document.createElement("span");
523    s.className = "ytb-text";
524    s.innerHTML = t.text ? t.text : t;
525    T.TextItem.superclass.constructor.call(this, s);
526};
527Ext.extend(T.TextItem, T.Item, {
528    enable:Ext.emptyFn,
529    disable:Ext.emptyFn,
530    focus:Ext.emptyFn
531});
532Ext.reg('tbtext', T.TextItem);
533
534
535/**
536 * @class Ext.Toolbar.Button
537 * @extends Ext.Button
538 * A button that renders into a toolbar. Use the <tt>handler</tt> config to specify a callback function
539 * to handle the button's click event.
540 * <pre><code>
541new Ext.Panel({
542        tbar : [
543                {text: 'OK', handler: okHandler} // tbbutton is the default xtype if not specified
544        ]
545});
546</code></pre>
547 * @constructor
548 * Creates a new Button
549 * @param {Object} config A standard {@link Ext.Button} config object
550 */
551T.Button = Ext.extend(Ext.Button, {
552    hideParent : true,
553
554    onDestroy : function(){
555        T.Button.superclass.onDestroy.call(this);
556        if(this.container){
557            this.container.remove();
558        }
559    }
560});
561Ext.reg('tbbutton', T.Button);
562
563/**
564 * @class Ext.Toolbar.SplitButton
565 * @extends Ext.SplitButton
566 * A split button that renders into a toolbar.
567 * <pre><code>
568new Ext.Panel({
569        tbar : [
570                {
571                        xtype: 'tbsplit',
572                        text: 'Options',
573                        handler: optionsHandler, // handle a click on the button itself
574                        menu: new Ext.menu.Menu({
575                        items: [
576                                // These items will display in a dropdown menu when the split arrow is clicked
577                                {text: 'Item 1', handler: item1Handler},
578                                {text: 'Item 2', handler: item2Handler}
579                        ]
580                        })
581                }
582        ]
583});
584</code></pre>
585 * @constructor
586 * Creates a new SplitButton
587 * @param {Object} config A standard {@link Ext.SplitButton} config object
588 */
589T.SplitButton = Ext.extend(Ext.SplitButton, {
590    hideParent : true,
591
592    onDestroy : function(){
593        T.SplitButton.superclass.onDestroy.call(this);
594        if(this.container){
595            this.container.remove();
596        }
597    }
598});
599
600Ext.reg('tbsplit', T.SplitButton);
601// backwards compat
602T.MenuButton = T.SplitButton;
603
604})();
Note: See TracBrowser for help on using the repository browser.