resources/roo.builder.js
authorAlan Knowles <alan@roojs.com>
Wed, 27 Aug 2014 07:54:39 +0000 (15:54 +0800)
committerAlan Knowles <alan@roojs.com>
Wed, 27 Aug 2014 07:54:39 +0000 (15:54 +0800)
resources/roo.builder.js

index a150278..24f00c7 100644 (file)
@@ -16,7 +16,114 @@ Builder  = {
     id : 1,
 
 
-
+  
+        saveHTML :  function(web_frame) 
+       {
+            print("TRAVERSE DOM?");
+            
+            var dom = document.body;
+            print(dom);
+            var ret = '';
+            //Roo.select('body > div',true).each(function(el) {
+            this.traverseDOMTree(function(s) { ret+=s; }, dom, 1);
+           alert("IPC:SAVEHTML:" + dom);
+            return ret;
+        },
+        
+        
+        traverseDOMTree : function(cb, currentElement, depth) {
+            if (!currentElement) {
+                
+                return;
+            }
+            if (currentElement.class_name.match(/roo-dynamic/)) {
+                return;
+            }
+            
+            //Roo.log(currentElement);
+            var j;
+            var nodeName = currentElement.node_name;
+            var tagName = currentElement.tag_name;
+            
+            if  (nodeName == '#text') {
+                cb(currentElement.node_value);
+                return;
+            
+            }
+             
+            
+            
+            if(nodeName == 'BR'){
+                cb("<BR/>");
+                return;
+            }
+            if (nodeName != 'BODY') {
+                
+            
+            
+                var i = 0;
+              // Prints the node tagName, such as <A>, <IMG>, etc
+                if (tagName) {
+                    var attr = [];
+                    for(i = 0; i < currentElement.attributes.length;i++) {
+                        var aname = currentElement.attributes.item(i).name;
+                        if (aname=='id') {
+                            aname= 'xbuilderid';
+                        }
+                        // skip
+                        if (currentElement.attributes.item(i).value == 'builderel') {
+                            return;
+                        }
+                        attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
+                    }
+                    
+                    
+                    cb("<"+currentElement.tag_name+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
+                } 
+                else {
+                  cb("[unknown tag]");
+                }
+            } else {
+                tagName = false;
+            }
+            // Traverse the tree
+            i = 0;
+            var currentElementChild = currentElement.child_nodes.item(i);
+            var allText = true;
+            while (currentElementChild) {
+                // Formatting code (indent the tree so it looks nice on the screen)
+                
+                if  (currentElementChild.node_name == '#text') {
+                    cb(currentElementChild.node_value);
+                    i++;
+                    currentElementChild=currentElement.child_nodes.item(i);
+                    continue;
+                }   
+                allText = false;
+                cb("\n");
+                for (j = 0; j < depth; j++) {
+                  // &#166 is just a vertical line
+                  cb("  ");
+                }               
+                
+                    
+                // Recursively traverse the tree structure of the child node
+                this.traverseDOMTree(cb, currentElementChild, depth+1);
+                i++;
+                currentElementChild=currentElement.child_nodes.item(i);
+            }
+            if (!allText) {
+                    // The remaining code is mostly for formatting the tree
+                cb("\n");
+                for (j = 0; j < depth - 1; j++) {
+                  cb("  ");
+                }     
+            }
+            if (tagName) {
+                cb("</"+tagName+">");
+            }
+            
+        },
 
 
        // this lot is to deal with draging // selecting? - not used at present