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.
10 * technically this is a wrapper around a tree of Roo elements (which defines a 'module'),
11 * used in conjunction with XComponent.build() it will create an instance of each element,
12 * then call addxtype() to build the User interface.
14 * Mypart.xyx = new Roo.XComponent({
16 parent : 'Mypart.xyz', // empty == document.element.!!
20 disabled : function() {}
22 tree : function() { // return an tree of xtype declared components
26 xtype : 'NestedLayoutPanel',
33 * It can be used to build a big heiracy, with parent etc.
34 * or you can just use this to render a single compoent to a dom element
35 * MYPART.render(Roo.Element | String(id) | dom_element )
42 * Roo is designed primarily as a single page application, so the UI build for a standard interface will
43 * expect a single 'TOP' level module normally indicated by the 'parent' of the XComponent definition being defined as false.
45 * Each sub module is expected to have a parent pointing to the class name of it's parent module.
47 * When the top level is false, a 'Roo.BorderLayout' is created and the element is flagged as 'topModule'
48 * - if mulitple topModules exist, the last one is defined as the top module.
52 * When the top level or multiple modules are to embedded into a existing HTML page,
53 * the parent element can container '#id' of the element where the module will be drawn.
57 * Unlike classic Roo, the bootstrap tends not to be used as a single page.
58 * it relies more on a include mechanism, where sub modules are included into an outer page.
59 * This is normally managed by the builder tools using Roo.apply( options, Included.Sub.Module )
61 * Bootstrap Roo Included elements
63 * Our builder application needs the ability to preview these sub compoennts. They will normally have parent=false set,
64 * hence confusing the component builder as it thinks there are multiple top level elements.
66 * String Over-ride & Translations
68 * Our builder application writes all the strings as _strings and _named_strings. This is to enable the translation of elements,
69 * and also the 'overlaying of string values - needed when different versions of the same application with different text content
70 * are needed. @see Roo.XComponent.overlayString
74 * @extends Roo.util.Observable
76 * @param cfg {Object} configuration of component
79 Roo.XComponent = function(cfg) {
84 * Fires when this the componnt is built
85 * @param {Roo.XComponent} c the component
90 this.region = this.region || 'center'; // default..
91 Roo.XComponent.register(this);
93 this.el = false; // where the layout goes..
97 Roo.extend(Roo.XComponent, Roo.util.Observable, {
100 * The created element (with Roo.factory())
107 * for BC - use el in new code
114 * for BC - use el in new code
120 * @cfg {Function|boolean} disabled
121 * If this module is disabled by some rule, return true from the funtion
126 * @cfg {String} parent
127 * Name of parent element which it get xtype added to..
132 * @cfg {String} order
133 * Used to set the order in which elements are created (usefull for multiple tabs)
139 * String to display while loading.
143 * @cfg {String} region
144 * Region to render component to (defaults to center)
150 * A single item array - the first element is the root of the tree..
151 * It's done this way to stay compatible with the Xtype system...
157 * The method that retuns the tree of parts that make up this compoennt
164 * render element to dom or tree
165 * @param {Roo.Element|String|DomElement} optional render to if parent is not set.
168 render : function(el)
172 var hp = this.parent ? 1 : 0;
173 Roo.debug && Roo.log(this);
175 var tree = this._tree ? this._tree() : this.tree();
178 if (!el && typeof(this.parent) == 'string' && this.parent.substring(0,1) == '#') {
179 // if parent is a '#.....' string, then let's use that..
180 var ename = this.parent.substr(1);
182 Roo.debug && Roo.log(ename);
184 case 'bootstrap-body':
185 if (typeof(tree.el) != 'undefined' && tree.el == document.body) {
186 // this is the BorderLayout standard?
187 this.parent = { el : true };
190 if (["Nest", "Content", "Grid", "Tree"].indexOf(tree.xtype) > -1) {
191 // need to insert stuff...
193 el : new Roo.bootstrap.layout.Border({
202 alwaysShowTabs: true,
212 if (typeof(Roo.bootstrap.Body) != 'undefined' ) {
213 this.parent = { el : new Roo.bootstrap.Body() };
214 Roo.debug && Roo.log("setting el to doc body");
217 throw "Container is bootstrap body, but Roo.bootstrap.Body is not defined";
221 this.parent = { el : true};
225 if (typeof(Roo.bootstrap) != 'undefined' && tree['|xns'] == 'Roo.bootstrap') {
226 this.parent = { el : true};
233 if (!el && !this.parent) {
234 Roo.debug && Roo.log("Warning - element can not be found :#" + ename );
239 Roo.debug && Roo.log("EL:");
240 Roo.debug && Roo.log(el);
241 Roo.debug && Roo.log("this.parent.el:");
242 Roo.debug && Roo.log(this.parent.el);
245 // altertive root elements ??? - we need a better way to indicate these.
246 var is_alt = Roo.XComponent.is_alt ||
247 (typeof(tree.el) != 'undefined' && tree.el == document.body) ||
248 (typeof(Roo.bootstrap) != 'undefined' && tree.xns == Roo.bootstrap) ||
249 (typeof(Roo.mailer) != 'undefined' && tree.xns == Roo.mailer) ;
253 if (!this.parent && is_alt) {
254 //el = Roo.get(document.body);
255 this.parent = { el : true };
262 Roo.debug && Roo.log("no parent - creating one");
264 el = el ? Roo.get(el) : false;
266 if (typeof(Roo.BorderLayout) == 'undefined' ) {
269 el : new Roo.bootstrap.layout.Border({
270 el: el || document.body,
278 alwaysShowTabs: false,
287 // it's a top level one..
289 el : new Roo.BorderLayout(el || document.body, {
296 alwaysShowTabs: el && hp? false : true,
297 hideTabs: el || !hp ? true : false,
305 if (!this.parent.el) {
306 // probably an old style ctor, which has been disabled.
310 // The 'tree' method is '_tree now'
312 tree.region = tree.region || this.region;
314 if (this.parent.el === true) {
315 // bootstrap... - body..
319 this.parent.el = Roo.factory(tree);
323 this.el = this.parent.el.addxtype(tree, undefined, is_body);
324 this.fireEvent('built', this);
326 this.panel = this.el;
327 this.layout = this.panel.layout;
328 this.parentLayout = this.parent.layout || false;
334 Roo.apply(Roo.XComponent, {
336 * @property hideProgress
337 * true to disable the building progress bar.. usefull on single page renders.
340 hideProgress : false,
342 * @property buildCompleted
343 * True when the builder has completed building the interface.
346 buildCompleted : false,
349 * @property topModule
350 * the upper most module - uses document.element as it's constructor.
358 * array of modules to be created by registration system.
359 * @type {Array} of Roo.XComponent
364 * @property elmodules
365 * array of modules to be created by which use #ID
366 * @type {Array} of Roo.XComponent
373 * Is an alternative Root - normally used by bootstrap or other systems,
374 * where the top element in the tree can wrap 'body'
375 * @type {boolean} (default false)
380 * @property build_from_html
381 * Build elements from html - used by bootstrap HTML stuff
382 * - this is cleared after build is completed
383 * @type {boolean} (default false)
386 build_from_html : false,
388 * Register components to be built later.
390 * This solves the following issues
391 * - Building is not done on page load, but after an authentication process has occured.
392 * - Interface elements are registered on page load
393 * - Parent Interface elements may not be loaded before child, so this handles that..
400 module : 'Pman.Tab.projectMgr',
402 parent : 'Pman.layout',
403 disabled : false, // or use a function..
406 * * @param {Object} details about module
408 register : function(obj) {
410 Roo.XComponent.event.fireEvent('register', obj);
411 switch(typeof(obj.disabled) ) {
417 if ( obj.disabled() ) {
429 this.modules.push(obj);
433 * convert a string to an object..
434 * eg. 'AAA.BBB' -> finds AAA.BBB
438 toObject : function(str)
440 if (!str || typeof(str) == 'object') {
443 if (str.substring(0,1) == '#') {
447 var ar = str.split('.');
452 eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
454 throw "Module not found : " + str;
458 throw "Module not found : " + str;
460 Roo.each(ar, function(e) {
461 if (typeof(o[e]) == 'undefined') {
462 throw "Module not found : " + str;
473 * move modules into their correct place in the tree..
476 preBuild : function ()
479 Roo.each(this.modules , function (obj)
481 Roo.XComponent.event.fireEvent('beforebuild', obj);
483 var opar = obj.parent;
485 obj.parent = this.toObject(opar);
487 Roo.debug && Roo.log("parent:toObject failed: " + e.toString());
492 Roo.debug && Roo.log("GOT top level module");
493 Roo.debug && Roo.log(obj);
494 obj.modules = new Roo.util.MixedCollection(false,
495 function(o) { return o.order + '' }
497 this.topModule = obj;
500 // parent is a string (usually a dom element name..)
501 if (typeof(obj.parent) == 'string') {
502 this.elmodules.push(obj);
505 if (obj.parent.constructor != Roo.XComponent) {
506 Roo.debug && Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
508 if (!obj.parent.modules) {
509 obj.parent.modules = new Roo.util.MixedCollection(false,
510 function(o) { return o.order + '' }
513 if (obj.parent.disabled) {
516 obj.parent.modules.add(obj);
521 * make a list of modules to build.
522 * @return {Array} list of modules.
525 buildOrder : function()
528 var cmp = function(a,b) {
529 return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
531 if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
532 throw "No top level modules to build";
535 // make a flat list in order of modules to build.
536 var mods = this.topModule ? [ this.topModule ] : [];
539 // elmodules (is a list of DOM based modules )
540 Roo.each(this.elmodules, function(e) {
542 if (!this.topModule &&
543 typeof(e.parent) == 'string' &&
544 e.parent.substring(0,1) == '#' &&
545 Roo.get(e.parent.substr(1))
554 // add modules to their parents..
555 var addMod = function(m) {
556 Roo.debug && Roo.log("build Order: add: " + m.name);
559 if (m.modules && !m.disabled) {
560 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
561 m.modules.keySort('ASC', cmp );
562 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
564 m.modules.each(addMod);
566 Roo.debug && Roo.log("build Order: no child modules");
568 // not sure if this is used any more..
570 m.finalize.name = m.name + " (clean up) ";
571 mods.push(m.finalize);
575 if (this.topModule && this.topModule.modules) {
576 this.topModule.modules.keySort('ASC', cmp );
577 this.topModule.modules.each(addMod);
583 * Build the registered modules.
584 * @param {Object} parent element.
585 * @param {Function} optional method to call after module has been added.
589 build : function(opts)
592 if (typeof(opts) != 'undefined') {
593 Roo.apply(this,opts);
597 var mods = this.buildOrder();
599 //this.allmods = mods;
600 //Roo.debug && Roo.log(mods);
602 if (!mods.length) { // should not happen
603 throw "NO modules!!!";
607 var msg = "Building Interface...";
608 // flash it up as modal - so we store the mask!?
609 if (!this.hideProgress && Roo.MessageBox) {
610 Roo.MessageBox.show({ title: 'loading' });
611 Roo.MessageBox.show({
612 title: "Please wait...",
621 var total = mods.length;
624 var progressRun = function() {
626 Roo.debug && Roo.log('hide?');
627 if (!this.hideProgress && Roo.MessageBox) {
628 Roo.MessageBox.hide();
630 Roo.XComponent.build_from_html = false; // reset, so dialogs will be build from javascript
632 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
638 var m = mods.shift();
641 Roo.debug && Roo.log(m);
642 // not sure if this is supported any more.. - modules that are are just function
643 if (typeof(m) == 'function') {
645 return progressRun.defer(10, _this);
649 msg = "Building Interface " + (total - mods.length) +
651 (m.name ? (' - ' + m.name) : '');
652 Roo.debug && Roo.log(msg);
653 if (!_this.hideProgress && Roo.MessageBox) {
654 Roo.MessageBox.updateProgress( (total - mods.length)/total, msg );
658 // is the module disabled?
659 var disabled = (typeof(m.disabled) == 'function') ?
660 m.disabled.call(m.module.disabled) : m.disabled;
664 return progressRun(); // we do not update the display!
672 // it's 10 on top level, and 1 on others??? why...
673 return progressRun.defer(10, _this);
676 progressRun.defer(1, _this);
682 * Overlay a set of modified strings onto a component
683 * This is dependant on our builder exporting the strings and 'named strings' elements.
685 * @param {Object} element to overlay on - eg. Pman.Dialog.Login
686 * @param {Object} associative array of 'named' string and it's new value.
689 overlayStrings : function( component, strings )
691 if (typeof(component['_named_strings']) == 'undefined') {
692 throw "ERROR: component does not have _named_strings";
694 for ( var k in strings ) {
695 var md = typeof(component['_named_strings'][k]) == 'undefined' ? false : component['_named_strings'][k];
697 component['_strings'][md] = strings[k];
699 Roo.log('could not find named string: ' + k + ' in');
715 * wrapper for event.on - aliased later..
716 * Typically use to register a event handler for register:
718 * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
727 Roo.XComponent.event = new Roo.util.Observable({
731 * Fires when an Component is registered,
732 * set the disable property on the Component to stop registration.
733 * @param {Roo.XComponent} c the component being registerd.
739 * Fires before each Component is built
740 * can be used to apply permissions.
741 * @param {Roo.XComponent} c the component being registerd.
744 'beforebuild' : true,
746 * @event buildcomplete
747 * Fires on the top level element when all elements have been built
748 * @param {Roo.XComponent} the top level component.
750 'buildcomplete' : true
755 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event);