builder.html.js
[app.Builder.js] / builder.html.js
1 //<script type="text/javascript">
2  
3
4 var MODULE = { isBuilder : true };
5 // BC
6 var _this = MODULE;
7
8 // the apprenderer.
9 Builder  = {
10     
11     scriptTag : false,
12     
13     id : 1,
14     
15     render : function(data)
16     {
17         // for debugging 
18          //console.log(data);        return;
19         if (this.scriptTag) { 
20             document.body.removeChild(this.scriptTag);
21             this.scriptTag = false;
22         }
23         
24         this.scriptTag = document.body.appendChild(document.createElement('script'));
25         this.scriptTag.setAttribute('type','text/javascript');
26          
27         this.id++;
28         this.scriptTag.appendChild(
29                     document.createTextNode(
30                             'Builder._src_' + this.id + ' =  ' + JSON.stringify(data, null,4)
31         ));
32          
33         
34         Roo.log(this.tree);
35         MODULE = { isBuilder : true }; 
36         _this = MODULE;
37         if (!Builder.click) {
38             Builder.click= Roo.get(document.body).on('click', this.onclick, this);
39         }
40         
41         var  ix = '_src' + this.id;
42         var  wait_for_tree() = function() {
43             Builder.tree = Builder[ix];
44             if (!Builder.tree) {
45                 Roo.log("Wating for tree");
46                 wait_for_tree.defer(100);
47                 return;
48             }
49              Builder.redraw(false);
50         }
51         wait_for_tree.defer(100);
52     },
53     
54     
55     
56     tree : {}, 
57     renderObj :  { isBuilder : true },
58     dialogroot : false,
59     
60     redrawClear : function(isAuto)
61     {
62         this.renderObj = { isBuilder : true };
63         
64         this.scroll = {
65             top:0,
66             left:0
67         };
68         
69          
70         
71         //if (this.panelroot) {
72         //    this.scroll = this.panelroot.el.getScroll();
73         //    this.layout.remove('center', this.panelroot);
74         //    this.panelroot = false;
75         //}
76         if (this.dialogroot) {
77             this.dialogroot.remove();
78             this.dialogroot = false;
79         }
80         if (this.layoutbase) {
81             //console.log(Builder.dump(this.layoutbase.el));
82             
83             
84             try {
85                 var pan = this.layoutbase.getRegion('center').getPanel(0);
86                 if (pan) {
87                     this.layoutbase.remove('center', pan);
88                 }
89                 
90                 
91             } catch( e) {
92                 console.log(e);
93                 console.log(JSON.stringify(e));
94                 // reload!!?
95             }
96             
97             
98             
99             //this.layoutbase= false;
100         }
101          
102         
103     },
104     
105     
106     redraw: function(isAuto)
107     {
108         
109         // top level is not relivant
110         this.redrawClear(isAuto);
111         
112         
113         var cfg =  this.tree;
114         //console.log(this.dump(cfg));
115         if (!cfg) {
116             return;
117         }
118         
119         
120         this.munge(cfg);
121         this.cfg = cfg;
122         //console.log(this.dump(cfg)); 
123         // we draw either a dialog or a tab..
124         
125         if (cfg.xtype == 'LayoutDialog') {
126             
127             cfg.modal = false;
128             //var xy  = Pman.Tab.BuilderPanel.panel.el.getXY();
129             //cfg.items[0].x = xy[0];
130             //cfg.items[0].y = xy[1];
131             //cfg.items[0].constraintoviewport = false;
132         
133             this.dialogroot = Roo.get( document.body).createChild({
134                 id : cfg.id
135             });
136             
137             
138             MODULE.dialog = new Roo[cfg.xtype](this.dialogroot, cfg);
139             //this.dialog.el.on('click', this.panelClick, this);
140             MODULE.dialog.show();
141             return;
142             
143         }
144         
145         // otherwise we are creating a layout area on the document..
146         
147          
148         // handles 
149         // contentpanel, nestedlayoutpanel, contentpanel.. etc. 
150         // force center region..
151         cfg.region = 'center';
152         cfg.background = false;
153         if (!this.layoutbase) {
154                 
155             this.layoutbase = new Ext.BorderLayout(document.body, {
156               
157                  center: {
158                     titlebar: false,
159                     autoScroll:false,
160                     closeOnTab: true,
161                     tabPosition: 'top',
162                     //resizeTabs: true,
163                     alwaysShowTabs: true,
164                     minTabWidth: 140
165                 }
166             });
167         }
168         try {
169             console.log("ADDING CFG");    
170             console.log(cfg)
171             this.layoutbase.addxtype(  cfg ); 
172         } catch (e) {
173             console.log("GOT ERROR? - saved in Builder.lastError");
174             Builder.lastError = e;
175             console.log(e);
176             console.log(typeof(e));
177             
178             console.log(this.dump(e));
179             console.trace();
180         }
181         
182         
183     },
184   
185     
186     munge :function (cfg, isListener)
187     {
188         var xitems = false;
189         //cfg.cls = cfg.cls || '';
190         //cfg.cls += ' ' + cfg.id;
191         if (!cfg.id) {
192             this.dump(cfg);
193         }
194         
195         //console.log(cfg.xtype + ': ' + cfg.id);
196         
197         if (cfg.items) {
198             xitems = cfg.items;
199             delete cfg.items;
200         }
201         if (typeof(cfg.background) != 'undefined') {
202             cfg.background = false;
203         }
204         
205         var xtype = (cfg['|xns'] || '')  + '.' + (cfg.xtype || '');
206         
207         for(var p in cfg){
208             // key is not string?!?!?!!?
209           
210             if (typeof(p) != 'string') {
211                 continue;
212             }
213             
214             if (typeof(cfg[p]) == 'object') { // listeners!!!
215                 this.munge(cfg[p], p == 'listeners');
216                 continue;
217             }
218             // SPECIAL - PIPE
219             if (p.charAt(0) == '|' || isListener) {
220                 
221                 if (!cfg[p].length) {
222                     delete cfg[p];
223                     continue;
224                 }
225                 var pp = p.charAt(0) == '|'  ? p.substring(1) : p;
226                 try {
227                     
228                     
229                     var _tmp = false;
230                     
231                     /** eval:var:MOUDULE **/
232                     /** eval:var:_this **/
233                     /** eval:var:_tmp **/
234                     // stupid IE can not return objects evaluated..
235                    // console.log('_tmp =(' + cfg[p] + ')');
236                     eval('_tmp =(' + cfg[p] + ')');
237                     cfg[pp] = _tmp;
238                     
239                     //if (typeof(_tmp) == 'undefined') {
240                     //    alert(cfg[p]);
241                    // }
242                    
243                 } catch(e) {  
244                     console.log('Error evaluating: '  + cfg[p] + "\r\n" + JSON.stringify(e)); 
245                 };
246                 if (pp != p) {
247                     delete cfg[p];
248                 }
249                 
250                     
251                 
252                 continue;
253             }
254             // skip '*'
255             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
256                 delete cfg[p];
257                 continue;
258             }
259             // normal..
260               
261         }
262         
263         if (cfg.xtype && cfg.xtype.match(/^Roo\./) && !cfg.xns) {
264             // xtype contains full path..
265             var bits = cfg.xtype.split('.');
266             bits.shift(); // remove roo..
267             cfg.xtype = bits.pop(); // get the last bit..
268             cfg.xns = Roo;
269             while (bits.length) {
270                 cfg.xns = cfg.xns[bits.shift()]; 
271             }
272              
273         }
274         if (cfg.xtype) {
275             if (!cfg.xns || typeof(cfg.xns[cfg.xtype]) == 'undefined') {
276                 throw "Invalid Xtype " + cfg.xtype + ' on ' + cfg.xtreepath;
277             }
278         }
279         if (!isListener) {
280             cfg.listeners = cfg.listeners || {};
281         }
282         // we can overlay some event handlers here..
283         
284        
285         //console.log('xtype'  + xtype)
286         switch(xtype) {
287             case 'Roo.LayoutDialog':
288                 cfg.listeners.resize = function(dlg, w,h)
289                 {
290                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
291                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
292                 }
293                 break;
294         }
295         
296         
297         
298         
299         
300         // now for all the children.. (items)
301         if (xitems === false) {
302             
303             return;
304         }
305         cfg.items = [];
306         for (var i = 0; i < xitems.length; i++) {
307             // if +builderhide set !!!! drop it!!
308             
309             
310             var xi = xitems[i];
311             if (typeof(xi['*prop']) != 'undefined') {
312                 //console.log('adding prop:' + xi['*prop']);
313                 
314                 var pr = xi['*prop'];
315                 this.munge(xi);
316                 // if prop is an array - then it's items are really the value..
317                 if (pr.match(/\[\]$/)) {
318                     //console.log('adding array?:' + pr);
319                     pr = pr.replace(/\[\]$/, '');
320                     cfg[pr] = cfg[pr]  || [];
321                     cfg[pr].push(xi);
322                     continue;
323                 }
324                 
325                 
326                 if (xi.xtype && xi.xtype  == 'Array') {
327                     cfg[pr] = xi.items;
328                 } else {
329                     //console.log('setting property:' + pr);
330                     cfg[pr] = xi;
331                 }
332                 
333                 
334                 continue;
335             }
336             this.munge(xi);
337             cfg.items.push(xi);
338         }
339         
340         if (cfg.items.length == 0) {
341             delete cfg.items;
342         }
343         console.log(cfg);
344         
345         
346     },
347     
348     
349     
350     cloneConfig : function(config) {
351                 if (!config) { return null; }
352                 var newConfig = {};
353                 for (var i in config) {
354                         if (typeof config[i] == 'object') {
355                                 newConfig[i] = this.cloneConfig(config[i]);
356                         } else if (typeof config[i] != 'function') {
357                                 newConfig[i] = config[i];
358                         }
359                 }
360                 return newConfig;
361         },
362     dump : function (arr,level) {
363         var dumped_text = "";
364         if(!level) level = 0;
365         if (level >  3) {
366             return '... TO DEEP ...';
367         }
368         //The padding given at the beginning of the line.
369         var level_padding = "";
370         for(var j=0;j<level+1;j++) level_padding += "    ";
371         
372         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
373             for(var item in arr) {
374                 
375                 var value = arr[item];
376                 if (item == 'xns') {
377                     continue;
378                 }
379                 if(typeof(value) == 'function') { //If it is an array,
380                     // fake dump...
381                     dumped_text += level_padding + "'" + item + "' : function() { ... },\n";
382                     continue;
383                 }
384                 if(typeof(value) == 'object') { //If it is an array,
385                     dumped_text += level_padding + "'" + item + "': {\n";
386                     dumped_text += this.dump(value,level+1);
387                     dumped_text += level_padding + "}\n";
388                     continue;
389                 }
390                 dumped_text += level_padding + "'" + item + "' : \"" + value + "\"\n";
391                 
392             }
393         } else { //Stings/Chars/Numbers etc.
394             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
395         }
396         return dumped_text;
397     },
398     findNode : function(ftg , method) {
399         if (!ftg) {
400             return; false
401         }
402       // console.log(ftg.id);
403         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
404             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
405             this[method]( nid );
406             return true;
407         }
408         // needs fixing..
409         if (ftg.dom.className.match(/[0-9]+/)) {
410             //console.log(ftg.dom.className);
411             var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
412             if (cmat) {
413                 this[method]( cmat[1] );
414                 return true;
415             }
416         }
417         
418         
419         
420         
421         return false;
422     },
423     
424     overPos: function(x,y) 
425     {
426         
427         var el = document.elementFromPoint(x,y);
428        // //console.log(el.id);
429        // console.log(document.body.innerHTML);
430         this.hover( {
431             getTarget : function () {
432                 return el;
433             },
434             stopEvent : function() {
435                 
436             }
437         });
438         
439         
440     },
441     onclick: function(e) {
442         var tg = Roo.get(e.getTarget());
443         if (!tg) {
444             //console.log('no target');
445             return;
446            }
447          
448         if (this.findNode(tg,'logClick')) {
449             return;
450         }
451         var dp = Roo.get(tg.up(''));
452         if (dp && this.findNode(dp,'logClick')) {
453             return;
454         }
455         
456         var ns = Roo.get(tg.getNextSibling());
457         if (ns && this.findNode(ns,'logClick')) {
458           
459             return;
460         }
461         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
462             return;
463         }
464         
465         for(var i =0; i < 5; i++) {
466             tg = Roo.get(tg.up(''));
467             if (!tg) {
468                 //console.log('no parent' + i);
469                 return;
470             }
471             if (tg && this.findNode(tg,'logClick')) {
472                 return;
473             }
474         }
475         //console.log('no target in parents');
476         
477     },
478     logClick : function(id) 
479     {
480          var bid = id.length ? 'builder-' + id : '';
481          console.log('{ "id" :  "' + bid + '"}');
482     },
483     
484     
485     hover : function(e) {
486         
487        
488         var tg = Roo.get(e.getTarget());
489         if (!tg) {
490             //console.log('no target');
491             this.logMove('');
492             return;
493            }
494          
495         if (this.findNode(tg,'logMove')) {
496             e.stopEvent();
497             return;
498         }
499         var dp = Roo.get(tg.up(''));
500         if (dp && this.findNode(dp,'logMove')) {
501             e.stopEvent();
502             return;
503         }
504         
505         var ns = Roo.get(tg.getNextSibling());
506         if (ns && this.findNode(ns,'logMove')) {
507             e.stopEvent();
508             return;
509         }
510         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
511             e.stopEvent();
512             return;
513         }
514         
515         for(var i =0; i < 5; i++) {
516             tg = Roo.get(tg.up(''));
517             if (!tg) {
518                 //console.log('no parent' + i);
519                 this.logMove('');
520                 return;
521             }
522             if (tg && this.findNode(tg,'logMove')) {
523                 e.stopEvent();
524                 return;
525             }
526         }
527         //console.log('no target in parents');
528         this.logMove('');
529     },
530     logMove : function (id) {
531         //console.log("LOGMOVE: " + id);
532         
533         if (this.lastID === id) {
534             return;
535        }
536        id = ''+ id;
537        var bid = id.length ? 'builder-' + id : '';
538        console.log('{ "hover-node" :  "' + bid + '"}');
539        this.lastID = id;
540     }
541             
542     
543 };
544