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