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