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