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