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