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