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