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