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