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 node.line = parent == null ? 0 : .line;
55 this.top = parent == null ? this : parent.top;
56 this.ignoreList = new Gee.ArrayList<string>();
57 this.ignoreWrappedList = new Gee.ArrayList<string>();
58 this.myvars = new Gee.ArrayList<string>();
59 this.vitems = new Gee.ArrayList<Node>();
64 string toValaNS(Node item)
66 var ns = item.get("xns") ;
67 if (ns == "GtkSource") {
72 public void toValaName(Node item, int depth =0)
76 var ns = this.toValaNS(item) ;
77 var cls = ns + item.get("xtype");
83 string id = item.get("id").length > 0 ?
84 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
89 if (id[0] == '*' || id[0] == '+') {
90 item.xvala_xcls = "Xcls_" + id.substring(1);
92 item.xvala_xcls = "Xcls_" + id;
98 this.vitems.add(item);
99 } else if (!item.props.has_key("id")) {
100 // use the file name..
101 item.xvala_xcls = this.file.name;
103 item.xvala_id = this.file.name;
108 if (item.items.size < 1) {
111 for(var i =0;i<item.items.size;i++) {
112 this.toValaName(item.items.get(i), depth+1);
117 * Main entry point to convert a file into a string..
119 public static string mungeFile(JsRender file)
121 if (file.tree == null) {
125 var n = new NodeToVala(file.tree, 0, null);
129 n.toValaName(file.tree);
132 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
133 n.cls = file.tree.xvala_cls;
134 n.xcls = file.tree.xvala_xcls;
140 public string munge ( )
142 //return this.mungeToString(this.node);
147 this.ignore("xtype");
153 this.addTopProperties();
155 this.addPlusProperties();
158 this.addWrappedCtor();
160 this.addInitMyVars();
161 this.addWrappedProperties();
166 this.addUserMethods();
173 public string mungeChild( Node cnode)
175 var x = new NodeToVala(cnode, this.depth+1, this);
179 public void globalVars()
181 if (this.depth > 0) {
185 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
188 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
197 // class xxx { WrappedGtk el; }
198 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
199 this.ret += this.pad + "public " + this.cls + " el;\n";
202 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
213 this.ret += pad + "public static " + xcls + " singleton()\n" +
215 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
216 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
218 this.ipad + "return _" + this.node.xvala_id +";\n" +
223 void addTopProperties()
225 if (this.depth > 0) {
228 // properties - global..??
230 var iter = this.vitems.list_iterator();
235 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
239 if (n.xvala_id[0] == '*') {
242 if (n.xvala_id[0] == '+') {
245 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
252 this.ret += "\n" + this.ipad + "// my vars (def)\n";
256 var cls = Palete.Gir.factoryFqn(this.node.fqn());
264 var iter = this.node.props.map_iterator();
265 while (iter.next()) {
266 var k = iter.get_key();
267 if (this.shouldIgnore(k)) {
270 var vv = k.strip().split(" ");
271 // user defined method
280 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
284 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
285 if (vv.length < min) {
286 // skip 'old js style properties without a type'
290 var kname = vv[vv.length-1];
292 if (this.shouldIgnore(kname)) {
296 // is it a class property...
297 if (cls.props.has_key(kname) && vv[0] != "#") {
304 this.ret += this.pad + "public " +
305 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
313 // if id of child is '+' then it's a property of this..
314 void addPlusProperties()
316 if (this.node.items.size < 1) {
319 var iter = this.node.items.list_iterator();
320 while (iter.next()) {
323 if (ci.xvala_id[0] != '+') {
324 continue; // skip generation of children?
327 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
342 this.ret += "\n" + this.pad + "// ctor \n";
343 if (this.node.has("* args")) {
344 // not sure what this is supposed to be ding..
346 cargs_str = ", " + this.node.get("* args");
347 //var ar = this.node.get("* args");.split(",");
348 //for (var ari =0; ari < ar.length; ari++) {
349 // cargs += (ar[ari].trim().split(" ").pop();
353 if (this.depth < 1) {
354 this.ret += this.pad + "public " + this.xcls + "(" +
355 cargs_str +")\n" + this.pad + "{\n";
360 this.ret+= this.pad + "public " + this.xcls + "(" +
361 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
370 this.ret += this.ipad + "_this = this;\n";
373 this.ret+= this.ipad + "_this = _owner;\n";
375 if (this.node.props.has_key("id")
377 this.node.xvala_id != ""
379 this.node.xvala_id[0] != '*'
381 this.node.xvala_id[0] != '+'
383 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
391 void addWrappedCtor()
394 // this may need to look up properties to fill in the arguments..
395 // introspection does not workk..... - as things like gtkmessagedialog
397 if (cls == 'Gtk.Table') {
399 var methods = this.palete.getPropertiesFor(cls, 'methods');
401 print(JSON.stringify(this.palete.proplist[cls], null,4));
405 if (this.node.has("* ctor")) {
408 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
411 // the ctor arguments...
414 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
415 //if (default_ctor == null) {
416 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
419 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
421 var iter =default_ctor.paramset.params.list_iterator();
422 while (iter.next()) {
423 var n = iter.get().name;
424 if (!this.node.has(n)) {
426 if (iter.get().type.contains("int")) {
430 if (iter.get().type.contains("float")) {
434 if (iter.get().type.contains("bool")) {
435 args += "true"; // always default to true?
438 // any other types???
443 this.ignoreWrapped(n);
446 var v = this.node.get(n);
448 if (iter.get().type == "string") {
449 v = "\"" + v.escape("") + "\"";
451 if (v == "TRUE" || v == "FALSE") {
459 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
465 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
472 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
473 //print(JSON.stringify(meths,null,4));Seed.quit();
477 // initialize.. my vars..
478 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
480 var iter = this.myvars.list_iterator();
485 var ar = k.strip().split(" ");
486 var kname = ar[ar.length-1];
488 var v = this.node.props.get(k);
493 if (v == "FALSE" || v == "TRUE") {
496 //FIXME -- check for raw string.. "string XXXX"
498 // if it's a string...
500 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
508 void addWrappedProperties()
510 var cls = Palete.Gir.factoryFqn(this.node.fqn());
514 // what are the properties of this class???
515 this.ret += "\n" + this.ipad + "// set gobject values\n";
517 var iter = cls.props.map_iterator();
518 while (iter.next()) {
519 var p = iter.get_key();
520 print("Check Write %s\n", p);
521 if (!this.node.has(p)) {
524 if (this.shouldIgnoreWrapped(p)) {
529 var v = this.node.get(p);
531 var nodekey = this.node.get_key(p);
533 // user defined properties.
534 if (nodekey[0] == '#') {
540 var is_raw = nodekey[0] == '$';
542 // what's the type.. - if it's a string.. then we quote it..
543 if (iter.get_value().type == "string" && !is_raw) {
544 v = "\"" + v.escape("") + "\"";
546 if (v == "TRUE" || v == "FALSE") {
549 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
554 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
566 if (this.node.items.size < 1) {
570 var iter = this.node.items.list_iterator();
572 while (iter.next()) {
577 if (ci.xvala_id[0] == '*') {
578 continue; // skip generation of children?
582 if (ci.has("* args")) {
584 var ar = ci.get("* args").split(",");
585 for (var ari = 0 ; ari < ar.length; ari++ ) {
586 var arg = ar[ari].split(" ");
587 xargs += "," + arg[arg.length -1];
591 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
592 "( _this " + xargs + ");\n" ;
594 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
596 if (ci.has("* prop")) {
597 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
601 // not sure why we have 'true' in pack?!?
602 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
606 string[] packing = { "add" };
607 if (ci.has("pack")) {
608 packing = ci.get("pack").split(",");
611 var pack = packing[0];
612 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
613 (packing.length > 1 ?
614 (", " + string.joinv(",", packing).substring(pack.length+1))
620 if (ci.xvala_id[0] != '+') {
621 continue; // skip generation of children?
624 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
633 if (!this.node.has("init")) {
636 this.ret+= "\n" + ipad + "// init method \n";
638 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
643 if (this.node.listeners.size < 1) {
649 this.ret+= "\n" + ipad + "// listeners \n";
651 var iter = this.node.listeners.map_iterator();
652 while (iter.next()) {
653 var k = iter.get_key();
654 var v = iter.get_value();
655 this.ret+= this.ipad + "this.el." + k + ".connect( " +
656 this.padMultiline(this.ipad,v) +");\n";
666 this.ret+= this.pad + "}\n";
671 * Standardize this crap...
673 * standard properties (use to set)
674 * If they are long values show the dialog..
677 * bool is_xxx :: can show a pulldown.. (true/false)
679 * $ string html = string with value interpolated eg. baseURL + ".."
680 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
681 * $ untypedvalue = javascript untyped value...
682 * _ string html ... = translatable..
685 * object properties (not part of the GOjbect being wrapped?
686 * # Gee.ArrayList<Xcls_fileitem> fileitems
691 * methods -- always text editor..
699 * * init -- big string?
701 * event handlers (listeners)
706 * +XXXX -- indicates it's a instance property / not glob...
707 * *XXXX -- skip writing glob property (used as classes that can be created...)
712 void addUserMethods()
715 this.ret+= "\n" + pad + "// user defined functions \n";
717 // user defined functions...
718 var iter = this.node.props.map_iterator();
720 var k = iter.get_key();
721 if (this.shouldIgnore(k)) {
724 // HOW TO DETERIME if its a method?
726 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
729 // function in the format of {type} (args) { .... }
730 var kk = k.substring(2);
731 var vv = iter.get_value();
732 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
741 if (this.depth > 0) {
742 this.ret+= this.inpad + "}\n";
745 var iter = this.node.items.list_iterator();
747 while (iter.next()) {
748 this.ret += this.mungeChild(iter.get());
751 if (this.depth < 1) {
752 this.ret+= this.inpad + "}\n";
757 string padMultiline(string pad, string str)
759 var ar = str.strip().split("\n");
760 return string.joinv("\n" + pad , ar);
763 void ignore(string i) {
764 this.ignoreList.add(i);
767 void ignoreWrapped(string i) {
768 this.ignoreWrappedList.add(i);
771 bool shouldIgnore(string i)
773 return ignoreList.contains(i);
775 bool shouldIgnoreWrapped(string i)
777 return ignoreWrappedList.contains(i);