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