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