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         cfg.listeners = cfg.listeners || {};
179         cfg.listeners.click = function(e) {
180             console.log('{ "id" : "' + e.id + '}');
181         };
182         console.log('xtype'  + xtype)
183         switch(xtype) {
184             case 'Roo.LayoutDialog':
185                 cfg.listeners.resize = function(dlg, w,h)
186                 {
187                     console.log('{ "id" : "' + dlg.id + '", "set" : "width", "value": ' + w + '}');
188                     console.log('{ "id" : "' + dlg.id + '", "set" : "height", "value": ' + h + '}');
189                 }
190                 break;
191         }
192         
193         
194         
195         
196         
197         // now for all the children.. (items)
198         if (xitems === false) {
199             return;
200         }
201         cfg.items = [];
202         for (var i = 0; i < xitems.length; i++) {
203             // if +builderhide set !!!! drop it!!
204             
205             
206             var xi = xitems[i];
207             if (typeof(xi['*prop']) != 'undefined') {
208                 var pr = xi['*prop'];
209                 this.munge(xi);
210                 // if prop is an array - then it's items are really the value..
211                 
212                 if (xi.xtype && xi.xtype  == 'Array') {
213                     cfg[pr] = xi.items;
214                 } else {
215                     cfg[pr] = xi;
216                 }
217                 
218                 
219                 continue;
220             }
221             this.munge(xi);
222             cfg.items.push(xi);
223         }
224         
225         if (cfg.items.length == 0) {
226             delete cfg.items;
227         }
228         
229         
230         
231     },
232     cloneConfig : function(config) {
233                 if (!config) { return null; }
234                 var newConfig = {};
235                 for (var i in config) {
236                         if (typeof config[i] == 'object') {
237                                 newConfig[i] = this.cloneConfig(config[i]);
238                         } else if (typeof config[i] != 'function') {
239                                 newConfig[i] = config[i];
240                         }
241                 }
242                 return newConfig;
243         },
244     dump : function (arr,level) {
245         var dumped_text = "";
246         if(!level) level = 0;
247         
248         //The padding given at the beginning of the line.
249         var level_padding = "";
250         for(var j=0;j<level+1;j++) level_padding += "    ";
251         
252         if(typeof(arr) == 'object') { //Array/Hashes/Objects 
253             for(var item in arr) {
254                 var value = arr[item];
255                 
256                 if(typeof(value) == 'object') { //If it is an array,
257                     dumped_text += level_padding + "'" + item + "' ...\n";
258                     dumped_text += this.dump(value,level+1);
259                 } else {
260                     dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
261                 }
262             }
263         } else { //Stings/Chars/Numbers etc.
264             dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
265         }
266         return dumped_text;
267     },
268     findNode : function(ftg) {
269         if (!ftg) {
270             return; false
271         }
272       // console.log(ftg.id);
273         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
274             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
275             this.logMove( nid );
276             return true;
277         }
278         // needs fixing..
279         //console.log(ftg.dom.className);
280         var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9]+)/);
281         
282         if (cmat) {
283             this.logMove( cmat[1] );
284             return true;
285         }
286         
287         return false;
288     },
289     
290     overPos: function(x,y) 
291     {
292         
293         var el = document.elementFromPoint(x,y);
294        // //console.log(el.id);
295        // console.log(document.body.innerHTML);
296         this.hover( {
297             getTarget : function () {
298                 return el;
299             },
300             stopEvent : function() {
301                 
302             }
303         });
304         
305         
306     },
307     
308     
309     hover : function(e) {
310         
311        
312         var tg = Roo.get(e.getTarget());
313         if (!tg) {
314             //console.log('no target');
315             this.logMove('');
316             return;
317            }
318          
319         if (this.findNode(tg)) {
320             e.stopEvent();
321             return;
322         }
323         var dp = Roo.get(tg.up(''));
324         if (dp && this.findNode(dp)) {
325             e.stopEvent();
326             return;
327         }
328         
329         var ns = Roo.get(tg.getNextSibling());
330         if (ns && this.findNode(ns)) {
331             e.stopEvent();
332             return;
333         }
334         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')))) {
335             e.stopEvent();
336             return;
337         }
338         
339         for(var i =0; i < 5; i++) {
340             tg = Roo.get(tg.up(''));
341             if (!tg) {
342                 //console.log('no parent' + i);
343                 this.logMove('');
344                 return;
345             }
346             if (tg && this.findNode(tg)) {
347                 e.stopEvent();
348                 return;
349             }
350         }
351         //console.log('no target in parents');
352         this.logMove('');
353     },
354     logMove : function (id) {
355         //console.log("LOGMOVE: " + id);
356         
357         if (this.lastID === id) {
358             return;
359        }
360        id = ''+ id;
361        var bid = id.length ? 'builder-' + id : '';
362        console.log('{ "hover-node" :  "' + bid + '"}');
363        this.lastID = id;
364     }
365             
366     
367 };
368