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