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