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,
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 this.el = this.parent.el.addxtype(tree);
178 this.fireEvent('built', this);
180 this.panel = this.el;
181 this.layout = this.panel.layout;
182 this.parentLayout = this.parent.layout || false;
188 Roo.apply(Roo.XComponent, {
190 * @property hideProgress
191 * true to disable the building progress bar.. usefull on single page renders.
194 hideProgress : false,
196 * @property buildCompleted
197 * True when the builder has completed building the interface.
200 buildCompleted : false,
203 * @property topModule
204 * the upper most module - uses document.element as it's constructor.
212 * array of modules to be created by registration system.
213 * @type {Array} of Roo.XComponent
218 * @property elmodules
219 * array of modules to be created by which use #ID
220 * @type {Array} of Roo.XComponent
227 * Register components to be built later.
229 * This solves the following issues
230 * - Building is not done on page load, but after an authentication process has occured.
231 * - Interface elements are registered on page load
232 * - Parent Interface elements may not be loaded before child, so this handles that..
239 module : 'Pman.Tab.projectMgr',
241 parent : 'Pman.layout',
242 disabled : false, // or use a function..
245 * * @param {Object} details about module
247 register : function(obj) {
249 Roo.XComponent.event.fireEvent('register', obj);
250 switch(typeof(obj.disabled) ) {
256 if ( obj.disabled() ) {
268 this.modules.push(obj);
272 * convert a string to an object..
273 * eg. 'AAA.BBB' -> finds AAA.BBB
277 toObject : function(str)
279 if (!str || typeof(str) == 'object') {
282 if (str.substring(0,1) == '#') {
286 var ar = str.split('.');
291 eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
293 throw "Module not found : " + str;
297 throw "Module not found : " + str;
299 Roo.each(ar, function(e) {
300 if (typeof(o[e]) == 'undefined') {
301 throw "Module not found : " + str;
312 * move modules into their correct place in the tree..
315 preBuild : function ()
318 Roo.each(this.modules , function (obj)
320 Roo.XComponent.event.fireEvent('beforebuild', obj);
322 var opar = obj.parent;
324 obj.parent = this.toObject(opar);
326 Roo.log("parent:toObject failed: " + e.toString());
331 Roo.debug && Roo.log("GOT top level module");
332 Roo.debug && Roo.log(obj);
333 obj.modules = new Roo.util.MixedCollection(false,
334 function(o) { return o.order + '' }
336 this.topModule = obj;
339 // parent is a string (usually a dom element name..)
340 if (typeof(obj.parent) == 'string') {
341 this.elmodules.push(obj);
344 if (obj.parent.constructor != Roo.XComponent) {
345 Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
347 if (!obj.parent.modules) {
348 obj.parent.modules = new Roo.util.MixedCollection(false,
349 function(o) { return o.order + '' }
352 if (obj.parent.disabled) {
355 obj.parent.modules.add(obj);
360 * make a list of modules to build.
361 * @return {Array} list of modules.
364 buildOrder : function()
367 var cmp = function(a,b) {
368 return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
370 if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
371 throw "No top level modules to build";
374 // make a flat list in order of modules to build.
375 var mods = this.topModule ? [ this.topModule ] : [];
377 // elmodules (is a list of DOM based modules )
378 Roo.each(this.elmodules, function(e) {
383 // add modules to their parents..
384 var addMod = function(m) {
385 Roo.debug && Roo.log("build Order: add: " + m.name);
388 if (m.modules && !m.disabled) {
389 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
390 m.modules.keySort('ASC', cmp );
391 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
393 m.modules.each(addMod);
395 Roo.debug && Roo.log("build Order: no child modules");
397 // not sure if this is used any more..
399 m.finalize.name = m.name + " (clean up) ";
400 mods.push(m.finalize);
404 if (this.topModule) {
405 this.topModule.modules.keySort('ASC', cmp );
406 this.topModule.modules.each(addMod);
412 * Build the registered modules.
413 * @param {Object} parent element.
414 * @param {Function} optional method to call after module has been added.
422 var mods = this.buildOrder();
424 //this.allmods = mods;
425 //Roo.debug && Roo.log(mods);
427 if (!mods.length) { // should not happen
428 throw "NO modules!!!";
432 var msg = "Building Interface...";
433 // flash it up as modal - so we store the mask!?
434 if (!this.hideProgress) {
435 Roo.MessageBox.show({ title: 'loading' });
436 Roo.MessageBox.show({
437 title: "Please wait...",
446 var total = mods.length;
449 var progressRun = function() {
451 Roo.debug && Roo.log('hide?');
452 if (!this.hideProgress) {
453 Roo.MessageBox.hide();
455 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
461 var m = mods.shift();
464 Roo.debug && Roo.log(m);
465 // not sure if this is supported any more.. - modules that are are just function
466 if (typeof(m) == 'function') {
468 return progressRun.defer(10, _this);
472 msg = "Building Interface " + (total - mods.length) +
474 (m.name ? (' - ' + m.name) : '');
475 Roo.debug && Roo.log(msg);
476 if (!this.hideProgress) {
477 Roo.MessageBox.updateProgress( (total - mods.length)/total, msg );
481 // is the module disabled?
482 var disabled = (typeof(m.disabled) == 'function') ?
483 m.disabled.call(m.module.disabled) : m.disabled;
487 return progressRun(); // we do not update the display!
495 // it's 10 on top level, and 1 on others??? why...
496 return progressRun.defer(10, _this);
499 progressRun.defer(1, _this);
513 * wrapper for event.on - aliased later..
514 * Typically use to register a event handler for register:
516 * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
525 Roo.XComponent.event = new Roo.util.Observable({
529 * Fires when an Component is registered,
530 * set the disable property on the Component to stop registration.
531 * @param {Roo.XComponent} c the component being registerd.
537 * Fires before each Component is built
538 * can be used to apply permissions.
539 * @param {Roo.XComponent} c the component being registerd.
542 'beforebuild' : true,
544 * @event buildcomplete
545 * Fires on the top level element when all elements have been built
546 * @param {Roo.XComponent} the top level component.
548 'buildcomplete' : true
553 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event);