initial import
[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     this.wrapper = Roo.DomHelper.append(document.body, // wrapper for IE7 strict & safari scroll issue
376         {tag: "div", cls: "x-layout-grid-wrapper x-layout-inactive-content"}, true);
377     this.wrapper.dom.appendChild(grid.getGridEl().dom);
378     Roo.GridPanel.superclass.constructor.call(this, this.wrapper, config);
379     
380     
381     if(this.toolbar){
382         this.toolbar.el.insertBefore(this.wrapper.dom.firstChild);
383     }
384     // xtype created footer. - not sure if will work as we normally have to render first..
385     if (this.footer && !this.footer.el && this.footer.xtype) {
386         
387         this.footer.container = this.grid.getView().getFooterPanel(true);
388         this.footer.dataSource = this.grid.dataSource;
389         this.footer= Roo.factory(this.footer, Roo);
390         
391     }
392     
393     grid.monitorWindowResize = false; // turn off autosizing
394     grid.autoHeight = false;
395     grid.autoWidth = false;
396     this.grid = grid;
397     this.grid.getGridEl().replaceClass("x-layout-inactive-content", "x-layout-component-panel");
398 };
399
400 Roo.extend(Roo.GridPanel, Roo.ContentPanel, {
401     getId : function(){
402         return this.grid.id;
403     },
404     
405     /**
406      * Returns the grid for this panel
407      * @return {Roo.grid.Grid} 
408      */
409     getGrid : function(){
410         return this.grid;    
411     },
412     
413     setSize : function(width, height){
414         if(!this.ignoreResize(width, height)){
415             var grid = this.grid;
416             var size = this.adjustForComponents(width, height);
417             grid.getGridEl().setSize(size.width, size.height);
418             grid.autoSize();
419         }
420     },
421     
422     beforeSlide : function(){
423         this.grid.getView().scroller.clip();
424     },
425     
426     afterSlide : function(){
427         this.grid.getView().scroller.unclip();
428     },
429     
430     destroy : function(){
431         this.grid.destroy();
432         delete this.grid;
433         Roo.GridPanel.superclass.destroy.call(this); 
434     }
435 });
436
437
438 /**
439  * @class Roo.NestedLayoutPanel
440  * @extends Roo.ContentPanel
441  * @constructor
442  * Create a new NestedLayoutPanel.
443  * 
444  * 
445  * @param {Roo.BorderLayout} layout The layout for this panel
446  * @param {String/Object} config A string to set only the title or a config object
447  */
448 Roo.NestedLayoutPanel = function(layout, config)
449 {
450     // construct with only one argument..
451     /* FIXME - implement nicer consturctors
452     if (layout.layout) {
453         config = layout;
454         layout = config.layout;
455         delete config.layout;
456     }
457     if (layout.xtype && !layout.getEl) {
458         // then layout needs constructing..
459         layout = Roo.factory(layout, Roo);
460     }
461     */
462     
463     Roo.NestedLayoutPanel.superclass.constructor.call(this, layout.getEl(), config);
464     
465     layout.monitorWindowResize = false; // turn off autosizing
466     this.layout = layout;
467     this.layout.getEl().addClass("x-layout-nested-layout");
468     
469     
470     
471 };
472
473 Roo.extend(Roo.NestedLayoutPanel, Roo.ContentPanel, {
474
475     setSize : function(width, height){
476         if(!this.ignoreResize(width, height)){
477             var size = this.adjustForComponents(width, height);
478             var el = this.layout.getEl();
479             el.setSize(size.width, size.height);
480             var touch = el.dom.offsetWidth;
481             this.layout.layout();
482             // ie requires a double layout on the first pass
483             if(Roo.isIE && !this.initialized){
484                 this.initialized = true;
485                 this.layout.layout();
486             }
487         }
488     },
489     
490     // activate all subpanels if not currently active..
491     
492     setActiveState : function(active){
493         this.active = active;
494         if(!active){
495             this.fireEvent("deactivate", this);
496             return;
497         }
498         
499         this.fireEvent("activate", this);
500         // not sure if this should happen before or after..
501         if (!this.layout) {
502             return; // should not happen..
503         }
504         var reg = false;
505         for (var r in this.layout.regions) {
506             reg = this.layout.getRegion(r);
507             if (reg.getActivePanel()) {
508                 //reg.showPanel(reg.getActivePanel()); // force it to activate.. 
509                 reg.setActivePanel(reg.getActivePanel());
510                 continue;
511             }
512             if (!reg.panels.length) {
513                 continue;
514             }
515             reg.showPanel(reg.getPanel(0));
516         }
517         
518         
519         
520         
521     },
522     
523     /**
524      * Returns the nested BorderLayout for this panel
525      * @return {Roo.BorderLayout} 
526      */
527     getLayout : function(){
528         return this.layout;
529     },
530     
531      /**
532      * Adds a xtype elements to the layout of the nested panel
533      * <pre><code>
534
535 panel.addxtype({
536        xtype : 'ContentPanel',
537        region: 'west',
538        items: [ .... ]
539    }
540 );
541
542 panel.addxtype({
543         xtype : 'NestedLayoutPanel',
544         region: 'west',
545         layout: {
546            center: { },
547            west: { }   
548         },
549         items : [ ... list of content panels or nested layout panels.. ]
550    }
551 );
552 </code></pre>
553      * @param {Object} cfg Xtype definition of item to add.
554      */
555     addxtype : function(cfg) {
556         return this.layout.addxtype(cfg);
557     
558     }
559 });
560
561 Roo.ScrollPanel = function(el, config, content){
562     config = config || {};
563     config.fitToFrame = true;
564     Roo.ScrollPanel.superclass.constructor.call(this, el, config, content);
565     
566     this.el.dom.style.overflow = "hidden";
567     var wrap = this.el.wrap({cls: "x-scroller x-layout-inactive-content"});
568     this.el.removeClass("x-layout-inactive-content");
569     this.el.on("mousewheel", this.onWheel, this);
570
571     var up = wrap.createChild({cls: "x-scroller-up", html: "&#160;"}, this.el.dom);
572     var down = wrap.createChild({cls: "x-scroller-down", html: "&#160;"});
573     up.unselectable(); down.unselectable();
574     up.on("click", this.scrollUp, this);
575     down.on("click", this.scrollDown, this);
576     up.addClassOnOver("x-scroller-btn-over");
577     down.addClassOnOver("x-scroller-btn-over");
578     up.addClassOnClick("x-scroller-btn-click");
579     down.addClassOnClick("x-scroller-btn-click");
580     this.adjustments = [0, -(up.getHeight() + down.getHeight())];
581
582     this.resizeEl = this.el;
583     this.el = wrap; this.up = up; this.down = down;
584 };
585
586 Roo.extend(Roo.ScrollPanel, Roo.ContentPanel, {
587     increment : 100,
588     wheelIncrement : 5,
589     scrollUp : function(){
590         this.resizeEl.scroll("up", this.increment, {callback: this.afterScroll, scope: this});
591     },
592
593     scrollDown : function(){
594         this.resizeEl.scroll("down", this.increment, {callback: this.afterScroll, scope: this});
595     },
596
597     afterScroll : function(){
598         var el = this.resizeEl;
599         var t = el.dom.scrollTop, h = el.dom.scrollHeight, ch = el.dom.clientHeight;
600         this.up[t == 0 ? "addClass" : "removeClass"]("x-scroller-btn-disabled");
601         this.down[h - t <= ch ? "addClass" : "removeClass"]("x-scroller-btn-disabled");
602     },
603
604     setSize : function(){
605         Roo.ScrollPanel.superclass.setSize.apply(this, arguments);
606         this.afterScroll();
607     },
608
609     onWheel : function(e){
610         var d = e.getWheelDelta();
611         this.resizeEl.dom.scrollTop -= (d*this.wheelIncrement);
612         this.afterScroll();
613         e.stopEvent();
614     },
615
616     setContent : function(content, loadScripts){
617         this.resizeEl.update(content, loadScripts);
618     }
619
620 });
621
622
623
624
625
626
627
628
629
630 /**
631  * @class Roo.TreePanel
632  * @extends Roo.ContentPanel
633  * @constructor
634  * Create a new TreePanel.
635  * @param {String/Object} config A string to set only the panel's title, or a config object
636  * @cfg {Roo.tree.TreePanel} tree The tree TreePanel, with config etc.
637  */
638 Roo.TreePanel = function(config){
639     var el = config.el;
640     var tree = config.tree;
641     delete config.tree; 
642     delete config.el; // hopefull!
643     Roo.TreePanel.superclass.constructor.call(this, el, config);
644     var treeEl = el.createChild();
645     this.tree = new Roo.tree.TreePanel(treeEl , tree);
646     //console.log(tree);
647     this.on('activate', function()
648     {
649         if (this.tree.rendered) {
650             return;
651         }
652         //console.log('render tree');
653         this.tree.render();
654     });
655     
656     this.on('resize',  function (cp, w, h) {
657             this.tree.innerCt.setWidth(w);
658             this.tree.innerCt.setHeight(h);
659             this.tree.innerCt.setStyle('overflow-y', 'auto');
660     });
661
662         
663     
664 };
665
666 Roo.extend(Roo.TreePanel, Roo.ContentPanel);
667
668
669
670
671
672
673
674
675
676
677