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