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