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){
45 if(el.autoCreate || el.xtype){ // xtype is available if this is called from factory
49 if (config && config.parentLayout) {
50 el = config.parentLayout.el.createChild();
53 if(el.autoCreate){ // xtype is available if this is called from factory
57 this.el = Roo.get(el);
58 if(!this.el && config && config.autoCreate){
59 if(typeof config.autoCreate == "object"){
60 if(!config.autoCreate.id){
61 config.autoCreate.id = config.id||el;
63 this.el = Roo.DomHelper.append(document.body,
64 config.autoCreate, true);
66 this.el = Roo.DomHelper.append(document.body,
67 {tag: "div", cls: "x-layout-inactive-content", id: config.id||el}, true);
72 this.closable = false;
75 if(typeof config == "string"){
78 Roo.apply(this, config);
81 if (this.toolbar && !this.toolbar.el && this.toolbar.xtype) {
82 this.wrapEl = this.el.wrap();
83 this.toolbar.container = this.el.insertSibling(false, 'before');
84 this.toolbar = new Roo.Toolbar(this.toolbar);
87 // xtype created footer. - not sure if will work as we normally have to render first..
88 if (this.footer && !this.footer.el && this.footer.xtype) {
90 this.wrapEl = this.el.wrap();
93 this.footer.container = this.wrapEl.createChild();
95 this.footer = Roo.factory(this.footer, Roo);
100 this.resizeEl = Roo.get(this.resizeEl, true);
102 this.resizeEl = this.el;
112 * Fires when this panel is activated.
113 * @param {Roo.ContentPanel} this
118 * Fires when this panel is activated.
119 * @param {Roo.ContentPanel} this
125 * Fires when this panel is resized if fitToFrame is true.
126 * @param {Roo.ContentPanel} this
127 * @param {Number} width The width after any component adjustments
128 * @param {Number} height The height after any component adjustments
134 * Fires when this tab is created
135 * @param {Roo.ContentPanel} this
146 this.resizeEl.setStyle("overflow", "auto");
148 // fix randome scrolling
149 this.el.on('scroll', function() {
150 Roo.log('fix random scolling');
151 this.scrollTo('top',0);
154 content = content || this.content;
156 this.setContent(content);
158 if(config && config.url){
159 this.setUrl(this.url, this.params, this.loadOnce);
164 Roo.ContentPanel.superclass.constructor.call(this);
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, '');
173 this.fireEvent('render', this);
176 Roo.extend(Roo.ContentPanel, Roo.util.Observable, {
178 setRegion : function(region){
179 this.region = region;
181 this.el.replaceClass("x-layout-inactive-content", "x-layout-active-content");
183 this.el.replaceClass("x-layout-active-content", "x-layout-inactive-content");
188 * Returns the toolbar for this Panel if one was configured.
189 * @return {Roo.Toolbar}
191 getToolbar : function(){
195 setActiveState : function(active){
196 this.active = active;
198 this.fireEvent("deactivate", this);
200 this.fireEvent("activate", this);
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
208 setContent : function(content, loadScripts){
209 this.el.update(content, loadScripts);
212 ignoreResize : function(w, h){
213 if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
216 this.lastSize = {width: w, height: h};
221 * Get the {@link Roo.UpdateManager} for this panel. Enables you to perform Ajax updates.
222 * @return {Roo.UpdateManager} The UpdateManager
224 getUpdateManager : function(){
225 return this.el.getUpdateManager();
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:
233 params: {param1: "foo", param2: "bar"}, // or a URL encoded string
234 callback: yourFunction,
235 scope: yourObject, //(optional scope)
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&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
251 var um = this.el.getUpdateManager();
252 um.update.apply(um, arguments);
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
264 setUrl : function(url, params, loadOnce){
265 if(this.refreshDelegate){
266 this.removeListener("activate", this.refreshDelegate);
268 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
269 this.on("activate", this.refreshDelegate);
270 return this.el.getUpdateManager();
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));
280 _setLoaded : function(){
285 * Returns this panel's id
293 * Returns this panel's element - used by regiosn to add.
294 * @return {Roo.Element}
297 return this.wrapEl || this.el;
300 adjustForComponents : function(width, height)
302 //Roo.log('adjustForComponents ');
303 if(this.resizeEl != this.el){
304 width -= this.el.getFrameWidth('lr');
305 height -= this.el.getFrameWidth('tb');
308 var te = this.toolbar.getEl();
309 height -= te.getHeight();
313 var te = this.footer.getEl();
314 //Roo.log("footer:" + te.getHeight());
316 height -= te.getHeight();
321 if(this.adjustments){
322 width += this.adjustments[0];
323 height += this.adjustments[1];
325 return {"width": width, "height": height};
328 setSize : function(width, height){
329 if(this.fitToFrame && !this.ignoreResize(width, height)){
330 if(this.fitContainer && this.resizeEl != this.el){
331 this.el.setSize(width, height);
333 var size = this.adjustForComponents(width, height);
334 this.resizeEl.setSize(this.autoWidth ? "auto" : size.width, this.autoHeight ? "auto" : size.height);
335 this.fireEvent('resize', this, size.width, size.height);
340 * Returns this panel's title
343 getTitle : function(){
348 * Set this panel's title
349 * @param {String} title
351 setTitle : function(title){
354 this.region.updatePanelTitle(this, title);
359 * Returns true is this panel was configured to be closable
362 isClosable : function(){
363 return this.closable;
366 beforeSlide : function(){
368 this.resizeEl.clip();
371 afterSlide : function(){
373 this.resizeEl.unclip();
377 * Force a content refresh from the URL specified in the {@link #setUrl} method.
378 * Will fail silently if the {@link #setUrl} method has not been called.
379 * This does not activate the panel, just updates its content.
381 refresh : function(){
382 if(this.refreshDelegate){
384 this.refreshDelegate();
389 * Destroys this panel
391 destroy : function(){
392 this.el.removeAllListeners();
393 var tempEl = document.createElement("span");
394 tempEl.appendChild(this.el.dom);
395 tempEl.innerHTML = "";
401 * form - if the content panel contains a form - this is a reference to it.
402 * @type {Roo.form.Form}
406 * view - if the content panel contains a view (Roo.DatePicker / Roo.View / Roo.JsonView)
407 * This contains a reference to it.
413 * Adds a xtype elements to the panel - currently only supports Forms, View, JsonView.
423 * @param {Object} cfg Xtype definition of item to add.
426 addxtype : function(cfg) {
427 if(cfg.xtype.match(/^UploadCropbox$/)) {
429 this.cropbox = new Roo.factory(cfg);
431 this.cropbox.render(this.el);
436 if (cfg.xtype.match(/^Form$/)) {
440 // el = this.footer.container.insertSibling(false, 'before');
442 el = this.el.createChild();
445 this.form = new Roo.form.Form(cfg);
448 if ( this.form.allItems.length) {
449 this.form.render(el.dom);
453 // should only have one of theses..
454 if ([ 'View', 'JsonView', 'DatePicker'].indexOf(cfg.xtype) > -1) {
455 // views.. should not be just added - used named prop 'view''
457 cfg.el = this.el.appendChild(document.createElement("div"));
460 var ret = new Roo.factory(cfg);
462 ret.render && ret.render(false, ''); // render blank..