3 * Code to convert node tree to Javascript...
5 * usage : x = (new JsRender.NodeToJs(node)).munge();
16 public class JsRender.NodeToVala : Object {
30 Gee.ArrayList<string> ignoreList;
31 Gee.ArrayList<string> ignoreWrappedList;
32 Gee.ArrayList<string> myvars;
33 Gee.ArrayList<Node> vitems; // top level items
38 * ctor - just initializes things
39 * - wraps a render node
41 public NodeToVala( Node node, int depth, NodeToVala? top)
47 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
48 this.pad = this.inpad + " ";
49 this.ipad = this.inpad + " ";
50 this.cls = node.xvala_cls;
51 this.xcls = node.xvala_xcls;
54 this.top = top == null ? this : top;
55 this.ignoreList = new Gee.ArrayList<string>();
56 this.ignoreWrappedList = new Gee.ArrayList<string>();
57 this.myvars = new Gee.ArrayList<string>();
58 this.vitems = new Gee.ArrayList<Node>();
63 string toValaNS(Node item)
65 var ns = item.get("xns") ;
66 if (ns == "GtkSource") {
71 public void toValaName(Node item, int depth =0)
75 var ns = this.toValaNS(item) ;
76 var cls = ns + item.get("xtype");
82 string id = item.get("id").length > 0 ?
83 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
88 if (id[0] == '*' || id[0] == '+') {
89 item.xvala_xcls = "Xcls_" + id.substring(1);
91 item.xvala_xcls = "Xcls_" + id;
97 this.vitems.add(item);
98 } else if (!item.props.has_key("id")) {
99 // use the file name..
100 item.xvala_xcls = this.file.name;
102 item.xvala_id = this.file.name;
107 if (item.items.size < 1) {
110 for(var i =0;i<item.items.size;i++) {
111 this.toValaName(item.items.get(i), depth+1);
116 * Main entry point to convert a file into a string..
118 public static string mungeFile(JsRender file)
120 if (file.tree == null) {
124 var n = new NodeToVala(file.tree, 0, null);
128 n.toValaName(file.tree);
131 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
132 n.cls = file.tree.xvala_cls;
133 n.xcls = file.tree.xvala_xcls;
139 public string munge ( )
141 //return this.mungeToString(this.node);
146 this.ignore("xtype");
152 this.addTopProperties();
154 this.addPlusProperties();
157 this.addWrappedCtor();
159 this.addInitMyVars();
160 this.addWrappedProperties();
165 this.addUserMethods();
172 public string mungeChild( Node cnode)
174 var x = new NodeToVala(cnode, this.depth+1, this.top);
178 public void globalVars()
180 if (this.depth > 0) {
184 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
187 this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
196 // class xxx { WrappedGtk el; }
197 this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
198 this.ret += this.pad + "public " + this.cls + " el;\n";
201 this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n";
212 this.ret += pad + "public static " + xcls + " singleton()\n" +
214 this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" +
215 this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
217 this.ipad + "return _" + this.node.xvala_id +";\n" +
222 void addTopProperties()
224 if (this.depth > 0) {
227 // properties - global..??
229 var iter = this.vitems.list_iterator();
234 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
238 if (n.xvala_id[0] == '*') {
241 if (n.xvala_id[0] == '+') {
244 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
251 this.ret += "\n" + this.ipad + "// my vars (def)\n";
255 var cls = Palete.Gir.factoryFqn(this.node.fqn());
263 var iter = this.node.props.map_iterator();
264 while (iter.next()) {
265 var k = iter.get_key();
266 if (this.shouldIgnore(k)) {
269 var vv = k.strip().split(" ");
270 // user defined method
279 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
283 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
284 if (vv.length < min) {
285 // skip 'old js style properties without a type'
289 var kname = vv[vv.length-1];
291 if (this.shouldIgnore(kname)) {
295 // is it a class property...
296 if (cls.props.has_key(kname) && vv[0] != "#") {
303 this.ret += this.pad + "public " +
304 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
312 // if id of child is '+' then it's a property of this..
313 void addPlusProperties()
315 if (this.node.items.size < 1) {
318 var iter = this.node.items.list_iterator();
319 while (iter.next()) {
322 if (ci.xvala_id[0] != '+') {
323 continue; // skip generation of children?
326 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
341 this.ret += "\n" + this.pad + "// ctor \n";
342 if (this.node.has("* args")) {
343 // not sure what this is supposed to be ding..
345 cargs_str = ", " + this.node.get("* args");
346 //var ar = this.node.get("* args");.split(",");
347 //for (var ari =0; ari < ar.length; ari++) {
348 // cargs += (ar[ari].trim().split(" ").pop();
352 if (this.depth < 1) {
353 this.ret += this.pad + "public " + this.xcls + "(" +
354 cargs_str +")\n" + this.pad + "{\n";
359 this.ret+= this.pad + "public " + this.xcls + "(" +
360 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
369 this.ret += this.ipad + "_this = this;\n";
372 this.ret+= this.ipad + "_this = _owner;\n";
374 if (this.node.props.has_key("id")
376 this.node.xvala_id != ""
378 this.node.xvala_id[0] != '*'
380 this.node.xvala_id[0] != '+'
382 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
390 void addWrappedCtor()
393 // this may need to look up properties to fill in the arguments..
394 // introspection does not workk..... - as things like gtkmessagedialog
396 if (cls == 'Gtk.Table') {
398 var methods = this.palete.getPropertiesFor(cls, 'methods');
400 print(JSON.stringify(this.palete.proplist[cls], null,4));
404 if (this.node.has("* ctor")) {
407 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
410 // the ctor arguments...
413 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
414 //if (default_ctor == null) {
415 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
418 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
420 var iter =default_ctor.paramset.params.list_iterator();
421 while (iter.next()) {
422 var n = iter.get().name;
423 if (!this.node.has(n)) {
425 if (iter.get().type.contains("int")) {
429 if (iter.get().type.contains("float")) {
433 if (iter.get().type.contains("bool")) {
434 args += "true"; // always default to true?
437 // any other types???
442 this.ignoreWrapped(n);
445 var v = this.node.get(n);
447 if (iter.get().type == "string") {
448 v = "\"" + v.escape("") + "\"";
450 if (v == "TRUE" || v == "FALSE") {
458 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
464 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
471 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
472 //print(JSON.stringify(meths,null,4));Seed.quit();
476 // initialize.. my vars..
477 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
479 var iter = this.myvars.list_iterator();
484 var ar = k.strip().split(" ");
485 var kname = ar[ar.length-1];
487 var v = this.node.props.get(k);
492 if (v == "FALSE" || v == "TRUE") {
495 //FIXME -- check for raw string.. "string XXXX"
497 // if it's a string...
499 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
507 void addWrappedProperties()
509 var cls = Palete.Gir.factoryFqn(this.node.fqn());
513 // what are the properties of this class???
514 this.ret += "\n" + this.ipad + "// set gobject values\n";
516 var iter = cls.props.map_iterator();
517 while (iter.next()) {
518 var p = iter.get_key();
519 print("Check Write %s\n", p);
520 if (!this.node.has(p)) {
523 if (this.shouldIgnoreWrapped(p)) {
528 var v = this.node.get(p);
530 var nodekey = this.node.get_key(p);
532 // user defined properties.
533 if (nodekey[0] == '#') {
539 var is_raw = nodekey[0] == '$';
541 // what's the type.. - if it's a string.. then we quote it..
542 if (iter.get_value().type == "string" && !is_raw) {
543 v = "\"" + v.escape("") + "\"";
545 if (v == "TRUE" || v == "FALSE") {
548 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
553 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
565 if (this.node.items.size < 1) {
569 var iter = this.node.items.list_iterator();
571 while (iter.next()) {
576 if (ci.xvala_id[0] == '*') {
577 continue; // skip generation of children?
581 if (ci.has("* args")) {
583 var ar = ci.get("* args").split(",");
584 for (var ari = 0 ; ari < ar.length; ari++ ) {
585 var arg = ar[ari].split(" ");
586 xargs += "," + arg[arg.length -1];
590 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
591 "( _this " + xargs + ");\n" ;
593 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
595 if (ci.has("* prop")) {
596 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
600 // not sure why we have 'true' in pack?!?
601 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
605 string[] packing = { "add" };
606 if (ci.has("pack")) {
607 packing = ci.get("pack").split(",");
610 var pack = packing[0];
611 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
612 (packing.length > 1 ?
613 (", " + string.joinv(",", packing).substring(pack.length+1))
619 if (ci.xvala_id[0] != '+') {
620 continue; // skip generation of children?
623 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
632 if (!this.node.has("init")) {
635 this.ret+= "\n" + ipad + "// init method \n";
637 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
642 if (this.node.listeners.size < 1) {
648 this.ret+= "\n" + ipad + "// listeners \n";
650 var iter = this.node.listeners.map_iterator();
651 while (iter.next()) {
652 var k = iter.get_key();
653 var v = iter.get_value();
654 this.ret+= this.ipad + "this.el." + k + ".connect( " +
655 this.padMultiline(this.ipad,v) +");\n";
665 this.ret+= this.pad + "}\n";
670 * Standardize this crap...
672 * standard properties (use to set)
673 * If they are long values show the dialog..
676 * bool is_xxx :: can show a pulldown.. (true/false)
678 * $ string html = string with value interpolated eg. baseURL + ".."
679 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
680 * $ untypedvalue = javascript untyped value...
681 * _ string html ... = translatable..
684 * object properties (not part of the GOjbect being wrapped?
685 * # Gee.ArrayList<Xcls_fileitem> fileitems
690 * methods -- always text editor..
698 * * init -- big string?
700 * event handlers (listeners)
705 * +XXXX -- indicates it's a instance property / not glob...
706 * *XXXX -- skip writing glob property (used as classes that can be created...)
711 void addUserMethods()
714 this.ret+= "\n" + pad + "// user defined functions \n";
716 // user defined functions...
717 var iter = this.node.props.map_iterator();
719 var k = iter.get_key();
720 if (this.shouldIgnore(k)) {
723 // HOW TO DETERIME if its a method?
725 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
728 // function in the format of {type} (args) { .... }
729 var kk = k.substring(2);
730 var vv = iter.get_value();
731 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
740 if (this.depth > 0) {
741 this.ret+= this.inpad + "}\n";
744 var iter = this.node.items.list_iterator();
746 while (iter.next()) {
747 this.ret += this.mungeChild(iter.get());
750 if (this.depth < 1) {
751 this.ret+= this.inpad + "}\n";
756 string padMultiline(string pad, string str)
758 var ar = str.strip().split("\n");
759 return string.joinv("\n" + pad , ar);
762 void ignore(string i) {
763 this.ignoreList.add(i);
766 void ignoreWrapped(string i) {
767 this.ignoreWrappedList.add(i);
770 bool shouldIgnore(string i)
772 return ignoreList.contains(i);
774 bool shouldIgnoreWrapped(string i)
776 return ignoreWrappedList.contains(i);