aca052131e784f43d641c3ac485dc2bc1da5a2ce
[app.Builder.js] / resources / roo.builder.js
1 //<script type="text/javascript">
2
3
4 // IPC: - via alert("IPC:{method}:{data}
5
6
7 Roo.XComponent.on("buildcomplete", Builder.saveHTML);
8
9 var MODULE = { isBuilder : true };
10 // BC
11 var _this = MODULE;
12
13 // the apprenderer.
14 Builder  = {
15      
16     scriptTag : false,
17     
18     id : 1,
19
20
21   
22     saveHTML :  function( ) 
23         {
24             //print("TRAVERSE DOM?");
25             
26             var dom = document.body;
27             //print(dom);
28             var ret = '';
29             //Roo.select('body > div',true).each(function(el) {
30             // if the tree is not ready yet?
31             
32             
33             this.traverseDOMTree(function(s) { ret+=s; }, dom, 1);
34                 alert("IPC:SAVEHTML:" + ret);
35             return ret;
36         },
37         
38         
39         traverseDOMTree : function(cb, currentElement, depth) {
40             if (!currentElement) {
41                 
42                 return;
43             }
44             console.log(currentElement);
45             if (currentElement.className.match(/roo-dynamic/)) {
46                 return;
47             }
48             
49             //Roo.log(currentElement);
50             var j;
51             var nodeName = currentElement.nodeName;
52             var tagName = currentElement.tagName;
53             
54             if  (nodeName == '#text') {
55                 cb(currentElement.nodeValue);
56                 return;
57             
58             }
59              
60             
61             
62             if(nodeName == 'BR'){
63                 cb("<BR/>");
64                 return;
65             }
66             if (nodeName != 'BODY') {
67                 
68             
69             
70                 var i = 0;
71               // Prints the node tagName, such as <A>, <IMG>, etc
72                 if (tagName) {
73                     var attr = [];
74                     for(i = 0; i < currentElement.attributes.length;i++) {
75                         var aname = currentElement.attributes.item(i).name;
76                         if (aname=='id') {
77                             aname= 'xbuilderid';
78                         }
79                         // skip
80                         if (currentElement.attributes.item(i).value == 'builderel') {
81                             return;
82                         }
83                         attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
84                     }
85                     
86                     
87                     cb("<"+currentElement.tagName+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
88                 } 
89                 else {
90                   cb("[unknown tag]");
91                 }
92             } else {
93                 tagName = false;
94             }
95             // Traverse the tree
96             i = 0;
97             var currentElementChild = currentElement.childNodes.item(i);
98             var allText = true;
99             while (currentElementChild) {
100                 // Formatting code (indent the tree so it looks nice on the screen)
101                 
102                 if  (currentElementChild.nodeName == '#text') {
103                     cb(currentElementChild.nodeValue);
104                     i++;
105                     currentElementChild=currentElement.childNodes.item(i);
106                     continue;
107                 }   
108                 allText = false;
109                 cb("\n");
110                 for (j = 0; j < depth; j++) {
111                   // &#166 is just a vertical line
112                   cb("  ");
113                 }               
114                 
115                     
116                 // Recursively traverse the tree structure of the child node
117                 this.traverseDOMTree(cb, currentElementChild, depth+1);
118                 i++;
119                 currentElementChild=currentElement.childNodes.item(i);
120             }
121             if (!allText) {
122                     // The remaining code is mostly for formatting the tree
123                 cb("\n");
124                 for (j = 0; j < depth - 1; j++) {
125                   cb("  ");
126                 }     
127             }
128             if (tagName) {
129                 cb("</"+tagName+">");
130             }
131             
132         },
133
134
135         // this lot is to deal with draging // selecting? - not used at present
136         // 
137         
138      
139     findNode : function(ftg , method) {
140         if (!ftg) {
141             return; false
142         }
143       // console.log(ftg.id);
144         if (ftg.id && typeof(ftg.id) == 'string' && ftg.id.match(/builder-/)) {
145             var nid = ftg.id.replace('builder-', '').replace('x-form-el-', '');
146             this[method]( nid );
147             return true;
148         }
149         // needs fixing..
150         if (ftg.dom.className.match(/[0-9]+/)) {
151             //console.log(ftg.dom.className);
152             var cmat = ftg.dom.className.match(/x-grid-hd-builder-(form-gen-[0-9:]+)/);
153             if (cmat) {
154                 this[method]( cmat[1] );
155                 return true;
156             }
157         }
158         
159         
160         
161         
162         return false;
163     },
164     
165     overPos: function(x,y) 
166     {
167         
168         var el = document.elementFromPoint(x,y);
169        // //console.log(el.id);
170        // console.log(document.body.innerHTML);
171         this.hover( {
172             getTarget : function () {
173                 return el;
174             },
175             stopEvent : function() {
176                 
177             }
178         });
179         
180         
181     },
182     onclick: function(e) {
183         var tg = Roo.get(e.getTarget());
184         if (!tg) {
185             //console.log('no target');
186             return;
187            }
188          
189         if (this.findNode(tg,'logClick')) {
190             return;
191         }
192         var dp = Roo.get(tg.up(''));
193         if (dp && this.findNode(dp,'logClick')) {
194             return;
195         }
196         
197         var ns = Roo.get(tg.getNextSibling());
198         if (ns && this.findNode(ns,'logClick')) {
199           
200             return;
201         }
202         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logClick') ) {
203             return;
204         }
205         
206         for(var i =0; i < 5; i++) {
207             tg = Roo.get(tg.up(''));
208             if (!tg) {
209                 //console.log('no parent' + i);
210                 return;
211             }
212             if (tg && this.findNode(tg,'logClick')) {
213                 return;
214             }
215         }
216         //console.log('no target in parents');
217         
218     },
219     logClick : function(id) 
220     {
221          var bid = id.length ? 'builder-' + id : '';
222          console.log('{ "id" :  "' + bid + '"}');
223     },
224     
225     
226     hover : function(e) {
227         
228        
229         var tg = Roo.get(e.getTarget());
230         if (!tg) {
231             //console.log('no target');
232             this.logMove('');
233             return;
234            }
235          
236         if (this.findNode(tg,'logMove')) {
237             e.stopEvent();
238             return;
239         }
240         var dp = Roo.get(tg.up(''));
241         if (dp && this.findNode(dp,'logMove')) {
242             e.stopEvent();
243             return;
244         }
245         
246         var ns = Roo.get(tg.getNextSibling());
247         if (ns && this.findNode(ns,'logMove')) {
248             e.stopEvent();
249             return;
250         }
251         if (ns && ns.down('') && this.findNode(Roo.get(ns.down('')) ,'logMove' )) {
252             e.stopEvent();
253             return;
254         }
255         
256         for(var i =0; i < 5; i++) {
257             tg = Roo.get(tg.up(''));
258             if (!tg) {
259                 //console.log('no parent' + i);
260                 this.logMove('');
261                 return;
262             }
263             if (tg && this.findNode(tg,'logMove')) {
264                 e.stopEvent();
265                 return;
266             }
267         }
268         //console.log('no target in parents');
269         this.logMove('');
270     },
271     logMove : function (id) {
272         //console.log("LOGMOVE: " + id);
273         
274         if (this.lastID === id) {
275             return;
276        }
277        id = ''+ id;
278        var bid = id.length ? 'builder-' + id : '';
279        console.log('{ "hover-node" :  "' + bid + '"}');
280        this.lastID = id;
281     },
282     clearBootstrap : function()
283     {
284         // if the page is not bootstrap
285         
286         if ( typeof(BuilderUseBootstrap) != 'undefined' ) {
287             Roo.log("it's boostrap - BuilderUseBootstrap is defined ");
288             // it's bootstrap - probably remove roo's css..
289             return;
290         }
291         Roo.log("remove css = BuilderUseBootstrap is not defined");
292         var rem = [];
293         var ar = document.getElementsByTagName('link');
294         for (var i = 0; i < ar.length;i++) {
295             var l = ar[i];
296             Roo.log(l.getAttribute('href'));
297             if (l.getAttribute('href').match(/bootstrap/)) {
298                 rem.push(l);
299                 
300                 
301             }
302             //code
303         }
304         Roo.each(rem, function(l) { l.parentNode.removeChild(l);});
305     },
306     
307     applyFlexy: function(tree)
308     {
309         if (typeof(tree['flexy:foreach']) != 'undefined') {
310             //Roo.log("add flexy:foreach");
311             tree.el.attr('flexy:foreach', tree['flexy:foreach']);
312         }
313         if (typeof(tree['flexy:if']) != 'undefined') {
314             //Roo.log("add flexy:if");
315             tree.el.attr('flexy:if', tree['flexy:if']);
316         }
317         if (typeof(tree['xtype-bootstrap']) != 'undefined') {
318             //Roo.log("add flexy:if");
319             tree.el.attr('xtype', tree['xtype-bootstrap']);
320         }
321         
322         
323         if (!tree.items || !tree.items.length) { return; }
324         
325         for (var i = 0; i < tree.items.length; i++){
326             this.applyFlexy(tree.items[i]);
327         }
328     }
329     
330      
331     
332 };
333 Roo.onReady(function() { Builder.clearBootstrap(); });
334 Roo.XComponent.on('buildcomplete', function() {
335     Roo.log("xcomponent built!");
336     
337     Builder.applyFlexy(Roo.XComponent.modules[0].el);
338 });