fix #7605 - revamp_image_managment_code
[roojs1] / Roo / bootstrap / Component.js
1 /*
2  * - LGPL
3  *
4  * base class for bootstrap elements.
5  * 
6  */
7
8 Roo.bootstrap = Roo.bootstrap || {};
9 /**
10  * @class Roo.bootstrap.Component
11  * @extends Roo.Component
12  * @abstract
13  * @children Roo.bootstrap.Component
14  * Bootstrap Component base class
15  * @cfg {String} cls css class
16  * @cfg {String} style any extra css
17  * @cfg {Object} xattr extra attributes to add to 'element' (used by builder to store stuff.)
18  * @cfg {Boolean} can_build_overlaid  True if element can be rebuild from a HTML page
19  * @cfg {string} dataId cutomer id
20  * @cfg {string} name Specifies name attribute
21  * @cfg {string} tooltip  Text for the tooltip
22  * @cfg {string} container_method method to fetch parents container element (used by NavHeaderbar -  getHeaderChildContainer)
23  * @cfg {string|object} visibilityEl (el|parent) What element to use for visibility (@see getVisibilityEl())
24  
25  * @constructor
26  * Do not use directly - it does not do anything..
27  * @param {Object} config The config object
28  */
29
30
31
32 Roo.bootstrap.Component = function(config){
33     Roo.bootstrap.Component.superclass.constructor.call(this, config);
34        
35     this.addEvents({
36         /**
37          * @event childrenrendered
38          * Fires when the children have been rendered..
39          * @param {Roo.bootstrap.Component} this
40          */
41         "childrenrendered" : true
42         
43         
44         
45     });
46     
47     
48 };
49
50 Roo.extend(Roo.bootstrap.Component, Roo.BoxComponent,  {
51     
52     
53     allowDomMove : false, // to stop relocations in parent onRender...
54     
55     cls : false,
56     
57     style : false,
58     
59     autoCreate : false,
60     
61     tooltip : null,
62     /**
63      * Initialize Events for the element
64      */
65     initEvents : function() { },
66     
67     xattr : false,
68     
69     parentId : false,
70     
71     can_build_overlaid : true,
72     
73     container_method : false,
74     
75     dataId : false,
76     
77     name : false,
78     
79     parent: function() {
80         // returns the parent component..
81         return Roo.ComponentMgr.get(this.parentId)
82         
83         
84     },
85     
86     // private
87     onRender : function(ct, position)
88     {
89        // Roo.log("Call onRender: " + this.xtype);
90         
91         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
92         
93         if(this.el){
94             if (this.el.attr('xtype')) {
95                 this.el.attr('xtypex', this.el.attr('xtype'));
96                 this.el.dom.removeAttribute('xtype');
97                 
98                 this.initEvents();
99             }
100             
101             return;
102         }
103         
104          
105         
106         var cfg = Roo.apply({},  this.getAutoCreate());
107         
108         cfg.id = this.id || Roo.id();
109         
110         // fill in the extra attributes 
111         if (this.xattr && typeof(this.xattr) =='object') {
112             for (var i in this.xattr) {
113                 cfg[i] = this.xattr[i];
114             }
115         }
116         
117         if(this.dataId){
118             cfg.dataId = this.dataId;
119         }
120         
121         if (this.cls) {
122             cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
123         }
124         
125         if (this.style) { // fixme needs to support more complex style data.
126             cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
127         }
128         
129         if(this.name){
130             cfg.name = this.name;
131         }
132         
133         this.el = ct.createChild(cfg, position);
134         
135         if (this.tooltip) {
136             this.tooltipEl().attr('tooltip', this.tooltip);
137         }
138         
139         if(this.tabIndex !== undefined){
140             this.el.dom.setAttribute('tabIndex', this.tabIndex);
141         }
142         
143         this.initEvents();
144         
145     },
146     /**
147      * Fetch the element to add children to
148      * @return {Roo.Element} defaults to this.el
149      */
150     getChildContainer : function()
151     {
152         return this.el;
153     },
154     getDocumentBody : function() // used by menus - as they are attached to the body so zIndexes work
155     {
156         return Roo.get(document.body);
157     },
158     
159     /**
160      * Fetch the element to display the tooltip on.
161      * @return {Roo.Element} defaults to this.el
162      */
163     tooltipEl : function()
164     {
165         return this.el;
166     },
167         
168     addxtype  : function(tree,cntr)
169     {
170         var cn = this;
171         
172         cn = Roo.factory(tree);
173         //Roo.log(['addxtype', cn]);
174            
175         cn.parentType = this.xtype; //??
176         cn.parentId = this.id;
177         
178         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
179         if (typeof(cn.container_method) == 'string') {
180             cntr = cn.container_method;
181         }
182         
183         
184         var has_flexy_each =  (typeof(tree['flexy:foreach']) != 'undefined');
185         
186         var has_flexy_if =  (typeof(tree['flexy:if']) != 'undefined');
187         
188         var build_from_html =  Roo.XComponent.build_from_html;
189           
190         var is_body  = (tree.xtype == 'Body') ;
191           
192         var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
193           
194         var self_cntr_el = Roo.get(this[cntr](false));
195         
196         // do not try and build conditional elements 
197         if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
198             return false;
199         }
200         
201         if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
202             if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
203                 return this.addxtypeChild(tree,cntr, is_body);
204             }
205             
206             var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
207                 
208             if(echild){
209                 return this.addxtypeChild(Roo.apply({}, tree),cntr);
210             }
211             
212             Roo.log('skipping render');
213             return cn;
214             
215         }
216         
217         var ret = false;
218         if (!build_from_html) {
219             return false;
220         }
221         
222         // this i think handles overlaying multiple children of the same type
223         // with the sam eelement.. - which might be buggy..
224         while (true) {
225             var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
226             
227             if (!echild) {
228                 break;
229             }
230             
231             if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
232                 break;
233             }
234             
235             ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
236         }
237        
238         return ret;
239     },
240     
241     
242     addxtypeChild : function (tree, cntr, is_body)
243     {
244         Roo.debug && Roo.log('addxtypeChild:' + cntr);
245         var cn = this;
246         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
247         
248         
249         var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
250                     (typeof(tree['flexy:foreach']) != 'undefined');
251           
252     
253         
254         skip_children = false;
255         // render the element if it's not BODY.
256         if (!is_body) {
257             
258             // if parent was disabled, then do not try and create the children..
259             if(!this[cntr](true)){
260                 tree.items = [];
261                 return tree;
262             }
263            
264             cn = Roo.factory(tree);
265            
266             cn.parentType = this.xtype; //??
267             cn.parentId = this.id;
268             
269             var build_from_html =  Roo.XComponent.build_from_html;
270             
271             
272             // does the container contain child eleemnts with 'xtype' attributes.
273             // that match this xtype..
274             // note - when we render we create these as well..
275             // so we should check to see if body has xtype set.
276             if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
277                
278                 var self_cntr_el = Roo.get(this[cntr](false));
279                 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
280                 if (echild) { 
281                     //Roo.log(Roo.XComponent.build_from_html);
282                     //Roo.log("got echild:");
283                     //Roo.log(echild);
284                 }
285                 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
286                 // and are not displayed -this causes this to use up the wrong element when matching.
287                 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
288                 
289                 
290                 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
291                   //  Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
292                   
293                   
294                   
295                     cn.el = echild;
296                   //  Roo.log("GOT");
297                     //echild.dom.removeAttribute('xtype');
298                 } else {
299                     Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
300                     Roo.debug && Roo.log(self_cntr_el);
301                     Roo.debug && Roo.log(echild);
302                     Roo.debug && Roo.log(cn);
303                 }
304             }
305            
306             
307            
308             // if object has flexy:if - then it may or may not be rendered.
309             if (build_from_html && has_flexy && !cn.el &&  cn.can_build_overlaid) {
310                 // skip a flexy if element.
311                 Roo.debug && Roo.log('skipping render');
312                 Roo.debug && Roo.log(tree);
313                 if (!cn.el) {
314                     Roo.debug && Roo.log('skipping all children');
315                     skip_children = true;
316                 }
317                 
318              } else {
319                  
320                 // actually if flexy:foreach is found, we really want to create 
321                 // multiple copies here...
322                 //Roo.log('render');
323                 //Roo.log(this[cntr]());
324                 // some elements do not have render methods.. like the layouts...
325                 /*
326                 if(this[cntr](true) === false){
327                     cn.items = [];
328                     return cn;
329                 }
330                 */
331                 cn.render && cn.render(this[cntr](true));
332                 
333              }
334             // then add the element..
335         }
336          
337         // handle the kids..
338         
339         var nitems = [];
340         /*
341         if (typeof (tree.menu) != 'undefined') {
342             tree.menu.parentType = cn.xtype;
343             tree.menu.triggerEl = cn.el;
344             nitems.push(cn.addxtype(Roo.apply({}, tree.menu)));
345             
346         }
347         */
348         if (!tree.items || !tree.items.length) {
349             cn.items = nitems;
350             //Roo.log(["no children", this]);
351             
352             return cn;
353         }
354          
355         var items = tree.items;
356         delete tree.items;
357         
358         //Roo.log(items.length);
359             // add the items..
360         if (!skip_children) {    
361             for(var i =0;i < items.length;i++) {
362               //  Roo.log(['add child', items[i]]);
363                 nitems.push(cn.addxtype(items[i].xns == false ? items[i] : Roo.apply({}, items[i])));
364             }
365         }
366         
367         cn.items = nitems;
368         
369         //Roo.log("fire childrenrendered");
370         
371         cn.fireEvent('childrenrendered', this);
372         
373         return cn;
374     },
375     
376     /**
377      * Set the element that will be used to show or hide
378      */
379     setVisibilityEl : function(el)
380     {
381         this.visibilityEl = el;
382     },
383     
384      /**
385      * Get the element that will be used to show or hide
386      */
387     getVisibilityEl : function()
388     {
389         if (typeof(this.visibilityEl) == 'object') {
390             return this.visibilityEl;
391         }
392         
393         if (typeof(this.visibilityEl) == 'string') {
394             return this.visibilityEl == 'parent' ? this.parent().getEl() : this.getEl();
395         }
396         
397         return this.getEl();
398     },
399     
400     /**
401      * Show a component - removes 'hidden' class
402      */
403     show : function()
404     {
405         if(!this.getVisibilityEl()){
406             return;
407         }
408          
409         this.getVisibilityEl().removeClass(['hidden','d-none']);
410         
411         this.fireEvent('show', this);
412         
413         
414     },
415     /**
416      * Hide a component - adds 'hidden' class
417      */
418     hide: function()
419     {
420         if(!this.getVisibilityEl()){
421             return;
422         }
423         
424         this.getVisibilityEl().addClass(['hidden','d-none']);
425         
426         this.fireEvent('hide', this);
427         
428     }
429 });
430
431