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)
140 Roo.log("Warning - element can not be found :#" + ename );
148 el = el ? Roo.get(el) : false;
150 // it's a top level one..
152 el : new Roo.BorderLayout(el || document.body, {
160 alwaysShowTabs: el && hp? false : true,
161 hideTabs: el || !hp ? true : false,
169 // The 'tree' method is '_tree now'
171 var tree = this._tree ? this._tree() : this.tree();
172 tree.region = tree.region || this.region;
173 this.el = this.parent.el.addxtype(tree);
174 this.fireEvent('built', this);
176 this.panel = this.el;
177 this.layout = this.panel.layout;
178 this.parentLayout = this.parent.layout || false;
184 Roo.apply(Roo.XComponent, {
187 * @property buildCompleted
188 * True when the builder has completed building the interface.
191 buildCompleted : false,
194 * @property topModule
195 * the upper most module - uses document.element as it's constructor.
203 * array of modules to be created by registration system.
204 * @type {Array} of Roo.XComponent
209 * @property elmodules
210 * array of modules to be created by which use #ID
211 * @type {Array} of Roo.XComponent
218 * Register components to be built later.
220 * This solves the following issues
221 * - Building is not done on page load, but after an authentication process has occured.
222 * - Interface elements are registered on page load
223 * - Parent Interface elements may not be loaded before child, so this handles that..
230 module : 'Pman.Tab.projectMgr',
232 parent : 'Pman.layout',
233 disabled : false, // or use a function..
236 * * @param {Object} details about module
238 register : function(obj) {
240 Roo.XComponent.event.fireEvent('register', obj);
241 switch(typeof(obj.disabled) ) {
247 if ( obj.disabled() ) {
258 this.modules.push(obj);
262 * convert a string to an object..
263 * eg. 'AAA.BBB' -> finds AAA.BBB
267 toObject : function(str)
269 if (!str || typeof(str) == 'object') {
272 if (str.substring(0,1) == '#') {
276 var ar = str.split('.');
281 eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
283 throw "Module not found : " + str;
287 throw "Module not found : " + str;
289 Roo.each(ar, function(e) {
290 if (typeof(o[e]) == 'undefined') {
291 throw "Module not found : " + str;
302 * move modules into their correct place in the tree..
305 preBuild : function ()
308 Roo.each(this.modules , function (obj)
310 var opar = obj.parent;
312 obj.parent = this.toObject(opar);
314 Roo.log("parent:toObject failed: " + e.toString());
319 Roo.debug && Roo.log("GOT top level module");
320 Roo.debug && Roo.log(obj);
321 obj.modules = new Roo.util.MixedCollection(false,
322 function(o) { return o.order + '' }
324 this.topModule = obj;
327 // parent is a string (usually a dom element name..)
328 if (typeof(obj.parent) == 'string') {
329 this.elmodules.push(obj);
332 if (obj.parent.constructor != Roo.XComponent) {
333 Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
335 if (!obj.parent.modules) {
336 obj.parent.modules = new Roo.util.MixedCollection(false,
337 function(o) { return o.order + '' }
341 obj.parent.modules.add(obj);
346 * make a list of modules to build.
347 * @return {Array} list of modules.
350 buildOrder : function()
353 var cmp = function(a,b) {
354 return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
356 if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
357 throw "No top level modules to build";
360 // make a flat list in order of modules to build.
361 var mods = this.topModule ? [ this.topModule ] : [];
363 // elmodules (is a list of DOM based modules )
364 Roo.each(this.elmodules,function(e) { mods.push(e) });
367 // add modules to their parents..
368 var addMod = function(m) {
369 Roo.debug && Roo.log("build Order: add: " + m.name);
373 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
374 m.modules.keySort('ASC', cmp );
375 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
377 m.modules.each(addMod);
379 Roo.debug && Roo.log("build Order: no child modules");
381 // not sure if this is used any more..
383 m.finalize.name = m.name + " (clean up) ";
384 mods.push(m.finalize);
388 if (this.topModule) {
389 this.topModule.modules.keySort('ASC', cmp );
390 this.topModule.modules.each(addMod);
396 * Build the registered modules.
397 * @param {Object} parent element.
398 * @param {Function} optional method to call after module has been added.
406 var mods = this.buildOrder();
408 //this.allmods = mods;
409 //Roo.debug && Roo.log(mods);
411 if (!mods.length) { // should not happen
412 throw "NO modules!!!";
416 var msg = "Building Interface...";
417 // flash it up as modal - so we store the mask!?
418 Roo.MessageBox.show({ title: 'loading' });
419 Roo.MessageBox.show({
420 title: "Please wait...",
428 var total = mods.length;
431 var progressRun = function() {
433 Roo.debug && Roo.log('hide?');
434 Roo.MessageBox.hide();
435 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
441 var m = mods.shift();
444 Roo.debug && Roo.log(m);
445 // not sure if this is supported any more.. - modules that are are just function
446 if (typeof(m) == 'function') {
448 return progressRun.defer(10, _this);
452 msg = "Building Interface " + (total - mods.length) +
454 (m.name ? (' - ' + m.name) : '');
455 Roo.debug && Roo.log(msg);
456 Roo.MessageBox.updateProgress( (total - mods.length)/total, msg );
459 // is the module disabled?
460 var disabled = (typeof(m.disabled) == 'function') ?
461 m.disabled.call(m.module.disabled) : m.disabled;
465 return progressRun(); // we do not update the display!
473 // it's 10 on top level, and 1 on others??? why...
474 return progressRun.defer(10, _this);
477 progressRun.defer(1, _this);
491 * wrapper for event.on - aliased later..
492 * Typically use to register a event handler for register:
494 * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
503 Roo.XComponent.event = new Roo.util.Observable({
507 * Fires when an Component is registered,
508 * set the disable property on the Component to stop registration.
509 * @param {Roo.XComponent} c the component being registerd.
514 * @event buildcomplete
515 * Fires on the top level element when all elements have been built
516 * @param {Roo.XComponent} the top level component.
518 'buildcomplete' : true
523 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event);