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             
267             return;
268         }
269         cfg.items = [];
270         for (var i = 0; i < xitems.length; i++) {
271             // if +builderhide set !!!! drop it!!
272             
273             
274             var xi = xitems[i];
275             if (typeof(xi['*prop']) != 'undefined') {
276                 //console.log('adding prop:' + xi['*prop']);
277                 
278                 var pr = xi['*prop'];
279                 this.munge(xi);
280                 // if prop is an array - then it's items are really the value..
281                 if (pr.match(/\[\]$/)) {
282                     //console.log('adding array?:' + pr);
283                     pr = pr.replace(/\[\]$/, '');
284                     cfg[pr] = cfg[pr]  || [];
285                     cfg[pr].push(xi);
286                     continue;
287                 }
288                 
289                 
290                 if (xi.xtype && xi.xtype  == 'Array') {
291                     cfg[pr] = xi.items;
292                 } else {
293                     //console.log('setting property:' + pr);
294                     cfg[pr] = xi;
295                 }
296                 
297                 
298                 continue;
299             }
300             this.munge(xi);
301             cfg.items.push(xi);
302         }
303         
304         if (cfg.items.length == 0) {
305             delete cfg.items;
306         }
307         console.log(cfg);
308         
309         
310     },
311     
312     
313     
314     cloneConfig : function(config) {
315                 if (!config) { return null; }
316                 var newConfig = {};
317                 for (var i in config) {
318                         if (typeof config[i] == 'object') {
319                                 newConfig[i] = this.cloneConfig(config[i]);
320                         } else if (typeof config[i] != 'function') {
321                                 newConfig[i] = config[i];
322                         }
323                 }
324                 return newConfig;
325         },
326     dump : function (arr,level) {
327         var dumped_text = "";
328         if(!level) level = 0;
329         if (level >  3) {
330             return '... TO DEEP ...';
331         }
332         //The padding given at the beginning of the line.
333         var level_padding = "";
334         for(var j=0;j<level+1;j++) level_padding += "    ";
335         
336         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
337             for(var item in arr) {
338                 
339                 var value = arr[item];
340                 if (item == 'xns') {
341                     continue;
342                 }
343                 if(typeof(value) == 'function') { //If it is an array,
344                     // fake dump...
345                     dumped_text += level_padding + "'" + item + "' : function() { ... },\n";
346                     continue;
347                 }
348                 if(typeof(value) == 'object') { //If it is an array,
349                     dumped_text += level_padding + "'" + item + "': {\n";
350                     dumped_text += this.dump(value,level+1);
351                     dumped_text += level_padding + "}\n";
352                     continue;
353                 }
354                 dumped_text += level_padding + "'" + item + "' : \"" + value + "\"\n";
355                 
356             }
357         } else { //Stings/Chars/Numbers etc.
358             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
359         }
360         return dumped_text;
361     },
362     findNode : function(ftg , method) {
363         if (!ftg) {
364             return; false
365         }
366       // console.log(ftg.id);
367         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
368             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
369             this[method]( nid );
370             return true;
371         }
372         // needs fixing..
373         if (ftg.dom.className.match(/[0-9]+/)) {
374             //console.log(ftg.dom.className);
375             var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
376             if (cmat) {
377                 this[method]( cmat[1] );
378                 return true;
379             }
380         }
381         
382         
383         
384         
385         return false;
386     },
387     
388     overPos: function(x,y) 
389     {
390         
391         var el = document.elementFromPoint(x,y);
392        // //console.log(el.id);
393        // console.log(document.body.innerHTML);
394         this.hover( {
395             getTarget : function () {
396                 return el;
397             },
398             stopEvent : function() {
399                 
400             }
401         });
402         
403         
404     },
405     onclick: function(e) {
406         var tg = Roo.get(e.getTarget());
407         if (!tg) {
408             //console.log('no target');
409             return;
410            }
411          
412         if (this.findNode(tg,'logClick')) {
413             return;
414         }
415         var dp = Roo.get(tg.up(''));
416         if (dp && this.findNode(dp,'logClick')) {
417             return;
418         }
419         
420         var ns = Roo.get(tg.getNextSibling());
421         if (ns && this.findNode(ns,'logClick')) {
422           
423             return;
424         }
425         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
426             return;
427         }
428         
429         for(var i =0; i < 5; i++) {
430             tg = Roo.get(tg.up(''));
431             if (!tg) {
432                 //console.log('no parent' + i);
433                 return;
434             }
435             if (tg && this.findNode(tg,'logClick')) {
436                 return;
437             }
438         }
439         //console.log('no target in parents');
440         
441     },
442     logClick : function(id) 
443     {
444          var bid = id.length ? 'builder-' + id : '';
445          console.log('{ "id" :  "' + bid + '"}');
446     },
447     
448     
449     hover : function(e) {
450         
451        
452         var tg = Roo.get(e.getTarget());
453         if (!tg) {
454             //console.log('no target');
455             this.logMove('');
456             return;
457            }
458          
459         if (this.findNode(tg,'logMove')) {
460             e.stopEvent();
461             return;
462         }
463         var dp = Roo.get(tg.up(''));
464         if (dp && this.findNode(dp,'logMove')) {
465             e.stopEvent();
466             return;
467         }
468         
469         var ns = Roo.get(tg.getNextSibling());
470         if (ns && this.findNode(ns,'logMove')) {
471             e.stopEvent();
472             return;
473         }
474         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
475             e.stopEvent();
476             return;
477         }
478         
479         for(var i =0; i < 5; i++) {
480             tg = Roo.get(tg.up(''));
481             if (!tg) {
482                 //console.log('no parent' + i);
483                 this.logMove('');
484                 return;
485             }
486             if (tg && this.findNode(tg,'logMove')) {
487                 e.stopEvent();
488                 return;
489             }
490         }
491         //console.log('no target in parents');
492         this.logMove('');
493     },
494     logMove : function (id) {
495         //console.log("LOGMOVE: " + id);
496         
497         if (this.lastID === id) {
498             return;
499        }
500        id = ''+ id;
501        var bid = id.length ? 'builder-' + id : '';
502        console.log('{ "hover-node" :  "' + bid + '"}');
503        this.lastID = id;
504     }
505             
506     
507 };
508