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.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";
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) {
207 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
209 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
211 //this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n";
220 // class xxx { WrappedGtk el; }
221 this.node.line_start = this.cur_line;
222 this.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
225 this.addLine(this.pad + "public " + this.cls + " el;");
227 this.addLine(this.pad + "private " + this.top.xcls + " _this;)";
239 this.addLine(pad + "public static " + xcls + " singleton()");
240 this.addLine(this.pad + "{")
241 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
242 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
243 this.addLine(this.ipad + "}");
244 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
245 this.addLine(this.pad + "}");
249 * when ID is used... on an element, it registeres a property on the top level...
250 * so that _this.ID always works..
253 void addTopProperties()
255 if (this.depth > 0) {
258 // properties - global..??
260 var iter = this.vitems.list_iterator();
265 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
269 if (n.xvala_id[0] == '*') {
272 if (n.xvala_id[0] == '+') {
275 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
281 * create properties that are not 'part of the wrapped element.
289 this.addLine(this.ipad + "// my vars (def)");
293 var cls = Palete.Gir.factoryFqn(this.node.fqn());
301 var iter = this.node.props.map_iterator();
302 while (iter.next()) {
303 var k = iter.get_key();
304 if (this.shouldIgnore(k)) {
307 var vv = k.strip().split(" ");
308 // user defined method
317 this.node.proplines.set(k, this.cur_line);
318 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
323 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
324 if (vv.length < min) {
325 // skip 'old js style properties without a type'
329 var kname = vv[vv.length-1];
331 if (this.shouldIgnore(kname)) {
335 // is it a class property...
336 if (cls.props.has_key(kname) && vv[0] != "#") {
342 this.node.proplines.set(k, this.cur_line);
343 this.addLine(this.pad + "public " +
344 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
352 // if id of child is '+' then it's a property of this..
353 void addPlusProperties()
355 if (this.node.items.size < 1) {
358 var iter = this.node.items.list_iterator();
359 while (iter.next()) {
362 if (ci.xvala_id[0] != '+') {
363 continue; // skip generation of children?
366 this.node.proplines.set(k, this.cur_line);
367 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
373 * add the constructor definition..
385 this.addLine(this.pad + "// ctor");
387 if (this.node.has("* args")) {
388 // not sure what this is supposed to be ding..
390 cargs_str = ", " + this.node.get("* args");
391 //var ar = this.node.get("* args");.split(",");
392 //for (var ari =0; ari < ar.length; ari++) {
393 // cargs += (ar[ari].trim().split(" ").pop();
397 if (this.depth < 1) {
399 // top level - does not pass the top level element..
400 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")")
401 this.addLine(this.pad + "{");
404 // for sub classes = we passs the top level as _owner
405 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
406 this.addLine(this.pad + "{");
412 * make sure _this is defined..
418 this.addLine( this.ipad + "_this = this;");
421 // for non top level = _this point to owner, and _this.ID is set
423 this.addLine( this.ipad + "_this = _owner;");
425 if (this.node.props.has_key("id")
427 this.node.xvala_id != ""
429 this.node.xvala_id[0] != '*'
431 this.node.xvala_id[0] != '+'
433 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
439 * Initialize this.el to point to the wrapped element.
444 void addWrappedCtor()
447 // this may need to look up properties to fill in the arguments..
448 // introspection does not workk..... - as things like gtkmessagedialog
450 if (cls == 'Gtk.Table') {
452 var methods = this.palete.getPropertiesFor(cls, 'methods');
454 print(JSON.stringify(this.palete.proplist[cls], null,4));
458 if (this.node.has("* ctor")) {
459 this.node.proplines.set("* ctor", this.curline);
460 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
464 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
467 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
469 var iter =default_ctor.paramset.params.list_iterator();
470 while (iter.next()) {
471 var n = iter.get().name;
472 if (!this.node.has(n)) {
474 if (iter.get().type.contains("int")) {
478 if (iter.get().type.contains("float")) {
482 if (iter.get().type.contains("bool")) {
483 args += "true"; // always default to true?
486 // any other types???
491 this.ignoreWrapped(n);
494 var v = this.node.get(n);
496 if (iter.get().type == "string") {
497 v = "\"" + v.escape("") + "\"";
499 if (v == "TRUE" || v == "FALSE") {
507 this.node.proplines.set("* xtype", this.curline);
508 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
512 this.node.proplines.set("* xtype", this.curline);
514 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
521 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
522 //print(JSON.stringify(meths,null,4));Seed.quit();
526 // initialize.. my vars..
528 this.addLine( this.ipad + "// my vars (dec)");
530 var iter = this.myvars.list_iterator();
535 var ar = k.strip().split(" ");
536 var kname = ar[ar.length-1];
538 var v = this.node.props.get(k);
543 if (v == "FALSE" || v == "TRUE") {
546 //FIXME -- check for raw string.. "string XXXX"
548 // if it's a string...
551 this.addLine(this.ipad + "this." + kname + " = " + v +";");
559 void addWrappedProperties()
561 var cls = Palete.Gir.factoryFqn(this.node.fqn());
565 // what are the properties of this class???
566 this.ret += "\n" + this.ipad + "// set gobject values\n";
568 var iter = cls.props.map_iterator();
569 while (iter.next()) {
570 var p = iter.get_key();
571 print("Check Write %s\n", p);
572 if (!this.node.has(p)) {
575 if (this.shouldIgnoreWrapped(p)) {
580 var v = this.node.get(p);
582 var nodekey = this.node.get_key(p);
584 // user defined properties.
585 if (nodekey[0] == '#') {
591 var is_raw = nodekey[0] == '$';
593 // what's the type.. - if it's a string.. then we quote it..
594 if (iter.get_value().type == "string" && !is_raw) {
595 v = "\"" + v.escape("") + "\"";
597 if (v == "TRUE" || v == "FALSE") {
600 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
605 this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type);
614 * pack the children into the parent.
616 * if the child's id starts with '*' then it is not packed...
617 * - this allows you to define children and add them manually..
623 if (this.node.items.size < 1) {
627 var iter = this.node.items.list_iterator();
629 while (iter.next()) {
634 if (ci.xvala_id[0] == '*') {
635 continue; // skip generation of children?
639 if (ci.has("* args")) {
641 var ar = ci.get("* args").split(",");
642 for (var ari = 0 ; ari < ar.length; ari++ ) {
643 var arg = ar[ari].split(" ");
644 xargs += "," + arg[arg.length -1];
647 // create the element..
648 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
649 "( _this " + xargs + ");" ;
651 // this is only needed if it does not have an ID???
652 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
654 if (ci.has("* prop")) {
655 this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;");
660 // not sure why we have 'true' in pack?!?
661 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
665 string[] packing = { "add" };
666 if (ci.has("pack")) {
667 packing = ci.get("pack").split(",");
670 var pack = packing[0];
671 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
672 (packing.length > 1 ?
673 (", " + string.joinv(",", packing).substring(pack.length+1))
679 if (ci.xvala_id[0] != '+') {
680 continue; // skip generation of children?
683 // this.{id - without the '+'} = the element...
684 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
693 if (!this.node.has("init")) {
697 this.addLine(ipad + "// init method");
699 this.node.proplines.set("init", this.curline);
700 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
705 if (this.node.listeners.size < 1) {
710 this.addLine(ipad + "//listeners");
714 var iter = this.node.listeners.map_iterator();
715 while (iter.next()) {
716 var k = iter.get_key();
717 var v = iter.get_value();
718 this.node.listenlines.set(k, this.curline);
719 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
720 this.padMultiline(this.ipad,v) +");");
728 this.addLine(this.pad + "}");
733 * Standardize this crap...
735 * standard properties (use to set)
736 * If they are long values show the dialog..
739 * bool is_xxx :: can show a pulldown.. (true/false)
741 * $ string html = string with value interpolated eg. baseURL + ".."
742 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
743 * $ untypedvalue = javascript untyped value...
744 * _ string html ... = translatable..
747 * object properties (not part of the GOjbect being wrapped?
748 * # Gee.ArrayList<Xcls_fileitem> fileitems
753 * methods -- always text editor..
761 * * init -- big string?
763 * event handlers (listeners)
768 * +XXXX -- indicates it's a instance property / not glob...
769 * *XXXX -- skip writing glob property (used as classes that can be created...)
774 void addUserMethods()
777 this.addLine(this.pad + "// user defined functions");
779 // user defined functions...
780 var iter = this.node.props.map_iterator();
782 var k = iter.get_key();
783 if (this.shouldIgnore(k)) {
786 // HOW TO DETERIME if its a method?
788 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
792 // function in the format of {type} (args) { .... }
793 var kk = k.substring(2);
794 var vv = iter.get_value();
795 this.node.proplines.set(k, this.curline);
796 this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));;
804 this.node.line_end = this.curline;
806 if (this.depth > 0) {
807 this.ret+= this.inpad + "}\n";
810 var iter = this.node.items.list_iterator();
812 while (iter.next()) {
813 this.addMultiLine(this.mungeChild(iter.get()));
816 if (this.depth < 1) {
817 this.addLine(this.inpad + "}");
822 string padMultiline(string pad, string str)
824 var ar = str.strip().split("\n");
825 return string.joinv("\n" + pad , ar);
828 void ignore(string i) {
829 this.ignoreList.add(i);
832 void ignoreWrapped(string i) {
833 this.ignoreWrappedList.add(i);
836 bool shouldIgnore(string i)
838 return ignoreList.contains(i);
840 bool shouldIgnoreWrapped(string i)
842 return ignoreWrappedList.contains(i);