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";
193 public void addMultiLine(string str= "")
196 this.cur_line += str.split("\n").length;
197 this.ret += str + "\n";
201 public void globalVars()
203 if (this.depth > 0) {
206 // Global Vars..??? when did this get removed..?
207 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
209 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
218 // class xxx { WrappedGtk el; }
219 this.node.line_start = this.cur_line;
220 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
223 this.addLine(this.pad + "public " + this.cls + " el;");
225 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
237 this.addLine(pad + "public static " + xcls + " singleton()");
238 this.addLine(this.pad + "{");
239 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
240 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
241 this.addLine(this.ipad + "}");
242 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
243 this.addLine(this.pad + "}");
247 * when ID is used... on an element, it registeres a property on the top level...
248 * so that _this.ID always works..
251 void addTopProperties()
253 if (this.depth > 0) {
256 // properties - global..??
258 var iter = this.vitems.list_iterator();
263 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
267 if (n.xvala_id[0] == '*') {
270 if (n.xvala_id[0] == '+') {
273 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
279 * create properties that are not 'part of the wrapped element.
287 this.addLine(this.ipad + "// my vars (def)");
291 var cls = Palete.Gir.factoryFqn(this.node.fqn());
299 var iter = this.node.props.map_iterator();
300 while (iter.next()) {
301 var k = iter.get_key();
302 if (this.shouldIgnore(k)) {
305 var vv = k.strip().split(" ");
306 // user defined method
315 this.node.setLine(this.cur_line, "p", k);
316 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
321 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
322 if (vv.length < min) {
323 // skip 'old js style properties without a type'
327 var kname = vv[vv.length-1];
329 if (this.shouldIgnore(kname)) {
333 // is it a class property...
334 if (cls.props.has_key(kname) && vv[0] != "#") {
339 this.node.setLine(this.cur_line, "p", k);
341 this.addLine(this.pad + "public " +
342 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
350 // if id of child is '+' then it's a property of this..
351 void addPlusProperties()
353 if (this.node.items.size < 1) {
356 var iter = this.node.items.list_iterator();
357 while (iter.next()) {
360 if (ci.xvala_id[0] != '+') {
361 continue; // skip generation of children?
365 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
371 * add the constructor definition..
383 this.addLine(this.pad + "// ctor");
385 if (this.node.has("* args")) {
386 // not sure what this is supposed to be ding..
388 cargs_str = ", " + this.node.get("* args");
389 //var ar = this.node.get("* args");.split(",");
390 //for (var ari =0; ari < ar.length; ari++) {
391 // cargs += (ar[ari].trim().split(" ").pop();
395 if (this.depth < 1) {
397 // top level - does not pass the top level element..
398 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
399 this.addLine(this.pad + "{");
402 // for sub classes = we passs the top level as _owner
403 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
404 this.addLine(this.pad + "{");
410 * make sure _this is defined..
416 this.addLine( this.ipad + "_this = this;");
419 // for non top level = _this point to owner, and _this.ID is set
421 this.addLine( this.ipad + "_this = _owner;");
423 if (this.node.props.has_key("id")
425 this.node.xvala_id != ""
427 this.node.xvala_id[0] != '*'
429 this.node.xvala_id[0] != '+'
431 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
437 * Initialize this.el to point to the wrapped element.
442 void addWrappedCtor()
445 // this may need to look up properties to fill in the arguments..
446 // introspection does not workk..... - as things like gtkmessagedialog
448 if (cls == 'Gtk.Table') {
450 var methods = this.palete.getPropertiesFor(cls, 'methods');
452 print(JSON.stringify(this.palete.proplist[cls], null,4));
456 if (this.node.has("* ctor")) {
457 this.node.setLine(this.cur_line, "p", "* ctor");
458 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
462 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
465 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
467 var iter =default_ctor.paramset.params.list_iterator();
468 while (iter.next()) {
469 var n = iter.get().name;
470 if (!this.node.has(n)) {
472 if (iter.get().type.contains("int")) {
476 if (iter.get().type.contains("float")) {
480 if (iter.get().type.contains("bool")) {
481 args += "true"; // always default to true?
484 // any other types???
489 this.ignoreWrapped(n);
492 var v = this.node.get(n);
494 if (iter.get().type == "string") {
495 v = "\"" + v.escape("") + "\"";
497 if (v == "TRUE" || v == "FALSE") {
505 this.node.setLine(this.cur_line, "p", "* xtype");
507 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
511 this.node.setLine(this.cur_line, "p", "* xtype");;
513 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
520 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
521 //print(JSON.stringify(meths,null,4));Seed.quit();
525 // initialize.. my vars..
527 this.addLine( this.ipad + "// my vars (dec)");
529 var iter = this.myvars.list_iterator();
534 var ar = k.strip().split(" ");
535 var kname = ar[ar.length-1];
537 var v = this.node.props.get(k);
542 if (v == "FALSE" || v == "TRUE") {
545 //FIXME -- check for raw string.. "string XXXX"
547 // if it's a string...
550 this.addLine(this.ipad + "this." + kname + " = " + v +";");
558 void addWrappedProperties()
560 var cls = Palete.Gir.factoryFqn(this.node.fqn());
564 // what are the properties of this class???
566 this.addLine(this.ipad + "// set gobject values");
569 var iter = cls.props.map_iterator();
570 while (iter.next()) {
571 var p = iter.get_key();
572 print("Check Write %s\n", p);
573 if (!this.node.has(p)) {
576 if (this.shouldIgnoreWrapped(p)) {
581 var v = this.node.get(p);
583 var nodekey = this.node.get_key(p);
585 // user defined properties.
586 if (nodekey[0] == '#') {
592 var is_raw = nodekey[0] == '$';
594 // what's the type.. - if it's a string.. then we quote it..
595 if (iter.get_value().type == "string" && !is_raw) {
596 v = "\"" + v.escape("") + "\"";
598 if (v == "TRUE" || v == "FALSE") {
601 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
606 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
615 * pack the children into the parent.
617 * if the child's id starts with '*' then it is not packed...
618 * - this allows you to define children and add them manually..
624 if (this.node.items.size < 1) {
628 var iter = this.node.items.list_iterator();
630 while (iter.next()) {
635 if (ci.xvala_id[0] == '*') {
636 continue; // skip generation of children?
640 if (ci.has("* args")) {
642 var ar = ci.get("* args").split(",");
643 for (var ari = 0 ; ari < ar.length; ari++ ) {
644 var arg = ar[ari].split(" ");
645 xargs += "," + arg[arg.length -1];
648 // create the element..
649 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
650 "( _this " + xargs + ");" );
652 // this is only needed if it does not have an ID???
653 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
655 if (ci.has("* prop")) {
656 this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;");
661 // not sure why we have 'true' in pack?!?
662 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
666 string[] packing = { "add" };
667 if (ci.has("pack")) {
668 packing = ci.get("pack").split(",");
671 var pack = packing[0];
672 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
673 (packing.length > 1 ?
674 (", " + string.joinv(",", packing).substring(pack.length+1))
680 if (ci.xvala_id[0] != '+') {
681 continue; // skip generation of children?
684 // this.{id - without the '+'} = the element...
685 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
694 if (!this.node.has("init")) {
698 this.addLine(ipad + "// init method");
700 this.node.setLine(this.cur_line, "p", "init");
702 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
707 if (this.node.listeners.size < 1) {
712 this.addLine(ipad + "//listeners");
716 var iter = this.node.listeners.map_iterator();
717 while (iter.next()) {
718 var k = iter.get_key();
719 var v = iter.get_value();
721 this.node.setLine(this.cur_line, "l", k);
722 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
723 this.padMultiline(this.ipad,v) +");");
731 this.addLine(this.pad + "}");
736 * Standardize this crap...
738 * standard properties (use to set)
739 * If they are long values show the dialog..
742 * bool is_xxx :: can show a pulldown.. (true/false)
744 * $ string html = string with value interpolated eg. baseURL + ".."
745 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
746 * $ untypedvalue = javascript untyped value...
747 * _ string html ... = translatable..
750 * object properties (not part of the GOjbect being wrapped?
751 * # Gee.ArrayList<Xcls_fileitem> fileitems
756 * methods -- always text editor..
764 * * init -- big string?
766 * event handlers (listeners)
771 * +XXXX -- indicates it's a instance property / not glob...
772 * *XXXX -- skip writing glob property (used as classes that can be created...)
777 void addUserMethods()
780 this.addLine(this.pad + "// user defined functions");
782 // user defined functions...
783 var iter = this.node.props.map_iterator();
785 var k = iter.get_key();
786 if (this.shouldIgnore(k)) {
789 // HOW TO DETERIME if its a method?
791 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
795 // function in the format of {type} (args) { .... }
796 var kk = k.substring(2);
797 var vv = iter.get_value();
798 this.node.setLine(this.cur_line, "p", k);
799 this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));;
807 this.node.line_end = this.cur_line;
808 this.node.sortLines();
811 if (this.depth > 0) {
812 this.addLine(this.inpad + "}");
815 var iter = this.node.items.list_iterator();
817 while (iter.next()) {
818 this.addMultiLine(this.mungeChild(iter.get()));
821 if (this.depth < 1) {
822 this.addLine(this.inpad + "}");
827 string padMultiline(string pad, string str)
829 var ar = str.strip().split("\n");
830 return string.joinv("\n" + pad , ar);
833 void ignore(string i) {
834 this.ignoreList.add(i);
837 void ignoreWrapped(string i) {
838 this.ignoreWrappedList.add(i);
841 bool shouldIgnore(string i)
843 return ignoreList.contains(i);
845 bool shouldIgnoreWrapped(string i)
847 return ignoreWrappedList.contains(i);