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