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