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);
181 public void globalVars()
183 if (this.depth > 0) {
187 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
190 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
199 // class xxx { WrappedGtk el; }
200 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
201 this.ret += this.pad + "public " + this.cls + " el;\n";
204 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
215 this.ret += pad + "public static " + xcls + " singleton()\n" +
217 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
218 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
220 this.ipad + "return _" + this.node.xvala_id +";\n" +
225 void addTopProperties()
227 if (this.depth > 0) {
230 // properties - global..??
232 var iter = this.vitems.list_iterator();
237 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
241 if (n.xvala_id[0] == '*') {
244 if (n.xvala_id[0] == '+') {
247 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
254 this.ret += "\n" + this.ipad + "// my vars (def)\n";
258 var cls = Palete.Gir.factoryFqn(this.node.fqn());
266 var iter = this.node.props.map_iterator();
267 while (iter.next()) {
268 var k = iter.get_key();
269 if (this.shouldIgnore(k)) {
272 var vv = k.strip().split(" ");
273 // user defined method
282 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
286 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
287 if (vv.length < min) {
288 // skip 'old js style properties without a type'
292 var kname = vv[vv.length-1];
294 if (this.shouldIgnore(kname)) {
298 // is it a class property...
299 if (cls.props.has_key(kname) && vv[0] != "#") {
306 this.ret += this.pad + "public " +
307 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
315 // if id of child is '+' then it's a property of this..
316 void addPlusProperties()
318 if (this.node.items.size < 1) {
321 var iter = this.node.items.list_iterator();
322 while (iter.next()) {
325 if (ci.xvala_id[0] != '+') {
326 continue; // skip generation of children?
329 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
344 this.ret += "\n" + this.pad + "// ctor \n";
345 if (this.node.has("* args")) {
346 // not sure what this is supposed to be ding..
348 cargs_str = ", " + this.node.get("* args");
349 //var ar = this.node.get("* args");.split(",");
350 //for (var ari =0; ari < ar.length; ari++) {
351 // cargs += (ar[ari].trim().split(" ").pop();
355 if (this.depth < 1) {
356 this.ret += this.pad + "public " + this.xcls + "(" +
357 cargs_str +")\n" + this.pad + "{\n";
362 this.ret+= this.pad + "public " + this.xcls + "(" +
363 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
372 this.ret += this.ipad + "_this = this;\n";
375 this.ret+= this.ipad + "_this = _owner;\n";
377 if (this.node.props.has_key("id")
379 this.node.xvala_id != ""
381 this.node.xvala_id[0] != '*'
383 this.node.xvala_id[0] != '+'
385 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
393 void addWrappedCtor()
396 // this may need to look up properties to fill in the arguments..
397 // introspection does not workk..... - as things like gtkmessagedialog
399 if (cls == 'Gtk.Table') {
401 var methods = this.palete.getPropertiesFor(cls, 'methods');
403 print(JSON.stringify(this.palete.proplist[cls], null,4));
407 if (this.node.has("* ctor")) {
410 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
413 // the ctor arguments...
416 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
417 //if (default_ctor == null) {
418 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
421 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
423 var iter =default_ctor.paramset.params.list_iterator();
424 while (iter.next()) {
425 var n = iter.get().name;
426 if (!this.node.has(n)) {
428 if (iter.get().type.contains("int")) {
432 if (iter.get().type.contains("float")) {
436 if (iter.get().type.contains("bool")) {
437 args += "true"; // always default to true?
440 // any other types???
445 this.ignoreWrapped(n);
448 var v = this.node.get(n);
450 if (iter.get().type == "string") {
451 v = "\"" + v.escape("") + "\"";
453 if (v == "TRUE" || v == "FALSE") {
461 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
467 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
474 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
475 //print(JSON.stringify(meths,null,4));Seed.quit();
479 // initialize.. my vars..
480 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
482 var iter = this.myvars.list_iterator();
487 var ar = k.strip().split(" ");
488 var kname = ar[ar.length-1];
490 var v = this.node.props.get(k);
495 if (v == "FALSE" || v == "TRUE") {
498 //FIXME -- check for raw string.. "string XXXX"
500 // if it's a string...
502 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
510 void addWrappedProperties()
512 var cls = Palete.Gir.factoryFqn(this.node.fqn());
516 // what are the properties of this class???
517 this.ret += "\n" + this.ipad + "// set gobject values\n";
519 var iter = cls.props.map_iterator();
520 while (iter.next()) {
521 var p = iter.get_key();
522 print("Check Write %s\n", p);
523 if (!this.node.has(p)) {
526 if (this.shouldIgnoreWrapped(p)) {
531 var v = this.node.get(p);
533 var nodekey = this.node.get_key(p);
535 // user defined properties.
536 if (nodekey[0] == '#') {
542 var is_raw = nodekey[0] == '$';
544 // what's the type.. - if it's a string.. then we quote it..
545 if (iter.get_value().type == "string" && !is_raw) {
546 v = "\"" + v.escape("") + "\"";
548 if (v == "TRUE" || v == "FALSE") {
551 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
556 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
568 if (this.node.items.size < 1) {
572 var iter = this.node.items.list_iterator();
574 while (iter.next()) {
579 if (ci.xvala_id[0] == '*') {
580 continue; // skip generation of children?
584 if (ci.has("* args")) {
586 var ar = ci.get("* args").split(",");
587 for (var ari = 0 ; ari < ar.length; ari++ ) {
588 var arg = ar[ari].split(" ");
589 xargs += "," + arg[arg.length -1];
593 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
594 "( _this " + xargs + ");\n" ;
596 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
598 if (ci.has("* prop")) {
599 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
603 // not sure why we have 'true' in pack?!?
604 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
608 string[] packing = { "add" };
609 if (ci.has("pack")) {
610 packing = ci.get("pack").split(",");
613 var pack = packing[0];
614 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
615 (packing.length > 1 ?
616 (", " + string.joinv(",", packing).substring(pack.length+1))
622 if (ci.xvala_id[0] != '+') {
623 continue; // skip generation of children?
626 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
635 if (!this.node.has("init")) {
638 this.ret+= "\n" + ipad + "// init method \n";
640 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
645 if (this.node.listeners.size < 1) {
651 this.ret+= "\n" + ipad + "// listeners \n";
653 var iter = this.node.listeners.map_iterator();
654 while (iter.next()) {
655 var k = iter.get_key();
656 var v = iter.get_value();
657 this.ret+= this.ipad + "this.el." + k + ".connect( " +
658 this.padMultiline(this.ipad,v) +");\n";
668 this.ret+= this.pad + "}\n";
673 * Standardize this crap...
675 * standard properties (use to set)
676 * If they are long values show the dialog..
679 * bool is_xxx :: can show a pulldown.. (true/false)
681 * $ string html = string with value interpolated eg. baseURL + ".."
682 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
683 * $ untypedvalue = javascript untyped value...
684 * _ string html ... = translatable..
687 * object properties (not part of the GOjbect being wrapped?
688 * # Gee.ArrayList<Xcls_fileitem> fileitems
693 * methods -- always text editor..
701 * * init -- big string?
703 * event handlers (listeners)
708 * +XXXX -- indicates it's a instance property / not glob...
709 * *XXXX -- skip writing glob property (used as classes that can be created...)
714 void addUserMethods()
717 this.ret+= "\n" + pad + "// user defined functions \n";
719 // user defined functions...
720 var iter = this.node.props.map_iterator();
722 var k = iter.get_key();
723 if (this.shouldIgnore(k)) {
726 // HOW TO DETERIME if its a method?
728 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
731 // function in the format of {type} (args) { .... }
732 var kk = k.substring(2);
733 var vv = iter.get_value();
734 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
743 if (this.depth > 0) {
744 this.ret+= this.inpad + "}\n";
747 var iter = this.node.items.list_iterator();
749 while (iter.next()) {
750 this.ret += this.mungeChild(iter.get());
753 if (this.depth < 1) {
754 this.ret+= this.inpad + "}\n";
759 string padMultiline(string pad, string str)
761 var ar = str.strip().split("\n");
762 return string.joinv("\n" + pad , ar);
765 void ignore(string i) {
766 this.ignoreList.add(i);
769 void ignoreWrapped(string i) {
770 this.ignoreWrappedList.add(i);
773 bool shouldIgnore(string i)
775 return ignoreList.contains(i);
777 bool shouldIgnoreWrapped(string i)
779 return ignoreWrappedList.contains(i);