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