3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
12 public class JsRender.NodeToVala : Object {
26 Gee.ArrayList<string> ignoreList;
27 Gee.ArrayList<string> ignoreWrappedList;
28 Gee.ArrayList<string> myvars;
29 Gee.ArrayList<Node> vitems; // top level items
33 public NodeToVala( Node node, int depth, NodeToVala? top)
39 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
40 this.pad = this.inpad + " ";
41 this.ipad = this.inpad + " ";
42 this.cls = node.xvala_cls;
43 this.xcls = node.xvala_xcls;
45 this.top = top == null ? this : top;
46 this.ignoreList = new Gee.ArrayList<string>();
47 this.ignoreWrappedList = new Gee.ArrayList<string>();
48 this.myvars = new Gee.ArrayList<string>();
49 this.vitems = new Gee.ArrayList<Node>();
54 string toValaNS(Node item)
56 var ns = item.get("xns") ;
57 if (ns == "GtkSource") {
62 public void toValaName(Node item, int depth =0)
66 var ns = this.toValaNS(item) ;
67 var cls = ns + item.get("xtype");
73 string id = item.get("id").length > 0 ?
74 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
79 if (id[0] == '*' || id[0] == '+') {
80 item.xvala_xcls = "Xcls_" + id.substring(1);
82 item.xvala_xcls = "Xcls_" + id;
88 this.vitems.add(item);
89 } else if (!item.props.has_key("id")) {
90 // use the file name..
91 item.xvala_xcls = this.file.name;
93 item.xvala_id = this.file.name;
98 if (item.items.size < 1) {
101 for(var i =0;i<item.items.size;i++) {
102 this.toValaName(item.items.get(i), depth+1);
107 * Main entry point to convert a file into a string..
109 public static string mungeFile(JsRender file)
111 if (file.tree == null) {
115 var n = new NodeToVala(file.tree, 0, null);
119 n.toValaName(file.tree);
122 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
123 n.cls = file.tree.xvala_cls;
124 n.xcls = file.tree.xvala_xcls;
130 public string munge ( )
132 //return this.mungeToString(this.node);
137 this.ignore("xtype");
143 this.addTopProperties();
145 this.addPlusProperties();
148 this.addWrappedCtor();
150 this.addInitMyVars();
151 this.addWrappedProperties();
156 this.addUserMethods();
163 public string mungeChild( Node cnode)
165 var x = new NodeToVala(cnode, this.depth+1, this.top);
169 public void globalVars()
171 if (this.depth > 0) {
175 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
178 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
187 // class xxx { WrappedGtk el; }
188 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
189 this.ret += this.pad + "public " + this.cls + " el;\n";
192 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
203 this.ret += pad + "public static " + xcls + " singleton()\n" +
205 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
206 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
208 this.ipad + "return _" + this.node.xvala_id +";\n" +
213 void addTopProperties()
215 if (this.depth > 0) {
218 // properties - global..??
220 var iter = this.vitems.list_iterator();
225 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
229 if (n.xvala_id[0] == '*') {
232 if (n.xvala_id[0] == '+') {
235 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
242 this.ret += "\n" + this.ipad + "// my vars (def)\n";
246 var cls = Palete.Gir.factoryFqn(this.node.fqn());
254 var iter = this.node.props.map_iterator();
255 while (iter.next()) {
256 var k = iter.get_key();
257 if (this.shouldIgnore(k)) {
260 var vv = k.strip().split(" ");
261 // user defined method
270 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
274 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
275 if (vv.length < min) {
276 // skip 'old js style properties without a type'
280 var kname = vv[vv.length-1];
282 if (this.shouldIgnore(kname)) {
286 // is it a class property...
287 if (cls.props.has_key(kname) && vv[0] != "#") {
294 this.ret += this.pad + "public " +
295 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
303 // if id of child is '+' then it's a property of this..
304 void addPlusProperties()
306 if (this.node.items.size < 1) {
309 var iter = this.node.items.list_iterator();
310 while (iter.next()) {
313 if (ci.xvala_id[0] != '+') {
314 continue; // skip generation of children?
317 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
332 this.ret += "\n" + this.pad + "// ctor \n";
333 if (this.node.has("* args")) {
334 // not sure what this is supposed to be ding..
336 cargs_str = ", " + this.node.get("* args");
337 //var ar = this.node.get("* args");.split(",");
338 //for (var ari =0; ari < ar.length; ari++) {
339 // cargs += (ar[ari].trim().split(" ").pop();
343 if (this.depth < 1) {
344 this.ret += this.pad + "public " + this.xcls + "(" +
345 cargs_str +")\n" + this.pad + "{\n";
350 this.ret+= this.pad + "public " + this.xcls + "(" +
351 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
360 this.ret += this.ipad + "_this = this;\n";
363 this.ret+= this.ipad + "_this = _owner;\n";
365 if (this.node.props.has_key("id")
367 this.node.xvala_id != ""
369 this.node.xvala_id[0] != '*'
371 this.node.xvala_id[0] != '+'
373 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
381 void addWrappedCtor()
384 // this may need to look up properties to fill in the arguments..
385 // introspection does not workk..... - as things like gtkmessagedialog
387 if (cls == 'Gtk.Table') {
389 var methods = this.palete.getPropertiesFor(cls, 'methods');
391 print(JSON.stringify(this.palete.proplist[cls], null,4));
395 if (this.node.has("* ctor")) {
398 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
401 // the ctor arguments...
404 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
405 //if (default_ctor == null) {
406 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
409 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
411 var iter =default_ctor.paramset.params.list_iterator();
412 while (iter.next()) {
413 var n = iter.get().name;
414 if (!this.node.has(n)) {
416 if (iter.get().type.contains("int")) {
420 if (iter.get().type.contains("float")) {
424 if (iter.get().type.contains("bool")) {
425 args += "true"; // always default to true?
428 // any other types???
433 this.ignoreWrapped(n);
436 var v = this.node.get(n);
438 if (iter.get().type == "string") {
439 v = "\"" + v.escape("") + "\"";
441 if (v == "TRUE" || v == "FALSE") {
449 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
455 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
462 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
463 //print(JSON.stringify(meths,null,4));Seed.quit();
467 // initialize.. my vars..
468 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
470 var iter = this.myvars.list_iterator();
475 var ar = k.strip().split(" ");
476 var kname = ar[ar.length-1];
478 var v = this.node.props.get(k);
483 if (v == "FALSE" || v == "TRUE") {
486 //FIXME -- check for raw string.. "string XXXX"
488 // if it's a string...
490 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
498 void addWrappedProperties()
500 var cls = Palete.Gir.factoryFqn(this.node.fqn());
504 // what are the properties of this class???
505 this.ret += "\n" + this.ipad + "// set gobject values\n";
507 var iter = cls.props.map_iterator();
508 while (iter.next()) {
509 var p = iter.get_key();
510 print("Check Write %s\n", p);
511 if (!this.node.has(p)) {
514 if (this.shouldIgnoreWrapped(p)) {
519 var v = this.node.get(p);
521 var nodekey = this.node.get_key(p);
523 // user defined properties.
524 if (nodekey[0] == '#') {
530 var is_raw = nodekey[0] == '$';
532 // what's the type.. - if it's a string.. then we quote it..
533 if (iter.get_value().type == "string" && !is_raw) {
534 v = "\"" + v.escape("") + "\"";
536 if (v == "TRUE" || v == "FALSE") {
539 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
544 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
556 if (this.node.items.size < 1) {
560 var iter = this.node.items.list_iterator();
562 while (iter.next()) {
567 if (ci.xvala_id[0] == '*') {
568 continue; // skip generation of children?
572 if (ci.has("* args")) {
574 var ar = ci.get("* args").split(",");
575 for (var ari = 0 ; ari < ar.length; ari++ ) {
576 var arg = ar[ari].split(" ");
577 xargs += "," + arg[arg.length -1];
581 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
582 "( _this " + xargs + ");\n" ;
584 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
586 if (ci.has("* prop")) {
587 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
591 // not sure why we have 'true' in pack?!?
592 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
596 string[] packing = { "add" };
597 if (ci.has("pack")) {
598 packing = ci.get("pack").split(",");
601 var pack = packing[0];
602 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
603 (packing.length > 1 ?
604 (", " + string.joinv(",", packing).substring(pack.length+1))
610 if (ci.xvala_id[0] != '+') {
611 continue; // skip generation of children?
614 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
623 if (!this.node.has("init")) {
626 this.ret+= "\n" + ipad + "// init method \n";
628 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
633 if (this.node.listeners.size < 1) {
639 this.ret+= "\n" + ipad + "// listeners \n";
641 var iter = this.node.listeners.map_iterator();
642 while (iter.next()) {
643 var k = iter.get_key();
644 var v = iter.get_value();
645 this.ret+= this.ipad + "this.el." + k + ".connect( " +
646 this.padMultiline(this.ipad,v) +");\n";
656 this.ret+= this.pad + "}\n";
661 * Standardize this crap...
663 * standard properties (use to set)
664 * If they are long values show the dialog..
667 * bool is_xxx :: can show a pulldown.. (true/false)
669 * $ string html = string with value interpolated eg. baseURL + ".."
670 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
671 * $ untypedvalue = javascript untyped value...
672 * _ string html ... = translatable..
675 * object properties (not part of the GOjbect being wrapped?
676 * # Gee.ArrayList<Xcls_fileitem> fileitems
681 * methods -- always text editor..
689 * * init -- big string?
691 * event handlers (listeners)
696 * +XXXX -- indicates it's a instance property / not glob...
697 * *XXXX -- skip writing glob property (used as classes that can be created...)
702 void addUserMethods()
705 this.ret+= "\n" + pad + "// user defined functions \n";
707 // user defined functions...
708 var iter = this.node.props.map_iterator();
710 var k = iter.get_key();
711 if (this.shouldIgnore(k)) {
714 // HOW TO DETERIME if its a method?
716 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
719 // function in the format of {type} (args) { .... }
720 var kk = k.substring(2);
721 var vv = iter.get_value();
722 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
731 if (this.depth > 0) {
732 this.ret+= this.inpad + "}\n";
735 var iter = this.node.items.list_iterator();
737 while (iter.next()) {
738 this.ret += this.mungeChild(iter.get());
741 if (this.depth < 1) {
742 this.ret+= this.inpad + "}\n";
747 string padMultiline(string pad, string str)
749 var ar = str.strip().split("\n");
750 return string.joinv("\n" + pad , ar);
753 void ignore(string i) {
754 this.ignoreList.add(i);
757 void ignoreWrapped(string i) {
758 this.ignoreWrappedList.add(i);
761 bool shouldIgnore(string i)
763 return ignoreList.contains(i);
765 bool shouldIgnoreWrapped(string i)
767 return ignoreWrappedList.contains(i);