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