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;
218 this.addLine(this.pad + "public " + this.cls + " el;");
220 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
232 this.addLine(pad + "public static " + xcls + " singleton()");
233 this.addLine(this.pad + "{")
234 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
235 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
236 this.addLine(this.ipad + "}");
237 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
238 this.addLine(this.pad + "}");
242 * when ID is used... on an element, it registeres a property on the top level...
243 * so that _this.ID always works..
246 void addTopProperties()
248 if (this.depth > 0) {
251 // properties - global..??
253 var iter = this.vitems.list_iterator();
258 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
262 if (n.xvala_id[0] == '*') {
265 if (n.xvala_id[0] == '+') {
268 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
274 * create properties that are not 'part of the wrapped element.
282 this.addLine(this.ipad + "// my vars (def)");
286 var cls = Palete.Gir.factoryFqn(this.node.fqn());
294 var iter = this.node.props.map_iterator();
295 while (iter.next()) {
296 var k = iter.get_key();
297 if (this.shouldIgnore(k)) {
300 var vv = k.strip().split(" ");
301 // user defined method
310 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
315 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
316 if (vv.length < min) {
317 // skip 'old js style properties without a type'
321 var kname = vv[vv.length-1];
323 if (this.shouldIgnore(kname)) {
327 // is it a class property...
328 if (cls.props.has_key(kname) && vv[0] != "#") {
335 this.ret += this.pad + "public " +
336 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
344 // if id of child is '+' then it's a property of this..
345 void addPlusProperties()
347 if (this.node.items.size < 1) {
350 var iter = this.node.items.list_iterator();
351 while (iter.next()) {
354 if (ci.xvala_id[0] != '+') {
355 continue; // skip generation of children?
358 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
373 this.ret += "\n" + this.pad + "// ctor \n";
374 if (this.node.has("* args")) {
375 // not sure what this is supposed to be ding..
377 cargs_str = ", " + this.node.get("* args");
378 //var ar = this.node.get("* args");.split(",");
379 //for (var ari =0; ari < ar.length; ari++) {
380 // cargs += (ar[ari].trim().split(" ").pop();
384 if (this.depth < 1) {
385 this.ret += this.pad + "public " + this.xcls + "(" +
386 cargs_str +")\n" + this.pad + "{\n";
391 this.ret+= this.pad + "public " + this.xcls + "(" +
392 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
401 this.ret += this.ipad + "_this = this;\n";
404 this.ret+= this.ipad + "_this = _owner;\n";
406 if (this.node.props.has_key("id")
408 this.node.xvala_id != ""
410 this.node.xvala_id[0] != '*'
412 this.node.xvala_id[0] != '+'
414 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
422 void addWrappedCtor()
425 // this may need to look up properties to fill in the arguments..
426 // introspection does not workk..... - as things like gtkmessagedialog
428 if (cls == 'Gtk.Table') {
430 var methods = this.palete.getPropertiesFor(cls, 'methods');
432 print(JSON.stringify(this.palete.proplist[cls], null,4));
436 if (this.node.has("* ctor")) {
439 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
442 // the ctor arguments...
445 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
446 //if (default_ctor == null) {
447 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
450 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
452 var iter =default_ctor.paramset.params.list_iterator();
453 while (iter.next()) {
454 var n = iter.get().name;
455 if (!this.node.has(n)) {
457 if (iter.get().type.contains("int")) {
461 if (iter.get().type.contains("float")) {
465 if (iter.get().type.contains("bool")) {
466 args += "true"; // always default to true?
469 // any other types???
474 this.ignoreWrapped(n);
477 var v = this.node.get(n);
479 if (iter.get().type == "string") {
480 v = "\"" + v.escape("") + "\"";
482 if (v == "TRUE" || v == "FALSE") {
490 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
496 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
503 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
504 //print(JSON.stringify(meths,null,4));Seed.quit();
508 // initialize.. my vars..
509 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
511 var iter = this.myvars.list_iterator();
516 var ar = k.strip().split(" ");
517 var kname = ar[ar.length-1];
519 var v = this.node.props.get(k);
524 if (v == "FALSE" || v == "TRUE") {
527 //FIXME -- check for raw string.. "string XXXX"
529 // if it's a string...
531 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
539 void addWrappedProperties()
541 var cls = Palete.Gir.factoryFqn(this.node.fqn());
545 // what are the properties of this class???
546 this.ret += "\n" + this.ipad + "// set gobject values\n";
548 var iter = cls.props.map_iterator();
549 while (iter.next()) {
550 var p = iter.get_key();
551 print("Check Write %s\n", p);
552 if (!this.node.has(p)) {
555 if (this.shouldIgnoreWrapped(p)) {
560 var v = this.node.get(p);
562 var nodekey = this.node.get_key(p);
564 // user defined properties.
565 if (nodekey[0] == '#') {
571 var is_raw = nodekey[0] == '$';
573 // what's the type.. - if it's a string.. then we quote it..
574 if (iter.get_value().type == "string" && !is_raw) {
575 v = "\"" + v.escape("") + "\"";
577 if (v == "TRUE" || v == "FALSE") {
580 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
585 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
597 if (this.node.items.size < 1) {
601 var iter = this.node.items.list_iterator();
603 while (iter.next()) {
608 if (ci.xvala_id[0] == '*') {
609 continue; // skip generation of children?
613 if (ci.has("* args")) {
615 var ar = ci.get("* args").split(",");
616 for (var ari = 0 ; ari < ar.length; ari++ ) {
617 var arg = ar[ari].split(" ");
618 xargs += "," + arg[arg.length -1];
622 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
623 "( _this " + xargs + ");\n" ;
625 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
627 if (ci.has("* prop")) {
628 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
632 // not sure why we have 'true' in pack?!?
633 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
637 string[] packing = { "add" };
638 if (ci.has("pack")) {
639 packing = ci.get("pack").split(",");
642 var pack = packing[0];
643 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
644 (packing.length > 1 ?
645 (", " + string.joinv(",", packing).substring(pack.length+1))
651 if (ci.xvala_id[0] != '+') {
652 continue; // skip generation of children?
655 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
664 if (!this.node.has("init")) {
667 this.ret+= "\n" + ipad + "// init method \n";
669 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
674 if (this.node.listeners.size < 1) {
680 this.ret+= "\n" + ipad + "// listeners \n";
682 var iter = this.node.listeners.map_iterator();
683 while (iter.next()) {
684 var k = iter.get_key();
685 var v = iter.get_value();
686 this.ret+= this.ipad + "this.el." + k + ".connect( " +
687 this.padMultiline(this.ipad,v) +");\n";
697 this.ret+= this.pad + "}\n";
702 * Standardize this crap...
704 * standard properties (use to set)
705 * If they are long values show the dialog..
708 * bool is_xxx :: can show a pulldown.. (true/false)
710 * $ string html = string with value interpolated eg. baseURL + ".."
711 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
712 * $ untypedvalue = javascript untyped value...
713 * _ string html ... = translatable..
716 * object properties (not part of the GOjbect being wrapped?
717 * # Gee.ArrayList<Xcls_fileitem> fileitems
722 * methods -- always text editor..
730 * * init -- big string?
732 * event handlers (listeners)
737 * +XXXX -- indicates it's a instance property / not glob...
738 * *XXXX -- skip writing glob property (used as classes that can be created...)
743 void addUserMethods()
746 this.ret+= "\n" + pad + "// user defined functions \n";
748 // user defined functions...
749 var iter = this.node.props.map_iterator();
751 var k = iter.get_key();
752 if (this.shouldIgnore(k)) {
755 // HOW TO DETERIME if its a method?
757 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
760 // function in the format of {type} (args) { .... }
761 var kk = k.substring(2);
762 var vv = iter.get_value();
763 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
772 if (this.depth > 0) {
773 this.ret+= this.inpad + "}\n";
776 var iter = this.node.items.list_iterator();
778 while (iter.next()) {
779 this.ret += this.mungeChild(iter.get());
782 if (this.depth < 1) {
783 this.ret+= this.inpad + "}\n";
788 string padMultiline(string pad, string str)
790 var ar = str.strip().split("\n");
791 return string.joinv("\n" + pad , ar);
794 void ignore(string i) {
795 this.ignoreList.add(i);
798 void ignoreWrapped(string i) {
799 this.ignoreWrappedList.add(i);
802 bool shouldIgnore(string i)
804 return ignoreList.contains(i);
806 bool shouldIgnoreWrapped(string i)
808 return ignoreWrappedList.contains(i);