4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
12 * @class Roo.ContentPanel
13 * @extends Roo.util.Observable
14 * @children Roo.form.Form Roo.JsonView Roo.View
15 * @parent Roo.BorderLayout Roo.LayoutDialog builder
16 * A basic ContentPanel element.
17 * @cfg {Boolean} fitToFrame True for this panel to adjust its size to fit when the region resizes (defaults to false)
18 * @cfg {Boolean} fitContainer When using {@link #fitToFrame} and {@link #resizeEl}, you can also fit the parent container (defaults to false)
19 * @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
20 * @cfg {Boolean} closable True if the panel can be closed/removed
21 * @cfg {Boolean} background True if the panel should not be activated when it is added (defaults to false)
22 * @cfg {String|HTMLElement|Element} resizeEl An element to resize if {@link #fitToFrame} is true (instead of this panel's element)
23 * @cfg {Roo.Toolbar} toolbar A toolbar for this panel
24 * @cfg {Boolean} autoScroll True to scroll overflow in this panel (use with {@link #fitToFrame})
25 * @cfg {String} title The title for this panel
26 * @cfg {Array} adjustments Values to <b>add</b> to the width/height when doing a {@link #fitToFrame} (default is [0, 0])
27 * @cfg {String} url Calls {@link #setUrl} with this value
28 * @cfg {String} region (center|north|south|east|west) [required] which region to put this panel on (when used with xtype constructors)
29 * @cfg {String|Object} params When used with {@link #url}, calls {@link #setUrl} with this value
30 * @cfg {Boolean} loadOnce When used with {@link #url}, calls {@link #setUrl} with this value
31 * @cfg {String} content Raw content to fill content panel with (uses setContent on construction.)
32 * @cfg {String} style Extra style to add to the content panel
33 * @cfg {Roo.menu.Menu} menu popup menu
36 * Create a new ContentPanel.
37 * @param {String/HTMLElement/Roo.Element} el The container element for this panel
38 * @param {String/Object} config A string to set only the title or a config object
39 * @param {String} content (optional) Set the HTML content for this panel
40 * @param {String} region (optional) Used by xtype constructors to add to regions. (values center,east,west,south,north)
42 Roo.ContentPanel = function(el, config, content){
46 if(el.autoCreate || el.xtype){ // xtype is available if this is called from factory
50 if (config && config.parentLayout) {
51 el = config.parentLayout.el.createChild();
54 if(el.autoCreate){ // xtype is available if this is called from factory
58 this.el = Roo.get(el);
59 if(!this.el && config && config.autoCreate){
60 if(typeof config.autoCreate == "object"){
61 if(!config.autoCreate.id){
62 config.autoCreate.id = config.id||el;
64 this.el = Roo.DomHelper.append(document.body,
65 config.autoCreate, true);
67 this.el = Roo.DomHelper.append(document.body,
68 {tag: "div", cls: "x-layout-inactive-content", id: config.id||el}, true);
73 this.closable = false;
76 if(typeof config == "string"){
79 Roo.apply(this, config);
82 if (this.toolbar && !this.toolbar.el && this.toolbar.xtype) {
83 this.wrapEl = this.el.wrap();
84 this.toolbar.container = this.el.insertSibling(false, 'before');
85 this.toolbar = new Roo.Toolbar(this.toolbar);
88 // xtype created footer. - not sure if will work as we normally have to render first..
89 if (this.footer && !this.footer.el && this.footer.xtype) {
91 this.wrapEl = this.el.wrap();
94 this.footer.container = this.wrapEl.createChild();
96 this.footer = Roo.factory(this.footer, Roo);
101 this.resizeEl = Roo.get(this.resizeEl, true);
103 this.resizeEl = this.el;
113 * Fires when this panel is activated.
114 * @param {Roo.ContentPanel} this
119 * Fires when this panel is activated.
120 * @param {Roo.ContentPanel} this
126 * Fires when this panel is resized if fitToFrame is true.
127 * @param {Roo.ContentPanel} this
128 * @param {Number} width The width after any component adjustments
129 * @param {Number} height The height after any component adjustments
135 * Fires when this tab is created
136 * @param {Roo.ContentPanel} this
147 this.resizeEl.setStyle("overflow", "auto");
149 // fix randome scrolling
150 this.el.on('scroll', function() {
151 Roo.log('fix random scolling');
152 this.scrollTo('top',0);
155 content = content || this.content;
157 this.setContent(content);
159 if(config && config.url){
160 this.setUrl(this.url, this.params, this.loadOnce);
165 Roo.ContentPanel.superclass.constructor.call(this);
167 if (this.view && typeof(this.view.xtype) != 'undefined') {
168 this.view.el = this.el.appendChild(document.createElement("div"));
169 this.view = Roo.factory(this.view);
170 this.view.render && this.view.render(false, '');
174 this.fireEvent('render', this);
177 Roo.extend(Roo.ContentPanel, Roo.util.Observable, {
179 setRegion : function(region){
180 this.region = region;
182 this.el.replaceClass("x-layout-inactive-content", "x-layout-active-content");
184 this.el.replaceClass("x-layout-active-content", "x-layout-inactive-content");
189 * Returns the toolbar for this Panel if one was configured.
190 * @return {Roo.Toolbar}
192 getToolbar : function(){
196 setActiveState : function(active){
197 this.active = active;
199 this.fireEvent("deactivate", this);
201 this.fireEvent("activate", this);
205 * Updates this panel's element
206 * @param {String} content The new content
207 * @param {Boolean} loadScripts (optional) true to look for and process scripts
209 setContent : function(content, loadScripts){
210 this.el.update(content, loadScripts);
213 ignoreResize : function(w, h){
214 if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
217 this.lastSize = {width: w, height: h};
222 * Get the {@link Roo.UpdateManager} for this panel. Enables you to perform Ajax updates.
223 * @return {Roo.UpdateManager} The UpdateManager
225 getUpdateManager : function(){
226 return this.el.getUpdateManager();
229 * Loads this content panel immediately with content from XHR. Note: to delay loading until the panel is activated, use {@link #setUrl}.
230 * @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:
234 params: {param1: "foo", param2: "bar"}, // or a URL encoded string
235 callback: yourFunction,
236 scope: yourObject, //(optional scope)
244 * The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
245 * 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.
246 * @param {String/Object} params (optional) The parameters to pass as either a URL encoded string "param1=1&param2=2" or an object {param1: 1, param2: 2}
247 * @param {Function} callback (optional) Callback when transaction is complete -- called with signature (oElement, bSuccess, oResponse)
248 * @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.
249 * @return {Roo.ContentPanel} this
252 var um = this.el.getUpdateManager();
253 um.update.apply(um, arguments);
259 * 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.
260 * @param {String/Function} url The URL to load the content from or a function to call to get the URL
261 * @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)
262 * @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)
263 * @return {Roo.UpdateManager} The UpdateManager
265 setUrl : function(url, params, loadOnce){
266 if(this.refreshDelegate){
267 this.removeListener("activate", this.refreshDelegate);
269 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
270 this.on("activate", this.refreshDelegate);
271 return this.el.getUpdateManager();
274 _handleRefresh : function(url, params, loadOnce){
275 if(!loadOnce || !this.loaded){
276 var updater = this.el.getUpdateManager();
277 updater.update(url, params, this._setLoaded.createDelegate(this));
281 _setLoaded : function(){
286 * Returns this panel's id
294 * Returns this panel's element - used by regiosn to add.
295 * @return {Roo.Element}
298 return this.wrapEl || this.el;
301 adjustForComponents : function(width, height)
303 //Roo.log('adjustForComponents ');
304 if(this.resizeEl != this.el){
305 width -= this.el.getFrameWidth('lr');
306 height -= this.el.getFrameWidth('tb');
309 var te = this.toolbar.getEl();
310 height -= te.getHeight();
314 var te = this.footer.getEl();
315 //Roo.log("footer:" + te.getHeight());
317 height -= te.getHeight();
322 if(this.adjustments){
323 width += this.adjustments[0];
324 height += this.adjustments[1];
326 return {"width": width, "height": height};
329 setSize : function(width, height){
330 if(this.fitToFrame && !this.ignoreResize(width, height)){
331 if(this.fitContainer && this.resizeEl != this.el){
332 this.el.setSize(width, height);
334 var size = this.adjustForComponents(width, height);
335 this.resizeEl.setSize(this.autoWidth ? "auto" : size.width, this.autoHeight ? "auto" : size.height);
336 this.fireEvent('resize', this, size.width, size.height);
341 * Returns this panel's title
344 getTitle : function(){
349 * Set this panel's title
350 * @param {String} title
352 setTitle : function(title){
355 this.region.updatePanelTitle(this, title);
360 * Returns true is this panel was configured to be closable
363 isClosable : function(){
364 return this.closable;
367 beforeSlide : function(){
369 this.resizeEl.clip();
372 afterSlide : function(){
374 this.resizeEl.unclip();
378 * Force a content refresh from the URL specified in the {@link #setUrl} method.
379 * Will fail silently if the {@link #setUrl} method has not been called.
380 * This does not activate the panel, just updates its content.
382 refresh : function(){
383 if(this.refreshDelegate){
385 this.refreshDelegate();
390 * Destroys this panel
392 destroy : function(){
393 this.el.removeAllListeners();
394 var tempEl = document.createElement("span");
395 tempEl.appendChild(this.el.dom);
396 tempEl.innerHTML = "";
402 * form - if the content panel contains a form - this is a reference to it.
403 * @type {Roo.form.Form}
407 * view - if the content panel contains a view (Roo.DatePicker / Roo.View / Roo.JsonView)
408 * This contains a reference to it.
414 * Adds a xtype elements to the panel - currently only supports Forms, View, JsonView.
424 * @param {Object} cfg Xtype definition of item to add.
427 addxtype : function(cfg) {
429 if (cfg.xtype.match(/^Form$/)) {
433 // el = this.footer.container.insertSibling(false, 'before');
435 el = this.el.createChild();
438 this.form = new Roo.form.Form(cfg);
441 if ( this.form.allItems.length) {
442 this.form.render(el.dom);
446 // should only have one of theses..
447 if ([ 'View', 'JsonView', 'DatePicker'].indexOf(cfg.xtype) > -1) {
448 // views.. should not be just added - used named prop 'view''
450 cfg.el = this.el.appendChild(document.createElement("div"));
453 var ret = new Roo.factory(cfg);
455 ret.render && ret.render(false, ''); // render blank..