3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
10 * key value view of properties.
13 * XXXXX : YYYYY -- standard - should be rendered as XXXX : "YYYY" usually.
14 * |XXXXX : YYYYY -- standard - should be rendered as XXXX : YYYY usually.
15 * |init -- the initialization...
16 * *prop : a property which is actually an object definition...
17 * *args : contructor args
18 * .ctor : Full contruct line...
21 * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type name
22 * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23 * "|void:clearFiles": "() .... some code...." | type name
29 * Standardize this crap...
31 * standard properties (use to set)
32 * If they are long values show the dialog..
34 * bool is_xxx :: can show a pulldown.. (true/false)
36 * $ string html = string with value interpolated eg. baseURL + ".."
37 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
38 * $ untypedvalue = javascript untyped value...
40 * object properties (not part of the GOjbect being wrapped?
41 * # Gee.ArrayList<Xcls_fileitem> fileitems
46 * methods -- always text editor..
54 * * init -- big string?
56 * event handlers (listeners)
61 * +XXXX -- indicates it's a instance property / not glob...
62 * *XXXX -- skip writing glob property (used as classes that can be created...)
63 * _XXXX -- (string) a translatable string.
82 color : #00c; font-weight: bold;
92 public class JsRender.Node : Object {
95 public static int uid_count = 0;
98 public Gee.ArrayList<Node> items; // child items..
100 public Gee.HashMap<string,string> props; // the properties..
101 public Gee.HashMap<string,string> listeners; // the listeners..
102 public string xvala_cls;
103 public string xvala_xcls; // 'Xcls_' + id;
104 public string xvala_id; // item id or ""
107 public int line_start;
109 public Gee.ArrayList<int> lines;
110 public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....
111 public Gee.ArrayList<int> node_lines;
112 public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
117 this.items = new Gee.ArrayList<Node>();
118 this.props = new Gee.HashMap<string,string>();
119 this.listeners = new Gee.HashMap<string,string>();
121 this.xvala_xcls = "";
124 this.line_start = -1;
126 this.lines = new Gee.ArrayList<int>();
127 this.line_map = new Gee.HashMap<int,string>();
128 this.node_lines = new Gee.ArrayList<int>();
129 this.node_lines_map = new Gee.HashMap<int,Node>();
133 public void setNodeLine(int line, Node node) {
134 //print("Add node @ %d\n", line);
135 this.node_lines.add(line);
136 this.node_lines_map.set(line, node);
140 public void setLine(int line, string type, string prop) {
141 this.lines.add(line);
142 this.line_map.set(line, type + ":" + prop);
143 GLib.debug("setLine %d, %s", line, type + ":" + prop);
145 public void sortLines() {
146 //print("sortLines\n");
147 this.lines.sort((a,b) => {
148 return (int)b-(int)a;
150 this.node_lines.sort((a,b) => {
151 return (int)a-(int)b;
154 public Node? lineToNode(int line)
156 //print("Searching for line %d\n",line);
158 //foreach(int el in this.node_lines) {
159 //print("all lines %d\n", el);
163 foreach(int el in this.node_lines) {
164 //print("?match %d\n", el);
177 //print("RETURNING NODE ON LINE %d", l);
178 return this.node_lines_map.get(l);
184 //print("RETURNING NODE ON LINE %d", l);
185 return this.node_lines_map.get(l);
190 public string lineToProp(int line)
192 // assume lineToNode called first...
194 //foreach(int el in this.lines) {
195 // //print("all lines %d\n", el);
199 foreach(int el in this.lines) {
200 //print("?match %d\n", el);
213 //print("RETURNING NODE ON LINE %d", l);
214 return this.line_map.get(l);
220 //print("RETURNING NODE ON LINE %d", l);
221 return this.line_map.get(l);
226 void dumpProps(string indent = "")
228 print("%s:\n" + this.fqn());
229 foreach(int el in this.node_lines) {
230 print("%s %s\n", indent, this.line_map.get(el));
232 foreach(Node n in this.items) {
233 n.dumpProps(indent + " ");
241 if (this.props.get("id") == null) {
243 return "uid-%d".printf(uid_count);
245 return this.props.get("id");
249 public bool hasChildren()
251 return this.items.size > 0;
253 public bool hasXnsType()
255 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
263 if (!this.hasXnsType ()) {
266 return this.props.get("$ xns") + "." + this.props.get("xtype");
269 public void setFqn(string name)
271 var ar = name.split(".");
272 this.props.set("xtype", ar[ar.length-1]);
273 var l = name.length - (ar[ar.length-1].length +1);
274 this.props.set("$ xns", name.substring(0, l));
275 //print("setFQN %s to %s\n", name , this.fqn());
279 // wrapper around get props that returns empty string if not found.
280 public string get(string key)
282 var k = this.props.get(key);
287 k = this.props.get("$ " + key);
292 var iter = this.props.map_iterator();
293 while (iter.next()) {
294 var kk = iter.get_key().split(" ");
295 if (kk[kk.length-1] == key) {
296 return iter.get_value();
305 public string get_key(string key)
307 var k = this.props.get(key);
312 k = this.props.get("$ " + key);
317 var iter = this.props.map_iterator();
318 while (iter.next()) {
319 var kk = iter.get_key().split(" ");
320 if (kk[kk.length-1] == key) {
321 return iter.get_key();
329 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
331 // key formats : XXXX
333 // string XXX - with type
334 // $ XXX - with flag (no type)
335 // $ string XXX - with flag
337 ktype = ""; // these used to contain '-' ???
338 kflag = ""; // these used to contain '-' ???
339 var kkv = key.strip().split(" ");
341 for (var i = 0; i < kkv.length; i++) {
342 if (kkv[i].length > 0 ) {
346 //print("normalize %s => %s\n", key,string.joinv("=:=",kk));
354 if (kk[0].length > 1) {
366 // everything blank otherwise...
368 public void set(string key, string value) {
369 this.props.set(key,value);
371 public bool has(string key)
373 var k = this.props.get(key);
377 var iter = this.props.map_iterator();
378 while (iter.next()) {
379 var kk = iter.get_key().strip().split(" ");
380 if (kk[kk.length-1] == key) {
391 if (this.parent == null) {
396 var nlist = new Gee.ArrayList<Node>();
397 for (var i =0;i < this.parent.items.size; i++) {
398 if (this.parent.items.get(i) == this) {
401 nlist.add(this.parent.items.get(i));
403 this.parent.items = nlist;
408 /* creates javascript based on the rules */
409 public Node? findProp(string n) {
410 for(var i=0;i< this.items.size;i++) {
411 var p = this.items.get(i).get("* prop");
412 if (this.items.get(i).get("* prop").length < 1) {
416 return this.items.get(i);
426 static Json.Generator gen = null;
428 public string quoteString(string str)
430 if (Node.gen == null) {
431 Node.gen = new Json.Generator();
433 var n = new Json.Node(Json.NodeType.VALUE);
436 Node.gen.set_root (n);
437 return Node.gen.to_data (null);
440 public void loadFromJson(Json.Object obj, int version) {
441 obj.foreach_member((o , key, value) => {
443 if (key == "items") {
444 var ar = value.get_array();
445 ar.foreach_element( (are, ix, el) => {
446 var node = new Node();
448 node.loadFromJson(el.get_object(), version);
449 this.items.add(node);
453 if (key == "listeners") {
454 var li = value.get_object();
455 li.foreach_member((lio , li_key, li_value) => {
456 this.listeners.set(li_key, li_value.get_string());
461 var v = value.get_value();
462 var sv = Value (typeof (string));
467 rkey = this.upgradeKey(key, (string)sv);
471 this.props.set(rkey, (string)sv);
479 public string upgradeKey(string key, string val)
482 if (key.length < 1) {
490 return "* " + key.substring(1);
495 if (key[0] == '.') { // v2 does not start with '.' ?
496 var bits = key.substring(1).split(":");
497 if (bits[0] == "signal") {
498 return "@" + string.joinv(" ", bits).substring(bits[0].length);
500 return "# " + string.joinv(" ", bits);
502 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
505 var bits = key.substring(1).split(":");
506 // two types '$' or '|' << for methods..
508 if (Regex.match_simple ("^function\\s*(", val.strip())) {
509 return "| " + key.substring(1);
513 if (Regex.match_simple ("^\\(", val.strip())) {
515 return "| " + string.joinv(" ", bits);
518 // guessing it's a property..
519 return "$ " + string.joinv(" ", bits);
530 public Node deepClone()
533 n.loadFromJson(this.toJsonObject(), 2);
537 public string toJsonString()
539 if (Node.gen == null) {
540 Node.gen = new Json.Generator();
544 var n = new Json.Node(Json.NodeType.OBJECT);
545 n.set_object(this.toJsonObject () );
546 Node.gen.set_root (n);
547 return Node.gen.to_data (null);
550 public Json.Object toJsonObject()
552 var ret = new Json.Object();
555 if (this.listeners.size > 0) {
556 var li = new Json.Object();
557 ret.set_object_member("listeners", li);
558 var liter = this.listeners.map_iterator();
559 while (liter.next()) {
560 li.set_string_member(liter.get_key(), liter.get_value());
564 if (this.props.size > 0 ) {
565 var iter = this.props.map_iterator();
566 while (iter.next()) {
567 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
570 if (this.items.size > 0) {
571 var ar = new Json.Array();
572 ret.set_array_member("items", ar);
575 for(var i =0;i < this.items.size;i++) {
576 ar.add_object_element(this.items.get(i).toJsonObject());
584 public void jsonObjectsetMember(Json.Object o, string key, string val) {
585 if (Lang.isBoolean(val)) {
586 o.set_boolean_member(key, val.down() == "false" ? false : true);
591 if (Lang.isNumber(val)) {
592 if (val.contains(".")) {
593 //print( "ADD " + key + "=" + val + " as a double?\n");
594 o.set_double_member(key, double.parse (val));
598 //print( "ADD " + key + "=" + val + " as a int?\n") ;
599 o.set_int_member(key,long.parse(val));
602 ///print( "ADD " + key + "=" + val + " as a string?\n");
603 o.set_string_member(key,val);
606 public string nodeTip()
608 var ret = this.nodeTitle(true);
612 var iter = this.props.map_iterator();
613 while (iter.next()) {
614 var i = iter.get_key().strip();
615 var val = iter.get_value().strip();
616 if (val == null || val.length < 1) {
621 GLib.Markup.escape_text(i) +"</b> : " +
622 GLib.Markup.escape_text(val.split("\n")[0]);
627 //if (i == "* init") {
631 if (Regex.match_simple("^\\s*function", val)) {
633 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
634 GLib.Markup.escape_text(val.split("\n")[0]);
637 if (Regex.match_simple("^\\s*\\(", val)) {
638 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
640 GLib.Markup.escape_text(val.split("\n")[0]);
645 iter = this.listeners.map_iterator();
646 while (iter.next()) {
647 var i = iter.get_key().strip();
648 var val = iter.get_value().strip();
649 if (val == null || val.length < 1) {
652 listen += "\n\t<b>" +
653 GLib.Markup.escape_text(i) +"</b> : " +
654 GLib.Markup.escape_text(val.split("\n")[0]);
659 if (props.length > 0) {
660 ret+="\n\nProperties:" + props;
662 if (funcs.length > 0) {
663 ret+="\n\nMethods:" + funcs;
665 if (listen.length > 0) {
666 ret+="\n\nListeners:" + listen;
671 public string nodeTitle(bool for_tip = false) {
674 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
675 //if (sr) txt.push('<s>');
677 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
679 //if (renderfull && c['|xns']) {
680 var fqn = this.fqn();
681 var fqn_ar = fqn.split(".");
682 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
686 //if (c.xtype) { txt.push(c.xtype); }
688 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
689 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
690 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
693 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
694 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
695 if (this.has("html") && this.get("html").length > 0) {
696 var ht = this.get("html").split("\n");
698 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
700 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
703 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
704 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
705 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
706 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
707 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
708 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
709 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
711 // for flat classes...
712 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
713 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
716 //if (sr) txt.push('</s>');
717 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);