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