ee57eddd77282f3cb1dbb0b08f3f954a3bec49db
[app.Builder.js] / builder.html.js
1 //<script type="text/javascript">
2  
3
4 var MODULE = { isBuilder : true };
5 // BC
6 var _this = MODULE;
7
8 // the apprenderer.
9 Builder  = {
10     
11     scriptTag : false,
12     
13     id : 1,
14     
15     render : function(data, clsname)
16     {
17         // for debugging 
18         // console.log(data);        return; 
19         //Roo.log(data);
20         //Roo.log(data);
21         // This would be alot simpler if we just use the XComponent code...
22         // data should now be dialog or xcomponent..
23         // only snag here is that we do not know the name currently..
24         //Roo.log(clsname);
25         var  ix = '_src_' + this.id++;
26         // should replace module name with Builder._src_{id}
27         data =  data.replace(clsname, 'Builder.' + ix);
28         // next.. we need to ensure that parent is set correctly..
29         // done by sender... otherwise building becomes difficult..
30         //data  += "\n" + 'Builder.' + ix + ".parent = '#renderel';\n";
31         
32         //Roo.log(data);return;
33         //Roo.log(data);
34         if (this.scriptTag) { 
35             document.body.removeChild(this.scriptTag);
36             this.scriptTag = false;
37         }
38         
39         this.scriptTag = document.body.appendChild(document.createElement('script'));
40         this.scriptTag.setAttribute('type','text/javascript');
41          
42         this.id++;
43         this.scriptTag.appendChild(
44                     document.createTextNode(
45                             data 
46         ));
47          
48         
49         //Roo.log(this.tree);
50         MODULE = { isBuilder : true }; 
51         _this = MODULE;
52         if (!Builder.click) {
53             Builder.click= Roo.get(document.body).on('click', this.onclick, this);
54         }
55         Roo.log('Builder.'+ ix);
56         Roo.XComponent.build();
57         return;
58         
59         return;
60         var  wait_for_tree = function() {
61             
62             Builder.tree = Builder[ix];
63             if (!Builder.tree) {
64                 Roo.log("Wating for tree : " + ix);
65                 wait_for_tree.defer(100);
66                 return;
67             }
68              Builder.redraw(false);
69         }
70         wait_for_tree.defer(100);
71     },
72     
73     
74     
75     tree : {}, 
76     renderObj :  { isBuilder : true },
77     dialogroot : false,
78     
79     redrawClear : function(isAuto)
80     {
81         this.renderObj = { isBuilder : true };
82         
83         this.scroll = {
84             top:0,
85             left:0
86         };
87         
88          
89         
90         //if (this.panelroot) {
91         //    this.scroll = this.panelroot.el.getScroll();
92         //    this.layout.remove('center', this.panelroot);
93         //    this.panelroot = false;
94         //}
95         if (this.dialogroot) {
96             this.dialogroot.remove();
97             this.dialogroot = false;
98         }
99         if (this.layoutbase) {
100             //console.log(Builder.dump(this.layoutbase.el));
101             
102             
103             try {
104                 var pan = this.layoutbase.getRegion('center').getPanel(0);
105                 if (pan) {
106                     this.layoutbase.remove('center', pan);
107                 }
108                 
109                 
110             } catch( e) {
111                 console.log(e);
112                 console.log(JSON.stringify(e));
113                 // reload!!?
114             }
115             
116             
117             
118             //this.layoutbase= false;
119         }
120          
121         
122     },
123     
124     
125     redraw: function(isAuto)
126     {
127         
128         // top level is not relivant
129         this.redrawClear(isAuto);
130         
131         
132         var cfg =  this.tree;
133         //console.log(this.dump(cfg));
134         if (!cfg) {
135             return;
136         }
137         
138         
139         this.munge(cfg);
140         this.cfg = cfg;
141         //console.log(this.dump(cfg)); 
142         // we draw either a dialog or a tab..
143         
144         if (cfg.xtype == 'LayoutDialog') {
145             
146             cfg.modal = false;
147             //var xy  = Pman.Tab.BuilderPanel.panel.el.getXY();
148             //cfg.items[0].x = xy[0];
149             //cfg.items[0].y = xy[1];
150             //cfg.items[0].constraintoviewport = false;
151         
152             this.dialogroot = Roo.get( document.body).createChild({
153                 id : cfg.id
154             });
155             
156             
157             MODULE.dialog = new Roo[cfg.xtype](this.dialogroot, cfg);
158             //this.dialog.el.on('click', this.panelClick, this);
159             MODULE.dialog.show();
160             return;
161             
162         }
163         
164         // otherwise we are creating a layout area on the document..
165         
166          
167         // handles 
168         // contentpanel, nestedlayoutpanel, contentpanel.. etc. 
169         // force center region..
170         cfg.region = 'center';
171         cfg.background = false;
172         if (!this.layoutbase) {
173                 
174             this.layoutbase = new Ext.BorderLayout(document.body, {
175               
176                  center: {
177                     titlebar: false,
178                     autoScroll:false,
179                     closeOnTab: true,
180                     tabPosition: 'top',
181                     //resizeTabs: true,
182                     alwaysShowTabs: true,
183                     minTabWidth: 140
184                 }
185             });
186         }
187         try {
188             console.log("ADDING CFG");    
189             console.log(cfg)
190             this.layoutbase.addxtype(  cfg ); 
191         } catch (e) {
192             console.log("GOT ERROR? - saved in Builder.lastError");
193             Builder.lastError = e;
194             console.log(e);
195             console.log(typeof(e));
196             
197             console.log(this.dump(e));
198             console.trace();
199         }
200         
201         
202     },
203   
204     
205     munge :function (cfg, isListener)
206     {
207         var xitems = false;
208         //cfg.cls = cfg.cls || '';
209         //cfg.cls += ' ' + cfg.id;
210         if (!cfg.id) {
211             this.dump(cfg);
212         }
213         
214         //console.log(cfg.xtype + ': ' + cfg.id);
215         
216         if (cfg.items) {
217             xitems = cfg.items;
218             delete cfg.items;
219         }
220         if (typeof(cfg.background) != 'undefined') {
221             cfg.background = false;
222         }
223         
224         var xtype = (cfg['|xns'] || '')  + '.' + (cfg.xtype || '');
225         
226         for(var p in cfg){
227             // key is not string?!?!?!!?
228           
229             if (typeof(p) != 'string') {
230                 continue;
231             }
232             
233             if (typeof(cfg[p]) == 'object') { // listeners!!!
234                 this.munge(cfg[p], p == 'listeners');
235                 continue;
236             }
237             // SPECIAL - PIPE
238             if (p.charAt(0) == '|' || isListener) {
239                 
240                 if (!cfg[p].length) {
241                     delete cfg[p];
242                     continue;
243                 }
244                 var pp = p.charAt(0) == '|'  ? p.substring(1) : p;
245                 try {
246                     
247                     
248                     var _tmp = false;
249                     
250                     /** eval:var:MOUDULE **/
251                     /** eval:var:_this **/
252                     /** eval:var:_tmp **/
253                     // stupid IE can not return objects evaluated..
254                    // console.log('_tmp =(' + cfg[p] + ')');
255                     eval('_tmp =(' + cfg[p] + ')');
256                     cfg[pp] = _tmp;
257                     
258                     //if (typeof(_tmp) == 'undefined') {
259                     //    alert(cfg[p]);
260                    // }
261                    
262                 } catch(e) {  
263                     console.log('Error evaluating: '  + cfg[p] + "\r\n" + JSON.stringify(e)); 
264                 };
265                 if (pp != p) {
266                     delete cfg[p];
267                 }
268                 
269                     
270                 
271                 continue;
272             }
273             // skip '*'
274             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
275                 delete cfg[p];
276                 continue;
277             }
278             // normal..
279               
280         }
281         
282         if (cfg.xtype && cfg.xtype.match(/^Roo\./) && !cfg.xns) {
283             // xtype contains full path..
284             var bits = cfg.xtype.split('.');
285             bits.shift(); // remove roo..
286             cfg.xtype = bits.pop(); // get the last bit..
287             cfg.xns = Roo;
288             while (bits.length) {
289                 cfg.xns = cfg.xns[bits.shift()]; 
290             }
291              
292         }
293         if (cfg.xtype) {
294             if (!cfg.xns || typeof(cfg.xns[cfg.xtype]) == 'undefined') {
295                 throw "Invalid Xtype " + cfg.xtype + ' on ' + cfg.xtreepath;
296             }
297         }
298         if (!isListener) {
299             cfg.listeners = cfg.listeners || {};
300         }
301         // we can overlay some event handlers here..
302         
303        
304         //console.log('xtype'  + xtype)
305         switch(xtype) {
306             case 'Roo.LayoutDialog':
307                 cfg.listeners.resize = function(dlg, w,h)
308                 {
309                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
310                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
311                 }
312                 break;
313         }
314         
315         
316         
317         
318         
319         // now for all the children.. (items)
320         if (xitems === false) {
321             
322             return;
323         }
324         cfg.items = [];
325         for (var i = 0; i < xitems.length; i++) {
326             // if +builderhide set !!!! drop it!!
327             
328             
329             var xi = xitems[i];
330             if (typeof(xi['*prop']) != 'undefined') {
331                 //console.log('adding prop:' + xi['*prop']);
332                 
333                 var pr = xi['*prop'];
334                 this.munge(xi);
335                 // if prop is an array - then it's items are really the value..
336                 if (pr.match(/\[\]$/)) {
337                     //console.log('adding array?:' + pr);
338                     pr = pr.replace(/\[\]$/, '');
339                     cfg[pr] = cfg[pr]  || [];
340                     cfg[pr].push(xi);
341                     continue;
342                 }
343                 
344                 
345                 if (xi.xtype && xi.xtype  == 'Array') {
346                     cfg[pr] = xi.items;
347                 } else {
348                     //console.log('setting property:' + pr);
349                     cfg[pr] = xi;
350                 }
351                 
352                 
353                 continue;
354             }
355             this.munge(xi);
356             cfg.items.push(xi);
357         }
358         
359         if (cfg.items.length == 0) {
360             delete cfg.items;
361         }
362         console.log(cfg);
363         
364         
365     },
366     
367     
368     
369     cloneConfig : function(config) {
370                 if (!config) { return null; }
371                 var newConfig = {};
372                 for (var i in config) {
373                         if (typeof config[i] == 'object') {
374                                 newConfig[i] = this.cloneConfig(config[i]);
375                         } else if (typeof config[i] != 'function') {
376                                 newConfig[i] = config[i];
377                         }
378                 }
379                 return newConfig;
380         },
381     dump : function (arr,level) {
382         var dumped_text = "";
383         if(!level) level = 0;
384         if (level >  3) {
385             return '... TO DEEP ...';
386         }
387         //The padding given at the beginning of the line.
388         var level_padding = "";
389         for(var j=0;j<level+1;j++) level_padding += "    ";
390         
391         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
392             for(var item in arr) {
393                 
394                 var value = arr[item];
395                 if (item == 'xns') {
396                     continue;
397                 }
398                 if(typeof(value) == 'function') { //If it is an array,
399                     // fake dump...
400                     dumped_text += level_padding + "'" + item + "' : function() { ... },\n";
401                     continue;
402                 }
403                 if(typeof(value) == 'object') { //If it is an array,
404                     dumped_text += level_padding + "'" + item + "': {\n";
405                     dumped_text += this.dump(value,level+1);
406                     dumped_text += level_padding + "}\n";
407                     continue;
408                 }
409                 dumped_text += level_padding + "'" + item + "' : \"" + value + "\"\n";
410                 
411             }
412         } else { //Stings/Chars/Numbers etc.
413             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
414         }
415         return dumped_text;
416     },
417     findNode : function(ftg , method) {
418         if (!ftg) {
419             return; false
420         }
421       // console.log(ftg.id);
422         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
423             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
424             this[method]( nid );
425             return true;
426         }
427         // needs fixing..
428         if (ftg.dom.className.match(/[0-9]+/)) {
429             //console.log(ftg.dom.className);
430             var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
431             if (cmat) {
432                 this[method]( cmat[1] );
433                 return true;
434             }
435         }
436         
437         
438         
439         
440         return false;
441     },
442     
443     overPos: function(x,y) 
444     {
445         
446         var el = document.elementFromPoint(x,y);
447        // //console.log(el.id);
448        // console.log(document.body.innerHTML);
449         this.hover( {
450             getTarget : function () {
451                 return el;
452             },
453             stopEvent : function() {
454                 
455             }
456         });
457         
458         
459     },
460     onclick: function(e) {
461         var tg = Roo.get(e.getTarget());
462         if (!tg) {
463             //console.log('no target');
464             return;
465            }
466          
467         if (this.findNode(tg,'logClick')) {
468             return;
469         }
470         var dp = Roo.get(tg.up(''));
471         if (dp && this.findNode(dp,'logClick')) {
472             return;
473         }
474         
475         var ns = Roo.get(tg.getNextSibling());
476         if (ns && this.findNode(ns,'logClick')) {
477           
478             return;
479         }
480         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
481             return;
482         }
483         
484         for(var i =0; i < 5; i++) {
485             tg = Roo.get(tg.up(''));
486             if (!tg) {
487                 //console.log('no parent' + i);
488                 return;
489             }
490             if (tg && this.findNode(tg,'logClick')) {
491                 return;
492             }
493         }
494         //console.log('no target in parents');
495         
496     },
497     logClick : function(id) 
498     {
499          var bid = id.length ? 'builder-' + id : '';
500          console.log('{ "id" :  "' + bid + '"}');
501     },
502     
503     
504     hover : function(e) {
505         
506        
507         var tg = Roo.get(e.getTarget());
508         if (!tg) {
509             //console.log('no target');
510             this.logMove('');
511             return;
512            }
513          
514         if (this.findNode(tg,'logMove')) {
515             e.stopEvent();
516             return;
517         }
518         var dp = Roo.get(tg.up(''));
519         if (dp && this.findNode(dp,'logMove')) {
520             e.stopEvent();
521             return;
522         }
523         
524         var ns = Roo.get(tg.getNextSibling());
525         if (ns && this.findNode(ns,'logMove')) {
526             e.stopEvent();
527             return;
528         }
529         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
530             e.stopEvent();
531             return;
532         }
533         
534         for(var i =0; i < 5; i++) {
535             tg = Roo.get(tg.up(''));
536             if (!tg) {
537                 //console.log('no parent' + i);
538                 this.logMove('');
539                 return;
540             }
541             if (tg && this.findNode(tg,'logMove')) {
542                 e.stopEvent();
543                 return;
544             }
545         }
546         //console.log('no target in parents');
547         this.logMove('');
548     },
549     logMove : function (id) {
550         //console.log("LOGMOVE: " + id);
551         
552         if (this.lastID === id) {
553             return;
554        }
555        id = ''+ id;
556        var bid = id.length ? 'builder-' + id : '';
557        console.log('{ "hover-node" :  "' + bid + '"}');
558        this.lastID = id;
559     }
560             
561     
562 };
563