3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
8 * We are changing this to output as we go.
9 * However... since line-endings on properties have ',' (not ;) like vala.
10 * we have to be a bit smarter about how to output.
19 public class JsRender.NodeToJs : Object {
21 static uint indent = 1;
22 static string indent_str = " ";
27 Gee.ArrayList<string> doubleStringProps; // need to think if this is a good idea like this
29 public JsRender renderer;
31 Gee.HashMap<string,string> out_props;
32 Gee.HashMap<string,string> out_listeners;
33 Gee.HashMap<string,Node> out_nodeprops;
34 Gee.ArrayList<Node> out_children;
35 Gee.HashMap<string,Gee.ArrayList<Node>> out_props_array;
36 Gee.HashMap<string,Gee.ArrayList<string>> out_props_array_plain;
44 public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad, NodeToJs? parent)
47 this.doubleStringProps = doubleStringProps;
50 //this.els = new Gee.ArrayList<string>();
51 //this.ar_props = new Gee.HashMap<string,string>();
55 // this is the bit that causes issues - we have to output as we go, otherwise we
56 // can not work out which line is which...
58 this.out_props = new Gee.HashMap<string,string>();
59 this.out_listeners = new Gee.HashMap<string,string>();
62 this.out_nodeprops = new Gee.HashMap<string,Node>() ;
63 this.out_children = new Gee.ArrayList<Node> ();
65 this.out_props_array = new Gee.HashMap<string,Gee.ArrayList<Node>>(); // filled in by 'checkChildren'
66 this.out_props_array_plain = new Gee.HashMap<string,Gee.ArrayList<string>>() ;
70 this.cur_line = parent == null ? 0 : parent.cur_line ; //-1 as we usuall concat onto the existin gline?
72 this.renderer = parent.renderer;
75 this.top = parent == null ? this : parent.top;
78 node.node_lines = new Gee.ArrayList<int>();
79 node.node_lines_map = new Gee.HashMap<int,Node>();
88 public string munge ( )
90 //return this.mungeToString(this.node);
95 //this.readArrayProps();
98 if (!this.node.props.has_key("* xinclude")) {
104 // no properties to output...
105 //if (this.els.size < 1) {
115 This currently works by creating a key/value array of this.els, which is just an array of properties..
116 this is so that join() works...
120 b) output plan properties.
121 c) output listeners..
123 g) output prop_arrays..
131 public Gee.ArrayList<string> orderedPropKeys() {
133 var ret = new Gee.ArrayList<string> ();
134 var niter = this.out_props.map_iterator();
135 while(niter.next()) {
136 ret.add(niter.get_key());
139 ret.sort(( a, b) => {
140 return ((string)a).collate((string)b);
141 //if (a == b) return 0;
142 //return a < b ? -1 : 1;
146 public Gee.ArrayList<string> orderedListenerKeys() {
148 var ret = new Gee.ArrayList<string> ();
149 var niter = this.out_listeners.map_iterator();
150 while(niter.next()) {
151 ret.add(niter.get_key());
154 ret.sort(( a, b) => {
155 return ((string)a).collate((string)b);
156 //if (a == b) return 0;
157 //return a < b ? -1 : 1;
163 public string mungeOut()
165 this.node.line_start = this.cur_line;
166 this.top.node.setNodeLine(this.cur_line, this.node);
167 var spad = this.pad.substring(0, this.pad.length-indent);
169 if (this.node.props.has_key("* xinclude")) {
170 this.addLine("Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), {",0 );
173 this.addLine("{", 0);
176 // output the items...
177 // work out remaining items...
181 var iter = this.orderedPropKeys().list_iterator();
186 var v = this.out_props.get(k);
187 this.node.setLine(this.cur_line, "p",k); //listener
188 this.addLine(this.pad + k + " : " + v + suffix, ',');
189 this.node.setLine(this.cur_line, "e", "");
195 if (this.out_listeners.size > 0 ) {
197 this.addLine(this.pad + "listeners : {", 0);
198 iter = this.orderedListenerKeys().list_iterator();
203 var v = this.out_listeners.get(k);
204 this.node.setLine(this.cur_line, "l",k); //listener
205 this.addLine(this.pad + indent_str + k + " : " + v , ',');
206 this.node.setLine(this.cur_line, "e", "");
210 this.addLine(this.pad + "}" ,',');
214 //------- at this point it is the end of the code relating directly to the object..
222 var niter = this.out_nodeprops.map_iterator();
224 while(niter.next()) {
225 var addstr = this.mungeChildNew(this.pad + indent_str, niter.get_value());
226 this.node.setLine(this.cur_line, "p",niter.get_key());
227 this.addLine(this.pad + niter.get_key() + " : " + addstr, ',');
228 if (niter.get_key() == "xns") {
229 this.addLine(this.pad + "'|xns' : '" + addstr + "'", ',');
232 this.node.setLine(this.cur_line, "e", "");
236 var piter = this.out_props_array.map_iterator();
238 while(piter.next()) {
239 this.node.setLine(this.cur_line, "p",niter.get_key());
240 this.addLine(this.pad + piter.get_key() + " : [", 0);
242 var pliter = piter.get_value().list_iterator();
243 while (pliter.next()) {
244 var addstr = this.mungeChildNew(this.pad + indent_str + indent_str, pliter.get());
245 this.addLine(this.pad + indent_str + addstr, ',');
246 this.node.setLine(this.cur_line, "e", "");
249 this.addLine(this.pad + "]" , ',');
253 if (this.out_children.size > 0) {
254 this.addLine(this.pad + "items : [" , 0);
255 var cniter = this.out_children.list_iterator();
256 while (cniter.next()) {
257 suffix = cniter.has_next() ? "," : "";
258 var addstr = this.mungeChildNew(this.pad + indent_str + indent_str, cniter.get());
259 this.addLine(this.pad + indent_str + addstr, ',');
263 this.addLine(this.pad + "]",',');
267 if (this.node.props.has_key("* xinclude")) {
268 this.addLine(spad + "})",0);
271 this.addLine( spad + "}", 0);
274 this.node.sortLines();
276 this.node.line_end = this.cur_line;
284 * if we end with a ','
288 char last_line_end = '!';
291 * add a line - note we will end up with an extra line break
292 * at beginning of nodes doing this..
294 * @param str = text to add..
295 * @param line_end = 0 (just add a line break)
296 * line_end = ',' and ","
299 public void addLine(string str, char line_end)
301 if (this.last_line_end != '!') {
302 this.ret += (this.last_line_end == 0 ? "" : this.last_line_end.to_string()) + "\n";
304 this.last_line_end = line_end;
305 this.cur_line += str.split("\n").length;
309 //this.ret += "/*%d(%d-%d)*/ ".printf(this.cur_line -1, this.node.line_start,this.node.line_end) + str + "\n";
313 public void closeLine() // send this before '}' or ']' to block output of ',' ...
315 this.last_line_end = 0;
318 /* public void addMultiLine(str= "")
321 //this.ret += "/ * %d(%d-%d) * / ".printf(this.cur_line, this.node.line_start,this.node.line_end)+ str + "\n";
322 this.ret += str + "\n";
323 this.cur_line += str.split("\n").length;
326 public string mungeChildNew(string pad , Node cnode )
328 var x = new NodeToJs(cnode, this.doubleStringProps, pad, this);
335 * loop through items[] array see if any of the children have '* prop'
336 * -- which means they are a property of this node.
337 * -- ADD TO : this.opt_props_array
341 public void checkChildren ()
345 // look throught he chilren == looking for * prop.. -- fixme might not work..
348 if (!this.node.hasChildren()) {
353 for (var ii =0; ii< this.node.items.size; ii++) {
354 var pl = this.node.items.get(ii);
355 if (!pl.props.has_key("* prop")) {
360 //print(JSON.stringify(pl,null,4));
362 //var prop = pl['*prop'] + '';
363 //delete pl['*prop'];
364 var prop = pl.get("* prop");
365 //print("got prop "+ prop + "\n");
368 if (! Regex.match_simple("\\[\\]$", prop)) {
369 // it's a standard prop..
371 // munge property..??
373 this.out_nodeprops.set(prop, pl);
381 var sprop = prop.replace("[]", "");
382 //print("sprop is : " + sprop + "\n");
384 // it's an array type..
386 if (!this.out_props_array.has_key(sprop)) {
387 this.out_props_array.set(sprop, new Gee.ArrayList<Node>());
391 this.out_props_array.get(sprop).add( pl);
392 //this.ar_props.set(sprop, nstr);
399 * Standardize this crap...
401 * standard properties (use to set)
402 * If they are long values show the dialog..
405 * bool is_xxx :: can show a pulldown.. (true/false)
407 * $ string html = string with value interpolated eg. baseURL + ".."
408 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
409 * $ untypedvalue = javascript untyped value...
410 * _ string html ... = translatable..
413 * object properties (not part of the GOjbect being wrapped?
414 * # Gee.ArrayList<Xcls_fileitem> fileitems
419 * methods -- always text editor..
427 * * init -- big string?
429 * event handlers (listeners)
434 * +XXXX -- indicates it's a instance property / not glob...
435 * *XXXX -- skip writing glob property (used as classes that can be created...)
439 public void readProps()
445 func_regex = new Regex("^\\s+|\\s+$");
446 } catch (RegexError e) {
447 print("failed to build regex");
450 // sort the key's so they always get rendered in the same order..
452 var keys = new Gee.ArrayList<string>();
453 var piter = this.node.props.map_iterator();
454 while (piter.next() ) {
458 this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
463 keys.sort(( a, b) => {
464 return ((string)a).collate((string)b);
465 //if (a == b) return 0;
466 //return a < b ? -1 : 1;
469 var has_cms = this.node.has("cms-id");
471 for (var i = 0; i< keys.size; i++) {
472 var key = this.node.get_key(keys.get(i));
473 //("ADD KEY %s\n", key);
478 this.node.normalize_key(key, out k, out kflag, out ktype);
481 var v = this.node.get(key);
484 //if (this.skip.contains(k) ) {
487 if ( Regex.match_simple("\\[\\]$", k)) {
488 // array .. not supported... here?
494 // skip builder stuff. prefixed with '.' .. just like unix fs..
495 if (kflag == ".") { // |. or . -- do not output..
499 // ignore '* prop'; ???
503 // handle cms-id // html
504 if (has_cms && k == "cms-id") {
505 continue; // ignore it...
507 // html must not be a dynamic property...
508 // note - we do not translate this either...
509 if (has_cms && k == "html" && kflag != "$") {
512 this.out_props.set("html", "Pman.Cms.content(" +
513 this.node.quoteString(this.renderer.name + "::" + this.node.get("cms-id")) +
515 this.node.quoteString(v) +
524 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
525 left = "'" + leftv + "'";
526 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
527 var val = this.node.quoteString(leftv);
529 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
535 // next.. is it a function.. or a raw string..
543 // ??? any others that are raw output..
545 // does not hapepnd with arrays..
546 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
553 str = func_regex.replace(v,v.length, 0, "");
555 print("regex failed");
561 var lines = str.split("\n");
563 if (lines.length > 0) {
564 nstr = string.joinv("\n" + this.pad, lines);
565 //nstr = string.joinv("\n", lines);
567 this.out_props.set(left, nstr);
573 //print("==> " + str + "\n");
574 //this.els.add(left + " : "+ nstr);
585 ktype.down() == "boolean"
587 ktype.down() == "bool"
589 ktype.down() == "number"
591 ktype.down() == "int"
592 ) { // boolean or number...?
593 this.out_props.set(left, v.down());
594 //this.els.add(left + " : " + v.down() );
598 // is it a translated string?
604 //if (this.doubleStringProps.size < 1) {
605 // this.els.add(left + this.node.quoteString(v));
609 if ((this.doubleStringProps.index_of(k) > -1) ||
610 (ktype.down() == "string" && k[0] == '_')
613 // then use the translated version...
616 (v.split("\n").length > 1 ?
617 ("\n" + this.pad + string.joinv(this.pad + "\n", v.split("\n")).replace("*/", "* - /") + "\n" + this.pad + "*/ ") :
618 (v.replace("*/", "* - /") + " */")
621 //this.els.add(left + " : _this._strings['" +
622 // GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
625 this.out_props.set(left, "_this._strings['" +
626 GLib.Checksum.compute_for_string (ChecksumType.MD5, v.strip()) +
631 // otherwise it needs to be encapsulated.. as single quotes..
633 var vv = this.node.quoteString(v);
634 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
635 //this.els.add(left + " : " + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
636 this.out_props.set(left, "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
644 public void readListeners()
647 if (this.node.listeners.size < 1) {
650 // munge the listeners.
651 //print("ADDING listeners?");
656 var keys = new Gee.ArrayList<string>();
657 var piter = this.node.listeners.map_iterator();
658 while (piter.next() ) {
660 keys.add(piter.get_key());
662 keys.sort(( a, b) => {
663 return ((string)a).collate((string)b);
664 //if (a == b) return 0;
665 //return a < b ? -1 : 1;
669 for (var i = 0; i< keys.size; i++) {
670 var key = keys.get(i);
671 var val = this.node.listeners.get(key);
675 var str = val.strip();
676 var lines = str.split("\n");
677 if (lines.length > 0) {
678 //str = string.joinv("\n" + this.pad + " ", lines);
679 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
682 this.out_listeners.set(key.replace("|", "") ,str);
691 public void iterChildren()
695 // finally munge the children...
696 if (this.node.items.size < 1) {
699 var itms = "items : [\n";
701 for(var i = 0; i < this.node.items.size;i++) {
702 var ele = this.node.items.get(i);
703 if (ele.props.has_key("* prop")) {
707 this.out_children.add(ele);
710 itms += "\n"+ this.pad + "]" + "\n";
711 //this.els.add(itms);
714 // finally output listeners...
716 public void xIncludeToString()