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