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