src/JsRender/NodeToJs.vala
[app.Builder.js] / src / JsRender / NodeToJs.vala
index 4138875..7130555 100644 (file)
@@ -4,6 +4,13 @@
  * 
  * usage : x = (new JsRender.NodeToJs(node)).munge();
  * 
+ *
+ *  We are changing this to output as we go.
+ *   However... since line-endings on properties have ',' (not ;) like vala.
+ *           we have to be a bit smarter about how to output.
+ *
+ *   
+ *
 */
 
 
@@ -14,10 +21,12 @@ public class JsRender.NodeToJs : Object {
        static uint indent = 1;
        static string indent_str = " ";
        
+       
+       
        Node node;
        Gee.ArrayList<string>  doubleStringProps;  // need to think if this is a good idea like this
        string pad;
-       
+       public JsRender renderer;
          
        Gee.HashMap<string,string> out_props;
        Gee.HashMap<string,string> out_listeners;       
@@ -42,16 +51,26 @@ public class JsRender.NodeToJs : Object {
                //this.ar_props = new Gee.HashMap<string,string>();
                
                
+               
+               // this is the bit that causes issues - we have to output as we go, otherwise we 
+               // can not work out which line is which...
+               
                this.out_props = new Gee.HashMap<string,string>();
                this.out_listeners = new Gee.HashMap<string,string>();  
+               
+               
                this.out_nodeprops = new Gee.HashMap<string,Node>() ;
                this.out_children = new Gee.ArrayList<Node> ();
-               this.out_props_array = new Gee.HashMap<string,Gee.ArrayList<Node>>() ;
+               
+               this.out_props_array = new Gee.HashMap<string,Gee.ArrayList<Node>>(); // filled in by 'checkChildren'
                this.out_props_array_plain = new Gee.HashMap<string,Gee.ArrayList<string>>() ;
        
                
                
                this.cur_line = parent == null ? 0 : parent.cur_line  ; //-1 as we usuall concat onto the existin gline?
+               if (parent != null) {
+                       this.renderer = parent.renderer;
+               }
                this.ret = "";
                this.top = parent == null ? this : parent.top;
                // reset the maps...
@@ -70,7 +89,6 @@ public class JsRender.NodeToJs : Object {
        {
                //return this.mungeToString(this.node);
 
-               this.node.line_start = this.cur_line;
                
                this.checkChildren();
                this.readProps();
@@ -149,10 +167,10 @@ public class JsRender.NodeToJs : Object {
                var spad = this.pad.substring(0, this.pad.length-indent);
                
                if (this.node.props.has_key("* xinclude")) {
-                       this.addLine("Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), {");
+                       this.addLine("Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), {",0 );
         
                } else {
-                       this.addLine("{");
+                       this.addLine("{", 0);
                }
                var suffix = "";
                // output the items...
@@ -174,23 +192,23 @@ public class JsRender.NodeToJs : Object {
                        var k = iter.get();
                        var v = this.out_props.get(k);
                        
-                       this.addMultiLine(this.pad + k + " : " + v + suffix);
+                       this.addLine(this.pad + k + " : " + v + suffix, ',');
                }
         
                // listeners..
                
                if (this.out_listeners.size > 0 ) { 
                        total_nodes--;
-                       this.addLine(this.pad + "listeners : {");
+                       this.addLine(this.pad + "listeners : {", 0);
                        iter = this.orderedListenerKeys().list_iterator();
                         
-                       var sz = this.out_listeners.size;
                        while(iter.next()) {
-                               sz--;
-                               suffix = sz > 0 ? "," : "";
+                               
                                var k = iter.get();
                                var v = this.out_listeners.get(k);
-                               this.addMultiLine(this.pad + indent_str + k + " : " + v + suffix);
+                               this.addLine(this.pad + indent_str + k + " : ", '');
+                               this.node.setLine(this.cur_line, ";",k);
+                               this.addLine( v,',');
                        }
                        suffix = total_nodes > 0 ? "," : "";
                        this.addLine(this.pad + "}" + suffix);                  
@@ -261,27 +279,43 @@ public class JsRender.NodeToJs : Object {
        
        }
        
-       
-       
-       
-       public void addLine(string str= "")
+       /**
+       * Line endings
+       *     if we end with a ','
+       *
+       */
+
+       char last_line_end = 0; 
+       
+       /**
+       * add a line - note we will end up with an extra line break 
+       *     at beginning of nodes doing this..
+       *
+       * @param str = text to add..
+       * @param line_end = 0  (just add a line break)
+       *        line_end = ','  and ","
+       *  
+       */
+       public void addLine(string str, char line_end)
        {
-               this.cur_line ++;
-               //this.ret += str+ "\n";
-               this.ret +=  "/*%d(%d-%d)*/ ".printf(this.cur_line -1, this.node.line_start,this.node.line_end) + str + "\n";
+               this.ret += (this.line_end == 0 ? "" : this.last_line_end) + "\n"; 
+               this.cur_line += str.split("\n").length;
+               this.ret += str;
+               
+               
+               //this.ret +=  "/*%d(%d-%d)*/ ".printf(this.cur_line -1, this.node.line_start,this.node.line_end) + str + "\n";
                
                
        }
        
-       public void addMultiLine(string str= "")
+/*     public void addMultiLine(str= "")
        {
-               var l = cur_line;
+               
+               //this.ret +=   "/ * %d(%d-%d) * / ".printf(this.cur_line, this.node.line_start,this.node.line_end)+ str + "\n";
+               this.ret +=   str + "\n";
                this.cur_line += str.split("\n").length;
-               //this.ret +=   "/*%d(%d-%d)*/ ".printf(l, this.node.line_start,this.node.line_end)+ str + "\n";
-               //this.ret +=   str + "\n";
        }
+ */
        public string mungeChildNew(string pad ,  Node cnode )
        {
                var x = new  NodeToJs(cnode, this.doubleStringProps, pad, this);
@@ -290,7 +324,12 @@ public class JsRender.NodeToJs : Object {
                return x.ret;
        }
        
-
+       /**
+       * loop through items[] array see if any of the children have '* prop'
+       * -- which means they are a property of this node.
+       * -- ADD TO : this.opt_props_array  
+       *
+       */
        
        public void checkChildren () 
        {
@@ -316,7 +355,7 @@ public class JsRender.NodeToJs : Object {
                        //var prop = pl['*prop'] + '';
                        //delete pl['*prop'];
                        var prop = pl.get("* prop");
-                       print("got prop "+ prop + "\n");
+                       //print("got prop "+ prop + "\n");
                        
                        // name ends in [];
                        if (! Regex.match_simple("\\[\\]$", prop)) {
@@ -333,7 +372,7 @@ public class JsRender.NodeToJs : Object {
 
                        
                        var sprop  = prop.replace("[]", "");
-                       print("sprop is : " + sprop + "\n");
+                       //print("sprop is : " + sprop + "\n");
                        
                        // it's an array type..
                        //var old = "";
@@ -428,11 +467,11 @@ public class JsRender.NodeToJs : Object {
                        //return a < b ? -1 : 1;
                });
                
-               
+               var has_cms = this.node.has("cms-id");
                
                for (var i = 0; i< keys.size; i++) {
                        var key = this.node.get_key(keys.get(i));
-                       print("ADD KEY %s\n", key);
+                       //("ADD KEY %s\n", key);
                        string k;
                        string ktype;
                        string kflag;
@@ -457,10 +496,30 @@ public class JsRender.NodeToJs : Object {
                        if (kflag == ".") { // |. or . -- do not output..
                                continue;
                        }
-                        if (kflag == "*") {
+                       if (kflag == "*") {
                                // ignore '* prop'; ??? 
                                continue;
-                        }
+                       }
+                       
+                       // handle cms-id // html
+                       if (has_cms && k == "cms-id") {
+                               continue; // ignore it...
+                       }
+                       // html must not be a dynamic property...
+                       // note - we do not translate this either...
+                       if (has_cms && k == "html" && kflag != "$") {
+                                
+
+                               this.out_props.set("html", "Pman.Cms.content(" + 
+                                       this.node.quoteString(this.renderer.name + "::" + this.node.get("cms-id")) +
+                                        ", " +
+                                       this.node.quoteString(v) +
+                                        ")");
+                                        
+                               continue;        
+                       }
+                       
+                       
                                
                        
                        if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
@@ -560,7 +619,7 @@ public class JsRender.NodeToJs : Object {
                                //      "']"
                                //);
                                this.out_props.set(left, "_this._strings['" + 
-                                       GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
+                                       GLib.Checksum.compute_for_string (ChecksumType.MD5, v.strip()) +
                                        "']" + com);
                                continue;
                        }