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