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);
180 public void addLine(string str= "")
183 this.ret += str + "\n";
187 public void globalVars()
189 if (this.depth > 0) {
193 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
195 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
197 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
206 // class xxx { WrappedGtk el; }
207 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
209 this.addLine(this.pad + "public " + this.cls + " el;");
212 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
224 this.addLine(pad + "public static " + xcls + " singleton()");
225 this.addLine(this.pad + "{")
226 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
227 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
228 this.addLine(this.ipad + "}");
229 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
230 this.addLine(this.pad + "}");
234 void addTopProperties()
236 if (this.depth > 0) {
239 // properties - global..??
241 var iter = this.vitems.list_iterator();
246 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
250 if (n.xvala_id[0] == '*') {
253 if (n.xvala_id[0] == '+') {
256 this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
263 this.ret += "\n" + this.ipad + "// my vars (def)\n";
267 var cls = Palete.Gir.factoryFqn(this.node.fqn());
275 var iter = this.node.props.map_iterator();
276 while (iter.next()) {
277 var k = iter.get_key();
278 if (this.shouldIgnore(k)) {
281 var vv = k.strip().split(" ");
282 // user defined method
291 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
295 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
296 if (vv.length < min) {
297 // skip 'old js style properties without a type'
301 var kname = vv[vv.length-1];
303 if (this.shouldIgnore(kname)) {
307 // is it a class property...
308 if (cls.props.has_key(kname) && vv[0] != "#") {
315 this.ret += this.pad + "public " +
316 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
324 // if id of child is '+' then it's a property of this..
325 void addPlusProperties()
327 if (this.node.items.size < 1) {
330 var iter = this.node.items.list_iterator();
331 while (iter.next()) {
334 if (ci.xvala_id[0] != '+') {
335 continue; // skip generation of children?
338 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
353 this.ret += "\n" + this.pad + "// ctor \n";
354 if (this.node.has("* args")) {
355 // not sure what this is supposed to be ding..
357 cargs_str = ", " + this.node.get("* args");
358 //var ar = this.node.get("* args");.split(",");
359 //for (var ari =0; ari < ar.length; ari++) {
360 // cargs += (ar[ari].trim().split(" ").pop();
364 if (this.depth < 1) {
365 this.ret += this.pad + "public " + this.xcls + "(" +
366 cargs_str +")\n" + this.pad + "{\n";
371 this.ret+= this.pad + "public " + this.xcls + "(" +
372 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
381 this.ret += this.ipad + "_this = this;\n";
384 this.ret+= this.ipad + "_this = _owner;\n";
386 if (this.node.props.has_key("id")
388 this.node.xvala_id != ""
390 this.node.xvala_id[0] != '*'
392 this.node.xvala_id[0] != '+'
394 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
402 void addWrappedCtor()
405 // this may need to look up properties to fill in the arguments..
406 // introspection does not workk..... - as things like gtkmessagedialog
408 if (cls == 'Gtk.Table') {
410 var methods = this.palete.getPropertiesFor(cls, 'methods');
412 print(JSON.stringify(this.palete.proplist[cls], null,4));
416 if (this.node.has("* ctor")) {
419 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
422 // the ctor arguments...
425 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
426 //if (default_ctor == null) {
427 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
430 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
432 var iter =default_ctor.paramset.params.list_iterator();
433 while (iter.next()) {
434 var n = iter.get().name;
435 if (!this.node.has(n)) {
437 if (iter.get().type.contains("int")) {
441 if (iter.get().type.contains("float")) {
445 if (iter.get().type.contains("bool")) {
446 args += "true"; // always default to true?
449 // any other types???
454 this.ignoreWrapped(n);
457 var v = this.node.get(n);
459 if (iter.get().type == "string") {
460 v = "\"" + v.escape("") + "\"";
462 if (v == "TRUE" || v == "FALSE") {
470 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
476 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
483 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
484 //print(JSON.stringify(meths,null,4));Seed.quit();
488 // initialize.. my vars..
489 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
491 var iter = this.myvars.list_iterator();
496 var ar = k.strip().split(" ");
497 var kname = ar[ar.length-1];
499 var v = this.node.props.get(k);
504 if (v == "FALSE" || v == "TRUE") {
507 //FIXME -- check for raw string.. "string XXXX"
509 // if it's a string...
511 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
519 void addWrappedProperties()
521 var cls = Palete.Gir.factoryFqn(this.node.fqn());
525 // what are the properties of this class???
526 this.ret += "\n" + this.ipad + "// set gobject values\n";
528 var iter = cls.props.map_iterator();
529 while (iter.next()) {
530 var p = iter.get_key();
531 print("Check Write %s\n", p);
532 if (!this.node.has(p)) {
535 if (this.shouldIgnoreWrapped(p)) {
540 var v = this.node.get(p);
542 var nodekey = this.node.get_key(p);
544 // user defined properties.
545 if (nodekey[0] == '#') {
551 var is_raw = nodekey[0] == '$';
553 // what's the type.. - if it's a string.. then we quote it..
554 if (iter.get_value().type == "string" && !is_raw) {
555 v = "\"" + v.escape("") + "\"";
557 if (v == "TRUE" || v == "FALSE") {
560 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
565 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
577 if (this.node.items.size < 1) {
581 var iter = this.node.items.list_iterator();
583 while (iter.next()) {
588 if (ci.xvala_id[0] == '*') {
589 continue; // skip generation of children?
593 if (ci.has("* args")) {
595 var ar = ci.get("* args").split(",");
596 for (var ari = 0 ; ari < ar.length; ari++ ) {
597 var arg = ar[ari].split(" ");
598 xargs += "," + arg[arg.length -1];
602 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
603 "( _this " + xargs + ");\n" ;
605 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
607 if (ci.has("* prop")) {
608 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
612 // not sure why we have 'true' in pack?!?
613 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
617 string[] packing = { "add" };
618 if (ci.has("pack")) {
619 packing = ci.get("pack").split(",");
622 var pack = packing[0];
623 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
624 (packing.length > 1 ?
625 (", " + string.joinv(",", packing).substring(pack.length+1))
631 if (ci.xvala_id[0] != '+') {
632 continue; // skip generation of children?
635 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
644 if (!this.node.has("init")) {
647 this.ret+= "\n" + ipad + "// init method \n";
649 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
654 if (this.node.listeners.size < 1) {
660 this.ret+= "\n" + ipad + "// listeners \n";
662 var iter = this.node.listeners.map_iterator();
663 while (iter.next()) {
664 var k = iter.get_key();
665 var v = iter.get_value();
666 this.ret+= this.ipad + "this.el." + k + ".connect( " +
667 this.padMultiline(this.ipad,v) +");\n";
677 this.ret+= this.pad + "}\n";
682 * Standardize this crap...
684 * standard properties (use to set)
685 * If they are long values show the dialog..
688 * bool is_xxx :: can show a pulldown.. (true/false)
690 * $ string html = string with value interpolated eg. baseURL + ".."
691 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
692 * $ untypedvalue = javascript untyped value...
693 * _ string html ... = translatable..
696 * object properties (not part of the GOjbect being wrapped?
697 * # Gee.ArrayList<Xcls_fileitem> fileitems
702 * methods -- always text editor..
710 * * init -- big string?
712 * event handlers (listeners)
717 * +XXXX -- indicates it's a instance property / not glob...
718 * *XXXX -- skip writing glob property (used as classes that can be created...)
723 void addUserMethods()
726 this.ret+= "\n" + pad + "// user defined functions \n";
728 // user defined functions...
729 var iter = this.node.props.map_iterator();
731 var k = iter.get_key();
732 if (this.shouldIgnore(k)) {
735 // HOW TO DETERIME if its a method?
737 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
740 // function in the format of {type} (args) { .... }
741 var kk = k.substring(2);
742 var vv = iter.get_value();
743 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
752 if (this.depth > 0) {
753 this.ret+= this.inpad + "}\n";
756 var iter = this.node.items.list_iterator();
758 while (iter.next()) {
759 this.ret += this.mungeChild(iter.get());
762 if (this.depth < 1) {
763 this.ret+= this.inpad + "}\n";
768 string padMultiline(string pad, string str)
770 var ar = str.strip().split("\n");
771 return string.joinv("\n" + pad , ar);
774 void ignore(string i) {
775 this.ignoreList.add(i);
778 void ignoreWrapped(string i) {
779 this.ignoreWrappedList.add(i);
782 bool shouldIgnore(string i)
784 return ignoreList.contains(i);
786 bool shouldIgnoreWrapped(string i)
788 return ignoreWrappedList.contains(i);