3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
16 public class JsRender.NodeToVala : Object {
31 Gee.ArrayList<string> ignoreList;
32 Gee.ArrayList<string> ignoreWrappedList;
33 Gee.ArrayList<string> myvars;
34 Gee.ArrayList<Node> vitems; // top level items
39 * ctor - just initializes things
40 * - wraps a render node
42 public NodeToVala( Node node, int depth, NodeToVala? parent)
48 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
49 this.pad = this.inpad + " ";
50 this.ipad = this.inpad + " ";
51 this.cls = node.xvala_cls;
52 this.xcls = node.xvala_xcls;
54 this.cur_line = parent == null ? 0 : parent.cur_line;
55 node.line = this.cur_line;
57 this.top = parent == null ? this : parent.top;
58 this.ignoreList = new Gee.ArrayList<string>();
59 this.ignoreWrappedList = new Gee.ArrayList<string>();
60 this.myvars = new Gee.ArrayList<string>();
61 this.vitems = new Gee.ArrayList<Node>();
66 string toValaNS(Node item)
68 var ns = item.get("xns") ;
69 if (ns == "GtkSource") {
74 public void toValaName(Node item, int depth =0)
78 var ns = this.toValaNS(item) ;
79 var cls = ns + item.get("xtype");
85 string id = item.get("id").length > 0 ?
86 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
91 if (id[0] == '*' || id[0] == '+') {
92 item.xvala_xcls = "Xcls_" + id.substring(1);
94 item.xvala_xcls = "Xcls_" + id;
100 this.vitems.add(item);
101 } else if (!item.props.has_key("id")) {
102 // use the file name..
103 item.xvala_xcls = this.file.name;
105 item.xvala_id = this.file.name;
110 if (item.items.size < 1) {
113 for(var i =0;i<item.items.size;i++) {
114 this.toValaName(item.items.get(i), depth+1);
119 * Main entry point to convert a file into a string..
121 public static string mungeFile(JsRender file)
123 if (file.tree == null) {
127 var n = new NodeToVala(file.tree, 0, null);
131 n.toValaName(file.tree);
134 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
135 n.cls = file.tree.xvala_cls;
136 n.xcls = file.tree.xvala_xcls;
142 public string munge ( )
144 //return this.mungeToString(this.node);
149 this.ignore("xtype");
155 this.addTopProperties();
157 this.addPlusProperties();
160 this.addWrappedCtor();
162 this.addInitMyVars();
163 this.addWrappedProperties();
168 this.addUserMethods();
175 public string mungeChild( Node cnode)
177 var x = new NodeToVala(cnode, this.depth+1, this);
180 public void addLine(string str)
187 public void globalVars()
189 if (this.depth > 0) {
193 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
195 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
197 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
206 // class xxx { WrappedGtk el; }
207 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
208 this.ret += this.pad + "public " + this.cls + " el;\n";
211 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
222 this.ret += pad + "public static " + xcls + " singleton()\n" +
224 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
225 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
227 this.ipad + "return _" + this.node.xvala_id +";\n" +
232 void addTopProperties()
234 if (this.depth > 0) {
237 // properties - global..??
239 var iter = this.vitems.list_iterator();
244 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
248 if (n.xvala_id[0] == '*') {
251 if (n.xvala_id[0] == '+') {
254 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
261 this.ret += "\n" + this.ipad + "// my vars (def)\n";
265 var cls = Palete.Gir.factoryFqn(this.node.fqn());
273 var iter = this.node.props.map_iterator();
274 while (iter.next()) {
275 var k = iter.get_key();
276 if (this.shouldIgnore(k)) {
279 var vv = k.strip().split(" ");
280 // user defined method
289 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
293 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
294 if (vv.length < min) {
295 // skip 'old js style properties without a type'
299 var kname = vv[vv.length-1];
301 if (this.shouldIgnore(kname)) {
305 // is it a class property...
306 if (cls.props.has_key(kname) && vv[0] != "#") {
313 this.ret += this.pad + "public " +
314 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
322 // if id of child is '+' then it's a property of this..
323 void addPlusProperties()
325 if (this.node.items.size < 1) {
328 var iter = this.node.items.list_iterator();
329 while (iter.next()) {
332 if (ci.xvala_id[0] != '+') {
333 continue; // skip generation of children?
336 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
351 this.ret += "\n" + this.pad + "// ctor \n";
352 if (this.node.has("* args")) {
353 // not sure what this is supposed to be ding..
355 cargs_str = ", " + this.node.get("* args");
356 //var ar = this.node.get("* args");.split(",");
357 //for (var ari =0; ari < ar.length; ari++) {
358 // cargs += (ar[ari].trim().split(" ").pop();
362 if (this.depth < 1) {
363 this.ret += this.pad + "public " + this.xcls + "(" +
364 cargs_str +")\n" + this.pad + "{\n";
369 this.ret+= this.pad + "public " + this.xcls + "(" +
370 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
379 this.ret += this.ipad + "_this = this;\n";
382 this.ret+= this.ipad + "_this = _owner;\n";
384 if (this.node.props.has_key("id")
386 this.node.xvala_id != ""
388 this.node.xvala_id[0] != '*'
390 this.node.xvala_id[0] != '+'
392 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
400 void addWrappedCtor()
403 // this may need to look up properties to fill in the arguments..
404 // introspection does not workk..... - as things like gtkmessagedialog
406 if (cls == 'Gtk.Table') {
408 var methods = this.palete.getPropertiesFor(cls, 'methods');
410 print(JSON.stringify(this.palete.proplist[cls], null,4));
414 if (this.node.has("* ctor")) {
417 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
420 // the ctor arguments...
423 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
424 //if (default_ctor == null) {
425 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
428 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
430 var iter =default_ctor.paramset.params.list_iterator();
431 while (iter.next()) {
432 var n = iter.get().name;
433 if (!this.node.has(n)) {
435 if (iter.get().type.contains("int")) {
439 if (iter.get().type.contains("float")) {
443 if (iter.get().type.contains("bool")) {
444 args += "true"; // always default to true?
447 // any other types???
452 this.ignoreWrapped(n);
455 var v = this.node.get(n);
457 if (iter.get().type == "string") {
458 v = "\"" + v.escape("") + "\"";
460 if (v == "TRUE" || v == "FALSE") {
468 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
474 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
481 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
482 //print(JSON.stringify(meths,null,4));Seed.quit();
486 // initialize.. my vars..
487 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
489 var iter = this.myvars.list_iterator();
494 var ar = k.strip().split(" ");
495 var kname = ar[ar.length-1];
497 var v = this.node.props.get(k);
502 if (v == "FALSE" || v == "TRUE") {
505 //FIXME -- check for raw string.. "string XXXX"
507 // if it's a string...
509 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
517 void addWrappedProperties()
519 var cls = Palete.Gir.factoryFqn(this.node.fqn());
523 // what are the properties of this class???
524 this.ret += "\n" + this.ipad + "// set gobject values\n";
526 var iter = cls.props.map_iterator();
527 while (iter.next()) {
528 var p = iter.get_key();
529 print("Check Write %s\n", p);
530 if (!this.node.has(p)) {
533 if (this.shouldIgnoreWrapped(p)) {
538 var v = this.node.get(p);
540 var nodekey = this.node.get_key(p);
542 // user defined properties.
543 if (nodekey[0] == '#') {
549 var is_raw = nodekey[0] == '$';
551 // what's the type.. - if it's a string.. then we quote it..
552 if (iter.get_value().type == "string" && !is_raw) {
553 v = "\"" + v.escape("") + "\"";
555 if (v == "TRUE" || v == "FALSE") {
558 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
563 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
575 if (this.node.items.size < 1) {
579 var iter = this.node.items.list_iterator();
581 while (iter.next()) {
586 if (ci.xvala_id[0] == '*') {
587 continue; // skip generation of children?
591 if (ci.has("* args")) {
593 var ar = ci.get("* args").split(",");
594 for (var ari = 0 ; ari < ar.length; ari++ ) {
595 var arg = ar[ari].split(" ");
596 xargs += "," + arg[arg.length -1];
600 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
601 "( _this " + xargs + ");\n" ;
603 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
605 if (ci.has("* prop")) {
606 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
610 // not sure why we have 'true' in pack?!?
611 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
615 string[] packing = { "add" };
616 if (ci.has("pack")) {
617 packing = ci.get("pack").split(",");
620 var pack = packing[0];
621 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
622 (packing.length > 1 ?
623 (", " + string.joinv(",", packing).substring(pack.length+1))
629 if (ci.xvala_id[0] != '+') {
630 continue; // skip generation of children?
633 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
642 if (!this.node.has("init")) {
645 this.ret+= "\n" + ipad + "// init method \n";
647 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
652 if (this.node.listeners.size < 1) {
658 this.ret+= "\n" + ipad + "// listeners \n";
660 var iter = this.node.listeners.map_iterator();
661 while (iter.next()) {
662 var k = iter.get_key();
663 var v = iter.get_value();
664 this.ret+= this.ipad + "this.el." + k + ".connect( " +
665 this.padMultiline(this.ipad,v) +");\n";
675 this.ret+= this.pad + "}\n";
680 * Standardize this crap...
682 * standard properties (use to set)
683 * If they are long values show the dialog..
686 * bool is_xxx :: can show a pulldown.. (true/false)
688 * $ string html = string with value interpolated eg. baseURL + ".."
689 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
690 * $ untypedvalue = javascript untyped value...
691 * _ string html ... = translatable..
694 * object properties (not part of the GOjbect being wrapped?
695 * # Gee.ArrayList<Xcls_fileitem> fileitems
700 * methods -- always text editor..
708 * * init -- big string?
710 * event handlers (listeners)
715 * +XXXX -- indicates it's a instance property / not glob...
716 * *XXXX -- skip writing glob property (used as classes that can be created...)
721 void addUserMethods()
724 this.ret+= "\n" + pad + "// user defined functions \n";
726 // user defined functions...
727 var iter = this.node.props.map_iterator();
729 var k = iter.get_key();
730 if (this.shouldIgnore(k)) {
733 // HOW TO DETERIME if its a method?
735 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
738 // function in the format of {type} (args) { .... }
739 var kk = k.substring(2);
740 var vv = iter.get_value();
741 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
750 if (this.depth > 0) {
751 this.ret+= this.inpad + "}\n";
754 var iter = this.node.items.list_iterator();
756 while (iter.next()) {
757 this.ret += this.mungeChild(iter.get());
760 if (this.depth < 1) {
761 this.ret+= this.inpad + "}\n";
766 string padMultiline(string pad, string str)
768 var ar = str.strip().split("\n");
769 return string.joinv("\n" + pad , ar);
772 void ignore(string i) {
773 this.ignoreList.add(i);
776 void ignoreWrapped(string i) {
777 this.ignoreWrappedList.add(i);
780 bool shouldIgnore(string i)
782 return ignoreList.contains(i);
784 bool shouldIgnoreWrapped(string i)
786 return ignoreWrappedList.contains(i);