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