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 * Main entry point to convert a file into a string..
108 public static string mungeFile(JsRender file)
110 if (file.tree == null) {
114 var n = new NodeToVala(file.tree, 0, null);
118 n.toValaName(file.tree);
121 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
122 n.cls = file.tree.xvala_cls;
123 n.xcls = file.tree.xvala_xcls;
129 public string munge ( )
131 //return this.mungeToString(this.node);
136 this.ignore("xtype");
142 this.addTopProperties();
144 this.addPlusProperties();
147 this.addWrappedCtor();
149 this.addInitMyVars();
150 this.addWrappedProperties();
155 this.addUserMethods();
162 public string mungeChild( Node cnode)
164 var x = new NodeToVala(cnode, this.depth+1, this.top);
168 public void globalVars()
170 if (this.depth > 0) {
174 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
177 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
186 // class xxx { WrappedGtk el; }
187 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
188 this.ret += this.pad + "public " + this.cls + " el;\n";
191 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
202 this.ret += pad + "public static " + xcls + " singleton()\n" +
204 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
205 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
207 this.ipad + "return _" + this.node.xvala_id +";\n" +
212 void addTopProperties()
214 if (this.depth > 0) {
217 // properties - global..??
219 var iter = this.vitems.list_iterator();
224 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
228 if (n.xvala_id[0] == '*') {
231 if (n.xvala_id[0] == '+') {
234 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
241 this.ret += "\n" + this.ipad + "// my vars (def)\n";
245 var cls = Palete.Gir.factoryFqn(this.node.fqn());
253 var iter = this.node.props.map_iterator();
254 while (iter.next()) {
255 var k = iter.get_key();
256 if (this.shouldIgnore(k)) {
259 var vv = k.strip().split(" ");
260 // user defined method
269 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
273 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
274 if (vv.length < min) {
275 // skip 'old js style properties without a type'
279 var kname = vv[vv.length-1];
281 if (this.shouldIgnore(kname)) {
285 // is it a class property...
286 if (cls.props.has_key(kname) && vv[0] != "#") {
293 this.ret += this.pad + "public " +
294 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
302 // if id of child is '+' then it's a property of this..
303 void addPlusProperties()
305 if (this.node.items.size < 1) {
308 var iter = this.node.items.list_iterator();
309 while (iter.next()) {
312 if (ci.xvala_id[0] != '+') {
313 continue; // skip generation of children?
316 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
331 this.ret += "\n" + this.pad + "// ctor \n";
332 if (this.node.has("* args")) {
333 // not sure what this is supposed to be ding..
335 cargs_str = ", " + this.node.get("* args");
336 //var ar = this.node.get("* args");.split(",");
337 //for (var ari =0; ari < ar.length; ari++) {
338 // cargs += (ar[ari].trim().split(" ").pop();
342 if (this.depth < 1) {
343 this.ret += this.pad + "public " + this.xcls + "(" +
344 cargs_str +")\n" + this.pad + "{\n";
349 this.ret+= this.pad + "public " + this.xcls + "(" +
350 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
359 this.ret += this.ipad + "_this = this;\n";
362 this.ret+= this.ipad + "_this = _owner;\n";
364 if (this.node.props.has_key("id")
366 this.node.xvala_id != ""
368 this.node.xvala_id[0] != '*'
370 this.node.xvala_id[0] != '+'
372 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
380 void addWrappedCtor()
383 // this may need to look up properties to fill in the arguments..
384 // introspection does not workk..... - as things like gtkmessagedialog
386 if (cls == 'Gtk.Table') {
388 var methods = this.palete.getPropertiesFor(cls, 'methods');
390 print(JSON.stringify(this.palete.proplist[cls], null,4));
394 if (this.node.has("* ctor")) {
397 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
400 // the ctor arguments...
403 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
404 //if (default_ctor == null) {
405 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
408 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
410 var iter =default_ctor.paramset.params.list_iterator();
411 while (iter.next()) {
412 var n = iter.get().name;
413 if (!this.node.has(n)) {
415 if (iter.get().type.contains("int")) {
419 if (iter.get().type.contains("float")) {
423 if (iter.get().type.contains("bool")) {
424 args += "true"; // always default to true?
427 // any other types???
432 this.ignoreWrapped(n);
435 var v = this.node.get(n);
437 if (iter.get().type == "string") {
438 v = "\"" + v.escape("") + "\"";
440 if (v == "TRUE" || v == "FALSE") {
448 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
454 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
461 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
462 //print(JSON.stringify(meths,null,4));Seed.quit();
466 // initialize.. my vars..
467 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
469 var iter = this.myvars.list_iterator();
474 var ar = k.strip().split(" ");
475 var kname = ar[ar.length-1];
477 var v = this.node.props.get(k);
482 if (v == "FALSE" || v == "TRUE") {
485 //FIXME -- check for raw string.. "string XXXX"
487 // if it's a string...
489 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
497 void addWrappedProperties()
499 var cls = Palete.Gir.factoryFqn(this.node.fqn());
503 // what are the properties of this class???
504 this.ret += "\n" + this.ipad + "// set gobject values\n";
506 var iter = cls.props.map_iterator();
507 while (iter.next()) {
508 var p = iter.get_key();
509 print("Check Write %s\n", p);
510 if (!this.node.has(p)) {
513 if (this.shouldIgnoreWrapped(p)) {
518 var v = this.node.get(p);
520 var nodekey = this.node.get_key(p);
522 // user defined properties.
523 if (nodekey[0] == '#') {
529 var is_raw = nodekey[0] == '$';
531 // what's the type.. - if it's a string.. then we quote it..
532 if (iter.get_value().type == "string" && !is_raw) {
533 v = "\"" + v.escape("") + "\"";
535 if (v == "TRUE" || v == "FALSE") {
538 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
543 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
555 if (this.node.items.size < 1) {
559 var iter = this.node.items.list_iterator();
561 while (iter.next()) {
566 if (ci.xvala_id[0] == '*') {
567 continue; // skip generation of children?
571 if (ci.has("* args")) {
573 var ar = ci.get("* args").split(",");
574 for (var ari = 0 ; ari < ar.length; ari++ ) {
575 var arg = ar[ari].split(" ");
576 xargs += "," + arg[arg.length -1];
580 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
581 "( _this " + xargs + ");\n" ;
583 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
585 if (ci.has("* prop")) {
586 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
590 // not sure why we have 'true' in pack?!?
591 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
595 string[] packing = { "add" };
596 if (ci.has("pack")) {
597 packing = ci.get("pack").split(",");
600 var pack = packing[0];
601 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
602 (packing.length > 1 ?
603 (", " + string.joinv(",", packing).substring(pack.length+1))
609 if (ci.xvala_id[0] != '+') {
610 continue; // skip generation of children?
613 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
622 if (!this.node.has("init")) {
625 this.ret+= "\n" + ipad + "// init method \n";
627 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
632 if (this.node.listeners.size < 1) {
638 this.ret+= "\n" + ipad + "// listeners \n";
640 var iter = this.node.listeners.map_iterator();
641 while (iter.next()) {
642 var k = iter.get_key();
643 var v = iter.get_value();
644 this.ret+= this.ipad + "this.el." + k + ".connect( " +
645 this.padMultiline(this.ipad,v) +");\n";
655 this.ret+= this.pad + "}\n";
660 * Standardize this crap...
662 * standard properties (use to set)
663 * If they are long values show the dialog..
666 * bool is_xxx :: can show a pulldown.. (true/false)
668 * $ string html = string with value interpolated eg. baseURL + ".."
669 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
670 * $ untypedvalue = javascript untyped value...
671 * _ string html ... = translatable..
674 * object properties (not part of the GOjbect being wrapped?
675 * # Gee.ArrayList<Xcls_fileitem> fileitems
680 * methods -- always text editor..
688 * * init -- big string?
690 * event handlers (listeners)
695 * +XXXX -- indicates it's a instance property / not glob...
696 * *XXXX -- skip writing glob property (used as classes that can be created...)
701 void addUserMethods()
704 this.ret+= "\n" + pad + "// user defined functions \n";
706 // user defined functions...
707 var iter = this.node.props.map_iterator();
709 var k = iter.get_key();
710 if (this.shouldIgnore(k)) {
713 // HOW TO DETERIME if its a method?
715 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
718 // function in the format of {type} (args) { .... }
719 var kk = k.substring(2);
720 var vv = iter.get_value();
721 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
730 if (this.depth > 0) {
731 this.ret+= this.inpad + "}\n";
734 var iter = this.node.items.list_iterator();
736 while (iter.next()) {
737 this.ret += this.mungeChild(iter.get());
740 if (this.depth < 1) {
741 this.ret+= this.inpad + "}\n";
746 string padMultiline(string pad, string str)
748 var ar = str.strip().split("\n");
749 return string.joinv("\n" + pad , ar);
752 void ignore(string i) {
753 this.ignoreList.add(i);
756 void ignoreWrapped(string i) {
757 this.ignoreWrappedList.add(i);
760 bool shouldIgnore(string i)
762 return ignoreList.contains(i);
764 bool shouldIgnoreWrapped(string i)
766 return ignoreWrappedList.contains(i);