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