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