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