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_start = this.cur_line;
66 node.line_end = this.cur_line;
67 node.lines = new Gee.ArrayList<int>();
68 node.line_map = new Gee.HashMap<int,string>();
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 += "/*%d*/ ".printf(this.cur_line-1) + str + "\n";
192 this.ret += str + "\n";
194 public void addMultiLine(string str= "")
196 var l = this.cur_line;
197 this.cur_line += str.split("\n").length;
198 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
199 this.ret += + str + "\n";
203 public void globalVars()
205 if (this.depth > 0) {
208 // Global Vars..??? when did this get removed..?
209 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
211 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
220 // class xxx { WrappedGtk el; }
221 this.node.line_start = this.cur_line;
222 this.addLine(inpad + "public class " + this.xcls + " : Object");
223 this.addLine(this.inpad + "{");
226 this.addLine(this.pad + "public " + this.cls + " el;");
228 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
240 this.addLine(pad + "public static " + xcls + " singleton()");
241 this.addLine(this.pad + "{");
242 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
243 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
244 this.addLine(this.ipad + "}");
245 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
246 this.addLine(this.pad + "}");
250 * when ID is used... on an element, it registeres a property on the top level...
251 * so that _this.ID always works..
254 void addTopProperties()
256 if (this.depth > 0) {
259 // properties - global..??
261 var iter = this.vitems.list_iterator();
266 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
270 if (n.xvala_id[0] == '*') {
273 if (n.xvala_id[0] == '+') {
276 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
282 * create properties that are not 'part of the wrapped element.
290 this.addLine(this.ipad + "// my vars (def)");
294 var cls = Palete.Gir.factoryFqn(this.node.fqn());
302 var iter = this.node.props.map_iterator();
303 while (iter.next()) {
304 var k = iter.get_key();
305 if (this.shouldIgnore(k)) {
308 var vv = k.strip().split(" ");
309 // user defined method
318 this.node.setLine(this.cur_line, "p", k);
319 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
324 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
325 if (vv.length < min) {
326 // skip 'old js style properties without a type'
330 var kname = vv[vv.length-1];
332 if (this.shouldIgnore(kname)) {
336 // is it a class property...
337 if (cls.props.has_key(kname) && vv[0] != "#") {
342 this.node.setLine(this.cur_line, "p", k);
344 this.addLine(this.pad + "public " +
345 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
353 // if id of child is '+' then it's a property of this..
354 void addPlusProperties()
356 if (this.node.items.size < 1) {
359 var iter = this.node.items.list_iterator();
360 while (iter.next()) {
363 if (ci.xvala_id[0] != '+') {
364 continue; // skip generation of children?
368 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
374 * add the constructor definition..
386 this.addLine(this.pad + "// ctor");
388 if (this.node.has("* args")) {
389 // not sure what this is supposed to be ding..
391 cargs_str = ", " + this.node.get("* args");
392 //var ar = this.node.get("* args");.split(",");
393 //for (var ari =0; ari < ar.length; ari++) {
394 // cargs += (ar[ari].trim().split(" ").pop();
398 if (this.depth < 1) {
400 // top level - does not pass the top level element..
401 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
402 this.addLine(this.pad + "{");
405 // for sub classes = we passs the top level as _owner
406 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
407 this.addLine(this.pad + "{");
413 * make sure _this is defined..
419 this.addLine( this.ipad + "_this = this;");
422 // for non top level = _this point to owner, and _this.ID is set
424 this.addLine( this.ipad + "_this = _owner;");
426 if (this.node.props.has_key("id")
428 this.node.xvala_id != ""
430 this.node.xvala_id[0] != '*'
432 this.node.xvala_id[0] != '+'
434 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
440 * Initialize this.el to point to the wrapped element.
445 void addWrappedCtor()
448 // this may need to look up properties to fill in the arguments..
449 // introspection does not workk..... - as things like gtkmessagedialog
451 if (cls == 'Gtk.Table') {
453 var methods = this.palete.getPropertiesFor(cls, 'methods');
455 print(JSON.stringify(this.palete.proplist[cls], null,4));
459 if (this.node.has("* ctor")) {
460 this.node.setLine(this.cur_line, "p", "* ctor");
461 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
465 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
468 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
470 var iter =default_ctor.paramset.params.list_iterator();
471 while (iter.next()) {
472 var n = iter.get().name;
473 if (!this.node.has(n)) {
475 if (iter.get().type.contains("int")) {
479 if (iter.get().type.contains("float")) {
483 if (iter.get().type.contains("bool")) {
484 args += "true"; // always default to true?
487 // any other types???
492 this.ignoreWrapped(n);
495 var v = this.node.get(n);
497 if (iter.get().type == "string") {
498 v = "\"" + v.escape("") + "\"";
500 if (v == "TRUE" || v == "FALSE") {
508 this.node.setLine(this.cur_line, "p", "* xtype");
510 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
514 this.node.setLine(this.cur_line, "p", "* xtype");;
516 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
523 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
524 //print(JSON.stringify(meths,null,4));Seed.quit();
528 // initialize.. my vars..
530 this.addLine( this.ipad + "// my vars (dec)");
532 var iter = this.myvars.list_iterator();
537 var ar = k.strip().split(" ");
538 var kname = ar[ar.length-1];
540 var v = this.node.props.get(k);
545 if (v == "FALSE" || v == "TRUE") {
548 //FIXME -- check for raw string.. "string XXXX"
550 // if it's a string...
553 this.addLine(this.ipad + "this." + kname + " = " + v +";");
561 void addWrappedProperties()
563 var cls = Palete.Gir.factoryFqn(this.node.fqn());
567 // what are the properties of this class???
569 this.addLine(this.ipad + "// set gobject values");
572 var iter = cls.props.map_iterator();
573 while (iter.next()) {
574 var p = iter.get_key();
575 print("Check Write %s\n", p);
576 if (!this.node.has(p)) {
579 if (this.shouldIgnoreWrapped(p)) {
584 var v = this.node.get(p);
586 var nodekey = this.node.get_key(p);
588 // user defined properties.
589 if (nodekey[0] == '#') {
595 var is_raw = nodekey[0] == '$';
597 // what's the type.. - if it's a string.. then we quote it..
598 if (iter.get_value().type == "string" && !is_raw) {
599 v = "\"" + v.escape("") + "\"";
601 if (v == "TRUE" || v == "FALSE") {
604 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
609 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
618 * pack the children into the parent.
620 * if the child's id starts with '*' then it is not packed...
621 * - this allows you to define children and add them manually..
627 if (this.node.items.size < 1) {
631 var iter = this.node.items.list_iterator();
633 while (iter.next()) {
638 if (ci.xvala_id[0] == '*') {
639 continue; // skip generation of children?
643 if (ci.has("* args")) {
645 var ar = ci.get("* args").split(",");
646 for (var ari = 0 ; ari < ar.length; ari++ ) {
647 var arg = ar[ari].split(" ");
648 xargs += "," + arg[arg.length -1];
651 // create the element..
652 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
653 "( _this " + xargs + ");" );
655 // this is only needed if it does not have an ID???
656 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
658 if (ci.has("* prop")) {
659 this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;");
664 // not sure why we have 'true' in pack?!?
665 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
669 string[] packing = { "add" };
670 if (ci.has("pack")) {
671 packing = ci.get("pack").split(",");
674 var pack = packing[0];
675 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
676 (packing.length > 1 ?
677 (", " + string.joinv(",", packing).substring(pack.length+1))
683 if (ci.xvala_id[0] != '+') {
684 continue; // skip generation of children?
687 // this.{id - without the '+'} = the element...
688 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
697 if (!this.node.has("init")) {
701 this.addLine(ipad + "// init method");
703 this.node.setLine(this.cur_line, "p", "init");
705 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
710 if (this.node.listeners.size < 1) {
715 this.addLine(ipad + "//listeners");
719 var iter = this.node.listeners.map_iterator();
720 while (iter.next()) {
721 var k = iter.get_key();
722 var v = iter.get_value();
724 this.node.setLine(this.cur_line, "l", k);
725 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
726 this.padMultiline(this.ipad,v) +");");
734 this.addLine(this.pad + "}");
739 * Standardize this crap...
741 * standard properties (use to set)
742 * If they are long values show the dialog..
745 * bool is_xxx :: can show a pulldown.. (true/false)
747 * $ string html = string with value interpolated eg. baseURL + ".."
748 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
749 * $ untypedvalue = javascript untyped value...
750 * _ string html ... = translatable..
753 * object properties (not part of the GOjbect being wrapped?
754 * # Gee.ArrayList<Xcls_fileitem> fileitems
759 * methods -- always text editor..
767 * * init -- big string?
769 * event handlers (listeners)
774 * +XXXX -- indicates it's a instance property / not glob...
775 * *XXXX -- skip writing glob property (used as classes that can be created...)
780 void addUserMethods()
783 this.addLine(this.pad + "// user defined functions");
785 // user defined functions...
786 var iter = this.node.props.map_iterator();
788 var k = iter.get_key();
789 if (this.shouldIgnore(k)) {
792 // HOW TO DETERIME if its a method?
794 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
798 // function in the format of {type} (args) { .... }
799 var kk = k.substring(2);
800 var vv = iter.get_value();
801 this.node.setLine(this.cur_line, "p", k);
802 this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));;
810 this.node.line_end = this.cur_line;
811 this.node.sortLines();
814 if (this.depth > 0) {
815 this.addLine(this.inpad + "}");
818 var iter = this.node.items.list_iterator();
820 while (iter.next()) {
821 this.addMultiLine(this.mungeChild(iter.get()));
824 if (this.depth < 1) {
825 this.addLine(this.inpad + "}");
830 string padMultiline(string pad, string str)
832 var ar = str.strip().split("\n");
833 return string.joinv("\n" + pad , ar);
836 void ignore(string i) {
837 this.ignoreList.add(i);
840 void ignoreWrapped(string i) {
841 this.ignoreWrappedList.add(i);
844 bool shouldIgnore(string i)
846 return ignoreList.contains(i);
848 bool shouldIgnoreWrapped(string i)
850 return ignoreWrappedList.contains(i);