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