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