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