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