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