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