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 += "/*%d*/ ".printf(this.cur_line-1) + 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");
221 this.addLine(this.inpad + "{");
224 this.addLine(this.pad + "public " + this.cls + " el;");
226 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
238 this.addLine(pad + "public static " + xcls + " singleton()");
239 this.addLine(this.pad + "{");
240 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
241 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
242 this.addLine(this.ipad + "}");
243 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
244 this.addLine(this.pad + "}");
248 * when ID is used... on an element, it registeres a property on the top level...
249 * so that _this.ID always works..
252 void addTopProperties()
254 if (this.depth > 0) {
257 // properties - global..??
259 var iter = this.vitems.list_iterator();
264 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
268 if (n.xvala_id[0] == '*') {
271 if (n.xvala_id[0] == '+') {
274 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
280 * create properties that are not 'part of the wrapped element.
288 this.addLine(this.ipad + "// my vars (def)");
292 var cls = Palete.Gir.factoryFqn(this.node.fqn());
300 var iter = this.node.props.map_iterator();
301 while (iter.next()) {
302 var k = iter.get_key();
303 if (this.shouldIgnore(k)) {
306 var vv = k.strip().split(" ");
307 // user defined method
316 this.node.setLine(this.cur_line, "p", k);
317 this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";");
322 var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2;
323 if (vv.length < min) {
324 // skip 'old js style properties without a type'
328 var kname = vv[vv.length-1];
330 if (this.shouldIgnore(kname)) {
334 // is it a class property...
335 if (cls.props.has_key(kname) && vv[0] != "#") {
340 this.node.setLine(this.cur_line, "p", k);
342 this.addLine(this.pad + "public " +
343 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";");
351 // if id of child is '+' then it's a property of this..
352 void addPlusProperties()
354 if (this.node.items.size < 1) {
357 var iter = this.node.items.list_iterator();
358 while (iter.next()) {
361 if (ci.xvala_id[0] != '+') {
362 continue; // skip generation of children?
366 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
372 * add the constructor definition..
384 this.addLine(this.pad + "// ctor");
386 if (this.node.has("* args")) {
387 // not sure what this is supposed to be ding..
389 cargs_str = ", " + this.node.get("* args");
390 //var ar = this.node.get("* args");.split(",");
391 //for (var ari =0; ari < ar.length; ari++) {
392 // cargs += (ar[ari].trim().split(" ").pop();
396 if (this.depth < 1) {
398 // top level - does not pass the top level element..
399 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
400 this.addLine(this.pad + "{");
403 // for sub classes = we passs the top level as _owner
404 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
405 this.addLine(this.pad + "{");
411 * make sure _this is defined..
417 this.addLine( this.ipad + "_this = this;");
420 // for non top level = _this point to owner, and _this.ID is set
422 this.addLine( this.ipad + "_this = _owner;");
424 if (this.node.props.has_key("id")
426 this.node.xvala_id != ""
428 this.node.xvala_id[0] != '*'
430 this.node.xvala_id[0] != '+'
432 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
438 * Initialize this.el to point to the wrapped element.
443 void addWrappedCtor()
446 // this may need to look up properties to fill in the arguments..
447 // introspection does not workk..... - as things like gtkmessagedialog
449 if (cls == 'Gtk.Table') {
451 var methods = this.palete.getPropertiesFor(cls, 'methods');
453 print(JSON.stringify(this.palete.proplist[cls], null,4));
457 if (this.node.has("* ctor")) {
458 this.node.setLine(this.cur_line, "p", "* ctor");
459 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
463 var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
466 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
468 var iter =default_ctor.paramset.params.list_iterator();
469 while (iter.next()) {
470 var n = iter.get().name;
471 if (!this.node.has(n)) {
473 if (iter.get().type.contains("int")) {
477 if (iter.get().type.contains("float")) {
481 if (iter.get().type.contains("bool")) {
482 args += "true"; // always default to true?
485 // any other types???
490 this.ignoreWrapped(n);
493 var v = this.node.get(n);
495 if (iter.get().type == "string") {
496 v = "\"" + v.escape("") + "\"";
498 if (v == "TRUE" || v == "FALSE") {
506 this.node.setLine(this.cur_line, "p", "* xtype");
508 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
512 this.node.setLine(this.cur_line, "p", "* xtype");;
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???
567 this.addLine(this.ipad + "// set gobject values");
570 var iter = cls.props.map_iterator();
571 while (iter.next()) {
572 var p = iter.get_key();
573 print("Check Write %s\n", p);
574 if (!this.node.has(p)) {
577 if (this.shouldIgnoreWrapped(p)) {
582 var v = this.node.get(p);
584 var nodekey = this.node.get_key(p);
586 // user defined properties.
587 if (nodekey[0] == '#') {
593 var is_raw = nodekey[0] == '$';
595 // what's the type.. - if it's a string.. then we quote it..
596 if (iter.get_value().type == "string" && !is_raw) {
597 v = "\"" + v.escape("") + "\"";
599 if (v == "TRUE" || v == "FALSE") {
602 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
607 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
616 * pack the children into the parent.
618 * if the child's id starts with '*' then it is not packed...
619 * - this allows you to define children and add them manually..
625 if (this.node.items.size < 1) {
629 var iter = this.node.items.list_iterator();
631 while (iter.next()) {
636 if (ci.xvala_id[0] == '*') {
637 continue; // skip generation of children?
641 if (ci.has("* args")) {
643 var ar = ci.get("* args").split(",");
644 for (var ari = 0 ; ari < ar.length; ari++ ) {
645 var arg = ar[ari].split(" ");
646 xargs += "," + arg[arg.length -1];
649 // create the element..
650 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
651 "( _this " + xargs + ");" );
653 // this is only needed if it does not have an ID???
654 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
656 if (ci.has("* prop")) {
657 this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;");
662 // not sure why we have 'true' in pack?!?
663 if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
667 string[] packing = { "add" };
668 if (ci.has("pack")) {
669 packing = ci.get("pack").split(",");
672 var pack = packing[0];
673 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
674 (packing.length > 1 ?
675 (", " + string.joinv(",", packing).substring(pack.length+1))
681 if (ci.xvala_id[0] != '+') {
682 continue; // skip generation of children?
685 // this.{id - without the '+'} = the element...
686 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
695 if (!this.node.has("init")) {
699 this.addLine(ipad + "// init method");
701 this.node.setLine(this.cur_line, "p", "init");
703 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
708 if (this.node.listeners.size < 1) {
713 this.addLine(ipad + "//listeners");
717 var iter = this.node.listeners.map_iterator();
718 while (iter.next()) {
719 var k = iter.get_key();
720 var v = iter.get_value();
722 this.node.setLine(this.cur_line, "l", k);
723 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
724 this.padMultiline(this.ipad,v) +");");
732 this.addLine(this.pad + "}");
737 * Standardize this crap...
739 * standard properties (use to set)
740 * If they are long values show the dialog..
743 * bool is_xxx :: can show a pulldown.. (true/false)
745 * $ string html = string with value interpolated eg. baseURL + ".."
746 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
747 * $ untypedvalue = javascript untyped value...
748 * _ string html ... = translatable..
751 * object properties (not part of the GOjbect being wrapped?
752 * # Gee.ArrayList<Xcls_fileitem> fileitems
757 * methods -- always text editor..
765 * * init -- big string?
767 * event handlers (listeners)
772 * +XXXX -- indicates it's a instance property / not glob...
773 * *XXXX -- skip writing glob property (used as classes that can be created...)
778 void addUserMethods()
781 this.addLine(this.pad + "// user defined functions");
783 // user defined functions...
784 var iter = this.node.props.map_iterator();
786 var k = iter.get_key();
787 if (this.shouldIgnore(k)) {
790 // HOW TO DETERIME if its a method?
792 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
796 // function in the format of {type} (args) { .... }
797 var kk = k.substring(2);
798 var vv = iter.get_value();
799 this.node.setLine(this.cur_line, "p", k);
800 this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));;
808 this.node.line_end = this.cur_line;
809 this.node.sortLines();
812 if (this.depth > 0) {
813 this.addLine(this.inpad + "}");
816 var iter = this.node.items.list_iterator();
818 while (iter.next()) {
819 this.addMultiLine(this.mungeChild(iter.get()));
822 if (this.depth < 1) {
823 this.addLine(this.inpad + "}");
828 string padMultiline(string pad, string str)
830 var ar = str.strip().split("\n");
831 return string.joinv("\n" + pad , ar);
834 void ignore(string i) {
835 this.ignoreList.add(i);
838 void ignoreWrapped(string i) {
839 this.ignoreWrappedList.add(i);
842 bool shouldIgnore(string i)
844 return ignoreList.contains(i);
846 bool shouldIgnoreWrapped(string i)
848 return ignoreWrappedList.contains(i);