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