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