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 );
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,
168 if (!this.parent.el) {
169 // probably an old style ctor, which has been disabled.
173 // The 'tree' method is '_tree now'
175 var tree = this._tree ? this._tree() : this.tree();
176 tree.region = tree.region || this.region;
177 if (this.parent.el === true) {
178 // bootstrap... - body..
179 this.parent.el = Roo.factory(tree);
181 this.el = this.parent.el.addxtype(tree);
182 this.fireEvent('built', this);
184 this.panel = this.el;
185 this.layout = this.panel.layout;
186 this.parentLayout = this.parent.layout || false;
192 Roo.apply(Roo.XComponent, {
194 * @property hideProgress
195 * true to disable the building progress bar.. usefull on single page renders.
198 hideProgress : false,
200 * @property buildCompleted
201 * True when the builder has completed building the interface.
204 buildCompleted : false,
207 * @property topModule
208 * the upper most module - uses document.element as it's constructor.
216 * array of modules to be created by registration system.
217 * @type {Array} of Roo.XComponent
222 * @property elmodules
223 * array of modules to be created by which use #ID
224 * @type {Array} of Roo.XComponent
231 * Register components to be built later.
233 * This solves the following issues
234 * - Building is not done on page load, but after an authentication process has occured.
235 * - Interface elements are registered on page load
236 * - Parent Interface elements may not be loaded before child, so this handles that..
243 module : 'Pman.Tab.projectMgr',
245 parent : 'Pman.layout',
246 disabled : false, // or use a function..
249 * * @param {Object} details about module
251 register : function(obj) {
253 Roo.XComponent.event.fireEvent('register', obj);
254 switch(typeof(obj.disabled) ) {
260 if ( obj.disabled() ) {
272 this.modules.push(obj);
276 * convert a string to an object..
277 * eg. 'AAA.BBB' -> finds AAA.BBB
281 toObject : function(str)
283 if (!str || typeof(str) == 'object') {
286 if (str.substring(0,1) == '#') {
290 var ar = str.split('.');
295 eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
297 throw "Module not found : " + str;
301 throw "Module not found : " + str;
303 Roo.each(ar, function(e) {
304 if (typeof(o[e]) == 'undefined') {
305 throw "Module not found : " + str;
316 * move modules into their correct place in the tree..
319 preBuild : function ()
323 Roo.each(this.modules , function (obj)
326 Roo.XComponent.event.fireEvent('beforebuild', obj);
328 var opar = obj.parent;
330 obj.parent = this.toObject(opar);
332 Roo.log("parent:toObject failed: " + e.toString());
337 Roo.debug && Roo.log("GOT top level module");
338 Roo.debug && Roo.log(obj);
339 obj.modules = new Roo.util.MixedCollection(false,
340 function(o) { return o.order + '' }
342 this.topModule = obj;
345 // parent is a string (usually a dom element name..)
346 if (typeof(obj.parent) == 'string') {
347 this.elmodules.push(obj);
351 if (obj.parent.constructor != Roo.XComponent) {
352 Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
354 if (!obj.parent.modules) {
355 obj.parent.modules = new Roo.util.MixedCollection(false,
356 function(o) { return o.order + '' }
359 if (obj.parent.disabled) {
362 obj.parent.modules.add(obj);
367 * make a list of modules to build.
368 * @return {Array} list of modules.
371 buildOrder : function()
376 var cmp = function(a,b) {
377 return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
379 if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
380 throw "No top level modules to build";
383 // make a flat list in order of modules to build.
384 var mods = this.topModule ? [ this.topModule ] : [];
387 // elmodules (is a list of DOM based modules )
388 Roo.each(this.elmodules, function(e) {
390 if (!this.topModule &&
391 typeof(e.parent) == 'string' &&
392 e.parent.substring(0,1) == '#' &&
393 Roo.get(e.parent.substr(1))
402 // add modules to their parents..
403 var addMod = function(m) {
404 Roo.debug && Roo.log("build Order: add: " + m.name);
407 if (m.modules && !m.disabled) {
408 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
409 m.modules.keySort('ASC', cmp );
410 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
412 m.modules.each(addMod);
414 Roo.debug && Roo.log("build Order: no child modules");
416 // not sure if this is used any more..
418 m.finalize.name = m.name + " (clean up) ";
419 mods.push(m.finalize);
423 if (this.topModule && this.topModule.modules) {
424 this.topModule.modules.keySort('ASC', cmp );
425 this.topModule.modules.each(addMod);
431 * Build the registered modules.
432 * @param {Object} parent element.
433 * @param {Function} optional method to call after module has been added.
442 Roo.log('this.elmodules');
443 Roo.log(this.elmodules);
444 var mods = this.buildOrder();
446 //this.allmods = mods;
447 //Roo.debug && Roo.log(mods);
449 if (!mods.length) { // should not happen
450 throw "NO modules!!!";
454 var msg = "Building Interface...";
455 // flash it up as modal - so we store the mask!?
456 if (!this.hideProgress && Roo.MessageBox) {
457 Roo.MessageBox.show({ title: 'loading' });
458 Roo.MessageBox.show({
459 title: "Please wait...",
468 var total = mods.length;
471 var progressRun = function() {
473 Roo.debug && Roo.log('hide?');
474 if (!this.hideProgress && Roo.MessageBox) {
475 Roo.MessageBox.hide();
477 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
483 var m = mods.shift();
486 Roo.debug && Roo.log(m);
487 // not sure if this is supported any more.. - modules that are are just function
488 if (typeof(m) == 'function') {
490 return progressRun.defer(10, _this);
494 msg = "Building Interface " + (total - mods.length) +
496 (m.name ? (' - ' + m.name) : '');
497 Roo.debug && Roo.log(msg);
498 if (!this.hideProgress && Roo.MessageBox) {
499 Roo.MessageBox.updateProgress( (total - mods.length)/total, msg );
503 // is the module disabled?
504 var disabled = (typeof(m.disabled) == 'function') ?
505 m.disabled.call(m.module.disabled) : m.disabled;
509 return progressRun(); // we do not update the display!
517 // it's 10 on top level, and 1 on others??? why...
518 return progressRun.defer(10, _this);
521 progressRun.defer(1, _this);
535 * wrapper for event.on - aliased later..
536 * Typically use to register a event handler for register:
538 * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
547 Roo.XComponent.event = new Roo.util.Observable({
551 * Fires when an Component is registered,
552 * set the disable property on the Component to stop registration.
553 * @param {Roo.XComponent} c the component being registerd.
559 * Fires before each Component is built
560 * can be used to apply permissions.
561 * @param {Roo.XComponent} c the component being registerd.
564 'beforebuild' : true,
566 * @event buildcomplete
567 * Fires on the top level element when all elements have been built
568 * @param {Roo.XComponent} the top level component.
570 'buildcomplete' : true
575 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event);