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.node.line = this.cur_line;
216 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
219 this.addLine(this.pad + "public " + this.cls + " el;");
221 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
233 this.addLine(pad + "public static " + xcls + " singleton()");
234 this.addLine(this.pad + "{")
235 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
236 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
237 this.addLine(this.ipad + "}");
238 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
239 this.addLine(this.pad + "}");
243 * when ID is used... on an element, it registeres a property on the top level...
244 * so that _this.ID always works..
247 void addTopProperties()
249 if (this.depth > 0) {
252 // properties - global..??
254 var iter = this.vitems.list_iterator();
259 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
263 if (n.xvala_id[0] == '*') {
266 if (n.xvala_id[0] == '+') {
269 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
275 * create properties that are not 'part of the wrapped element.
283 this.addLine(this.ipad + "// my vars (def)");
287 var cls = Palete.Gir.factoryFqn(this.node.fqn());
295 var iter = this.node.props.map_iterator();
296 while (iter.next()) {
297 var k = iter.get_key();
298 if (this.shouldIgnore(k)) {
301 var vv = k.strip().split(" ");
302 // user defined method
311 this.node.proplines.set(k, this.cur_line);
312 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
317 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
318 if (vv.length < min) {
319 // skip 'old js style properties without a type'
323 var kname = vv[vv.length-1];
325 if (this.shouldIgnore(kname)) {
329 // is it a class property...
330 if (cls.props.has_key(kname) && vv[0] != "#") {
336 this.node.proplines.set(k, this.cur_line);
337 this.addLine(this.pad + "public " +
338 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
346 // if id of child is '+' then it's a property of this..
347 void addPlusProperties()
349 if (this.node.items.size < 1) {
352 var iter = this.node.items.list_iterator();
353 while (iter.next()) {
356 if (ci.xvala_id[0] != '+') {
357 continue; // skip generation of children?
360 this.node.proplines.set(k, this.cur_line);
361 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
367 * add the constructor definition..
379 this.addLine(this.pad + "// ctor");
381 if (this.node.has("* args")) {
382 // not sure what this is supposed to be ding..
384 cargs_str = ", " + this.node.get("* args");
385 //var ar = this.node.get("* args");.split(",");
386 //for (var ari =0; ari < ar.length; ari++) {
387 // cargs += (ar[ari].trim().split(" ").pop();
391 if (this.depth < 1) {
393 // top level - does not pass the top level element..
394 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")")
395 this.addLine(this.pad + "{");
398 // for sub classes = we passs the top level as _owner
399 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
400 this.addLine(this.pad + "{");
406 * make sure _this is defined..
412 this.addLine( this.ipad + "_this = this;");
415 // for non top level = _this point to owner, and _this.ID is set
417 this.addLine( this.ipad + "_this = _owner;");
419 if (this.node.props.has_key("id")
421 this.node.xvala_id != ""
423 this.node.xvala_id[0] != '*'
425 this.node.xvala_id[0] != '+'
427 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
433 * Initialize this.el to point to the wrapped element.
438 void addWrappedCtor()
441 // this may need to look up properties to fill in the arguments..
442 // introspection does not workk..... - as things like gtkmessagedialog
444 if (cls == 'Gtk.Table') {
446 var methods = this.palete.getPropertiesFor(cls, 'methods');
448 print(JSON.stringify(this.palete.proplist[cls], null,4));
452 if (this.node.has("* ctor")) {
453 this.node.proplines.set("* ctor", this.curline);
454 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
458 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
461 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
463 var iter =default_ctor.paramset.params.list_iterator();
464 while (iter.next()) {
465 var n = iter.get().name;
466 if (!this.node.has(n)) {
468 if (iter.get().type.contains("int")) {
472 if (iter.get().type.contains("float")) {
476 if (iter.get().type.contains("bool")) {
477 args += "true"; // always default to true?
480 // any other types???
485 this.ignoreWrapped(n);
488 var v = this.node.get(n);
490 if (iter.get().type == "string") {
491 v = "\"" + v.escape("") + "\"";
493 if (v == "TRUE" || v == "FALSE") {
501 this.node.proplines.set("* xtype", this.curline);
502 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
506 this.node.proplines.set("* xtype", this.curline);
508 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
515 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
516 //print(JSON.stringify(meths,null,4));Seed.quit();
520 // initialize.. my vars..
521 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
523 var iter = this.myvars.list_iterator();
528 var ar = k.strip().split(" ");
529 var kname = ar[ar.length-1];
531 var v = this.node.props.get(k);
536 if (v == "FALSE" || v == "TRUE") {
539 //FIXME -- check for raw string.. "string XXXX"
541 // if it's a string...
543 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
551 void addWrappedProperties()
553 var cls = Palete.Gir.factoryFqn(this.node.fqn());
557 // what are the properties of this class???
558 this.ret += "\n" + this.ipad + "// set gobject values\n";
560 var iter = cls.props.map_iterator();
561 while (iter.next()) {
562 var p = iter.get_key();
563 print("Check Write %s\n", p);
564 if (!this.node.has(p)) {
567 if (this.shouldIgnoreWrapped(p)) {
572 var v = this.node.get(p);
574 var nodekey = this.node.get_key(p);
576 // user defined properties.
577 if (nodekey[0] == '#') {
583 var is_raw = nodekey[0] == '$';
585 // what's the type.. - if it's a string.. then we quote it..
586 if (iter.get_value().type == "string" && !is_raw) {
587 v = "\"" + v.escape("") + "\"";
589 if (v == "TRUE" || v == "FALSE") {
592 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
597 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
609 if (this.node.items.size < 1) {
613 var iter = this.node.items.list_iterator();
615 while (iter.next()) {
620 if (ci.xvala_id[0] == '*') {
621 continue; // skip generation of children?
625 if (ci.has("* args")) {
627 var ar = ci.get("* args").split(",");
628 for (var ari = 0 ; ari < ar.length; ari++ ) {
629 var arg = ar[ari].split(" ");
630 xargs += "," + arg[arg.length -1];
634 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
635 "( _this " + xargs + ");\n" ;
637 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
639 if (ci.has("* prop")) {
640 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
644 // not sure why we have 'true' in pack?!?
645 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
649 string[] packing = { "add" };
650 if (ci.has("pack")) {
651 packing = ci.get("pack").split(",");
654 var pack = packing[0];
655 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
656 (packing.length > 1 ?
657 (", " + string.joinv(",", packing).substring(pack.length+1))
663 if (ci.xvala_id[0] != '+') {
664 continue; // skip generation of children?
667 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
676 if (!this.node.has("init")) {
679 this.ret+= "\n" + ipad + "// init method \n";
681 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
686 if (this.node.listeners.size < 1) {
692 this.ret+= "\n" + ipad + "// listeners \n";
694 var iter = this.node.listeners.map_iterator();
695 while (iter.next()) {
696 var k = iter.get_key();
697 var v = iter.get_value();
698 this.ret+= this.ipad + "this.el." + k + ".connect( " +
699 this.padMultiline(this.ipad,v) +");\n";
709 this.ret+= this.pad + "}\n";
714 * Standardize this crap...
716 * standard properties (use to set)
717 * If they are long values show the dialog..
720 * bool is_xxx :: can show a pulldown.. (true/false)
722 * $ string html = string with value interpolated eg. baseURL + ".."
723 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
724 * $ untypedvalue = javascript untyped value...
725 * _ string html ... = translatable..
728 * object properties (not part of the GOjbect being wrapped?
729 * # Gee.ArrayList<Xcls_fileitem> fileitems
734 * methods -- always text editor..
742 * * init -- big string?
744 * event handlers (listeners)
749 * +XXXX -- indicates it's a instance property / not glob...
750 * *XXXX -- skip writing glob property (used as classes that can be created...)
755 void addUserMethods()
758 this.ret+= "\n" + pad + "// user defined functions \n";
760 // user defined functions...
761 var iter = this.node.props.map_iterator();
763 var k = iter.get_key();
764 if (this.shouldIgnore(k)) {
767 // HOW TO DETERIME if its a method?
769 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
772 // function in the format of {type} (args) { .... }
773 var kk = k.substring(2);
774 var vv = iter.get_value();
775 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
784 if (this.depth > 0) {
785 this.ret+= this.inpad + "}\n";
788 var iter = this.node.items.list_iterator();
790 while (iter.next()) {
791 this.ret += this.mungeChild(iter.get());
794 if (this.depth < 1) {
795 this.ret+= this.inpad + "}\n";
800 string padMultiline(string pad, string str)
802 var ar = str.strip().split("\n");
803 return string.joinv("\n" + pad , ar);
806 void ignore(string i) {
807 this.ignoreList.add(i);
810 void ignoreWrapped(string i) {
811 this.ignoreWrappedList.add(i);
814 bool shouldIgnore(string i)
816 return ignoreList.contains(i);
818 bool shouldIgnoreWrapped(string i)
820 return ignoreWrappedList.contains(i);