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