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