2 * Original code for Roojs - LGPL
3 * <script type="text/javascript">
7 * @class Roo.XComponent
8 * A delayed Element creator...
9 * Or a way to group chunks of interface together.
11 * Mypart.xyx = new Roo.XComponent({
13 parent : 'Mypart.xyz', // empty == document.element.!!
17 disabled : function() {}
19 tree : function() { // return an tree of xtype declared components
23 xtype : 'NestedLayoutPanel',
30 * It can be used to build a big heiracy, with parent etc.
31 * or you can just use this to render a single compoent to a dom element
32 * MYPART.render(Roo.Element | String(id) | dom_element )
34 * @extends Roo.util.Observable
36 * @param cfg {Object} configuration of component
39 Roo.XComponent = function(cfg) {
44 * Fires when this the componnt is built
45 * @param {Roo.XComponent} c the component
50 this.region = this.region || 'center'; // default..
51 Roo.XComponent.register(this);
53 this.el = false; // where the layout goes..
57 Roo.extend(Roo.XComponent, Roo.util.Observable, {
60 * The created element (with Roo.factory())
67 * for BC - use el in new code
74 * for BC - use el in new code
80 * @cfg {Function|boolean} disabled
81 * If this module is disabled by some rule, return true from the funtion
86 * @cfg {String} parent
87 * Name of parent element which it get xtype added to..
93 * Used to set the order in which elements are created (usefull for multiple tabs)
99 * String to display while loading.
103 * @cfg {String} region
104 * Region to render component to (defaults to center)
110 * A single item array - the first element is the root of the tree..
111 * It's done this way to stay compatible with the Xtype system...
117 * The method that retuns the tree of parts that make up this compoennt
124 * render element to dom or tree
125 * @param {Roo.Element|String|DomElement} optional render to if parent is not set.
128 render : function(el)
132 var hp = this.parent ? 1 : 0;
134 if (!el && typeof(this.parent) == 'string' && this.parent.substring(0,1) == '#') {
135 // if parent is a '#.....' string, then let's use that..
136 var ename = this.parent.substr(1)
137 this.parent = (this.parent == '#bootstrap') ? { el : true} : false; // flags it as a top module...
139 if (!el && !this.parent) {
140 Roo.log("Warning - element can not be found :#" + ename );
144 var tree = this._tree ? this._tree() : this.tree();
147 if (!this.parent && ) {
153 Roo.log("no parent - creating one");
155 el = el ? Roo.get(el) : false;
157 // it's a top level one..
159 el : new Roo.BorderLayout(el || document.body, {
167 alwaysShowTabs: el && hp? false : true,
168 hideTabs: el || !hp ? true : false,
175 if (!this.parent.el) {
176 // probably an old style ctor, which has been disabled.
180 // The 'tree' method is '_tree now'
182 tree.region = tree.region || this.region;
184 if (this.parent.el === true) {
185 // bootstrap... - body..
186 this.parent.el = Roo.factory(tree);
189 this.el = this.parent.el.addxtype(tree);
190 this.fireEvent('built', this);
192 this.panel = this.el;
193 this.layout = this.panel.layout;
194 this.parentLayout = this.parent.layout || false;
200 Roo.apply(Roo.XComponent, {
202 * @property hideProgress
203 * true to disable the building progress bar.. usefull on single page renders.
206 hideProgress : false,
208 * @property buildCompleted
209 * True when the builder has completed building the interface.
212 buildCompleted : false,
215 * @property topModule
216 * the upper most module - uses document.element as it's constructor.
224 * array of modules to be created by registration system.
225 * @type {Array} of Roo.XComponent
230 * @property elmodules
231 * array of modules to be created by which use #ID
232 * @type {Array} of Roo.XComponent
238 * @property build_from_html
239 * Build elements from html - used by bootstrap HTML stuff
240 * - this is cleared after build is completed
241 * @type {boolean} true (default false)
244 build_from_html : false,
247 * Register components to be built later.
249 * This solves the following issues
250 * - Building is not done on page load, but after an authentication process has occured.
251 * - Interface elements are registered on page load
252 * - Parent Interface elements may not be loaded before child, so this handles that..
259 module : 'Pman.Tab.projectMgr',
261 parent : 'Pman.layout',
262 disabled : false, // or use a function..
265 * * @param {Object} details about module
267 register : function(obj) {
269 Roo.XComponent.event.fireEvent('register', obj);
270 switch(typeof(obj.disabled) ) {
276 if ( obj.disabled() ) {
288 this.modules.push(obj);
292 * convert a string to an object..
293 * eg. 'AAA.BBB' -> finds AAA.BBB
297 toObject : function(str)
299 if (!str || typeof(str) == 'object') {
302 if (str.substring(0,1) == '#') {
306 var ar = str.split('.');
311 eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
313 throw "Module not found : " + str;
317 throw "Module not found : " + str;
319 Roo.each(ar, function(e) {
320 if (typeof(o[e]) == 'undefined') {
321 throw "Module not found : " + str;
332 * move modules into their correct place in the tree..
335 preBuild : function ()
338 Roo.each(this.modules , function (obj)
340 Roo.XComponent.event.fireEvent('beforebuild', obj);
342 var opar = obj.parent;
344 obj.parent = this.toObject(opar);
346 Roo.log("parent:toObject failed: " + e.toString());
351 Roo.debug && Roo.log("GOT top level module");
352 Roo.debug && Roo.log(obj);
353 obj.modules = new Roo.util.MixedCollection(false,
354 function(o) { return o.order + '' }
356 this.topModule = obj;
359 // parent is a string (usually a dom element name..)
360 if (typeof(obj.parent) == 'string') {
361 this.elmodules.push(obj);
364 if (obj.parent.constructor != Roo.XComponent) {
365 Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
367 if (!obj.parent.modules) {
368 obj.parent.modules = new Roo.util.MixedCollection(false,
369 function(o) { return o.order + '' }
372 if (obj.parent.disabled) {
375 obj.parent.modules.add(obj);
380 * make a list of modules to build.
381 * @return {Array} list of modules.
384 buildOrder : function()
387 var cmp = function(a,b) {
388 return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
390 if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
391 throw "No top level modules to build";
394 // make a flat list in order of modules to build.
395 var mods = this.topModule ? [ this.topModule ] : [];
398 // elmodules (is a list of DOM based modules )
399 Roo.each(this.elmodules, function(e) {
401 if (!this.topModule &&
402 typeof(e.parent) == 'string' &&
403 e.parent.substring(0,1) == '#' &&
404 Roo.get(e.parent.substr(1))
413 // add modules to their parents..
414 var addMod = function(m) {
415 Roo.debug && Roo.log("build Order: add: " + m.name);
418 if (m.modules && !m.disabled) {
419 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
420 m.modules.keySort('ASC', cmp );
421 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
423 m.modules.each(addMod);
425 Roo.debug && Roo.log("build Order: no child modules");
427 // not sure if this is used any more..
429 m.finalize.name = m.name + " (clean up) ";
430 mods.push(m.finalize);
434 if (this.topModule && this.topModule.modules) {
435 this.topModule.modules.keySort('ASC', cmp );
436 this.topModule.modules.each(addMod);
442 * Build the registered modules.
443 * @param {Object} parent element.
444 * @param {Function} optional method to call after module has been added.
448 build : function(opts)
451 if (typeof(opts) != 'undefined') {
452 Roo.apply(this,opts);
456 var mods = this.buildOrder();
458 //this.allmods = mods;
459 //Roo.debug && Roo.log(mods);
461 if (!mods.length) { // should not happen
462 throw "NO modules!!!";
466 var msg = "Building Interface...";
467 // flash it up as modal - so we store the mask!?
468 if (!this.hideProgress && Roo.MessageBox) {
469 Roo.MessageBox.show({ title: 'loading' });
470 Roo.MessageBox.show({
471 title: "Please wait...",
480 var total = mods.length;
483 var progressRun = function() {
485 Roo.debug && Roo.log('hide?');
486 if (!this.hideProgress && Roo.MessageBox) {
487 Roo.MessageBox.hide();
489 Roo.XComponent.build_from_html = false; // reset, so dialogs will be build from javascript
491 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
497 var m = mods.shift();
500 Roo.debug && Roo.log(m);
501 // not sure if this is supported any more.. - modules that are are just function
502 if (typeof(m) == 'function') {
504 return progressRun.defer(10, _this);
508 msg = "Building Interface " + (total - mods.length) +
510 (m.name ? (' - ' + m.name) : '');
511 Roo.debug && Roo.log(msg);
512 if (!this.hideProgress && Roo.MessageBox) {
513 Roo.MessageBox.updateProgress( (total - mods.length)/total, msg );
517 // is the module disabled?
518 var disabled = (typeof(m.disabled) == 'function') ?
519 m.disabled.call(m.module.disabled) : m.disabled;
523 return progressRun(); // we do not update the display!
531 // it's 10 on top level, and 1 on others??? why...
532 return progressRun.defer(10, _this);
535 progressRun.defer(1, _this);
549 * wrapper for event.on - aliased later..
550 * Typically use to register a event handler for register:
552 * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
561 Roo.XComponent.event = new Roo.util.Observable({
565 * Fires when an Component is registered,
566 * set the disable property on the Component to stop registration.
567 * @param {Roo.XComponent} c the component being registerd.
573 * Fires before each Component is built
574 * can be used to apply permissions.
575 * @param {Roo.XComponent} c the component being registerd.
578 'beforebuild' : true,
580 * @event buildcomplete
581 * Fires on the top level element when all elements have been built
582 * @param {Roo.XComponent} the top level component.
584 'buildcomplete' : true
589 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event);