allow string based values for comboboxarray
[roojs1] / Roo / ContentPanel.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 /**
12  * @class Roo.ContentPanel
13  * @extends Roo.util.Observable
14  * A basic ContentPanel element.
15  * @cfg {Boolean}   fitToFrame    True for this panel to adjust its size to fit when the region resizes  (defaults to false)
16  * @cfg {Boolean}   fitContainer   When using {@link #fitToFrame} and {@link #resizeEl}, you can also fit the parent container  (defaults to false)
17  * @cfg {Boolean/Object} autoCreate True to auto generate the DOM element for this panel, or a {@link Roo.DomHelper} config of the element to create
18  * @cfg {Boolean}   closable      True if the panel can be closed/removed
19  * @cfg {Boolean}   background    True if the panel should not be activated when it is added (defaults to false)
20  * @cfg {String/HTMLElement/Element} resizeEl An element to resize if {@link #fitToFrame} is true (instead of this panel's element)
21  * @cfg {Toolbar}   toolbar       A toolbar for this panel
22  * @cfg {Boolean} autoScroll    True to scroll overflow in this panel (use with {@link #fitToFrame})
23  * @cfg {String} title          The title for this panel
24  * @cfg {Array} adjustments     Values to <b>add</b> to the width/height when doing a {@link #fitToFrame} (default is [0, 0])
25  * @cfg {String} url            Calls {@link #setUrl} with this value
26  * @cfg {String} region         (center|north|south|east|west) which region to put this panel on (when used with xtype constructors)
27  * @cfg {String/Object} params  When used with {@link #url}, calls {@link #setUrl} with this value
28  * @cfg {Boolean} loadOnce      When used with {@link #url}, calls {@link #setUrl} with this value
29  * @cfg {String}    content        Raw content to fill content panel with (uses setContent on construction.)
30
31  * @constructor
32  * Create a new ContentPanel.
33  * @param {String/HTMLElement/Roo.Element} el The container element for this panel
34  * @param {String/Object} config A string to set only the title or a config object
35  * @param {String} content (optional) Set the HTML content for this panel
36  * @param {String} region (optional) Used by xtype constructors to add to regions. (values center,east,west,south,north)
37  */
38 Roo.ContentPanel = function(el, config, content){
39     
40      
41     /*
42     if(el.autoCreate || el.xtype){ // xtype is available if this is called from factory
43         config = el;
44         el = Roo.id();
45     }
46     if (config && config.parentLayout) { 
47         el = config.parentLayout.el.createChild(); 
48     }
49     */
50     if(el.autoCreate){ // xtype is available if this is called from factory
51         config = el;
52         el = Roo.id();
53     }
54     this.el = Roo.get(el);
55     if(!this.el && config && config.autoCreate){
56         if(typeof config.autoCreate == "object"){
57             if(!config.autoCreate.id){
58                 config.autoCreate.id = config.id||el;
59             }
60             this.el = Roo.DomHelper.append(document.body,
61                         config.autoCreate, true);
62         }else{
63             this.el = Roo.DomHelper.append(document.body,
64                         {tag: "div", cls: "x-layout-inactive-content", id: config.id||el}, true);
65         }
66     }
67     this.closable = false;
68     this.loaded = false;
69     this.active = false;
70     if(typeof config == "string"){
71         this.title = config;
72     }else{
73         Roo.apply(this, config);
74     }
75     
76     if (this.toolbar && !this.toolbar.el && this.toolbar.xtype) {
77         this.wrapEl = this.el.wrap();
78         this.toolbar.container = this.el.insertSibling(false, 'before');
79         this.toolbar = new Roo.Toolbar(this.toolbar);
80     }
81     
82     // xtype created footer. - not sure if will work as we normally have to render first..
83     if (this.footer && !this.footer.el && this.footer.xtype) {
84         if (!this.wrapEl) {
85             this.wrapEl = this.el.wrap();
86         }
87     
88         this.footer.container = this.wrapEl.createChild();
89          
90         this.footer = Roo.factory(this.footer, Roo);
91         
92     }
93     
94     if(this.resizeEl){
95         this.resizeEl = Roo.get(this.resizeEl, true);
96     }else{
97         this.resizeEl = this.el;
98     }
99     // handle view.xtype
100     
101  
102     
103     
104     this.addEvents({
105         /**
106          * @event activate
107          * Fires when this panel is activated. 
108          * @param {Roo.ContentPanel} this
109          */
110         "activate" : true,
111         /**
112          * @event deactivate
113          * Fires when this panel is activated. 
114          * @param {Roo.ContentPanel} this
115          */
116         "deactivate" : true,
117
118         /**
119          * @event resize
120          * Fires when this panel is resized if fitToFrame is true.
121          * @param {Roo.ContentPanel} this
122          * @param {Number} width The width after any component adjustments
123          * @param {Number} height The height after any component adjustments
124          */
125         "resize" : true,
126         
127          /**
128          * @event render
129          * Fires when this tab is created
130          * @param {Roo.ContentPanel} this
131          */
132         "render" : true
133          
134         
135     });
136     
137
138     
139     
140     if(this.autoScroll){
141         this.resizeEl.setStyle("overflow", "auto");
142     } else {
143         // fix randome scrolling
144         this.el.on('scroll', function() {
145             Roo.log('fix random scolling');
146             this.scrollTo('top',0); 
147         });
148     }
149     content = content || this.content;
150     if(content){
151         this.setContent(content);
152     }
153     if(config && config.url){
154         this.setUrl(this.url, this.params, this.loadOnce);
155     }
156     
157     
158     
159     Roo.ContentPanel.superclass.constructor.call(this);
160     
161     if (this.view && typeof(this.view.xtype) != 'undefined') {
162         this.view.el = this.el.appendChild(document.createElement("div"));
163         this.view = Roo.factory(this.view); 
164         this.view.render  &&  this.view.render(false, '');  
165     }
166     
167     
168     this.fireEvent('render', this);
169 };
170
171 Roo.extend(Roo.ContentPanel, Roo.util.Observable, {
172     tabTip:'',
173     setRegion : function(region){
174         this.region = region;
175         if(region){
176            this.el.replaceClass("x-layout-inactive-content", "x-layout-active-content");
177         }else{
178            this.el.replaceClass("x-layout-active-content", "x-layout-inactive-content");
179         } 
180     },
181     
182     /**
183      * Returns the toolbar for this Panel if one was configured. 
184      * @return {Roo.Toolbar} 
185      */
186     getToolbar : function(){
187         return this.toolbar;
188     },
189     
190     setActiveState : function(active){
191         this.active = active;
192         if(!active){
193             this.fireEvent("deactivate", this);
194         }else{
195             this.fireEvent("activate", this);
196         }
197     },
198     /**
199      * Updates this panel's element
200      * @param {String} content The new content
201      * @param {Boolean} loadScripts (optional) true to look for and process scripts
202     */
203     setContent : function(content, loadScripts){
204         this.el.update(content, loadScripts);
205     },
206
207     ignoreResize : function(w, h){
208         if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
209             return true;
210         }else{
211             this.lastSize = {width: w, height: h};
212             return false;
213         }
214     },
215     /**
216      * Get the {@link Roo.UpdateManager} for this panel. Enables you to perform Ajax updates.
217      * @return {Roo.UpdateManager} The UpdateManager
218      */
219     getUpdateManager : function(){
220         return this.el.getUpdateManager();
221     },
222      /**
223      * Loads this content panel immediately with content from XHR. Note: to delay loading until the panel is activated, use {@link #setUrl}.
224      * @param {Object/String/Function} url The url for this request or a function to call to get the url or a config object containing any of the following options:
225 <pre><code>
226 panel.load({
227     url: "your-url.php",
228     params: {param1: "foo", param2: "bar"}, // or a URL encoded string
229     callback: yourFunction,
230     scope: yourObject, //(optional scope)
231     discardUrl: false,
232     nocache: false,
233     text: "Loading...",
234     timeout: 30,
235     scripts: false
236 });
237 </code></pre>
238      * The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
239      * are shorthand for <i>disableCaching</i>, <i>indicatorText</i> and <i>loadScripts</i> and are used to set their associated property on this panel UpdateManager instance.
240      * @param {String/Object} params (optional) The parameters to pass as either a URL encoded string "param1=1&amp;param2=2" or an object {param1: 1, param2: 2}
241      * @param {Function} callback (optional) Callback when transaction is complete -- called with signature (oElement, bSuccess, oResponse)
242      * @param {Boolean} discardUrl (optional) By default when you execute an update the defaultUrl is changed to the last used URL. If true, it will not store the URL.
243      * @return {Roo.ContentPanel} this
244      */
245     load : function(){
246         var um = this.el.getUpdateManager();
247         um.update.apply(um, arguments);
248         return this;
249     },
250
251
252     /**
253      * Set a URL to be used to load the content for this panel. When this panel is activated, the content will be loaded from that URL.
254      * @param {String/Function} url The URL to load the content from or a function to call to get the URL
255      * @param {String/Object} params (optional) The string params for the update call or an object of the params. See {@link Roo.UpdateManager#update} for more details. (Defaults to null)
256      * @param {Boolean} loadOnce (optional) Whether to only load the content once. If this is false it makes the Ajax call every time this panel is activated. (Defaults to false)
257      * @return {Roo.UpdateManager} The UpdateManager
258      */
259     setUrl : function(url, params, loadOnce){
260         if(this.refreshDelegate){
261             this.removeListener("activate", this.refreshDelegate);
262         }
263         this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
264         this.on("activate", this.refreshDelegate);
265         return this.el.getUpdateManager();
266     },
267     
268     _handleRefresh : function(url, params, loadOnce){
269         if(!loadOnce || !this.loaded){
270             var updater = this.el.getUpdateManager();
271             updater.update(url, params, this._setLoaded.createDelegate(this));
272         }
273     },
274     
275     _setLoaded : function(){
276         this.loaded = true;
277     }, 
278     
279     /**
280      * Returns this panel's id
281      * @return {String} 
282      */
283     getId : function(){
284         return this.el.id;
285     },
286     
287     /** 
288      * Returns this panel's element - used by regiosn to add.
289      * @return {Roo.Element} 
290      */
291     getEl : function(){
292         return this.wrapEl || this.el;
293     },
294     
295     adjustForComponents : function(width, height)
296     {
297         //Roo.log('adjustForComponents ');
298         if(this.resizeEl != this.el){
299             width -= this.el.getFrameWidth('lr');
300             height -= this.el.getFrameWidth('tb');
301         }
302         if(this.toolbar){
303             var te = this.toolbar.getEl();
304             height -= te.getHeight();
305             te.setWidth(width);
306         }
307         if(this.footer){
308             var te = this.footer.getEl();
309             //Roo.log("footer:" + te.getHeight());
310             
311             height -= te.getHeight();
312             te.setWidth(width);
313         }
314         
315         
316         if(this.adjustments){
317             width += this.adjustments[0];
318             height += this.adjustments[1];
319         }
320         return {"width": width, "height": height};
321     },
322     
323     setSize : function(width, height){
324         if(this.fitToFrame && !this.ignoreResize(width, height)){
325             if(this.fitContainer && this.resizeEl != this.el){
326                 this.el.setSize(width, height);
327             }
328             var size = this.adjustForComponents(width, height);
329             this.resizeEl.setSize(this.autoWidth ? "auto" : size.width, this.autoHeight ? "auto" : size.height);
330             this.fireEvent('resize', this, size.width, size.height);
331         }
332     },
333     
334     /**
335      * Returns this panel's title
336      * @return {String} 
337      */
338     getTitle : function(){
339         return this.title;
340     },
341     
342     /**
343      * Set this panel's title
344      * @param {String} title
345      */
346     setTitle : function(title){
347         this.title = title;
348         if(this.region){
349             this.region.updatePanelTitle(this, title);
350         }
351     },
352     
353     /**
354      * Returns true is this panel was configured to be closable
355      * @return {Boolean} 
356      */
357     isClosable : function(){
358         return this.closable;
359     },
360     
361     beforeSlide : function(){
362         this.el.clip();
363         this.resizeEl.clip();
364     },
365     
366     afterSlide : function(){
367         this.el.unclip();
368         this.resizeEl.unclip();
369     },
370     
371     /**
372      *   Force a content refresh from the URL specified in the {@link #setUrl} method.
373      *   Will fail silently if the {@link #setUrl} method has not been called.
374      *   This does not activate the panel, just updates its content.
375      */
376     refresh : function(){
377         if(this.refreshDelegate){
378            this.loaded = false;
379            this.refreshDelegate();
380         }
381     },
382     
383     /**
384      * Destroys this panel
385      */
386     destroy : function(){
387         this.el.removeAllListeners();
388         var tempEl = document.createElement("span");
389         tempEl.appendChild(this.el.dom);
390         tempEl.innerHTML = "";
391         this.el.remove();
392         this.el = null;
393     },
394     
395     /**
396      * form - if the content panel contains a form - this is a reference to it.
397      * @type {Roo.form.Form}
398      */
399     form : false,
400     /**
401      * view - if the content panel contains a view (Roo.DatePicker / Roo.View / Roo.JsonView)
402      *    This contains a reference to it.
403      * @type {Roo.View}
404      */
405     view : false,
406     
407       /**
408      * Adds a xtype elements to the panel - currently only supports Forms, View, JsonView.
409      * <pre><code>
410
411 layout.addxtype({
412        xtype : 'Form',
413        items: [ .... ]
414    }
415 );
416
417 </code></pre>
418      * @param {Object} cfg Xtype definition of item to add.
419      */
420     
421     addxtype : function(cfg) {
422         // add form..
423         if (cfg.xtype.match(/^Form$/)) {
424             
425             var el;
426             //if (this.footer) {
427             //    el = this.footer.container.insertSibling(false, 'before');
428             //} else {
429                 el = this.el.createChild();
430             //}
431
432             this.form = new  Roo.form.Form(cfg);
433             
434             
435             if ( this.form.allItems.length) {
436                 this.form.render(el.dom);
437             }
438             return this.form;
439         }
440         // should only have one of theses..
441         if ([ 'View', 'JsonView', 'DatePicker'].indexOf(cfg.xtype) > -1) {
442             // views.. should not be just added - used named prop 'view''
443             
444             cfg.el = this.el.appendChild(document.createElement("div"));
445             // factory?
446             
447             var ret = new Roo.factory(cfg);
448              
449              ret.render && ret.render(false, ''); // render blank..
450             this.view = ret;
451             return ret;
452         }
453         return false;
454     }
455 });
456
457 /**
458  * @class Roo.GridPanel
459  * @extends Roo.ContentPanel
460  * @constructor
461  * Create a new GridPanel.
462  * @param {Roo.grid.Grid} grid The grid for this panel
463  * @param {String/Object} config A string to set only the panel's title, or a config object
464  */
465 Roo.GridPanel = function(grid, config){
466     
467   
468     this.wrapper = Roo.DomHelper.append(document.body, // wrapper for IE7 strict & safari scroll issue
469         {tag: "div", cls: "x-layout-grid-wrapper x-layout-inactive-content"}, true);
470         
471     this.wrapper.dom.appendChild(grid.getGridEl().dom);
472     
473     Roo.GridPanel.superclass.constructor.call(this, this.wrapper, config);
474     
475     if(this.toolbar){
476         this.toolbar.el.insertBefore(this.wrapper.dom.firstChild);
477     }
478     // xtype created footer. - not sure if will work as we normally have to render first..
479     if (this.footer && !this.footer.el && this.footer.xtype) {
480         
481         this.footer.container = this.grid.getView().getFooterPanel(true);
482         this.footer.dataSource = this.grid.dataSource;
483         this.footer = Roo.factory(this.footer, Roo);
484         
485     }
486     
487     grid.monitorWindowResize = false; // turn off autosizing
488     grid.autoHeight = false;
489     grid.autoWidth = false;
490     this.grid = grid;
491     this.grid.getGridEl().replaceClass("x-layout-inactive-content", "x-layout-component-panel");
492 };
493
494 Roo.extend(Roo.GridPanel, Roo.ContentPanel, {
495     getId : function(){
496         return this.grid.id;
497     },
498     
499     /**
500      * Returns the grid for this panel
501      * @return {Roo.grid.Grid} 
502      */
503     getGrid : function(){
504         return this.grid;    
505     },
506     
507     setSize : function(width, height){
508         if(!this.ignoreResize(width, height)){
509             var grid = this.grid;
510             var size = this.adjustForComponents(width, height);
511             grid.getGridEl().setSize(size.width, size.height);
512             grid.autoSize();
513         }
514     },
515     
516     beforeSlide : function(){
517         this.grid.getView().scroller.clip();
518     },
519     
520     afterSlide : function(){
521         this.grid.getView().scroller.unclip();
522     },
523     
524     destroy : function(){
525         this.grid.destroy();
526         delete this.grid;
527         Roo.GridPanel.superclass.destroy.call(this); 
528     }
529 });
530
531
532 /**
533  * @class Roo.NestedLayoutPanel
534  * @extends Roo.ContentPanel
535  * @constructor
536  * Create a new NestedLayoutPanel.
537  * 
538  * 
539  * @param {Roo.BorderLayout} layout The layout for this panel
540  * @param {String/Object} config A string to set only the title or a config object
541  */
542 Roo.NestedLayoutPanel = function(layout, config)
543 {
544     // construct with only one argument..
545     /* FIXME - implement nicer consturctors
546     if (layout.layout) {
547         config = layout;
548         layout = config.layout;
549         delete config.layout;
550     }
551     if (layout.xtype && !layout.getEl) {
552         // then layout needs constructing..
553         layout = Roo.factory(layout, Roo);
554     }
555     */
556     
557     
558     Roo.NestedLayoutPanel.superclass.constructor.call(this, layout.getEl(), config);
559     
560     layout.monitorWindowResize = false; // turn off autosizing
561     this.layout = layout;
562     this.layout.getEl().addClass("x-layout-nested-layout");
563     
564     
565     
566     
567 };
568
569 Roo.extend(Roo.NestedLayoutPanel, Roo.ContentPanel, {
570
571     setSize : function(width, height){
572         if(!this.ignoreResize(width, height)){
573             var size = this.adjustForComponents(width, height);
574             var el = this.layout.getEl();
575             el.setSize(size.width, size.height);
576             var touch = el.dom.offsetWidth;
577             this.layout.layout();
578             // ie requires a double layout on the first pass
579             if(Roo.isIE && !this.initialized){
580                 this.initialized = true;
581                 this.layout.layout();
582             }
583         }
584     },
585     
586     // activate all subpanels if not currently active..
587     
588     setActiveState : function(active){
589         this.active = active;
590         if(!active){
591             this.fireEvent("deactivate", this);
592             return;
593         }
594         
595         this.fireEvent("activate", this);
596         // not sure if this should happen before or after..
597         if (!this.layout) {
598             return; // should not happen..
599         }
600         var reg = false;
601         for (var r in this.layout.regions) {
602             reg = this.layout.getRegion(r);
603             if (reg.getActivePanel()) {
604                 //reg.showPanel(reg.getActivePanel()); // force it to activate.. 
605                 reg.setActivePanel(reg.getActivePanel());
606                 continue;
607             }
608             if (!reg.panels.length) {
609                 continue;
610             }
611             reg.showPanel(reg.getPanel(0));
612         }
613         
614         
615         
616         
617     },
618     
619     /**
620      * Returns the nested BorderLayout for this panel
621      * @return {Roo.BorderLayout} 
622      */
623     getLayout : function(){
624         return this.layout;
625     },
626     
627      /**
628      * Adds a xtype elements to the layout of the nested panel
629      * <pre><code>
630
631 panel.addxtype({
632        xtype : 'ContentPanel',
633        region: 'west',
634        items: [ .... ]
635    }
636 );
637
638 panel.addxtype({
639         xtype : 'NestedLayoutPanel',
640         region: 'west',
641         layout: {
642            center: { },
643            west: { }   
644         },
645         items : [ ... list of content panels or nested layout panels.. ]
646    }
647 );
648 </code></pre>
649      * @param {Object} cfg Xtype definition of item to add.
650      */
651     addxtype : function(cfg) {
652         return this.layout.addxtype(cfg);
653     
654     }
655 });
656
657 Roo.ScrollPanel = function(el, config, content){
658     config = config || {};
659     config.fitToFrame = true;
660     Roo.ScrollPanel.superclass.constructor.call(this, el, config, content);
661     
662     this.el.dom.style.overflow = "hidden";
663     var wrap = this.el.wrap({cls: "x-scroller x-layout-inactive-content"});
664     this.el.removeClass("x-layout-inactive-content");
665     this.el.on("mousewheel", this.onWheel, this);
666
667     var up = wrap.createChild({cls: "x-scroller-up", html: "&#160;"}, this.el.dom);
668     var down = wrap.createChild({cls: "x-scroller-down", html: "&#160;"});
669     up.unselectable(); down.unselectable();
670     up.on("click", this.scrollUp, this);
671     down.on("click", this.scrollDown, this);
672     up.addClassOnOver("x-scroller-btn-over");
673     down.addClassOnOver("x-scroller-btn-over");
674     up.addClassOnClick("x-scroller-btn-click");
675     down.addClassOnClick("x-scroller-btn-click");
676     this.adjustments = [0, -(up.getHeight() + down.getHeight())];
677
678     this.resizeEl = this.el;
679     this.el = wrap; this.up = up; this.down = down;
680 };
681
682 Roo.extend(Roo.ScrollPanel, Roo.ContentPanel, {
683     increment : 100,
684     wheelIncrement : 5,
685     scrollUp : function(){
686         this.resizeEl.scroll("up", this.increment, {callback: this.afterScroll, scope: this});
687     },
688
689     scrollDown : function(){
690         this.resizeEl.scroll("down", this.increment, {callback: this.afterScroll, scope: this});
691     },
692
693     afterScroll : function(){
694         var el = this.resizeEl;
695         var t = el.dom.scrollTop, h = el.dom.scrollHeight, ch = el.dom.clientHeight;
696         this.up[t == 0 ? "addClass" : "removeClass"]("x-scroller-btn-disabled");
697         this.down[h - t <= ch ? "addClass" : "removeClass"]("x-scroller-btn-disabled");
698     },
699
700     setSize : function(){
701         Roo.ScrollPanel.superclass.setSize.apply(this, arguments);
702         this.afterScroll();
703     },
704
705     onWheel : function(e){
706         var d = e.getWheelDelta();
707         this.resizeEl.dom.scrollTop -= (d*this.wheelIncrement);
708         this.afterScroll();
709         e.stopEvent();
710     },
711
712     setContent : function(content, loadScripts){
713         this.resizeEl.update(content, loadScripts);
714     }
715
716 });
717
718
719
720
721
722
723
724
725
726 /**
727  * @class Roo.TreePanel
728  * @extends Roo.ContentPanel
729  * @constructor
730  * Create a new TreePanel. - defaults to fit/scoll contents.
731  * @param {String/Object} config A string to set only the panel's title, or a config object
732  * @cfg {Roo.tree.TreePanel} tree The tree TreePanel, with config etc.
733  */
734 Roo.TreePanel = function(config){
735     var el = config.el;
736     var tree = config.tree;
737     delete config.tree; 
738     delete config.el; // hopefull!
739     
740     // wrapper for IE7 strict & safari scroll issue
741     
742     var treeEl = el.createChild();
743     config.resizeEl = treeEl;
744     
745     
746     
747     Roo.TreePanel.superclass.constructor.call(this, el, config);
748  
749  
750     this.tree = new Roo.tree.TreePanel(treeEl , tree);
751     //console.log(tree);
752     this.on('activate', function()
753     {
754         if (this.tree.rendered) {
755             return;
756         }
757         //console.log('render tree');
758         this.tree.render();
759     });
760     // this should not be needed.. - it's actually the 'el' that resizes?
761     // actuall it breaks the containerScroll - dragging nodes auto scroll at top
762     
763     //this.on('resize',  function (cp, w, h) {
764     //        this.tree.innerCt.setWidth(w);
765     //        this.tree.innerCt.setHeight(h);
766     //        //this.tree.innerCt.setStyle('overflow-y', 'auto');
767     //});
768
769         
770     
771 };
772
773 Roo.extend(Roo.TreePanel, Roo.ContentPanel, {   
774     fitToFrame : true,
775     autoScroll : true
776 });
777
778
779
780
781
782
783
784
785
786
787