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) {
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.proplines.set(k, this.cur_line);
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] != "#") {
340 this.node.proplines.set(k, this.cur_line);
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?
364 this.node.proplines.set(k, this.cur_line);
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.proplines.set("* ctor", this.curline);
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.proplines.set("* xtype", this.curline);
506 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
510 this.node.proplines.set("* xtype", this.curline);
512 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
519 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
520 //print(JSON.stringify(meths,null,4));Seed.quit();
524 // initialize.. my vars..
526 this.addLine( this.ipad + "// my vars (dec)");
528 var iter = this.myvars.list_iterator();
533 var ar = k.strip().split(" ");
534 var kname = ar[ar.length-1];
536 var v = this.node.props.get(k);
541 if (v == "FALSE" || v == "TRUE") {
544 //FIXME -- check for raw string.. "string XXXX"
546 // if it's a string...
549 this.addLine(this.ipad + "this." + kname + " = " + v +";");
557 void addWrappedProperties()
559 var cls = Palete.Gir.factoryFqn(this.node.fqn());
563 // what are the properties of this class???
565 this.addLine(this.ipad + "// set gobject values");
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.addLine("%sthis.el.%s = %s;".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.addLine(this.inpad + "}");
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);