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