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>();
70 node.node_lines = new Gee.ArrayList<int>();
71 node.node_line_map = new Gee.HashMap<int,Node>();
77 string toValaNS(Node item)
79 var ns = item.get("xns") ;
80 if (ns == "GtkSource") {
85 public void toValaName(Node item, int depth =0)
89 var ns = this.toValaNS(item) ;
90 var cls = ns + item.get("xtype");
96 string id = item.get("id").length > 0 ?
97 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
102 if (id[0] == '*' || id[0] == '+') {
103 item.xvala_xcls = "Xcls_" + id.substring(1);
105 item.xvala_xcls = "Xcls_" + id;
111 this.vitems.add(item);
112 } else if (!item.props.has_key("id")) {
113 // use the file name..
114 item.xvala_xcls = this.file.name;
116 item.xvala_id = this.file.name;
121 if (item.items.size < 1) {
124 for(var i =0;i<item.items.size;i++) {
125 this.toValaName(item.items.get(i), depth+1);
130 * Main entry point to convert a file into a string..
132 public static string mungeFile(JsRender file)
134 if (file.tree == null) {
138 var n = new NodeToVala(file.tree, 0, null);
142 n.toValaName(file.tree);
145 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
146 n.cls = file.tree.xvala_cls;
147 n.xcls = file.tree.xvala_xcls;
153 public string munge ( )
155 //return this.mungeToString(this.node);
160 this.ignore("xtype");
166 this.addTopProperties();
168 this.addPlusProperties();
171 this.addWrappedCtor();
173 this.addInitMyVars();
174 this.addWrappedProperties();
179 this.addUserMethods();
186 public string mungeChild( Node cnode)
188 var x = new NodeToVala(cnode, this.depth+1, this);
191 public void addLine(string str= "")
194 //this.ret += "/*%d*/ ".printf(this.cur_line-1) + str + "\n";
195 this.ret += str + "\n";
197 public void addMultiLine(string str= "")
199 var l = this.cur_line;
200 this.cur_line += str.split("\n").length;
201 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
202 this.ret += str + "\n";
206 public void globalVars()
208 if (this.depth > 0) {
211 // Global Vars..??? when did this get removed..?
212 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
214 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
223 // class xxx { WrappedGtk el; }
224 this.node.line_start = this.cur_line;
225 this.addLine(inpad + "public class " + this.xcls + " : Object");
226 this.addLine(this.inpad + "{");
229 this.addLine(this.pad + "public " + this.cls + " el;");
231 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
243 this.addLine(pad + "public static " + xcls + " singleton()");
244 this.addLine(this.pad + "{");
245 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
246 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
247 this.addLine(this.ipad + "}");
248 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
249 this.addLine(this.pad + "}");
253 * when ID is used... on an element, it registeres a property on the top level...
254 * so that _this.ID always works..
257 void addTopProperties()
259 if (this.depth > 0) {
262 // properties - global..??
264 var iter = this.vitems.list_iterator();
269 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
273 if (n.xvala_id[0] == '*') {
276 if (n.xvala_id[0] == '+') {
279 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
285 * create properties that are not 'part of the wrapped element.
293 this.addLine(this.ipad + "// my vars (def)");
297 var cls = Palete.Gir.factoryFqn(this.node.fqn());
305 var iter = this.node.props.map_iterator();
306 while (iter.next()) {
307 var k = iter.get_key();
308 if (this.shouldIgnore(k)) {
311 var vv = k.strip().split(" ");
312 // user defined method
321 this.node.setLine(this.cur_line, "p", k);
322 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
327 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
328 if (vv.length < min) {
329 // skip 'old js style properties without a type'
333 var kname = vv[vv.length-1];
335 if (this.shouldIgnore(kname)) {
339 // is it a class property...
340 if (cls.props.has_key(kname) && vv[0] != "#") {
345 this.node.setLine(this.cur_line, "p", k);
347 this.addLine(this.pad + "public " +
348 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
356 // if id of child is '+' then it's a property of this..
357 void addPlusProperties()
359 if (this.node.items.size < 1) {
362 var iter = this.node.items.list_iterator();
363 while (iter.next()) {
366 if (ci.xvala_id[0] != '+') {
367 continue; // skip generation of children?
371 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
377 * add the constructor definition..
389 this.addLine(this.pad + "// ctor");
391 if (this.node.has("* args")) {
392 // not sure what this is supposed to be ding..
394 cargs_str = ", " + this.node.get("* args");
395 //var ar = this.node.get("* args");.split(",");
396 //for (var ari =0; ari < ar.length; ari++) {
397 // cargs += (ar[ari].trim().split(" ").pop();
401 if (this.depth < 1) {
403 // top level - does not pass the top level element..
404 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
405 this.addLine(this.pad + "{");
408 // for sub classes = we passs the top level as _owner
409 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
410 this.addLine(this.pad + "{");
416 * make sure _this is defined..
422 this.addLine( this.ipad + "_this = this;");
425 // for non top level = _this point to owner, and _this.ID is set
427 this.addLine( this.ipad + "_this = _owner;");
429 if (this.node.props.has_key("id")
431 this.node.xvala_id != ""
433 this.node.xvala_id[0] != '*'
435 this.node.xvala_id[0] != '+'
437 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
443 * Initialize this.el to point to the wrapped element.
448 void addWrappedCtor()
451 // this may need to look up properties to fill in the arguments..
452 // introspection does not workk..... - as things like gtkmessagedialog
454 if (cls == 'Gtk.Table') {
456 var methods = this.palete.getPropertiesFor(cls, 'methods');
458 print(JSON.stringify(this.palete.proplist[cls], null,4));
462 if (this.node.has("* ctor")) {
463 this.node.setLine(this.cur_line, "p", "* ctor");
464 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
468 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
471 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
473 var iter =default_ctor.paramset.params.list_iterator();
474 while (iter.next()) {
475 var n = iter.get().name;
476 if (!this.node.has(n)) {
478 if (iter.get().type.contains("int")) {
482 if (iter.get().type.contains("float")) {
486 if (iter.get().type.contains("bool")) {
487 args += "true"; // always default to true?
490 // any other types???
495 this.ignoreWrapped(n);
498 var v = this.node.get(n);
500 if (iter.get().type == "string") {
501 v = "\"" + v.escape("") + "\"";
503 if (v == "TRUE" || v == "FALSE") {
511 this.node.setLine(this.cur_line, "p", "* xtype");
513 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
517 this.node.setLine(this.cur_line, "p", "* xtype");;
519 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
526 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
527 //print(JSON.stringify(meths,null,4));Seed.quit();
531 // initialize.. my vars..
533 this.addLine( this.ipad + "// my vars (dec)");
535 var iter = this.myvars.list_iterator();
540 var ar = k.strip().split(" ");
541 var kname = ar[ar.length-1];
543 var v = this.node.props.get(k);
548 if (v == "FALSE" || v == "TRUE") {
551 //FIXME -- check for raw string.. "string XXXX"
553 // if it's a string...
556 this.addLine(this.ipad + "this." + kname + " = " + v +";");
564 void addWrappedProperties()
566 var cls = Palete.Gir.factoryFqn(this.node.fqn());
570 // what are the properties of this class???
572 this.addLine(this.ipad + "// set gobject values");
575 var iter = cls.props.map_iterator();
576 while (iter.next()) {
577 var p = iter.get_key();
578 print("Check Write %s\n", p);
579 if (!this.node.has(p)) {
582 if (this.shouldIgnoreWrapped(p)) {
587 var v = this.node.get(p);
589 var nodekey = this.node.get_key(p);
591 // user defined properties.
592 if (nodekey[0] == '#') {
598 var is_raw = nodekey[0] == '$';
600 // what's the type.. - if it's a string.. then we quote it..
601 if (iter.get_value().type == "string" && !is_raw) {
602 v = "\"" + v.escape("") + "\"";
604 if (v == "TRUE" || v == "FALSE") {
607 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
612 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
621 * pack the children into the parent.
623 * if the child's id starts with '*' then it is not packed...
624 * - this allows you to define children and add them manually..
630 if (this.node.items.size < 1) {
634 var iter = this.node.items.list_iterator();
636 while (iter.next()) {
641 if (ci.xvala_id[0] == '*') {
642 continue; // skip generation of children?
646 if (ci.has("* args")) {
648 var ar = ci.get("* args").split(",");
649 for (var ari = 0 ; ari < ar.length; ari++ ) {
650 var arg = ar[ari].split(" ");
651 xargs += "," + arg[arg.length -1];
654 // create the element..
655 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
656 "( _this " + xargs + ");" );
658 // this is only needed if it does not have an ID???
659 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
661 if (ci.has("* prop")) {
662 this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;");
667 // not sure why we have 'true' in pack?!?
668 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
672 string[] packing = { "add" };
673 if (ci.has("pack")) {
674 packing = ci.get("pack").split(",");
677 var pack = packing[0];
678 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
679 (packing.length > 1 ?
680 (", " + string.joinv(",", packing).substring(pack.length+1))
686 if (ci.xvala_id[0] != '+') {
687 continue; // skip generation of children?
690 // this.{id - without the '+'} = the element...
691 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
700 if (!this.node.has("init")) {
704 this.addLine(ipad + "// init method");
706 this.node.setLine(this.cur_line, "p", "init");
708 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
713 if (this.node.listeners.size < 1) {
718 this.addLine(ipad + "//listeners");
722 var iter = this.node.listeners.map_iterator();
723 while (iter.next()) {
724 var k = iter.get_key();
725 var v = iter.get_value();
727 this.node.setLine(this.cur_line, "l", k);
728 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
729 this.padMultiline(this.ipad,v) +");");
737 this.addLine(this.pad + "}");
742 * Standardize this crap...
744 * standard properties (use to set)
745 * If they are long values show the dialog..
748 * bool is_xxx :: can show a pulldown.. (true/false)
750 * $ string html = string with value interpolated eg. baseURL + ".."
751 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
752 * $ untypedvalue = javascript untyped value...
753 * _ string html ... = translatable..
756 * object properties (not part of the GOjbect being wrapped?
757 * # Gee.ArrayList<Xcls_fileitem> fileitems
762 * methods -- always text editor..
770 * * init -- big string?
772 * event handlers (listeners)
777 * +XXXX -- indicates it's a instance property / not glob...
778 * *XXXX -- skip writing glob property (used as classes that can be created...)
783 void addUserMethods()
786 this.addLine(this.pad + "// user defined functions");
788 // user defined functions...
789 var iter = this.node.props.map_iterator();
791 var k = iter.get_key();
792 if (this.shouldIgnore(k)) {
795 // HOW TO DETERIME if its a method?
797 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
801 // function in the format of {type} (args) { .... }
802 var kk = k.substring(2);
803 var vv = iter.get_value();
804 this.node.setLine(this.cur_line, "p", k);
805 this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));;
813 this.node.line_end = this.cur_line;
814 this.node.sortLines();
817 if (this.depth > 0) {
818 this.addLine(this.inpad + "}");
821 var iter = this.node.items.list_iterator();
823 while (iter.next()) {
824 this.addMultiLine(this.mungeChild(iter.get()));
827 if (this.depth < 1) {
828 this.addLine(this.inpad + "}");
833 string padMultiline(string pad, string str)
835 var ar = str.strip().split("\n");
836 return string.joinv("\n" + pad , ar);
839 void ignore(string i) {
840 this.ignoreList.add(i);
843 void ignoreWrapped(string i) {
844 this.ignoreWrappedList.add(i);
847 bool shouldIgnore(string i)
849 return ignoreList.contains(i);
851 bool shouldIgnoreWrapped(string i)
853 return ignoreWrappedList.contains(i);