3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
16 public class JsRender.NodeToVala : Object {
30 Gee.ArrayList<string> ignoreList;
31 Gee.ArrayList<string> ignoreWrappedList;
32 Gee.ArrayList<string> myvars;
33 Gee.ArrayList<Node> vitems; // top level items
37 public NodeToVala( Node node, int depth, NodeToVala? top)
43 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
44 this.pad = this.inpad + " ";
45 this.ipad = this.inpad + " ";
46 this.cls = node.xvala_cls;
47 this.xcls = node.xvala_xcls;
50 this.top = top == null ? this : top;
51 this.ignoreList = new Gee.ArrayList<string>();
52 this.ignoreWrappedList = new Gee.ArrayList<string>();
53 this.myvars = new Gee.ArrayList<string>();
54 this.vitems = new Gee.ArrayList<Node>();
59 string toValaNS(Node item)
61 var ns = item.get("xns") ;
62 if (ns == "GtkSource") {
67 public void toValaName(Node item, int depth =0)
71 var ns = this.toValaNS(item) ;
72 var cls = ns + item.get("xtype");
78 string id = item.get("id").length > 0 ?
79 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
84 if (id[0] == '*' || id[0] == '+') {
85 item.xvala_xcls = "Xcls_" + id.substring(1);
87 item.xvala_xcls = "Xcls_" + id;
93 this.vitems.add(item);
94 } else if (!item.props.has_key("id")) {
95 // use the file name..
96 item.xvala_xcls = this.file.name;
98 item.xvala_id = this.file.name;
103 if (item.items.size < 1) {
106 for(var i =0;i<item.items.size;i++) {
107 this.toValaName(item.items.get(i), depth+1);
112 * Main entry point to convert a file into a string..
114 public static string mungeFile(JsRender file)
116 if (file.tree == null) {
120 var n = new NodeToVala(file.tree, 0, null);
124 n.toValaName(file.tree);
127 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
128 n.cls = file.tree.xvala_cls;
129 n.xcls = file.tree.xvala_xcls;
135 public string munge ( )
137 //return this.mungeToString(this.node);
142 this.ignore("xtype");
148 this.addTopProperties();
150 this.addPlusProperties();
153 this.addWrappedCtor();
155 this.addInitMyVars();
156 this.addWrappedProperties();
161 this.addUserMethods();
168 public string mungeChild( Node cnode)
170 var x = new NodeToVala(cnode, this.depth+1, this.top);
174 public void globalVars()
176 if (this.depth > 0) {
180 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
183 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
192 // class xxx { WrappedGtk el; }
193 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
194 this.ret += this.pad + "public " + this.cls + " el;\n";
197 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
208 this.ret += pad + "public static " + xcls + " singleton()\n" +
210 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
211 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
213 this.ipad + "return _" + this.node.xvala_id +";\n" +
218 void addTopProperties()
220 if (this.depth > 0) {
223 // properties - global..??
225 var iter = this.vitems.list_iterator();
230 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
234 if (n.xvala_id[0] == '*') {
237 if (n.xvala_id[0] == '+') {
240 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
247 this.ret += "\n" + this.ipad + "// my vars (def)\n";
251 var cls = Palete.Gir.factoryFqn(this.node.fqn());
259 var iter = this.node.props.map_iterator();
260 while (iter.next()) {
261 var k = iter.get_key();
262 if (this.shouldIgnore(k)) {
265 var vv = k.strip().split(" ");
266 // user defined method
275 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
279 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
280 if (vv.length < min) {
281 // skip 'old js style properties without a type'
285 var kname = vv[vv.length-1];
287 if (this.shouldIgnore(kname)) {
291 // is it a class property...
292 if (cls.props.has_key(kname) && vv[0] != "#") {
299 this.ret += this.pad + "public " +
300 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
308 // if id of child is '+' then it's a property of this..
309 void addPlusProperties()
311 if (this.node.items.size < 1) {
314 var iter = this.node.items.list_iterator();
315 while (iter.next()) {
318 if (ci.xvala_id[0] != '+') {
319 continue; // skip generation of children?
322 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
337 this.ret += "\n" + this.pad + "// ctor \n";
338 if (this.node.has("* args")) {
339 // not sure what this is supposed to be ding..
341 cargs_str = ", " + this.node.get("* args");
342 //var ar = this.node.get("* args");.split(",");
343 //for (var ari =0; ari < ar.length; ari++) {
344 // cargs += (ar[ari].trim().split(" ").pop();
348 if (this.depth < 1) {
349 this.ret += this.pad + "public " + this.xcls + "(" +
350 cargs_str +")\n" + this.pad + "{\n";
355 this.ret+= this.pad + "public " + this.xcls + "(" +
356 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
365 this.ret += this.ipad + "_this = this;\n";
368 this.ret+= this.ipad + "_this = _owner;\n";
370 if (this.node.props.has_key("id")
372 this.node.xvala_id != ""
374 this.node.xvala_id[0] != '*'
376 this.node.xvala_id[0] != '+'
378 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
386 void addWrappedCtor()
389 // this may need to look up properties to fill in the arguments..
390 // introspection does not workk..... - as things like gtkmessagedialog
392 if (cls == 'Gtk.Table') {
394 var methods = this.palete.getPropertiesFor(cls, 'methods');
396 print(JSON.stringify(this.palete.proplist[cls], null,4));
400 if (this.node.has("* ctor")) {
403 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
406 // the ctor arguments...
409 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
410 //if (default_ctor == null) {
411 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
414 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
416 var iter =default_ctor.paramset.params.list_iterator();
417 while (iter.next()) {
418 var n = iter.get().name;
419 if (!this.node.has(n)) {
421 if (iter.get().type.contains("int")) {
425 if (iter.get().type.contains("float")) {
429 if (iter.get().type.contains("bool")) {
430 args += "true"; // always default to true?
433 // any other types???
438 this.ignoreWrapped(n);
441 var v = this.node.get(n);
443 if (iter.get().type == "string") {
444 v = "\"" + v.escape("") + "\"";
446 if (v == "TRUE" || v == "FALSE") {
454 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
460 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
467 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
468 //print(JSON.stringify(meths,null,4));Seed.quit();
472 // initialize.. my vars..
473 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
475 var iter = this.myvars.list_iterator();
480 var ar = k.strip().split(" ");
481 var kname = ar[ar.length-1];
483 var v = this.node.props.get(k);
488 if (v == "FALSE" || v == "TRUE") {
491 //FIXME -- check for raw string.. "string XXXX"
493 // if it's a string...
495 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
503 void addWrappedProperties()
505 var cls = Palete.Gir.factoryFqn(this.node.fqn());
509 // what are the properties of this class???
510 this.ret += "\n" + this.ipad + "// set gobject values\n";
512 var iter = cls.props.map_iterator();
513 while (iter.next()) {
514 var p = iter.get_key();
515 print("Check Write %s\n", p);
516 if (!this.node.has(p)) {
519 if (this.shouldIgnoreWrapped(p)) {
524 var v = this.node.get(p);
526 var nodekey = this.node.get_key(p);
528 // user defined properties.
529 if (nodekey[0] == '#') {
535 var is_raw = nodekey[0] == '$';
537 // what's the type.. - if it's a string.. then we quote it..
538 if (iter.get_value().type == "string" && !is_raw) {
539 v = "\"" + v.escape("") + "\"";
541 if (v == "TRUE" || v == "FALSE") {
544 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
549 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
561 if (this.node.items.size < 1) {
565 var iter = this.node.items.list_iterator();
567 while (iter.next()) {
572 if (ci.xvala_id[0] == '*') {
573 continue; // skip generation of children?
577 if (ci.has("* args")) {
579 var ar = ci.get("* args").split(",");
580 for (var ari = 0 ; ari < ar.length; ari++ ) {
581 var arg = ar[ari].split(" ");
582 xargs += "," + arg[arg.length -1];
586 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
587 "( _this " + xargs + ");\n" ;
589 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
591 if (ci.has("* prop")) {
592 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
596 // not sure why we have 'true' in pack?!?
597 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
601 string[] packing = { "add" };
602 if (ci.has("pack")) {
603 packing = ci.get("pack").split(",");
606 var pack = packing[0];
607 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
608 (packing.length > 1 ?
609 (", " + string.joinv(",", packing).substring(pack.length+1))
615 if (ci.xvala_id[0] != '+') {
616 continue; // skip generation of children?
619 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
628 if (!this.node.has("init")) {
631 this.ret+= "\n" + ipad + "// init method \n";
633 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
638 if (this.node.listeners.size < 1) {
644 this.ret+= "\n" + ipad + "// listeners \n";
646 var iter = this.node.listeners.map_iterator();
647 while (iter.next()) {
648 var k = iter.get_key();
649 var v = iter.get_value();
650 this.ret+= this.ipad + "this.el." + k + ".connect( " +
651 this.padMultiline(this.ipad,v) +");\n";
661 this.ret+= this.pad + "}\n";
666 * Standardize this crap...
668 * standard properties (use to set)
669 * If they are long values show the dialog..
672 * bool is_xxx :: can show a pulldown.. (true/false)
674 * $ string html = string with value interpolated eg. baseURL + ".."
675 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
676 * $ untypedvalue = javascript untyped value...
677 * _ string html ... = translatable..
680 * object properties (not part of the GOjbect being wrapped?
681 * # Gee.ArrayList<Xcls_fileitem> fileitems
686 * methods -- always text editor..
694 * * init -- big string?
696 * event handlers (listeners)
701 * +XXXX -- indicates it's a instance property / not glob...
702 * *XXXX -- skip writing glob property (used as classes that can be created...)
707 void addUserMethods()
710 this.ret+= "\n" + pad + "// user defined functions \n";
712 // user defined functions...
713 var iter = this.node.props.map_iterator();
715 var k = iter.get_key();
716 if (this.shouldIgnore(k)) {
719 // HOW TO DETERIME if its a method?
721 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
724 // function in the format of {type} (args) { .... }
725 var kk = k.substring(2);
726 var vv = iter.get_value();
727 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
736 if (this.depth > 0) {
737 this.ret+= this.inpad + "}\n";
740 var iter = this.node.items.list_iterator();
742 while (iter.next()) {
743 this.ret += this.mungeChild(iter.get());
746 if (this.depth < 1) {
747 this.ret+= this.inpad + "}\n";
752 string padMultiline(string pad, string str)
754 var ar = str.strip().split("\n");
755 return string.joinv("\n" + pad , ar);
758 void ignore(string i) {
759 this.ignoreList.add(i);
762 void ignoreWrapped(string i) {
763 this.ignoreWrappedList.add(i);
766 bool shouldIgnore(string i)
768 return ignoreList.contains(i);
770 bool shouldIgnoreWrapped(string i)
772 return ignoreWrappedList.contains(i);