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