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;
55 node.line = this.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>();
66 string toValaNS(Node item)
68 var ns = item.get("xns") ;
69 if (ns == "GtkSource") {
74 public void toValaName(Node item, int depth =0)
78 var ns = this.toValaNS(item) ;
79 var cls = ns + item.get("xtype");
85 string id = item.get("id").length > 0 ?
86 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
91 if (id[0] == '*' || id[0] == '+') {
92 item.xvala_xcls = "Xcls_" + id.substring(1);
94 item.xvala_xcls = "Xcls_" + id;
100 this.vitems.add(item);
101 } else if (!item.props.has_key("id")) {
102 // use the file name..
103 item.xvala_xcls = this.file.name;
105 item.xvala_id = this.file.name;
110 if (item.items.size < 1) {
113 for(var i =0;i<item.items.size;i++) {
114 this.toValaName(item.items.get(i), depth+1);
119 * Main entry point to convert a file into a string..
121 public static string mungeFile(JsRender file)
123 if (file.tree == null) {
127 var n = new NodeToVala(file.tree, 0, null);
131 n.toValaName(file.tree);
134 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
135 n.cls = file.tree.xvala_cls;
136 n.xcls = file.tree.xvala_xcls;
142 public string munge ( )
144 //return this.mungeToString(this.node);
149 this.ignore("xtype");
155 this.addTopProperties();
157 this.addPlusProperties();
160 this.addWrappedCtor();
162 this.addInitMyVars();
163 this.addWrappedProperties();
168 this.addUserMethods();
175 public string mungeChild( Node cnode)
177 var x = new NodeToVala(cnode, this.depth+1, this);
180 public void addLine(string str= "")
183 this.ret += str + "\n";
187 public void globalVars()
189 if (this.depth > 0) {
193 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
195 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
197 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
206 // class xxx { WrappedGtk el; }
207 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
209 this.addLine(this.pad + "public " + this.cls + " el;");
212 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
224 this.addLine(pad + "public static " + xcls + " singleton()");
225 this.addLine(this.pad + "{")
226 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
227 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
228 this.addLine(this.ipad + "}");
229 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
230 this.addLine(this.pad + "}");
234 void addTopProperties()
236 if (this.depth > 0) {
239 // properties - global..??
241 var iter = this.vitems.list_iterator();
246 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
250 if (n.xvala_id[0] == '*') {
253 if (n.xvala_id[0] == '+') {
256 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
264 this.ret += "\n" + this.ipad + "// my vars (def)\n";
268 var cls = Palete.Gir.factoryFqn(this.node.fqn());
276 var iter = this.node.props.map_iterator();
277 while (iter.next()) {
278 var k = iter.get_key();
279 if (this.shouldIgnore(k)) {
282 var vv = k.strip().split(" ");
283 // user defined method
292 this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n";
296 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
297 if (vv.length < min) {
298 // skip 'old js style properties without a type'
302 var kname = vv[vv.length-1];
304 if (this.shouldIgnore(kname)) {
308 // is it a class property...
309 if (cls.props.has_key(kname) && vv[0] != "#") {
316 this.ret += this.pad + "public " +
317 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
325 // if id of child is '+' then it's a property of this..
326 void addPlusProperties()
328 if (this.node.items.size < 1) {
331 var iter = this.node.items.list_iterator();
332 while (iter.next()) {
335 if (ci.xvala_id[0] != '+') {
336 continue; // skip generation of children?
339 this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
354 this.ret += "\n" + this.pad + "// ctor \n";
355 if (this.node.has("* args")) {
356 // not sure what this is supposed to be ding..
358 cargs_str = ", " + this.node.get("* args");
359 //var ar = this.node.get("* args");.split(",");
360 //for (var ari =0; ari < ar.length; ari++) {
361 // cargs += (ar[ari].trim().split(" ").pop();
365 if (this.depth < 1) {
366 this.ret += this.pad + "public " + this.xcls + "(" +
367 cargs_str +")\n" + this.pad + "{\n";
372 this.ret+= this.pad + "public " + this.xcls + "(" +
373 this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
382 this.ret += this.ipad + "_this = this;\n";
385 this.ret+= this.ipad + "_this = _owner;\n";
387 if (this.node.props.has_key("id")
389 this.node.xvala_id != ""
391 this.node.xvala_id[0] != '*'
393 this.node.xvala_id[0] != '+'
395 this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n";
403 void addWrappedCtor()
406 // this may need to look up properties to fill in the arguments..
407 // introspection does not workk..... - as things like gtkmessagedialog
409 if (cls == 'Gtk.Table') {
411 var methods = this.palete.getPropertiesFor(cls, 'methods');
413 print(JSON.stringify(this.palete.proplist[cls], null,4));
417 if (this.node.has("* ctor")) {
420 this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
423 // the ctor arguments...
426 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
427 //if (default_ctor == null) {
428 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
431 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
433 var iter =default_ctor.paramset.params.list_iterator();
434 while (iter.next()) {
435 var n = iter.get().name;
436 if (!this.node.has(n)) {
438 if (iter.get().type.contains("int")) {
442 if (iter.get().type.contains("float")) {
446 if (iter.get().type.contains("bool")) {
447 args += "true"; // always default to true?
450 // any other types???
455 this.ignoreWrapped(n);
458 var v = this.node.get(n);
460 if (iter.get().type == "string") {
461 v = "\"" + v.escape("") + "\"";
463 if (v == "TRUE" || v == "FALSE") {
471 this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
477 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
484 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
485 //print(JSON.stringify(meths,null,4));Seed.quit();
489 // initialize.. my vars..
490 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
492 var iter = this.myvars.list_iterator();
497 var ar = k.strip().split(" ");
498 var kname = ar[ar.length-1];
500 var v = this.node.props.get(k);
505 if (v == "FALSE" || v == "TRUE") {
508 //FIXME -- check for raw string.. "string XXXX"
510 // if it's a string...
512 this.ret += this.ipad + "this." + kname + " = " + v +";\n";
520 void addWrappedProperties()
522 var cls = Palete.Gir.factoryFqn(this.node.fqn());
526 // what are the properties of this class???
527 this.ret += "\n" + this.ipad + "// set gobject values\n";
529 var iter = cls.props.map_iterator();
530 while (iter.next()) {
531 var p = iter.get_key();
532 print("Check Write %s\n", p);
533 if (!this.node.has(p)) {
536 if (this.shouldIgnoreWrapped(p)) {
541 var v = this.node.get(p);
543 var nodekey = this.node.get_key(p);
545 // user defined properties.
546 if (nodekey[0] == '#') {
552 var is_raw = nodekey[0] == '$';
554 // what's the type.. - if it's a string.. then we quote it..
555 if (iter.get_value().type == "string" && !is_raw) {
556 v = "\"" + v.escape("") + "\"";
558 if (v == "TRUE" || v == "FALSE") {
561 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
566 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
578 if (this.node.items.size < 1) {
582 var iter = this.node.items.list_iterator();
584 while (iter.next()) {
589 if (ci.xvala_id[0] == '*') {
590 continue; // skip generation of children?
594 if (ci.has("* args")) {
596 var ar = ci.get("* args").split(",");
597 for (var ari = 0 ; ari < ar.length; ari++ ) {
598 var arg = ar[ari].split(" ");
599 xargs += "," + arg[arg.length -1];
603 this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
604 "( _this " + xargs + ");\n" ;
606 this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
608 if (ci.has("* prop")) {
609 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
613 // not sure why we have 'true' in pack?!?
614 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
618 string[] packing = { "add" };
619 if (ci.has("pack")) {
620 packing = ci.get("pack").split(",");
623 var pack = packing[0];
624 this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
625 (packing.length > 1 ?
626 (", " + string.joinv(",", packing).substring(pack.length+1))
632 if (ci.xvala_id[0] != '+') {
633 continue; // skip generation of children?
636 this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n";
645 if (!this.node.has("init")) {
648 this.ret+= "\n" + ipad + "// init method \n";
650 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
655 if (this.node.listeners.size < 1) {
661 this.ret+= "\n" + ipad + "// listeners \n";
663 var iter = this.node.listeners.map_iterator();
664 while (iter.next()) {
665 var k = iter.get_key();
666 var v = iter.get_value();
667 this.ret+= this.ipad + "this.el." + k + ".connect( " +
668 this.padMultiline(this.ipad,v) +");\n";
678 this.ret+= this.pad + "}\n";
683 * Standardize this crap...
685 * standard properties (use to set)
686 * If they are long values show the dialog..
689 * bool is_xxx :: can show a pulldown.. (true/false)
691 * $ string html = string with value interpolated eg. baseURL + ".."
692 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
693 * $ untypedvalue = javascript untyped value...
694 * _ string html ... = translatable..
697 * object properties (not part of the GOjbect being wrapped?
698 * # Gee.ArrayList<Xcls_fileitem> fileitems
703 * methods -- always text editor..
711 * * init -- big string?
713 * event handlers (listeners)
718 * +XXXX -- indicates it's a instance property / not glob...
719 * *XXXX -- skip writing glob property (used as classes that can be created...)
724 void addUserMethods()
727 this.ret+= "\n" + pad + "// user defined functions \n";
729 // user defined functions...
730 var iter = this.node.props.map_iterator();
732 var k = iter.get_key();
733 if (this.shouldIgnore(k)) {
736 // HOW TO DETERIME if its a method?
738 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
741 // function in the format of {type} (args) { .... }
742 var kk = k.substring(2);
743 var vv = iter.get_value();
744 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
753 if (this.depth > 0) {
754 this.ret+= this.inpad + "}\n";
757 var iter = this.node.items.list_iterator();
759 while (iter.next()) {
760 this.ret += this.mungeChild(iter.get());
763 if (this.depth < 1) {
764 this.ret+= this.inpad + "}\n";
769 string padMultiline(string pad, string str)
771 var ar = str.strip().split("\n");
772 return string.joinv("\n" + pad , ar);
775 void ignore(string i) {
776 this.ignoreList.add(i);
779 void ignoreWrapped(string i) {
780 this.ignoreWrappedList.add(i);
783 bool shouldIgnore(string i)
785 return ignoreList.contains(i);
787 bool shouldIgnoreWrapped(string i)
789 return ignoreWrappedList.contains(i);