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