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