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         this.tree = data;
9         
10         if (!Builder.click) {
11             Builder.click= Roo.get(document.body).on('click', this.onclick, this);
12          
13         }
14         
15         this.redraw(false);
16     },
17     
18     
19     tree : {}, 
20     renderObj :  { isBuilder : true },
21     dialogroot : false,
22     
23     redrawClear : function(isAuto)
24     {
25         this.renderObj = { isBuilder : true };
26         
27         this.scroll = {
28             top:0,
29             left:0
30         };
31         
32          
33         
34         //if (this.panelroot) {
35         //    this.scroll = this.panelroot.el.getScroll();
36         //    this.layout.remove('center', this.panelroot);
37         //    this.panelroot = false;
38         //}
39         if (this.dialogroot) {
40             this.dialogroot.remove();
41             this.dialogroot = false;
42         }
43         if (this.layoutbase) {
44             this.layoutbase.remove();
45             this.layoutbase= false;
46         }
47          
48         
49     },
50     
51     redraw: function(isAuto)
52     {
53         
54         // top level is not relivant
55         this.redrawClear(isAuto);
56         
57         
58         var cfg =  this.tree;
59         //console.log(this.dump(cfg));
60         if (!cfg) {
61             return;
62         }
63         
64         
65         this.munge(cfg);
66         
67         // we draw either a dialog or a tab..
68         
69         if (cfg.xtype == 'LayoutDialog') {
70             
71             cfg.modal = false;
72             //var xy  = Pman.Tab.BuilderPanel.panel.el.getXY();
73             //cfg.items[0].x = xy[0];
74             //cfg.items[0].y = xy[1];
75             //cfg.items[0].constraintoviewport = false;
76         
77             this.dialogroot = Roo.get( document.body).createChild({
78                 id : cfg.id
79             });
80              
81             this.dialog = new Roo[cfg.xtype](this.dialogroot, cfg);
82             //this.dialog.el.on('click', this.panelClick, this);
83             this.dialog.show();
84             return;
85             
86         }
87         
88         // otherwise we are creating a layout area on the document..
89         
90          
91         // handles 
92         // contentpanel, nestedlayoutpanel, contentpanel.. etc. 
93         // force center region..
94         cfg.region = 'center';
95         cfg.background = false;
96         this.layoutbase = new Ext.BorderLayout(document.body, {
97           
98              center: {
99                 titlebar: false,
100                 autoScroll:false,
101                 closeOnTab: true,
102                 tabPosition: 'top',
103                 //resizeTabs: true,
104                 alwaysShowTabs: true,
105                 minTabWidth: 140
106             }
107         });
108          
109         this.layoutbase.addxtype(  cfg ); 
110         
111         
112         
113     },
114   
115     
116     munge :function (cfg)
117     {
118         var xitems = false;
119         //cfg.cls = cfg.cls || '';
120         //cfg.cls += ' ' + cfg.id;
121         if (!cfg.id) {
122             this.dump(cfg);
123         }
124         //console.log(cfg.xtype + ': ' + cfg.id);
125         
126         if (cfg.items) {
127             xitems = cfg.items;
128             delete cfg.items;
129         }
130         if (typeof(cfg.background) != 'undefined') {
131             cfg.background = false;
132         }
133         
134         var xtype = (cfg['|xns'] || '')  + '.' + (cfg.xtype || '');
135         
136         for(var p in cfg){
137             // key is not string?!?!?!!?
138             if (typeof(p) != 'string') {
139                 continue;
140             }
141             
142             if (typeof(cfg[p]) == 'object') { // listeners!!!
143                 this.munge(cfg[p]);
144                 continue;
145             }
146             // SPECIAL - PIPE
147             if (p.charAt(0) == '|') {
148                 
149                 if (!cfg[p].length) {
150                     delete cfg[p];
151                     continue;
152                 }
153                 try {
154                     var _tmp = false;
155                     
156                     var _this = this.renderObj; /// fake '_this' object..
157                     /** eval:var:_this **/
158                     /** eval:var:_tmp **/
159                     // stupid IE can not return objects evaluated..
160                     eval('_tmp =(' + cfg[p] + ')');
161                     cfg[p.substr(1)] = _tmp;
162                     //if (typeof(_tmp) == 'undefined') {
163                     //    alert(cfg[p]);
164                    // }
165                    
166                 } catch(e) {  console.log('Error evaluating: '  + cfg[p]); };
167                 delete cfg[p];
168                     
169                 
170                 continue;
171             }
172             // skip '*'
173             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
174                 delete cfg[p];
175                 continue;
176             }
177             // normal..
178               
179         }
180         
181         // we can overlay some event handlers here..
182         cfg.listeners = cfg.listeners || {};
183        
184         console.log('xtype'  + xtype)
185         switch(xtype) {
186             case 'Roo.LayoutDialog':
187                 cfg.listeners.resize = function(dlg, w,h)
188                 {
189                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
190                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
191                 }
192                 break;
193         }
194         
195         
196         
197         
198         
199         // now for all the children.. (items)
200         if (xitems === false) {
201             return;
202         }
203         cfg.items = [];
204         for (var i = 0; i < xitems.length; i++) {
205             // if +builderhide set !!!! drop it!!
206             
207             
208             var xi = xitems[i];
209             if (typeof(xi['*prop']) != 'undefined') {
210                 var pr = xi['*prop'];
211                 this.munge(xi);
212                 // if prop is an array - then it's items are really the value..
213                 if (pr.match(/\[\]$/)) {
214                     pr = pr.replace(/\[\]$/, '');
215                     cfg[pr] = cfg[pr]  || [];
216                     cfg[pr].push(xi);
217                     continue;
218                 }
219                 
220                 
221                 if (xi.xtype && xi.xtype  == 'Array') {
222                     cfg[pr] = xi.items;
223                 } else {
224                     cfg[pr] = xi;
225                 }
226                 
227                 
228                 continue;
229             }
230             this.munge(xi);
231             cfg.items.push(xi);
232         }
233         
234         if (cfg.items.length == 0) {
235             delete cfg.items;
236         }
237         
238         
239         
240     },
241     
242     
243     
244     cloneConfig : function(config) {
245                 if (!config) { return null; }
246                 var newConfig = {};
247                 for (var i in config) {
248                         if (typeof config[i] == 'object') {
249                                 newConfig[i] = this.cloneConfig(config[i]);
250                         } else if (typeof config[i] != 'function') {
251                                 newConfig[i] = config[i];
252                         }
253                 }
254                 return newConfig;
255         },
256     dump : function (arr,level) {
257         var dumped_text = "";
258         if(!level) level = 0;
259         if (level >  3) {
260             return '... TO DEEP ...';
261         }
262         //The padding given at the beginning of the line.
263         var level_padding = "";
264         for(var j=0;j<level+1;j++) level_padding += "    ";
265         
266         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
267             for(var item in arr) {
268                 var value = arr[item];
269                 
270                 if(typeof(value) == 'function') { //If it is an array,
271                     // fake dump...
272                     dumped_text += level_padding + "'" + item + "' : function() { ... },\n";
273                     continue;
274                 }
275                 if(typeof(value) == 'object') { //If it is an array,
276                     dumped_text += level_padding + "'" + item + "': {\n";
277                     dumped_text += this.dump(value,level+1);
278                     dumped_text += level_padding + "}\n";
279                     continue;
280                 }
281                 dumped_text += level_padding + "'" + item + "' : \"" + value + "\"\n";
282                 
283             }
284         } else { //Stings/Chars/Numbers etc.
285             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
286         }
287         return dumped_text;
288     },
289     findNode : function(ftg , method) {
290         if (!ftg) {
291             return; false
292         }
293       // console.log(ftg.id);
294         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
295             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
296             this[method]( nid );
297             return true;
298         }
299         // needs fixing..
300         console.log(ftg.dom.className);
301         var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
302         
303         if (cmat) {
304             this[method]( cmat[1] );
305             return true;
306         }
307         
308         return false;
309     },
310     
311     overPos: function(x,y) 
312     {
313         
314         var el = document.elementFromPoint(x,y);
315        // //console.log(el.id);
316        // console.log(document.body.innerHTML);
317         this.hover( {
318             getTarget : function () {
319                 return el;
320             },
321             stopEvent : function() {
322                 
323             }
324         });
325         
326         
327     },
328     onclick: function(e) {
329         var tg = Roo.get(e.getTarget());
330         if (!tg) {
331             //console.log('no target');
332             return;
333            }
334          
335         if (this.findNode(tg,'logClick')) {
336             return;
337         }
338         var dp = Roo.get(tg.up(''));
339         if (dp && this.findNode(dp,'logClick')) {
340             return;
341         }
342         
343         var ns = Roo.get(tg.getNextSibling());
344         if (ns && this.findNode(ns,'logClick')) {
345           
346             return;
347         }
348         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
349             return;
350         }
351         
352         for(var i =0; i < 5; i++) {
353             tg = Roo.get(tg.up(''));
354             if (!tg) {
355                 //console.log('no parent' + i);
356                 return;
357             }
358             if (tg && this.findNode(tg,'logClick')) {
359                 return;
360             }
361         }
362         //console.log('no target in parents');
363         
364     },
365     logClick : function(id) 
366     {
367          var bid = id.length ? 'builder-' + id : '';
368          console.log('{ "id" :  "' + bid + '"}');
369     },
370     
371     
372     hover : function(e) {
373         
374        
375         var tg = Roo.get(e.getTarget());
376         if (!tg) {
377             //console.log('no target');
378             this.logMove('');
379             return;
380            }
381          
382         if (this.findNode(tg,'logMove')) {
383             e.stopEvent();
384             return;
385         }
386         var dp = Roo.get(tg.up(''));
387         if (dp && this.findNode(dp,'logMove')) {
388             e.stopEvent();
389             return;
390         }
391         
392         var ns = Roo.get(tg.getNextSibling());
393         if (ns && this.findNode(ns,'logMove')) {
394             e.stopEvent();
395             return;
396         }
397         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
398             e.stopEvent();
399             return;
400         }
401         
402         for(var i =0; i < 5; i++) {
403             tg = Roo.get(tg.up(''));
404             if (!tg) {
405                 //console.log('no parent' + i);
406                 this.logMove('');
407                 return;
408             }
409             if (tg && this.findNode(tg,'logMove')) {
410                 e.stopEvent();
411                 return;
412             }
413         }
414         //console.log('no target in parents');
415         this.logMove('');
416     },
417     logMove : function (id) {
418         //console.log("LOGMOVE: " + id);
419         
420         if (this.lastID === id) {
421             return;
422        }
423        id = ''+ id;
424        var bid = id.length ? 'builder-' + id : '';
425        console.log('{ "hover-node" :  "' + bid + '"}');
426        this.lastID = id;
427     }
428             
429     
430 };
431