X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=src%2FJsRender%2FNodeToJs.vala;h=1bdb3b9925cd29b9224271d55850e26aa4f61321;hb=HEAD;hp=a46ba488494c799959e6d9ea36f5d6aef793a0a9;hpb=4f4304f81dc43b6b38c4a39ec38c40f91b154fd8;p=app.Builder.js diff --git a/src/JsRender/NodeToJs.vala b/src/JsRender/NodeToJs.vala index a46ba4884..1bdb3b992 100644 --- a/src/JsRender/NodeToJs.vala +++ b/src/JsRender/NodeToJs.vala @@ -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,14 +21,13 @@ public class JsRender.NodeToJs : Object { static uint indent = 1; static string indent_str = " "; + + Node node; Gee.ArrayList doubleStringProps; // need to think if this is a good idea like this string pad; - - //Gee.ArrayList els; - //Gee.ArrayList skip; - Gee.HashMap ar_props; - + public JsRender renderer; + Gee.HashMap out_props; Gee.HashMap out_listeners; Gee.HashMap out_nodeprops; @@ -42,19 +48,29 @@ public class JsRender.NodeToJs : Object { this.pad = pad; //this.els = new Gee.ArrayList(); - this.ar_props = new Gee.HashMap(); + //this.ar_props = new Gee.HashMap(); + + + // 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(); this.out_listeners = new Gee.HashMap(); + + this.out_nodeprops = new Gee.HashMap() ; this.out_children = new Gee.ArrayList (); - this.out_props_array = new Gee.HashMap>() ; + + this.out_props_array = new Gee.HashMap>(); // filled in by 'checkChildren' this.out_props_array_plain = new Gee.HashMap>() ; 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... @@ -73,7 +89,6 @@ public class JsRender.NodeToJs : Object { { //return this.mungeToString(this.node); - this.node.line_start = this.cur_line; this.checkChildren(); this.readProps(); @@ -93,31 +108,7 @@ public class JsRender.NodeToJs : Object { this.mungeOut(); return this.ret; - - /* - // oprops... - - var spad = this.pad.substring(0, this.pad.length-indent); - - - var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ; - //print ("STR PROPS: " + str_props); - - - - if (!this.node.props.has_key("* xinclude")) { - return "{\n" + - this.pad + str_props + - "\n" + spad + "}"; - } - // xinclude... - - - return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+ - "{\n" + - this.pad + str_props + - "\n" + spad + "})"; - */ + } /** @@ -176,168 +167,179 @@ 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... // work out remaining items... - var total_nodes = this.out_props.size + - this.out_props_array_plain.size + - (this.out_listeners.size > 0 ? 1 : 0) + - this.out_nodeprops.size + - this.out_props_array.size + - (this.out_children.size > 0 ? 1 : 0); - - - // * prop - - var niter = this.out_nodeprops.map_iterator(); - - while(niter.next()) { - total_nodes--; - suffix = total_nodes > 0 ? "," : ""; - var l = this.pad + niter.get_key() + " : " + - this.mungeChildNew(this.pad + indent_str, niter.get_value()) + suffix; - this.addMultiLine(l); - } + + // output xns / xtype first.. + if (this.out_props.has_key("xtype")) { + var v = this.out_props.get("xtype"); + this.node.setLine(this.cur_line, "p","xtype"); + this.addLine(this.pad + "xtype" + " : " + v + suffix, ','); + } // plain properties. var iter = this.orderedPropKeys().list_iterator(); while(iter.next()) { - total_nodes--; - suffix = total_nodes > 0 ? "," : ""; + + var k = iter.get(); + if (k == "xns" || k == "xtype") { + continue; + } + var v = this.out_props.get(k); + this.node.setLine(this.cur_line, "p",k); + this.addLine(this.pad + k + " : " + v + suffix, ','); + + this.node.setLine(this.cur_line, "e", ""); - this.addMultiLine(this.pad + k + " : " + v + suffix); } - /* - // out_props_array_plain -- not used? - var paiter = this.out_props_array_plain.map_iterator(); - - while(paiter.next()) { - total_nodes--; - - this.addLine(this.pad + paiter.get_key() + " : ["); - var paliter = paiter.get_value().list_iterator(); - while (paliter.next()) { - suffix = paliter.has_next() ? "," : ""; - this.addMultiLine(this.pad + indent_str + paliter.get() + suffix); + + // listeners.. + + if (this.out_listeners.size > 0 ) { + + this.addLine(this.pad + "listeners : {", 0); + iter = this.orderedListenerKeys().list_iterator(); + + while(iter.next()) { + + var k = iter.get(); + var v = this.out_listeners.get(k); + this.node.setLine(this.cur_line, "l",k); //listener + this.addLine(this.pad + indent_str + k + " : " + v , ','); + this.node.setLine(this.cur_line, "e", ""); } - - suffix = total_nodes > 0 ? "," : ""; -// this.mungeChild(this.pad + indent_str, niter.get_value()) - this.addLine(this.pad + "]" + suffix); - } - */ + + this.closeLine(); + this.addLine(this.pad + "}" ,','); + + } + //------- at this point it is the end of the code relating directly to the object.. - + if (this.out_props.has_key("xns")) { + var v = this.out_props.get("xns"); + this.node.setLine(this.cur_line, "p","xns"); + this.addLine(this.pad + "xns" + " : " + v + suffix, ','); + this.node.setLine(this.cur_line, "p","| xns"); + this.addLine(this.pad + "'|xns' : '" + v + "'", ','); + this.node.setLine(this.cur_line, "e", ""); + } + + this.node.line_end = this.cur_line; + + // * prop + + var niter = this.out_nodeprops.map_iterator(); + + while(niter.next()) { + var addstr = this.mungeChildNew(this.pad + indent_str, niter.get_value()); + //print("add str: %s\n", addstr); + this.node.setLine(this.cur_line, "p",niter.get_key()); + this.addLine(this.pad + niter.get_key() + " : " + addstr, ','); + + this.node.setLine(this.cur_line, "e", ""); + } // prop arrays... var piter = this.out_props_array.map_iterator(); while(piter.next()) { - total_nodes--; - - this.addLine(this.pad + piter.get_key() + " : ["); + this.node.setLine(this.cur_line, "p",piter.get_key()); + this.addLine(this.pad + piter.get_key() + " : [", 0); + var pliter = piter.get_value().list_iterator(); while (pliter.next()) { - suffix = pliter.has_next() ? "," : ""; - this.addMultiLine(this.pad + indent_str + - this.mungeChildNew(this.pad + indent_str + indent_str, pliter.get()) + suffix); + var addstr = this.mungeChildNew(this.pad + indent_str + indent_str, pliter.get()); + this.addLine(this.pad + indent_str + addstr, ','); + this.node.setLine(this.cur_line, "e", ""); } - - suffix = total_nodes > 0 ? "," : ""; - - this.addLine(this.pad + "]" + suffix); + this.closeLine(); + this.addLine(this.pad + "]" , ','); } - // listeners.. - if (this.out_listeners.size > 0 ) { - total_nodes--; - this.addLine(this.pad + "listeners : {"); - 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); - } - suffix = total_nodes > 0 ? "," : ""; - this.addLine(this.pad + "}" + suffix); - - } // children.. if (this.out_children.size > 0) { - this.addLine(this.pad + "items : [" ); + this.addLine(this.pad + "items : [" , 0); var cniter = this.out_children.list_iterator(); while (cniter.next()) { suffix = cniter.has_next() ? "," : ""; - this.addMultiLine(this.pad + indent_str + - this.mungeChildNew(this.pad + indent_str + indent_str, cniter.get()) + suffix - ); + var addstr = this.mungeChildNew(this.pad + indent_str + indent_str, cniter.get()); + this.addLine(this.pad + indent_str + addstr, ','); + this.node.setLine(this.cur_line, "e", ""); } - - this.addLine(this.pad + "]"); + this.closeLine(); + this.addLine(this.pad + "]",','); } - + this.node.setLine(this.cur_line, "e", ""); + this.closeLine(); if (this.node.props.has_key("* xinclude")) { - this.ret += spad + "})"; + this.addLine(spad + "})",0); } else { - this.ret += spad + "}"; + this.addLine( spad + "}", 0); } - this.node.line_end = this.cur_line; + this.node.sortLines(); + + + return this.ret; } - - - + /** + * Line endings + * if we end with a ',' + * + */ + + char last_line_end = '!'; - public void addLine(string str= "") + /** + * 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*/ ".printf(this.cur_line -1) + str + "\n"; + if (this.last_line_end != '!') { + this.ret += (this.last_line_end == 0 ? "" : this.last_line_end.to_string()) + "\n"; + } + this.last_line_end = line_end; + 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= "") - { - //var l = cur_line; - this.cur_line += str.split("\n").length; - //this.ret += "/*%d*/ ".printf(l) + str + "\n"; - this.ret += str + "\n"; - } - - string gLibStringListJoin( string sep, Gee.ArrayList ar) + public void closeLine() // send this before '}' or ']' to block output of ',' ... { - var ret = ""; - for (var i = 0; i < ar.size; i++) { - ret += i>0 ? sep : ""; - ret += ar.get(i); - } - return ret; - + this.last_line_end = 0; } - public string mungeChild(string pad , Node cnode) + +/* public void addMultiLine(str= "") { - var x = new NodeToJs(cnode, this.doubleStringProps, pad, this); - return x.munge(); + + //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; } - + */ public string mungeChildNew(string pad , Node cnode ) { var x = new NodeToJs(cnode, this.doubleStringProps, pad, this); @@ -346,7 +348,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 () { @@ -372,7 +379,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)) { @@ -381,11 +388,7 @@ public class JsRender.NodeToJs : Object { // munge property..?? this.out_nodeprops.set(prop, pl); - - //this.els.add( prop + " : " + this.mungeChild ( this.pad + indent_str, pl)); - - - //keys.push(prop); + continue; } @@ -393,21 +396,17 @@ 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 = ""; - if (!this.ar_props.has_key(sprop)) { - - this.ar_props.set(sprop, ""); + //var old = ""; + if (!this.out_props_array.has_key(sprop)) { this.out_props_array.set(sprop, new Gee.ArrayList()); - } else { - old = this.ar_props.get(sprop); } - var nstr = old += old.length > 0 ? ",\n" : ""; - nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str , pl); + + this.out_props_array.get(sprop).add( pl); - this.ar_props.set(sprop, nstr); + //this.ar_props.set(sprop, nstr); } @@ -458,18 +457,10 @@ public class JsRender.NodeToJs : Object { { string left; Regex func_regex ; - - if (this.node.props.has_key("$ xns")) { - this.out_props.set("'|xns'", "'" + this.node.props.get("$ xns") + "'" ); - - //this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'"); - - } - - + try { func_regex = new Regex("^\\s+|\\s+$"); - } catch (Error e) { + } catch (RegexError e) { print("failed to build regex"); return; } @@ -492,11 +483,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; @@ -521,10 +512,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)) { @@ -571,6 +582,11 @@ public class JsRender.NodeToJs : Object { //nstr = string.joinv("\n", lines); } this.out_props.set(left, nstr); + + + + + //print("==> " + str + "\n"); //this.els.add(left + " : "+ nstr); continue; @@ -615,7 +631,7 @@ public class JsRender.NodeToJs : Object { var com = " /* " + (v.split("\n").length > 1 ? - ("\n" + string.joinv(this.pad + "\n", v.split("\n")).replace("*/", "* - /") + "\n" + this.pad + "*/ ") : + ("\n" + this.pad + string.joinv(this.pad + "\n", v.split("\n")).replace("*/", "* - /") + "\n" + this.pad + "*/ ") : (v.replace("*/", "* - /") + " */") ); @@ -624,7 +640,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; } @@ -641,51 +657,7 @@ public class JsRender.NodeToJs : Object { } } - /* - public void readArrayProps() - { - - // this is not needed in the new version - // as array props are handled more directly.. - - // handle the childitems that are arrays.. eg. button[] = { }... - - // note this does not handle a mix of nodes and properties with the same - - string left; - - var iter = this.ar_props.map_iterator(); - while (iter.next()) { - var k = iter.get_key(); - var right = iter.get_value(); - - string leftv = k[0] == '|' ? k.substring(1) : k; - if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) { - left = "'" + leftv + "'"; - } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted. - var val = this.node.quoteString(leftv); - - left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'"; - } else { - left = leftv; - } - - - if (right.length > 0){ - //if (this.out_props_array_plain.has_key(left)) { - // this.out_props_array_plain.set(left, new Gee.ArrayList()); - //} - //this.out_props_array_plain.get(left).add(right); - - //this.els.add(left + " : [\n" + this.pad + indent_str + indent_str + - // right + "\n" + this.pad + "]"); - } - - - } - - } - */ + public void readListeners() { @@ -710,30 +682,26 @@ public class JsRender.NodeToJs : Object { //return a < b ? -1 : 1; }); - var itms = "listeners : {\n"; - + for (var i = 0; i< keys.size; i++) { var key = keys.get(i); var val = this.node.listeners.get(key); - itms += i >0 ? ",\n" : ""; - // + // var str = val.strip(); var lines = str.split("\n"); if (lines.length > 0) { //str = string.joinv("\n" + this.pad + " ", lines); str = string.joinv("\n" + this.pad + indent_str + indent_str , lines); } - - itms += this.pad + indent_str + key.replace("|", "") + " : " + str; + this.out_listeners.set(key.replace("|", "") ,str); } - itms += "\n" + this.pad + "}"; - //print ( "ADD " + itms); - //this.els.add(itms); + + } @@ -746,18 +714,13 @@ public class JsRender.NodeToJs : Object { return; } var itms = "items : [\n"; - var n = 0; + //var n = 0; for(var i = 0; i < this.node.items.size;i++) { var ele = this.node.items.get(i); if (ele.props.has_key("* prop")) { continue; } - if (n > 0) { - itms += ",\n"; - } - n++; - itms += this.pad + indent_str + - this.mungeChild( this.pad + indent_str + indent_str , ele); + this.out_children.add(ele); }