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