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