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         
260         //The padding given at the beginning of the line.
261         var level_padding = "";
262         for(var j=0;j<level+1;j++) level_padding += "    ";
263         
264         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
265             for(var item in arr) {
266                 var value = arr[item];
267                 
268                 if(typeof(value) == 'function') { //If it is an array,
269                     // fake dump...
270                     dumped_text += level_padding + "'" + item + "' => \"FUNCTION\"\n";
271                     continue;
272                 }
273                 if(typeof(value) == 'object') { //If it is an array,
274                     dumped_text += level_padding + "'" + item + "' ...\n";
275                     dumped_text += this.dump(value,level+1);
276                 } else {
277                     dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
278                 }
279             }
280         } else { //Stings/Chars/Numbers etc.
281             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
282         }
283         return dumped_text;
284     },
285     findNode : function(ftg , method) {
286         if (!ftg) {
287             return; false
288         }
289       // console.log(ftg.id);
290         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
291             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
292             this[method]( nid );
293             return true;
294         }
295         // needs fixing..
296         console.log(ftg.dom.className);
297         var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
298         
299         if (cmat) {
300             this[method]( cmat[1] );
301             return true;
302         }
303         
304         return false;
305     },
306     
307     overPos: function(x,y) 
308     {
309         
310         var el = document.elementFromPoint(x,y);
311        // //console.log(el.id);
312        // console.log(document.body.innerHTML);
313         this.hover( {
314             getTarget : function () {
315                 return el;
316             },
317             stopEvent : function() {
318                 
319             }
320         });
321         
322         
323     },
324     onclick: function(e) {
325         var tg = Roo.get(e.getTarget());
326         if (!tg) {
327             //console.log('no target');
328             return;
329            }
330          
331         if (this.findNode(tg,'logClick')) {
332             return;
333         }
334         var dp = Roo.get(tg.up(''));
335         if (dp && this.findNode(dp,'logClick')) {
336             return;
337         }
338         
339         var ns = Roo.get(tg.getNextSibling());
340         if (ns && this.findNode(ns,'logClick')) {
341           
342             return;
343         }
344         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
345             return;
346         }
347         
348         for(var i =0; i < 5; i++) {
349             tg = Roo.get(tg.up(''));
350             if (!tg) {
351                 //console.log('no parent' + i);
352                 return;
353             }
354             if (tg && this.findNode(tg,'logClick')) {
355                 return;
356             }
357         }
358         //console.log('no target in parents');
359         
360     },
361     logClick : function(id) 
362     {
363          var bid = id.length ? 'builder-' + id : '';
364          console.log('{ "id" :  "' + bid + '"}');
365     },
366     
367     
368     hover : function(e) {
369         
370        
371         var tg = Roo.get(e.getTarget());
372         if (!tg) {
373             //console.log('no target');
374             this.logMove('');
375             return;
376            }
377          
378         if (this.findNode(tg,'logMove')) {
379             e.stopEvent();
380             return;
381         }
382         var dp = Roo.get(tg.up(''));
383         if (dp && this.findNode(dp,'logMove')) {
384             e.stopEvent();
385             return;
386         }
387         
388         var ns = Roo.get(tg.getNextSibling());
389         if (ns && this.findNode(ns,'logMove')) {
390             e.stopEvent();
391             return;
392         }
393         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
394             e.stopEvent();
395             return;
396         }
397         
398         for(var i =0; i < 5; i++) {
399             tg = Roo.get(tg.up(''));
400             if (!tg) {
401                 //console.log('no parent' + i);
402                 this.logMove('');
403                 return;
404             }
405             if (tg && this.findNode(tg,'logMove')) {
406                 e.stopEvent();
407                 return;
408             }
409         }
410         //console.log('no target in parents');
411         this.logMove('');
412     },
413     logMove : function (id) {
414         //console.log("LOGMOVE: " + id);
415         
416         if (this.lastID === id) {
417             return;
418        }
419        id = ''+ id;
420        var bid = id.length ? 'builder-' + id : '';
421        console.log('{ "hover-node" :  "' + bid + '"}');
422        this.lastID = id;
423     }
424             
425     
426 };
427