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         var layoutroot = Roo.get( document.body).createChild({
109                 id : cfg.id
110         });
111         this.layoutbase.add( new Roo[cfg.xtype](layoutroot , cfg)); 
112         
113         
114         
115     },
116   
117     
118     munge :function (cfg)
119     {
120         var xitems = false;
121         //cfg.cls = cfg.cls || '';
122         //cfg.cls += ' ' + cfg.id;
123         if (!cfg.id) {
124             this.dump(cfg);
125         }
126         //console.log(cfg.xtype + ': ' + cfg.id);
127         
128         if (cfg.items) {
129             xitems = cfg.items;
130             delete cfg.items;
131         }
132         if (typeof(cfg.background) != 'undefined') {
133             cfg.background = false;
134         }
135         
136         var xtype = (cfg['|xns'] || '')  + '.' + (cfg.xtype || '');
137         
138         for(var p in cfg){
139             // key is not string?!?!?!!?
140             if (typeof(p) != 'string') {
141                 continue;
142             }
143             
144             if (typeof(cfg[p]) == 'object') { // listeners!!!
145                 this.munge(cfg[p]);
146                 continue;
147             }
148             // SPECIAL - PIPE
149             if (p.charAt(0) == '|') {
150                 
151                 if (!cfg[p].length) {
152                     delete cfg[p];
153                     continue;
154                 }
155                 try {
156                     var _tmp = false;
157                     
158                     var _this = this.renderObj; /// fake '_this' object..
159                     /** eval:var:_this **/
160                     /** eval:var:_tmp **/
161                     // stupid IE can not return objects evaluated..
162                     eval('_tmp =(' + cfg[p] + ')');
163                     cfg[p.substr(1)] = _tmp;
164                     //if (typeof(_tmp) == 'undefined') {
165                     //    alert(cfg[p]);
166                    // }
167                    
168                 } catch(e) {  console.log('Error evaluating: '  + cfg[p]); };
169                 delete cfg[p];
170                     
171                 
172                 continue;
173             }
174             // skip '*'
175             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
176                 delete cfg[p];
177                 continue;
178             }
179             // normal..
180               
181         }
182         
183         // we can overlay some event handlers here..
184         cfg.listeners = cfg.listeners || {};
185        
186         console.log('xtype'  + xtype)
187         switch(xtype) {
188             case 'Roo.LayoutDialog':
189                 cfg.listeners.resize = function(dlg, w,h)
190                 {
191                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
192                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
193                 }
194                 break;
195         }
196         
197         
198         
199         
200         
201         // now for all the children.. (items)
202         if (xitems === false) {
203             return;
204         }
205         cfg.items = [];
206         for (var i = 0; i < xitems.length; i++) {
207             // if +builderhide set !!!! drop it!!
208             
209             
210             var xi = xitems[i];
211             if (typeof(xi['*prop']) != 'undefined') {
212                 var pr = xi['*prop'];
213                 this.munge(xi);
214                 // if prop is an array - then it's items are really the value..
215                 if (pr.match(/\[\]$/)) {
216                     pr = pr.replace(/\[\]$/, '');
217                     cfg[pr] = cfg[pr]  || [];
218                     cfg[pr].push(xi);
219                     continue;
220                 }
221                 
222                 
223                 if (xi.xtype && xi.xtype  == 'Array') {
224                     cfg[pr] = xi.items;
225                 } else {
226                     cfg[pr] = xi;
227                 }
228                 
229                 
230                 continue;
231             }
232             this.munge(xi);
233             cfg.items.push(xi);
234         }
235         
236         if (cfg.items.length == 0) {
237             delete cfg.items;
238         }
239         
240         
241         
242     },
243     
244     
245     
246     cloneConfig : function(config) {
247                 if (!config) { return null; }
248                 var newConfig = {};
249                 for (var i in config) {
250                         if (typeof config[i] == 'object') {
251                                 newConfig[i] = this.cloneConfig(config[i]);
252                         } else if (typeof config[i] != 'function') {
253                                 newConfig[i] = config[i];
254                         }
255                 }
256                 return newConfig;
257         },
258     dump : function (arr,level) {
259         var dumped_text = "";
260         if(!level) level = 0;
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) == 'object') { //If it is an array,
271                     dumped_text += level_padding + "'" + item + "' ...\n";
272                     dumped_text += this.dump(value,level+1);
273                 } else {
274                     dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
275                 }
276             }
277         } else { //Stings/Chars/Numbers etc.
278             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
279         }
280         return dumped_text;
281     },
282     findNode : function(ftg , method) {
283         if (!ftg) {
284             return; false
285         }
286       // console.log(ftg.id);
287         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
288             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
289             this[method]( nid );
290             return true;
291         }
292         // needs fixing..
293         console.log(ftg.dom.className);
294         var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
295         
296         if (cmat) {
297             this[method]( cmat[1] );
298             return true;
299         }
300         
301         return false;
302     },
303     
304     overPos: function(x,y) 
305     {
306         
307         var el = document.elementFromPoint(x,y);
308        // //console.log(el.id);
309        // console.log(document.body.innerHTML);
310         this.hover( {
311             getTarget : function () {
312                 return el;
313             },
314             stopEvent : function() {
315                 
316             }
317         });
318         
319         
320     },
321     onclick: function(e) {
322         var tg = Roo.get(e.getTarget());
323         if (!tg) {
324             //console.log('no target');
325             return;
326            }
327          
328         if (this.findNode(tg,'logClick')) {
329             return;
330         }
331         var dp = Roo.get(tg.up(''));
332         if (dp && this.findNode(dp,'logClick')) {
333             return;
334         }
335         
336         var ns = Roo.get(tg.getNextSibling());
337         if (ns && this.findNode(ns,'logClick')) {
338           
339             return;
340         }
341         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
342             return;
343         }
344         
345         for(var i =0; i < 5; i++) {
346             tg = Roo.get(tg.up(''));
347             if (!tg) {
348                 //console.log('no parent' + i);
349                 return;
350             }
351             if (tg && this.findNode(tg,'logClick')) {
352                 return;
353             }
354         }
355         //console.log('no target in parents');
356         
357     },
358     logClick : function(id) 
359     {
360          var bid = id.length ? 'builder-' + id : '';
361          console.log('{ "id" :  "' + bid + '"}');
362     },
363     
364     
365     hover : function(e) {
366         
367        
368         var tg = Roo.get(e.getTarget());
369         if (!tg) {
370             //console.log('no target');
371             this.logMove('');
372             return;
373            }
374          
375         if (this.findNode(tg,'logMove')) {
376             e.stopEvent();
377             return;
378         }
379         var dp = Roo.get(tg.up(''));
380         if (dp && this.findNode(dp,'logMove')) {
381             e.stopEvent();
382             return;
383         }
384         
385         var ns = Roo.get(tg.getNextSibling());
386         if (ns && this.findNode(ns,'logMove')) {
387             e.stopEvent();
388             return;
389         }
390         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
391             e.stopEvent();
392             return;
393         }
394         
395         for(var i =0; i < 5; i++) {
396             tg = Roo.get(tg.up(''));
397             if (!tg) {
398                 //console.log('no parent' + i);
399                 this.logMove('');
400                 return;
401             }
402             if (tg && this.findNode(tg,'logMove')) {
403                 e.stopEvent();
404                 return;
405             }
406         }
407         //console.log('no target in parents');
408         this.logMove('');
409     },
410     logMove : function (id) {
411         //console.log("LOGMOVE: " + id);
412         
413         if (this.lastID === id) {
414             return;
415        }
416        id = ''+ id;
417        var bid = id.length ? 'builder-' + id : '';
418        console.log('{ "hover-node" :  "' + bid + '"}');
419        this.lastID = id;
420     }
421             
422     
423 };
424