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