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