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