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