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 node.line = this.cur_line;
65 node.proplines = new Gee.HashMap<string,int>();
66 node.listenlines = Gee.HashMap<string,int>();
73 string toValaNS(Node item)
75 var ns = item.get("xns") ;
76 if (ns == "GtkSource") {
81 public void toValaName(Node item, int depth =0)
85 var ns = this.toValaNS(item) ;
86 var cls = ns + item.get("xtype");
92 string id = item.get("id").length > 0 ?
93 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
98 if (id[0] == '*' || id[0] == '+') {
99 item.xvala_xcls = "Xcls_" + id.substring(1);
101 item.xvala_xcls = "Xcls_" + id;
107 this.vitems.add(item);
108 } else if (!item.props.has_key("id")) {
109 // use the file name..
110 item.xvala_xcls = this.file.name;
112 item.xvala_id = this.file.name;
117 if (item.items.size < 1) {
120 for(var i =0;i<item.items.size;i++) {
121 this.toValaName(item.items.get(i), depth+1);
126 * Main entry point to convert a file into a string..
128 public static string mungeFile(JsRender file)
130 if (file.tree == null) {
134 var n = new NodeToVala(file.tree, 0, null);
138 n.toValaName(file.tree);
141 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
142 n.cls = file.tree.xvala_cls;
143 n.xcls = file.tree.xvala_xcls;
149 public string munge ( )
151 //return this.mungeToString(this.node);
156 this.ignore("xtype");
162 this.addTopProperties();
164 this.addPlusProperties();
167 this.addWrappedCtor();
169 this.addInitMyVars();
170 this.addWrappedProperties();
175 this.addUserMethods();
182 public string mungeChild( Node cnode)
184 var x = new NodeToVala(cnode, this.depth+1, this);
187 public void addLine(string str= "")
190 this.ret += str + "\n";
194 public void globalVars()
196 if (this.depth > 0) {
200 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
202 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
204 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
213 // class xxx { WrappedGtk el; }
214 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
216 this.addLine(this.pad + "public " + this.cls + " el;");
219 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
231 this.addLine(pad + "public static " + xcls + " singleton()");
232 this.addLine(this.pad + "{")
233 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
234 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
235 this.addLine(this.ipad + "}");
236 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
237 this.addLine(this.pad + "}");
241 void addTopProperties()
243 if (this.depth > 0) {
246 // properties - global..??
248 var iter = this.vitems.list_iterator();
253 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
257 if (n.xvala_id[0] == '*') {
260 if (n.xvala_id[0] == '+') {
263 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
271 this.ret += "\n" + this.ipad + "// my vars (def)\n";
275 var cls = Palete.Gir.factoryFqn(this.node.fqn());
283 var iter = this.node.props.map_iterator();
284 while (iter.next()) {
285 var k = iter.get_key();
286 if (this.shouldIgnore(k)) {
289 var vv = k.strip().split(" ");
290 // user defined method
299 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
303 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
304 if (vv.length < min) {
305 // skip 'old js style properties without a type'
309 var kname = vv[vv.length-1];
311 if (this.shouldIgnore(kname)) {
315 // is it a class property...
316 if (cls.props.has_key(kname) && vv[0] != "#") {
323 this.ret += this.pad + "public " +
324 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
332 // if id of child is '+' then it's a property of this..
333 void addPlusProperties()
335 if (this.node.items.size < 1) {
338 var iter = this.node.items.list_iterator();
339 while (iter.next()) {
342 if (ci.xvala_id[0] != '+') {
343 continue; // skip generation of children?
346 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
361 this.ret += "\n" + this.pad + "// ctor \n";
362 if (this.node.has("* args")) {
363 // not sure what this is supposed to be ding..
365 cargs_str = ", " + this.node.get("* args");
366 //var ar = this.node.get("* args");.split(",");
367 //for (var ari =0; ari < ar.length; ari++) {
368 // cargs += (ar[ari].trim().split(" ").pop();
372 if (this.depth < 1) {
373 this.ret += this.pad + "public " + this.xcls + "(" +
374 cargs_str +")\n" + this.pad + "{\n";
379 this.ret+= this.pad + "public " + this.xcls + "(" +
380 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
389 this.ret += this.ipad + "_this = this;\n";
392 this.ret+= this.ipad + "_this = _owner;\n";
394 if (this.node.props.has_key("id")
396 this.node.xvala_id != ""
398 this.node.xvala_id[0] != '*'
400 this.node.xvala_id[0] != '+'
402 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
410 void addWrappedCtor()
413 // this may need to look up properties to fill in the arguments..
414 // introspection does not workk..... - as things like gtkmessagedialog
416 if (cls == 'Gtk.Table') {
418 var methods = this.palete.getPropertiesFor(cls, 'methods');
420 print(JSON.stringify(this.palete.proplist[cls], null,4));
424 if (this.node.has("* ctor")) {
427 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
430 // the ctor arguments...
433 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
434 //if (default_ctor == null) {
435 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
438 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
440 var iter =default_ctor.paramset.params.list_iterator();
441 while (iter.next()) {
442 var n = iter.get().name;
443 if (!this.node.has(n)) {
445 if (iter.get().type.contains("int")) {
449 if (iter.get().type.contains("float")) {
453 if (iter.get().type.contains("bool")) {
454 args += "true"; // always default to true?
457 // any other types???
462 this.ignoreWrapped(n);
465 var v = this.node.get(n);
467 if (iter.get().type == "string") {
468 v = "\"" + v.escape("") + "\"";
470 if (v == "TRUE" || v == "FALSE") {
478 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
484 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
491 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
492 //print(JSON.stringify(meths,null,4));Seed.quit();
496 // initialize.. my vars..
497 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
499 var iter = this.myvars.list_iterator();
504 var ar = k.strip().split(" ");
505 var kname = ar[ar.length-1];
507 var v = this.node.props.get(k);
512 if (v == "FALSE" || v == "TRUE") {
515 //FIXME -- check for raw string.. "string XXXX"
517 // if it's a string...
519 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
527 void addWrappedProperties()
529 var cls = Palete.Gir.factoryFqn(this.node.fqn());
533 // what are the properties of this class???
534 this.ret += "\n" + this.ipad + "// set gobject values\n";
536 var iter = cls.props.map_iterator();
537 while (iter.next()) {
538 var p = iter.get_key();
539 print("Check Write %s\n", p);
540 if (!this.node.has(p)) {
543 if (this.shouldIgnoreWrapped(p)) {
548 var v = this.node.get(p);
550 var nodekey = this.node.get_key(p);
552 // user defined properties.
553 if (nodekey[0] == '#') {
559 var is_raw = nodekey[0] == '$';
561 // what's the type.. - if it's a string.. then we quote it..
562 if (iter.get_value().type == "string" && !is_raw) {
563 v = "\"" + v.escape("") + "\"";
565 if (v == "TRUE" || v == "FALSE") {
568 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
573 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
585 if (this.node.items.size < 1) {
589 var iter = this.node.items.list_iterator();
591 while (iter.next()) {
596 if (ci.xvala_id[0] == '*') {
597 continue; // skip generation of children?
601 if (ci.has("* args")) {
603 var ar = ci.get("* args").split(",");
604 for (var ari = 0 ; ari < ar.length; ari++ ) {
605 var arg = ar[ari].split(" ");
606 xargs += "," + arg[arg.length -1];
610 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
611 "( _this " + xargs + ");\n" ;
613 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
615 if (ci.has("* prop")) {
616 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
620 // not sure why we have 'true' in pack?!?
621 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
625 string[] packing = { "add" };
626 if (ci.has("pack")) {
627 packing = ci.get("pack").split(",");
630 var pack = packing[0];
631 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
632 (packing.length > 1 ?
633 (", " + string.joinv(",", packing).substring(pack.length+1))
639 if (ci.xvala_id[0] != '+') {
640 continue; // skip generation of children?
643 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
652 if (!this.node.has("init")) {
655 this.ret+= "\n" + ipad + "// init method \n";
657 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
662 if (this.node.listeners.size < 1) {
668 this.ret+= "\n" + ipad + "// listeners \n";
670 var iter = this.node.listeners.map_iterator();
671 while (iter.next()) {
672 var k = iter.get_key();
673 var v = iter.get_value();
674 this.ret+= this.ipad + "this.el." + k + ".connect( " +
675 this.padMultiline(this.ipad,v) +");\n";
685 this.ret+= this.pad + "}\n";
690 * Standardize this crap...
692 * standard properties (use to set)
693 * If they are long values show the dialog..
696 * bool is_xxx :: can show a pulldown.. (true/false)
698 * $ string html = string with value interpolated eg. baseURL + ".."
699 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
700 * $ untypedvalue = javascript untyped value...
701 * _ string html ... = translatable..
704 * object properties (not part of the GOjbect being wrapped?
705 * # Gee.ArrayList<Xcls_fileitem> fileitems
710 * methods -- always text editor..
718 * * init -- big string?
720 * event handlers (listeners)
725 * +XXXX -- indicates it's a instance property / not glob...
726 * *XXXX -- skip writing glob property (used as classes that can be created...)
731 void addUserMethods()
734 this.ret+= "\n" + pad + "// user defined functions \n";
736 // user defined functions...
737 var iter = this.node.props.map_iterator();
739 var k = iter.get_key();
740 if (this.shouldIgnore(k)) {
743 // HOW TO DETERIME if its a method?
745 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
748 // function in the format of {type} (args) { .... }
749 var kk = k.substring(2);
750 var vv = iter.get_value();
751 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
760 if (this.depth > 0) {
761 this.ret+= this.inpad + "}\n";
764 var iter = this.node.items.list_iterator();
766 while (iter.next()) {
767 this.ret += this.mungeChild(iter.get());
770 if (this.depth < 1) {
771 this.ret+= this.inpad + "}\n";
776 string padMultiline(string pad, string str)
778 var ar = str.strip().split("\n");
779 return string.joinv("\n" + pad , ar);
782 void ignore(string i) {
783 this.ignoreList.add(i);
786 void ignoreWrapped(string i) {
787 this.ignoreWrappedList.add(i);
790 bool shouldIgnore(string i)
792 return ignoreList.contains(i);
794 bool shouldIgnoreWrapped(string i)
796 return ignoreWrappedList.contains(i);