Roo/bootstrap/panel/Content.js
[roojs1] / Roo / bootstrap / panel / Content.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.bootstrap.panel.Content = function( config){
39     
40     var el = config.el;
41     var content = config.content;
42
43     if(config.autoCreate){ // xtype is available if this is called from factory
44         el = Roo.id();
45     }
46     this.el = Roo.get(el);
47     if(!this.el && config && config.autoCreate){
48         if(typeof config.autoCreate == "object"){
49             if(!config.autoCreate.id){
50                 config.autoCreate.id = config.id||el;
51             }
52             this.el = Roo.DomHelper.append(document.body,
53                         config.autoCreate, true);
54         }else{
55             var elcfg =  {   tag: "div",
56                             cls: "roo-layout-inactive-content",
57                             id: config.id||el
58                             };
59             if (config.html) {
60                 elcfg.html = config.html;
61                 
62             }
63                         
64             this.el = Roo.DomHelper.append(document.body, elcfg , true);
65         }
66     } 
67     this.closable = false;
68     this.loaded = false;
69     this.active = false;
70    
71       
72     if (config.toolbar && !config.toolbar.el && config.toolbar.xtype) {
73         
74         this.toolbar = new config.toolbar.xns[config.toolbar.xtype](config.toolbar);
75         
76         this.wrapEl = this.el.wrap();
77         var ti = [];
78         if (config.toolbar.items) {
79             ti = config.toolbar.items ;
80             delete config.toolbar.items ;
81         }
82         
83         var nitems = [];
84         this.toolbar.render(this.wrapEl, 'before');
85         for(var i =0;i < ti.length;i++) {
86           //  Roo.log(['add child', items[i]]);
87             nitems.push(this.toolbar.addxtype(Roo.apply({}, ti[i])));
88         }
89         this.toolbar.items = nitems;
90         this.toolbar.el.insertBefore(this.wrapEl.dom.firstChild);
91         delete config.toolbar;
92         
93     }
94     /*
95     // xtype created footer. - not sure if will work as we normally have to render first..
96     if (this.footer && !this.footer.el && this.footer.xtype) {
97         if (!this.wrapEl) {
98             this.wrapEl = this.el.wrap();
99         }
100     
101         this.footer.container = this.wrapEl.createChild();
102          
103         this.footer = Roo.factory(this.footer, Roo);
104         
105     }
106     */
107     
108      if(typeof config == "string"){
109         this.title = config;
110     }else{
111         Roo.apply(this, config);
112     }
113     
114     if(this.resizeEl){
115         this.resizeEl = Roo.get(this.resizeEl, true);
116     }else{
117         this.resizeEl = this.el;
118     }
119     // handle view.xtype
120     
121  
122     
123     
124     this.addEvents({
125         /**
126          * @event activate
127          * Fires when this panel is activated. 
128          * @param {Roo.ContentPanel} this
129          */
130         "activate" : true,
131         /**
132          * @event deactivate
133          * Fires when this panel is activated. 
134          * @param {Roo.ContentPanel} this
135          */
136         "deactivate" : true,
137
138         /**
139          * @event resize
140          * Fires when this panel is resized if fitToFrame is true.
141          * @param {Roo.ContentPanel} this
142          * @param {Number} width The width after any component adjustments
143          * @param {Number} height The height after any component adjustments
144          */
145         "resize" : true,
146         
147          /**
148          * @event render
149          * Fires when this tab is created
150          * @param {Roo.ContentPanel} this
151          */
152         "render" : true
153         
154         
155         
156     });
157     
158
159     
160     
161     if(this.autoScroll){
162         this.resizeEl.setStyle("overflow", "auto");
163     } else {
164         // fix randome scrolling
165         //this.el.on('scroll', function() {
166         //    Roo.log('fix random scolling');
167         //    this.scrollTo('top',0); 
168         //});
169     }
170     content = content || this.content;
171     if(content){
172         this.setContent(content);
173     }
174     if(config && config.url){
175         this.setUrl(this.url, this.params, this.loadOnce);
176     }
177     
178     
179     
180     Roo.bootstrap.panel.Content.superclass.constructor.call(this);
181     
182     if (this.view && typeof(this.view.xtype) != 'undefined') {
183         this.view.el = this.el.appendChild(document.createElement("div"));
184         this.view = Roo.factory(this.view); 
185         this.view.render  &&  this.view.render(false, '');  
186     }
187     
188     
189     this.fireEvent('render', this);
190 };
191
192 Roo.extend(Roo.bootstrap.panel.Content, Roo.bootstrap.Component, {
193     tabTip:'',
194     setRegion : function(region){
195         this.region = region;
196         if(region && !this.background){
197            this.el.replaceClass("roo-layout-inactive-content", "roo-layout-active-content");
198            this.el.setStyle('relative');
199         }else{
200            this.el.replaceClass("roo-layout-active-content", "roo-layout-inactive-content");
201            this.el.setStyle('absolute');
202         } 
203     },
204     
205     /**
206      * Returns the toolbar for this Panel if one was configured. 
207      * @return {Roo.Toolbar} 
208      */
209     getToolbar : function(){
210         return this.toolbar;
211     },
212     
213     setActiveState : function(active){
214         this.active = active;
215         if(!active){
216             this.fireEvent("deactivate", this);
217         }else{
218             this.fireEvent("activate", this);
219         }
220     },
221     /**
222      * Updates this panel's element
223      * @param {String} content The new content
224      * @param {Boolean} loadScripts (optional) true to look for and process scripts
225     */
226     setContent : function(content, loadScripts){
227         this.el.update(content, loadScripts);
228     },
229
230     ignoreResize : function(w, h){
231         if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
232             return true;
233         }else{
234             this.lastSize = {width: w, height: h};
235             return false;
236         }
237     },
238     /**
239      * Get the {@link Roo.UpdateManager} for this panel. Enables you to perform Ajax updates.
240      * @return {Roo.UpdateManager} The UpdateManager
241      */
242     getUpdateManager : function(){
243         return this.el.getUpdateManager();
244     },
245      /**
246      * Loads this content panel immediately with content from XHR. Note: to delay loading until the panel is activated, use {@link #setUrl}.
247      * @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:
248 <pre><code>
249 panel.load({
250     url: "your-url.php",
251     params: {param1: "foo", param2: "bar"}, // or a URL encoded string
252     callback: yourFunction,
253     scope: yourObject, //(optional scope)
254     discardUrl: false,
255     nocache: false,
256     text: "Loading...",
257     timeout: 30,
258     scripts: false
259 });
260 </code></pre>
261      * The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
262      * 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.
263      * @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}
264      * @param {Function} callback (optional) Callback when transaction is complete -- called with signature (oElement, bSuccess, oResponse)
265      * @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.
266      * @return {Roo.ContentPanel} this
267      */
268     load : function(){
269         var um = this.el.getUpdateManager();
270         um.update.apply(um, arguments);
271         return this;
272     },
273
274
275     /**
276      * 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.
277      * @param {String/Function} url The URL to load the content from or a function to call to get the URL
278      * @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)
279      * @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)
280      * @return {Roo.UpdateManager} The UpdateManager
281      */
282     setUrl : function(url, params, loadOnce){
283         if(this.refreshDelegate){
284             this.removeListener("activate", this.refreshDelegate);
285         }
286         this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
287         this.on("activate", this.refreshDelegate);
288         return this.el.getUpdateManager();
289     },
290     
291     _handleRefresh : function(url, params, loadOnce){
292         if(!loadOnce || !this.loaded){
293             var updater = this.el.getUpdateManager();
294             updater.update(url, params, this._setLoaded.createDelegate(this));
295         }
296     },
297     
298     _setLoaded : function(){
299         this.loaded = true;
300     }, 
301     
302     /**
303      * Returns this panel's id
304      * @return {String} 
305      */
306     getId : function(){
307         return this.el.id;
308     },
309     
310     /** 
311      * Returns this panel's element - used by regiosn to add.
312      * @return {Roo.Element} 
313      */
314     getEl : function(){
315         return this.wrapEl || this.el;
316     },
317     
318    
319     
320     adjustForComponents : function(width, height)
321     {
322         //Roo.log('adjustForComponents ');
323         if(this.resizeEl != this.el){
324             width -= this.el.getFrameWidth('lr');
325             height -= this.el.getFrameWidth('tb');
326         }
327         if(this.toolbar){
328             var te = this.toolbar.getEl();
329             height -= te.getHeight();
330             te.setWidth(width);
331         }
332         if(this.footer){
333             var te = this.footer.getEl();
334             Roo.log("footer:" + te.getHeight());
335             
336             height -= te.getHeight();
337             te.setWidth(width);
338         }
339         
340         
341         if(this.adjustments){
342             width += this.adjustments[0];
343             height += this.adjustments[1];
344         }
345         return {"width": width, "height": height};
346     },
347     
348     setSize : function(width, height){
349         if(this.fitToFrame && !this.ignoreResize(width, height)){
350             if(this.fitContainer && this.resizeEl != this.el){
351                 this.el.setSize(width, height);
352             }
353             var size = this.adjustForComponents(width, height);
354             this.resizeEl.setSize(this.autoWidth ? "auto" : size.width, this.autoHeight ? "auto" : size.height);
355             this.fireEvent('resize', this, size.width, size.height);
356         }
357     },
358     
359     /**
360      * Returns this panel's title
361      * @return {String} 
362      */
363     getTitle : function(){
364         return this.title;
365     },
366     
367     /**
368      * Set this panel's title
369      * @param {String} title
370      */
371     setTitle : function(title){
372         this.title = title;
373         if(this.region){
374             this.region.updatePanelTitle(this, title);
375         }
376     },
377     
378     /**
379      * Returns true is this panel was configured to be closable
380      * @return {Boolean} 
381      */
382     isClosable : function(){
383         return this.closable;
384     },
385     
386     beforeSlide : function(){
387         this.el.clip();
388         this.resizeEl.clip();
389     },
390     
391     afterSlide : function(){
392         this.el.unclip();
393         this.resizeEl.unclip();
394     },
395     
396     /**
397      *   Force a content refresh from the URL specified in the {@link #setUrl} method.
398      *   Will fail silently if the {@link #setUrl} method has not been called.
399      *   This does not activate the panel, just updates its content.
400      */
401     refresh : function(){
402         if(this.refreshDelegate){
403            this.loaded = false;
404            this.refreshDelegate();
405         }
406     },
407     
408     /**
409      * Destroys this panel
410      */
411     destroy : function(){
412         this.el.removeAllListeners();
413         var tempEl = document.createElement("span");
414         tempEl.appendChild(this.el.dom);
415         tempEl.innerHTML = "";
416         this.el.remove();
417         this.el = null;
418     },
419     
420     /**
421      * form - if the content panel contains a form - this is a reference to it.
422      * @type {Roo.form.Form}
423      */
424     form : false,
425     /**
426      * view - if the content panel contains a view (Roo.DatePicker / Roo.View / Roo.JsonView)
427      *    This contains a reference to it.
428      * @type {Roo.View}
429      */
430     view : false,
431     
432       /**
433      * Adds a xtype elements to the panel - currently only supports Forms, View, JsonView.
434      * <pre><code>
435
436 layout.addxtype({
437        xtype : 'Form',
438        items: [ .... ]
439    }
440 );
441
442 </code></pre>
443      * @param {Object} cfg Xtype definition of item to add.
444      */
445     
446     
447     getChildContainer: function () {
448         return this.getEl();
449     }
450     
451     
452     /*
453         var  ret = new Roo.factory(cfg);
454         return ret;
455         
456         
457         // add form..
458         if (cfg.xtype.match(/^Form$/)) {
459             
460             var el;
461             //if (this.footer) {
462             //    el = this.footer.container.insertSibling(false, 'before');
463             //} else {
464                 el = this.el.createChild();
465             //}
466
467             this.form = new  Roo.form.Form(cfg);
468             
469             
470             if ( this.form.allItems.length) {
471                 this.form.render(el.dom);
472             }
473             return this.form;
474         }
475         // should only have one of theses..
476         if ([ 'View', 'JsonView', 'DatePicker'].indexOf(cfg.xtype) > -1) {
477             // views.. should not be just added - used named prop 'view''
478             
479             cfg.el = this.el.appendChild(document.createElement("div"));
480             // factory?
481             
482             var ret = new Roo.factory(cfg);
483              
484              ret.render && ret.render(false, ''); // render blank..
485             this.view = ret;
486             return ret;
487         }
488         return false;
489     }
490     \*/
491 });
492