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