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