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;
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>();
64 // initialize line data..
65 node.line = this.cur_line;
66 node.proplines = new Gee.HashMap<string,int>();
67 node.listenlines = Gee.HashMap<string,int>();
74 string toValaNS(Node item)
76 var ns = item.get("xns") ;
77 if (ns == "GtkSource") {
82 public void toValaName(Node item, int depth =0)
86 var ns = this.toValaNS(item) ;
87 var cls = ns + item.get("xtype");
93 string id = item.get("id").length > 0 ?
94 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
99 if (id[0] == '*' || id[0] == '+') {
100 item.xvala_xcls = "Xcls_" + id.substring(1);
102 item.xvala_xcls = "Xcls_" + id;
108 this.vitems.add(item);
109 } else if (!item.props.has_key("id")) {
110 // use the file name..
111 item.xvala_xcls = this.file.name;
113 item.xvala_id = this.file.name;
118 if (item.items.size < 1) {
121 for(var i =0;i<item.items.size;i++) {
122 this.toValaName(item.items.get(i), depth+1);
127 * Main entry point to convert a file into a string..
129 public static string mungeFile(JsRender file)
131 if (file.tree == null) {
135 var n = new NodeToVala(file.tree, 0, null);
139 n.toValaName(file.tree);
142 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
143 n.cls = file.tree.xvala_cls;
144 n.xcls = file.tree.xvala_xcls;
150 public string munge ( )
152 //return this.mungeToString(this.node);
157 this.ignore("xtype");
163 this.addTopProperties();
165 this.addPlusProperties();
168 this.addWrappedCtor();
170 this.addInitMyVars();
171 this.addWrappedProperties();
176 this.addUserMethods();
183 public string mungeChild( Node cnode)
185 var x = new NodeToVala(cnode, this.depth+1, this);
188 public void addLine(string str= "")
191 this.ret += str + "\n";
195 public void globalVars()
197 if (this.depth > 0) {
201 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
203 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
205 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
214 // class xxx { WrappedGtk el; }
215 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
216 this.node.line = this.cur_line;
219 this.addLine(this.pad + "public " + this.cls + " el;");
222 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
234 this.addLine(pad + "public static " + xcls + " singleton()");
235 this.addLine(this.pad + "{")
236 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
237 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
238 this.addLine(this.ipad + "}");
239 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
240 this.addLine(this.pad + "}");
244 void addTopProperties()
246 if (this.depth > 0) {
249 // properties - global..??
251 var iter = this.vitems.list_iterator();
256 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
260 if (n.xvala_id[0] == '*') {
263 if (n.xvala_id[0] == '+') {
266 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
274 this.ret += "\n" + this.ipad + "// my vars (def)\n";
278 var cls = Palete.Gir.factoryFqn(this.node.fqn());
286 var iter = this.node.props.map_iterator();
287 while (iter.next()) {
288 var k = iter.get_key();
289 if (this.shouldIgnore(k)) {
292 var vv = k.strip().split(" ");
293 // user defined method
302 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
306 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
307 if (vv.length < min) {
308 // skip 'old js style properties without a type'
312 var kname = vv[vv.length-1];
314 if (this.shouldIgnore(kname)) {
318 // is it a class property...
319 if (cls.props.has_key(kname) && vv[0] != "#") {
326 this.ret += this.pad + "public " +
327 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
335 // if id of child is '+' then it's a property of this..
336 void addPlusProperties()
338 if (this.node.items.size < 1) {
341 var iter = this.node.items.list_iterator();
342 while (iter.next()) {
345 if (ci.xvala_id[0] != '+') {
346 continue; // skip generation of children?
349 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
364 this.ret += "\n" + this.pad + "// ctor \n";
365 if (this.node.has("* args")) {
366 // not sure what this is supposed to be ding..
368 cargs_str = ", " + this.node.get("* args");
369 //var ar = this.node.get("* args");.split(",");
370 //for (var ari =0; ari < ar.length; ari++) {
371 // cargs += (ar[ari].trim().split(" ").pop();
375 if (this.depth < 1) {
376 this.ret += this.pad + "public " + this.xcls + "(" +
377 cargs_str +")\n" + this.pad + "{\n";
382 this.ret+= this.pad + "public " + this.xcls + "(" +
383 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
392 this.ret += this.ipad + "_this = this;\n";
395 this.ret+= this.ipad + "_this = _owner;\n";
397 if (this.node.props.has_key("id")
399 this.node.xvala_id != ""
401 this.node.xvala_id[0] != '*'
403 this.node.xvala_id[0] != '+'
405 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
413 void addWrappedCtor()
416 // this may need to look up properties to fill in the arguments..
417 // introspection does not workk..... - as things like gtkmessagedialog
419 if (cls == 'Gtk.Table') {
421 var methods = this.palete.getPropertiesFor(cls, 'methods');
423 print(JSON.stringify(this.palete.proplist[cls], null,4));
427 if (this.node.has("* ctor")) {
430 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
433 // the ctor arguments...
436 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
437 //if (default_ctor == null) {
438 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
441 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
443 var iter =default_ctor.paramset.params.list_iterator();
444 while (iter.next()) {
445 var n = iter.get().name;
446 if (!this.node.has(n)) {
448 if (iter.get().type.contains("int")) {
452 if (iter.get().type.contains("float")) {
456 if (iter.get().type.contains("bool")) {
457 args += "true"; // always default to true?
460 // any other types???
465 this.ignoreWrapped(n);
468 var v = this.node.get(n);
470 if (iter.get().type == "string") {
471 v = "\"" + v.escape("") + "\"";
473 if (v == "TRUE" || v == "FALSE") {
481 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
487 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
494 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
495 //print(JSON.stringify(meths,null,4));Seed.quit();
499 // initialize.. my vars..
500 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
502 var iter = this.myvars.list_iterator();
507 var ar = k.strip().split(" ");
508 var kname = ar[ar.length-1];
510 var v = this.node.props.get(k);
515 if (v == "FALSE" || v == "TRUE") {
518 //FIXME -- check for raw string.. "string XXXX"
520 // if it's a string...
522 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
530 void addWrappedProperties()
532 var cls = Palete.Gir.factoryFqn(this.node.fqn());
536 // what are the properties of this class???
537 this.ret += "\n" + this.ipad + "// set gobject values\n";
539 var iter = cls.props.map_iterator();
540 while (iter.next()) {
541 var p = iter.get_key();
542 print("Check Write %s\n", p);
543 if (!this.node.has(p)) {
546 if (this.shouldIgnoreWrapped(p)) {
551 var v = this.node.get(p);
553 var nodekey = this.node.get_key(p);
555 // user defined properties.
556 if (nodekey[0] == '#') {
562 var is_raw = nodekey[0] == '$';
564 // what's the type.. - if it's a string.. then we quote it..
565 if (iter.get_value().type == "string" && !is_raw) {
566 v = "\"" + v.escape("") + "\"";
568 if (v == "TRUE" || v == "FALSE") {
571 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
576 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
588 if (this.node.items.size < 1) {
592 var iter = this.node.items.list_iterator();
594 while (iter.next()) {
599 if (ci.xvala_id[0] == '*') {
600 continue; // skip generation of children?
604 if (ci.has("* args")) {
606 var ar = ci.get("* args").split(",");
607 for (var ari = 0 ; ari < ar.length; ari++ ) {
608 var arg = ar[ari].split(" ");
609 xargs += "," + arg[arg.length -1];
613 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
614 "( _this " + xargs + ");\n" ;
616 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
618 if (ci.has("* prop")) {
619 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
623 // not sure why we have 'true' in pack?!?
624 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
628 string[] packing = { "add" };
629 if (ci.has("pack")) {
630 packing = ci.get("pack").split(",");
633 var pack = packing[0];
634 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
635 (packing.length > 1 ?
636 (", " + string.joinv(",", packing).substring(pack.length+1))
642 if (ci.xvala_id[0] != '+') {
643 continue; // skip generation of children?
646 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
655 if (!this.node.has("init")) {
658 this.ret+= "\n" + ipad + "// init method \n";
660 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
665 if (this.node.listeners.size < 1) {
671 this.ret+= "\n" + ipad + "// listeners \n";
673 var iter = this.node.listeners.map_iterator();
674 while (iter.next()) {
675 var k = iter.get_key();
676 var v = iter.get_value();
677 this.ret+= this.ipad + "this.el." + k + ".connect( " +
678 this.padMultiline(this.ipad,v) +");\n";
688 this.ret+= this.pad + "}\n";
693 * Standardize this crap...
695 * standard properties (use to set)
696 * If they are long values show the dialog..
699 * bool is_xxx :: can show a pulldown.. (true/false)
701 * $ string html = string with value interpolated eg. baseURL + ".."
702 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
703 * $ untypedvalue = javascript untyped value...
704 * _ string html ... = translatable..
707 * object properties (not part of the GOjbect being wrapped?
708 * # Gee.ArrayList<Xcls_fileitem> fileitems
713 * methods -- always text editor..
721 * * init -- big string?
723 * event handlers (listeners)
728 * +XXXX -- indicates it's a instance property / not glob...
729 * *XXXX -- skip writing glob property (used as classes that can be created...)
734 void addUserMethods()
737 this.ret+= "\n" + pad + "// user defined functions \n";
739 // user defined functions...
740 var iter = this.node.props.map_iterator();
742 var k = iter.get_key();
743 if (this.shouldIgnore(k)) {
746 // HOW TO DETERIME if its a method?
748 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
751 // function in the format of {type} (args) { .... }
752 var kk = k.substring(2);
753 var vv = iter.get_value();
754 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
763 if (this.depth > 0) {
764 this.ret+= this.inpad + "}\n";
767 var iter = this.node.items.list_iterator();
769 while (iter.next()) {
770 this.ret += this.mungeChild(iter.get());
773 if (this.depth < 1) {
774 this.ret+= this.inpad + "}\n";
779 string padMultiline(string pad, string str)
781 var ar = str.strip().split("\n");
782 return string.joinv("\n" + pad , ar);
785 void ignore(string i) {
786 this.ignoreList.add(i);
789 void ignoreWrapped(string i) {
790 this.ignoreWrappedList.add(i);
793 bool shouldIgnore(string i)
795 return ignoreList.contains(i);
797 bool shouldIgnoreWrapped(string i)
799 return ignoreWrappedList.contains(i);