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