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 pp = p.charAt(0) == '|'  ? p.substring(1) : p;
191                     
192                     var _tmp = false;
193                     
194                     
195                     /** eval:var:_this **/
196                     /** eval:var:_tmp **/
197                     // stupid IE can not return objects evaluated..
198                    // console.log('_tmp =(' + cfg[p] + ')');
199                     eval('_tmp =(' + cfg[p] + ')');
200                     cfg[pp] = _tmp;
201                     
202                     //if (typeof(_tmp) == 'undefined') {
203                     //    alert(cfg[p]);
204                    // }
205                    
206                 } catch(e) {  
207                     console.log('Error evaluating: '  + cfg[p] + "\r\n" + JSON.stringify(e)); 
208                 };
209                 delete cfg[p];
210                     
211                 
212                 continue;
213             }
214             // skip '*'
215             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
216                 delete cfg[p];
217                 continue;
218             }
219             // normal..
220               
221         }
222         
223         if (cfg.xtype && cfg.xtype.match(/^Roo\./) && !cfg.xns) {
224             // xtype contains full path..
225             var bits = cfg.xtype.split('.');
226             bits.shift(); // remove roo..
227             cfg.xtype = bits.pop(); // get the last bit..
228             cfg.xns = Roo;
229             while (bits.length) {
230                 cfg.xns = cfg.xns[bits.shift()]; 
231             }
232              
233         }
234         if (cfg.xtype) {
235             if (!cfg.xns || typeof(cfg.xns[cfg.xtype]) == 'undefined') {
236                 throw "Invalid Xtype " + cfg.xtype + ' on ' + cfg.xtreepath;
237             }
238         }
239         
240         // we can overlay some event handlers here..
241         cfg.listeners = cfg.listeners || {};
242        
243         //console.log('xtype'  + xtype)
244         switch(xtype) {
245             case 'Roo.LayoutDialog':
246                 cfg.listeners.resize = function(dlg, w,h)
247                 {
248                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
249                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
250                 }
251                 break;
252         }
253         
254         
255         
256         
257         
258         // now for all the children.. (items)
259         if (xitems === false) {
260             return;
261         }
262         cfg.items = [];
263         for (var i = 0; i < xitems.length; i++) {
264             // if +builderhide set !!!! drop it!!
265             
266             
267             var xi = xitems[i];
268             if (typeof(xi['*prop']) != 'undefined') {
269                 //console.log('adding prop:' + xi['*prop']);
270                 
271                 var pr = xi['*prop'];
272                 this.munge(xi);
273                 // if prop is an array - then it's items are really the value..
274                 if (pr.match(/\[\]$/)) {
275                     //console.log('adding array?:' + pr);
276                     pr = pr.replace(/\[\]$/, '');
277                     cfg[pr] = cfg[pr]  || [];
278                     cfg[pr].push(xi);
279                     continue;
280                 }
281                 
282                 
283                 if (xi.xtype && xi.xtype  == 'Array') {
284                     cfg[pr] = xi.items;
285                 } else {
286                     //console.log('setting property:' + pr);
287                     cfg[pr] = xi;
288                 }
289                 
290                 
291                 continue;
292             }
293             this.munge(xi);
294             cfg.items.push(xi);
295         }
296         
297         if (cfg.items.length == 0) {
298             delete cfg.items;
299         }
300         console.log(cfg);
301         
302         
303     },
304     
305     
306     
307     cloneConfig : function(config) {
308                 if (!config) { return null; }
309                 var newConfig = {};
310                 for (var i in config) {
311                         if (typeof config[i] == 'object') {
312                                 newConfig[i] = this.cloneConfig(config[i]);
313                         } else if (typeof config[i] != 'function') {
314                                 newConfig[i] = config[i];
315                         }
316                 }
317                 return newConfig;
318         },
319     dump : function (arr,level) {
320         var dumped_text = "";
321         if(!level) level = 0;
322         if (level >  3) {
323             return '... TO DEEP ...';
324         }
325         //The padding given at the beginning of the line.
326         var level_padding = "";
327         for(var j=0;j<level+1;j++) level_padding += "    ";
328         
329         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
330             for(var item in arr) {
331                 
332                 var value = arr[item];
333                 if (item == 'xns') {
334                     continue;
335                 }
336                 if(typeof(value) == 'function') { //If it is an array,
337                     // fake dump...
338                     dumped_text += level_padding + "'" + item + "' : function() { ... },\n";
339                     continue;
340                 }
341                 if(typeof(value) == 'object') { //If it is an array,
342                     dumped_text += level_padding + "'" + item + "': {\n";
343                     dumped_text += this.dump(value,level+1);
344                     dumped_text += level_padding + "}\n";
345                     continue;
346                 }
347                 dumped_text += level_padding + "'" + item + "' : \"" + value + "\"\n";
348                 
349             }
350         } else { //Stings/Chars/Numbers etc.
351             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
352         }
353         return dumped_text;
354     },
355     findNode : function(ftg , method) {
356         if (!ftg) {
357             return; false
358         }
359       // console.log(ftg.id);
360         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
361             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
362             this[method]( nid );
363             return true;
364         }
365         // needs fixing..
366         if (ftg.dom.className.match(/[0-9]+/)) {
367             //console.log(ftg.dom.className);
368             var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
369             if (cmat) {
370                 this[method]( cmat[1] );
371                 return true;
372             }
373         }
374         
375         
376         
377         
378         return false;
379     },
380     
381     overPos: function(x,y) 
382     {
383         
384         var el = document.elementFromPoint(x,y);
385        // //console.log(el.id);
386        // console.log(document.body.innerHTML);
387         this.hover( {
388             getTarget : function () {
389                 return el;
390             },
391             stopEvent : function() {
392                 
393             }
394         });
395         
396         
397     },
398     onclick: function(e) {
399         var tg = Roo.get(e.getTarget());
400         if (!tg) {
401             //console.log('no target');
402             return;
403            }
404          
405         if (this.findNode(tg,'logClick')) {
406             return;
407         }
408         var dp = Roo.get(tg.up(''));
409         if (dp && this.findNode(dp,'logClick')) {
410             return;
411         }
412         
413         var ns = Roo.get(tg.getNextSibling());
414         if (ns && this.findNode(ns,'logClick')) {
415           
416             return;
417         }
418         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
419             return;
420         }
421         
422         for(var i =0; i < 5; i++) {
423             tg = Roo.get(tg.up(''));
424             if (!tg) {
425                 //console.log('no parent' + i);
426                 return;
427             }
428             if (tg && this.findNode(tg,'logClick')) {
429                 return;
430             }
431         }
432         //console.log('no target in parents');
433         
434     },
435     logClick : function(id) 
436     {
437          var bid = id.length ? 'builder-' + id : '';
438          console.log('{ "id" :  "' + bid + '"}');
439     },
440     
441     
442     hover : function(e) {
443         
444        
445         var tg = Roo.get(e.getTarget());
446         if (!tg) {
447             //console.log('no target');
448             this.logMove('');
449             return;
450            }
451          
452         if (this.findNode(tg,'logMove')) {
453             e.stopEvent();
454             return;
455         }
456         var dp = Roo.get(tg.up(''));
457         if (dp && this.findNode(dp,'logMove')) {
458             e.stopEvent();
459             return;
460         }
461         
462         var ns = Roo.get(tg.getNextSibling());
463         if (ns && this.findNode(ns,'logMove')) {
464             e.stopEvent();
465             return;
466         }
467         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
468             e.stopEvent();
469             return;
470         }
471         
472         for(var i =0; i < 5; i++) {
473             tg = Roo.get(tg.up(''));
474             if (!tg) {
475                 //console.log('no parent' + i);
476                 this.logMove('');
477                 return;
478             }
479             if (tg && this.findNode(tg,'logMove')) {
480                 e.stopEvent();
481                 return;
482             }
483         }
484         //console.log('no target in parents');
485         this.logMove('');
486     },
487     logMove : function (id) {
488         //console.log("LOGMOVE: " + id);
489         
490         if (this.lastID === id) {
491             return;
492        }
493        id = ''+ id;
494        var bid = id.length ? 'builder-' + id : '';
495        console.log('{ "hover-node" :  "' + bid + '"}');
496        this.lastID = id;
497     }
498             
499     
500 };
501