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