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