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             if (typeof(p) != 'string') {
169                 continue;
170             }
171             
172             if (typeof(cfg[p]) == 'object') { // listeners!!!
173                 this.munge(cfg[p]);
174                 continue;
175             }
176             // SPECIAL - PIPE
177             if (p.charAt(0) == '|') {
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[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