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