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";
189 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
191 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
200 // class xxx { WrappedGtk el; }
201 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
202 this.ret += this.pad + "public " + this.cls + " el;\n";
205 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
216 this.ret += pad + "public static " + xcls + " singleton()\n" +
218 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
219 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
221 this.ipad + "return _" + this.node.xvala_id +";\n" +
226 void addTopProperties()
228 if (this.depth > 0) {
231 // properties - global..??
233 var iter = this.vitems.list_iterator();
238 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
242 if (n.xvala_id[0] == '*') {
245 if (n.xvala_id[0] == '+') {
248 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
255 this.ret += "\n" + this.ipad + "// my vars (def)\n";
259 var cls = Palete.Gir.factoryFqn(this.node.fqn());
267 var iter = this.node.props.map_iterator();
268 while (iter.next()) {
269 var k = iter.get_key();
270 if (this.shouldIgnore(k)) {
273 var vv = k.strip().split(" ");
274 // user defined method
283 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
287 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
288 if (vv.length < min) {
289 // skip 'old js style properties without a type'
293 var kname = vv[vv.length-1];
295 if (this.shouldIgnore(kname)) {
299 // is it a class property...
300 if (cls.props.has_key(kname) && vv[0] != "#") {
307 this.ret += this.pad + "public " +
308 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
316 // if id of child is '+' then it's a property of this..
317 void addPlusProperties()
319 if (this.node.items.size < 1) {
322 var iter = this.node.items.list_iterator();
323 while (iter.next()) {
326 if (ci.xvala_id[0] != '+') {
327 continue; // skip generation of children?
330 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
345 this.ret += "\n" + this.pad + "// ctor \n";
346 if (this.node.has("* args")) {
347 // not sure what this is supposed to be ding..
349 cargs_str = ", " + this.node.get("* args");
350 //var ar = this.node.get("* args");.split(",");
351 //for (var ari =0; ari < ar.length; ari++) {
352 // cargs += (ar[ari].trim().split(" ").pop();
356 if (this.depth < 1) {
357 this.ret += this.pad + "public " + this.xcls + "(" +
358 cargs_str +")\n" + this.pad + "{\n";
363 this.ret+= this.pad + "public " + this.xcls + "(" +
364 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
373 this.ret += this.ipad + "_this = this;\n";
376 this.ret+= this.ipad + "_this = _owner;\n";
378 if (this.node.props.has_key("id")
380 this.node.xvala_id != ""
382 this.node.xvala_id[0] != '*'
384 this.node.xvala_id[0] != '+'
386 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
394 void addWrappedCtor()
397 // this may need to look up properties to fill in the arguments..
398 // introspection does not workk..... - as things like gtkmessagedialog
400 if (cls == 'Gtk.Table') {
402 var methods = this.palete.getPropertiesFor(cls, 'methods');
404 print(JSON.stringify(this.palete.proplist[cls], null,4));
408 if (this.node.has("* ctor")) {
411 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
414 // the ctor arguments...
417 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
418 //if (default_ctor == null) {
419 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
422 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
424 var iter =default_ctor.paramset.params.list_iterator();
425 while (iter.next()) {
426 var n = iter.get().name;
427 if (!this.node.has(n)) {
429 if (iter.get().type.contains("int")) {
433 if (iter.get().type.contains("float")) {
437 if (iter.get().type.contains("bool")) {
438 args += "true"; // always default to true?
441 // any other types???
446 this.ignoreWrapped(n);
449 var v = this.node.get(n);
451 if (iter.get().type == "string") {
452 v = "\"" + v.escape("") + "\"";
454 if (v == "TRUE" || v == "FALSE") {
462 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
468 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
475 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
476 //print(JSON.stringify(meths,null,4));Seed.quit();
480 // initialize.. my vars..
481 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
483 var iter = this.myvars.list_iterator();
488 var ar = k.strip().split(" ");
489 var kname = ar[ar.length-1];
491 var v = this.node.props.get(k);
496 if (v == "FALSE" || v == "TRUE") {
499 //FIXME -- check for raw string.. "string XXXX"
501 // if it's a string...
503 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
511 void addWrappedProperties()
513 var cls = Palete.Gir.factoryFqn(this.node.fqn());
517 // what are the properties of this class???
518 this.ret += "\n" + this.ipad + "// set gobject values\n";
520 var iter = cls.props.map_iterator();
521 while (iter.next()) {
522 var p = iter.get_key();
523 print("Check Write %s\n", p);
524 if (!this.node.has(p)) {
527 if (this.shouldIgnoreWrapped(p)) {
532 var v = this.node.get(p);
534 var nodekey = this.node.get_key(p);
536 // user defined properties.
537 if (nodekey[0] == '#') {
543 var is_raw = nodekey[0] == '$';
545 // what's the type.. - if it's a string.. then we quote it..
546 if (iter.get_value().type == "string" && !is_raw) {
547 v = "\"" + v.escape("") + "\"";
549 if (v == "TRUE" || v == "FALSE") {
552 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
557 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
569 if (this.node.items.size < 1) {
573 var iter = this.node.items.list_iterator();
575 while (iter.next()) {
580 if (ci.xvala_id[0] == '*') {
581 continue; // skip generation of children?
585 if (ci.has("* args")) {
587 var ar = ci.get("* args").split(",");
588 for (var ari = 0 ; ari < ar.length; ari++ ) {
589 var arg = ar[ari].split(" ");
590 xargs += "," + arg[arg.length -1];
594 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
595 "( _this " + xargs + ");\n" ;
597 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
599 if (ci.has("* prop")) {
600 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
604 // not sure why we have 'true' in pack?!?
605 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
609 string[] packing = { "add" };
610 if (ci.has("pack")) {
611 packing = ci.get("pack").split(",");
614 var pack = packing[0];
615 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
616 (packing.length > 1 ?
617 (", " + string.joinv(",", packing).substring(pack.length+1))
623 if (ci.xvala_id[0] != '+') {
624 continue; // skip generation of children?
627 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
636 if (!this.node.has("init")) {
639 this.ret+= "\n" + ipad + "// init method \n";
641 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
646 if (this.node.listeners.size < 1) {
652 this.ret+= "\n" + ipad + "// listeners \n";
654 var iter = this.node.listeners.map_iterator();
655 while (iter.next()) {
656 var k = iter.get_key();
657 var v = iter.get_value();
658 this.ret+= this.ipad + "this.el." + k + ".connect( " +
659 this.padMultiline(this.ipad,v) +");\n";
669 this.ret+= this.pad + "}\n";
674 * Standardize this crap...
676 * standard properties (use to set)
677 * If they are long values show the dialog..
680 * bool is_xxx :: can show a pulldown.. (true/false)
682 * $ string html = string with value interpolated eg. baseURL + ".."
683 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
684 * $ untypedvalue = javascript untyped value...
685 * _ string html ... = translatable..
688 * object properties (not part of the GOjbect being wrapped?
689 * # Gee.ArrayList<Xcls_fileitem> fileitems
694 * methods -- always text editor..
702 * * init -- big string?
704 * event handlers (listeners)
709 * +XXXX -- indicates it's a instance property / not glob...
710 * *XXXX -- skip writing glob property (used as classes that can be created...)
715 void addUserMethods()
718 this.ret+= "\n" + pad + "// user defined functions \n";
720 // user defined functions...
721 var iter = this.node.props.map_iterator();
723 var k = iter.get_key();
724 if (this.shouldIgnore(k)) {
727 // HOW TO DETERIME if its a method?
729 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
732 // function in the format of {type} (args) { .... }
733 var kk = k.substring(2);
734 var vv = iter.get_value();
735 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
744 if (this.depth > 0) {
745 this.ret+= this.inpad + "}\n";
748 var iter = this.node.items.list_iterator();
750 while (iter.next()) {
751 this.ret += this.mungeChild(iter.get());
754 if (this.depth < 1) {
755 this.ret+= this.inpad + "}\n";
760 string padMultiline(string pad, string str)
762 var ar = str.strip().split("\n");
763 return string.joinv("\n" + pad , ar);
766 void ignore(string i) {
767 this.ignoreList.add(i);
770 void ignoreWrapped(string i) {
771 this.ignoreWrappedList.add(i);
774 bool shouldIgnore(string i)
776 return ignoreList.contains(i);
778 bool shouldIgnoreWrapped(string i)
780 return ignoreWrappedList.contains(i);