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