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