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 * 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 * @cfg {Boolean} badges render the badges
33 * Create a new ContentPanel.
34 * @param {String/HTMLElement/Roo.Element} el The container element for this panel
35 * @param {String/Object} config A string to set only the title or a config object
36 * @param {String} content (optional) Set the HTML content for this panel
37 * @param {String} region (optional) Used by xtype constructors to add to regions. (values center,east,west,south,north)
39 Roo.bootstrap.panel.Content = function( config){
41 this.tpl = config.tpl || false;
44 var content = config.content;
46 if(config.autoCreate){ // xtype is available if this is called from factory
49 this.el = Roo.get(el);
50 if(!this.el && config && config.autoCreate){
51 if(typeof config.autoCreate == "object"){
52 if(!config.autoCreate.id){
53 config.autoCreate.id = config.id||el;
55 this.el = Roo.DomHelper.append(document.body,
56 config.autoCreate, true);
58 var elcfg = { tag: "div",
59 cls: "roo-layout-inactive-content",
63 elcfg.html = config.html;
67 this.el = Roo.DomHelper.append(document.body, elcfg , true);
70 this.closable = false;
75 if (config.toolbar && !config.toolbar.el && config.toolbar.xtype) {
77 this.toolbar = new config.toolbar.xns[config.toolbar.xtype](config.toolbar);
79 this.wrapEl = this.el; //this.el.wrap();
81 if (config.toolbar.items) {
82 ti = config.toolbar.items ;
83 delete config.toolbar.items ;
87 this.toolbar.render(this.wrapEl, 'before');
88 for(var i =0;i < ti.length;i++) {
89 // Roo.log(['add child', items[i]]);
90 nitems.push(this.toolbar.addxtype(Roo.apply({}, ti[i])));
92 this.toolbar.items = nitems;
93 this.toolbar.el.insertBefore(this.wrapEl.dom.firstChild);
94 delete config.toolbar;
98 // xtype created footer. - not sure if will work as we normally have to render first..
99 if (this.footer && !this.footer.el && this.footer.xtype) {
101 this.wrapEl = this.el.wrap();
104 this.footer.container = this.wrapEl.createChild();
106 this.footer = Roo.factory(this.footer, Roo);
111 if(typeof config == "string"){
114 Roo.apply(this, config);
118 this.resizeEl = Roo.get(this.resizeEl, true);
120 this.resizeEl = this.el;
130 * Fires when this panel is activated.
131 * @param {Roo.ContentPanel} this
136 * Fires when this panel is activated.
137 * @param {Roo.ContentPanel} this
143 * Fires when this panel is resized if fitToFrame is true.
144 * @param {Roo.ContentPanel} this
145 * @param {Number} width The width after any component adjustments
146 * @param {Number} height The height after any component adjustments
152 * Fires when this tab is created
153 * @param {Roo.ContentPanel} this
165 this.resizeEl.setStyle("overflow", "auto");
167 // fix randome scrolling
168 //this.el.on('scroll', function() {
169 // Roo.log('fix random scolling');
170 // this.scrollTo('top',0);
173 content = content || this.content;
175 this.setContent(content);
177 if(config && config.url){
178 this.setUrl(this.url, this.params, this.loadOnce);
183 Roo.bootstrap.panel.Content.superclass.constructor.call(this);
185 if (this.view && typeof(this.view.xtype) != 'undefined') {
186 this.view.el = this.el.appendChild(document.createElement("div"));
187 this.view = Roo.factory(this.view);
188 this.view.render && this.view.render(false, '');
192 this.fireEvent('render', this);
195 Roo.extend(Roo.bootstrap.panel.Content, Roo.bootstrap.Component, {
199 setRegion : function(region){
200 this.region = region;
201 this.setActiveClass(region && !this.background);
205 setActiveClass: function(state)
208 this.el.replaceClass("roo-layout-inactive-content", "roo-layout-active-content");
209 this.el.setStyle('position','relative');
211 this.el.replaceClass("roo-layout-active-content", "roo-layout-inactive-content");
212 this.el.setStyle('position', 'absolute');
217 * Returns the toolbar for this Panel if one was configured.
218 * @return {Roo.Toolbar}
220 getToolbar : function(){
224 setActiveState : function(active)
226 this.active = active;
227 this.setActiveClass(active);
229 if(this.fireEvent("deactivate", this) === false){
234 this.fireEvent("activate", this);
238 * Updates this panel's element
239 * @param {String} content The new content
240 * @param {Boolean} loadScripts (optional) true to look for and process scripts
242 setContent : function(content, loadScripts){
243 this.el.update(content, loadScripts);
246 ignoreResize : function(w, h){
247 if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
250 this.lastSize = {width: w, height: h};
255 * Get the {@link Roo.UpdateManager} for this panel. Enables you to perform Ajax updates.
256 * @return {Roo.UpdateManager} The UpdateManager
258 getUpdateManager : function(){
259 return this.el.getUpdateManager();
262 * Loads this content panel immediately with content from XHR. Note: to delay loading until the panel is activated, use {@link #setUrl}.
263 * @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:
267 params: {param1: "foo", param2: "bar"}, // or a URL encoded string
268 callback: yourFunction,
269 scope: yourObject, //(optional scope)
277 * The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
278 * 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.
279 * @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}
280 * @param {Function} callback (optional) Callback when transaction is complete -- called with signature (oElement, bSuccess, oResponse)
281 * @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.
282 * @return {Roo.ContentPanel} this
285 var um = this.el.getUpdateManager();
286 um.update.apply(um, arguments);
292 * 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.
293 * @param {String/Function} url The URL to load the content from or a function to call to get the URL
294 * @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)
295 * @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)
296 * @return {Roo.UpdateManager} The UpdateManager
298 setUrl : function(url, params, loadOnce){
299 if(this.refreshDelegate){
300 this.removeListener("activate", this.refreshDelegate);
302 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
303 this.on("activate", this.refreshDelegate);
304 return this.el.getUpdateManager();
307 _handleRefresh : function(url, params, loadOnce){
308 if(!loadOnce || !this.loaded){
309 var updater = this.el.getUpdateManager();
310 updater.update(url, params, this._setLoaded.createDelegate(this));
314 _setLoaded : function(){
319 * Returns this panel's id
327 * Returns this panel's element - used by regiosn to add.
328 * @return {Roo.Element}
331 return this.wrapEl || this.el;
336 adjustForComponents : function(width, height)
338 //Roo.log('adjustForComponents ');
339 if(this.resizeEl != this.el){
340 width -= this.el.getFrameWidth('lr');
341 height -= this.el.getFrameWidth('tb');
344 var te = this.toolbar.getEl();
346 height -= te.getHeight();
349 var te = this.footer.getEl();
351 height -= te.getHeight();
355 if(this.adjustments){
356 width += this.adjustments[0];
357 height += this.adjustments[1];
359 return {"width": width, "height": height};
362 setSize : function(width, height){
363 if(this.fitToFrame && !this.ignoreResize(width, height)){
364 if(this.fitContainer && this.resizeEl != this.el){
365 this.el.setSize(width, height);
367 var size = this.adjustForComponents(width, height);
368 this.resizeEl.setSize(this.autoWidth ? "auto" : size.width, this.autoHeight ? "auto" : size.height);
369 this.fireEvent('resize', this, size.width, size.height);
374 * Returns this panel's title
377 getTitle : function(){
379 if (typeof(this.title) != 'object') {
384 for (var k in this.title) {
385 if (!this.title.hasOwnProperty(k)) {
389 if (k.indexOf('-') >= 0) {
390 var s = k.split('-');
391 for (var i = 0; i<s.length; i++) {
392 t += "<span class='visible-"+s[i]+"'>"+this.title[k]+"</span>";
395 t += "<span class='visible-"+k+"'>"+this.title[k]+"</span>";
402 * Set this panel's title
403 * @param {String} title
405 setTitle : function(title){
408 this.region.updatePanelTitle(this, title);
413 * Returns true is this panel was configured to be closable
416 isClosable : function(){
417 return this.closable;
420 beforeSlide : function(){
422 this.resizeEl.clip();
425 afterSlide : function(){
427 this.resizeEl.unclip();
431 * Force a content refresh from the URL specified in the {@link #setUrl} method.
432 * Will fail silently if the {@link #setUrl} method has not been called.
433 * This does not activate the panel, just updates its content.
435 refresh : function(){
436 if(this.refreshDelegate){
438 this.refreshDelegate();
443 * Destroys this panel
445 destroy : function(){
446 this.el.removeAllListeners();
447 var tempEl = document.createElement("span");
448 tempEl.appendChild(this.el.dom);
449 tempEl.innerHTML = "";
455 * form - if the content panel contains a form - this is a reference to it.
456 * @type {Roo.form.Form}
460 * view - if the content panel contains a view (Roo.DatePicker / Roo.View / Roo.JsonView)
461 * This contains a reference to it.
467 * Adds a xtype elements to the panel - currently only supports Forms, View, JsonView.
477 * @param {Object} cfg Xtype definition of item to add.
481 getChildContainer: function () {
487 var ret = new Roo.factory(cfg);
492 if (cfg.xtype.match(/^Form$/)) {
496 // el = this.footer.container.insertSibling(false, 'before');
498 el = this.el.createChild();
501 this.form = new Roo.form.Form(cfg);
504 if ( this.form.allItems.length) {
505 this.form.render(el.dom);
509 // should only have one of theses..
510 if ([ 'View', 'JsonView', 'DatePicker'].indexOf(cfg.xtype) > -1) {
511 // views.. should not be just added - used named prop 'view''
513 cfg.el = this.el.appendChild(document.createElement("div"));
516 var ret = new Roo.factory(cfg);
518 ret.render && ret.render(false, ''); // render blank..