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