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