3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
12 public class JsRender.NodeToVala : Object {
25 Gee.ArrayList<string> ignoreList;
26 Gee.ArrayList<string> ignoreWrappedList;
27 Gee.ArrayList<string> myvars;
28 Gee.ArrayList<Node> vitems; // top level items
32 public NodeToVala( Node node, int depth, NodeToVala? top)
38 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
39 this.pad = this.inpad + " ";
40 this.ipad = this.inpad + " ";
41 this.cls = node.xvala_cls;
42 this.xcls = node.xvala_xcls;
44 this.top = top == null ? this : top;
45 this.ignoreList = new Gee.ArrayList<string>();
46 this.ignoreWrappedList = new Gee.ArrayList<string>();
47 this.myvars = new Gee.ArrayList<string>();
48 this.vitems = new Gee.ArrayList<Node>();
53 string toValaNS(Node item)
55 var ns = item.get("xns") ;
56 if (ns == "GtkSource") {
61 public void toValaName(Node item, int depth =0)
65 var ns = this.toValaNS(item) ;
66 var cls = ns + item.get("xtype");
72 string id = item.get("id").length > 0 ?
73 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
78 if (id[0] == '*' || id[0] == '+') {
79 item.xvala_xcls = "Xcls_" + id.substring(1);
81 item.xvala_xcls = "Xcls_" + id;
87 this.vitems.add(item);
88 } else if (!item.props.has_key("id")) {
89 // use the file name..
90 item.xvala_xcls = this.file.name;
92 item.xvala_id = this.file.name;
97 if (item.items.size < 1) {
100 for(var i =0;i<item.items.size;i++) {
101 this.toValaName(item.items.get(i), depth+1);
106 public static string mungeFile(JsRender file)
108 if (file.tree == null) {
112 var n = new NodeToVala(file.tree, 0, null);
116 n.toValaName(file.tree);
119 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
120 n.cls = file.tree.xvala_cls;
121 n.xcls = file.tree.xvala_xcls;
127 public string munge ( )
129 //return this.mungeToString(this.node);
134 this.ignore("xtype");
140 this.addTopProperties();
142 this.addPlusProperties();
145 this.addWrappedCtor();
147 this.addInitMyVars();
148 this.addWrappedProperties();
153 this.addUserMethods();
160 public string mungeChild( Node cnode)
162 var x = new NodeToVala(cnode, this.depth+1, this.top);
166 public void globalVars()
168 if (this.depth > 0) {
172 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
175 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
184 // class xxx { WrappedGtk el; }
185 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
186 this.ret += this.pad + "public " + this.cls + " el;\n";
189 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
200 this.ret += pad + "public static " + xcls + " singleton()\n" +
202 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
203 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
205 this.ipad + "return _" + this.node.xvala_id +";\n" +
210 void addTopProperties()
212 if (this.depth > 0) {
215 // properties - global..??
217 var iter = this.vitems.list_iterator();
222 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
226 if (n.xvala_id[0] == '*') {
229 if (n.xvala_id[0] == '+') {
232 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
239 this.ret += "\n" + this.ipad + "// my vars (def)\n";
243 var cls = Palete.Gir.factoryFqn(this.node.fqn());
251 var iter = this.node.props.map_iterator();
252 while (iter.next()) {
253 var k = iter.get_key();
254 if (this.shouldIgnore(k)) {
257 var vv = k.strip().split(" ");
258 // user defined method
267 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
271 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
272 if (vv.length < min) {
273 // skip 'old js style properties without a type'
277 var kname = vv[vv.length-1];
279 if (this.shouldIgnore(kname)) {
283 // is it a class property...
284 if (cls.props.has_key(kname) && vv[0] != "#") {
291 this.ret += this.pad + "public " +
292 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
300 // if id of child is '+' then it's a property of this..
301 void addPlusProperties()
303 if (this.node.items.size < 1) {
306 var iter = this.node.items.list_iterator();
307 while (iter.next()) {
310 if (ci.xvala_id[0] != '+') {
311 continue; // skip generation of children?
314 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
329 this.ret += "\n" + this.pad + "// ctor \n";
330 if (this.node.has("* args")) {
331 // not sure what this is supposed to be ding..
333 cargs_str = ", " + this.node.get("* args");
334 //var ar = this.node.get("* args");.split(",");
335 //for (var ari =0; ari < ar.length; ari++) {
336 // cargs += (ar[ari].trim().split(" ").pop();
340 if (this.depth < 1) {
341 this.ret += this.pad + "public " + this.xcls + "(" +
342 cargs_str +")\n" + this.pad + "{\n";
347 this.ret+= this.pad + "public " + this.xcls + "(" +
348 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
357 this.ret += this.ipad + "_this = this;\n";
360 this.ret+= this.ipad + "_this = _owner;\n";
362 if (this.node.props.has_key("id")
364 this.node.xvala_id != ""
366 this.node.xvala_id[0] != '*'
368 this.node.xvala_id[0] != '+'
370 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
378 void addWrappedCtor()
381 // this may need to look up properties to fill in the arguments..
382 // introspection does not workk..... - as things like gtkmessagedialog
384 if (cls == 'Gtk.Table') {
386 var methods = this.palete.getPropertiesFor(cls, 'methods');
388 print(JSON.stringify(this.palete.proplist[cls], null,4));
392 if (this.node.has("* ctor")) {
395 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
398 // the ctor arguments...
401 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
402 //if (default_ctor == null) {
403 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
406 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
408 var iter =default_ctor.paramset.params.list_iterator();
409 while (iter.next()) {
410 var n = iter.get().name;
411 if (!this.node.has(n)) {
413 if (iter.get().type.contains("int")) {
417 if (iter.get().type.contains("float")) {
421 if (iter.get().type.contains("bool")) {
422 args += "true"; // always default to true?
425 // any other types???
430 this.ignoreWrapped(n);
433 var v = this.node.get(n);
435 if (iter.get().type == "string") {
436 v = "\"" + v.escape("") + "\"";
438 if (v == "TRUE" || v == "FALSE") {
446 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
452 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
459 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
460 //print(JSON.stringify(meths,null,4));Seed.quit();
464 // initialize.. my vars..
465 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
467 var iter = this.myvars.list_iterator();
472 var ar = k.strip().split(" ");
473 var kname = ar[ar.length-1];
475 var v = this.node.props.get(k);
480 if (v == "FALSE" || v == "TRUE") {
483 //FIXME -- check for raw string.. "string XXXX"
485 // if it's a string...
487 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
495 void addWrappedProperties()
497 var cls = Palete.Gir.factoryFqn(this.node.fqn());
501 // what are the properties of this class???
502 this.ret += "\n" + this.ipad + "// set gobject values\n";
504 var iter = cls.props.map_iterator();
505 while (iter.next()) {
506 var p = iter.get_key();
507 print("Check Write %s\n", p);
508 if (!this.node.has(p)) {
511 if (this.shouldIgnoreWrapped(p)) {
516 var v = this.node.get(p);
518 var nodekey = this.node.get_key(p);
520 // user defined properties.
521 if (nodekey[0] == '#') {
527 var is_raw = nodekey[0] == '$';
529 // what's the type.. - if it's a string.. then we quote it..
530 if (iter.get_value().type == "string" && !is_raw) {
531 v = "\"" + v.escape("") + "\"";
533 if (v == "TRUE" || v == "FALSE") {
536 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
541 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
553 if (this.node.items.size < 1) {
557 var iter = this.node.items.list_iterator();
559 while (iter.next()) {
564 if (ci.xvala_id[0] == '*') {
565 continue; // skip generation of children?
569 if (ci.has("* args")) {
571 var ar = ci.get("* args").split(",");
572 for (var ari = 0 ; ari < ar.length; ari++ ) {
573 var arg = ar[ari].split(" ");
574 xargs += "," + arg[arg.length -1];
578 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
579 "( _this " + xargs + ");\n" ;
581 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
583 if (ci.has("* prop")) {
584 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
588 // not sure why we have 'true' in pack?!?
589 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
593 string[] packing = { "add" };
594 if (ci.has("pack")) {
595 packing = ci.get("pack").split(",");
598 var pack = packing[0];
599 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
600 (packing.length > 1 ?
601 (", " + string.joinv(",", packing).substring(pack.length+1))
607 if (ci.xvala_id[0] != '+') {
608 continue; // skip generation of children?
611 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
620 if (!this.node.has("init")) {
623 this.ret+= "\n" + ipad + "// init method \n";
625 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
630 if (this.node.listeners.size < 1) {
636 this.ret+= "\n" + ipad + "// listeners \n";
638 var iter = this.node.listeners.map_iterator();
639 while (iter.next()) {
640 var k = iter.get_key();
641 var v = iter.get_value();
642 this.ret+= this.ipad + "this.el." + k + ".connect( " +
643 this.padMultiline(this.ipad,v) +");\n";
653 this.ret+= this.pad + "}\n";
658 * Standardize this crap...
660 * standard properties (use to set)
661 * If they are long values show the dialog..
664 * bool is_xxx :: can show a pulldown.. (true/false)
666 * $ string html = string with value interpolated eg. baseURL + ".."
667 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
668 * $ untypedvalue = javascript untyped value...
669 * _ string html ... = translatable..
672 * object properties (not part of the GOjbect being wrapped?
673 * # Gee.ArrayList<Xcls_fileitem> fileitems
678 * methods -- always text editor..
686 * * init -- big string?
688 * event handlers (listeners)
693 * +XXXX -- indicates it's a instance property / not glob...
694 * *XXXX -- skip writing glob property (used as classes that can be created...)
699 void addUserMethods()
702 this.ret+= "\n" + pad + "// user defined functions \n";
704 // user defined functions...
705 var iter = this.node.props.map_iterator();
707 var k = iter.get_key();
708 if (this.shouldIgnore(k)) {
711 // HOW TO DETERIME if its a method?
713 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
716 // function in the format of {type} (args) { .... }
717 var kk = k.substring(2);
718 var vv = iter.get_value();
719 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
728 if (this.depth > 0) {
729 this.ret+= this.inpad + "}\n";
732 var iter = this.node.items.list_iterator();
734 while (iter.next()) {
735 this.ret += this.mungeChild(iter.get());
738 if (this.depth < 1) {
739 this.ret+= this.inpad + "}\n";
744 string padMultiline(string pad, string str)
746 var ar = str.strip().split("\n");
747 return string.joinv("\n" + pad , ar);
750 void ignore(string i) {
751 this.ignoreList.add(i);
754 void ignoreWrapped(string i) {
755 this.ignoreWrappedList.add(i);
758 bool shouldIgnore(string i)
760 return ignoreList.contains(i);
762 bool shouldIgnoreWrapped(string i)
764 return ignoreWrappedList.contains(i);