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