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