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);
139 public void setLine(int line, string type, string prop) {
140 this.lines.add(line);
141 this.line_map.set(line, type + ":" + prop);
143 public void sortLines() {
144 //print("sortLines\n");
145 this.lines.sort((a,b) => {
146 return (int)b-(int)a;
148 this.node_lines.sort((a,b) => {
149 return (int)a-(int)b;
152 public Node? lineToNode(int line)
154 //print("Searching for line %d\n",line);
156 foreach(int el in this.node_lines) {
157 //print("all lines %d\n", el);
161 foreach(int el in this.node_lines) {
162 //print("?match %d\n", el);
175 //print("RETURNING NODE ON LINE %d", l);
176 return this.node_lines_map.get(l);
182 //print("RETURNING NODE ON LINE %d", l);
183 return this.node_lines_map.get(l);
191 if (this.props.get("id") == null) {
193 return "uid-%d".printf(uid_count);
195 return this.props.get("id");
199 public bool hasChildren()
201 return this.items.size > 0;
203 public bool hasXnsType()
205 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
213 if (!this.hasXnsType ()) {
216 return this.props.get("$ xns") + "." + this.props.get("xtype");
219 public void setFqn(string name)
221 var ar = name.split(".");
222 this.props.set("xtype", ar[ar.length-1]);
223 var l = name.length - (ar[ar.length-1].length +1);
224 this.props.set("$ xns", name.substring(0, l));
225 //print("setFQN %s to %s\n", name , this.fqn());
229 // wrapper around get props that returns empty string if not found.
230 public string get(string key)
232 var k = this.props.get(key);
237 k = this.props.get("$ " + key);
242 var iter = this.props.map_iterator();
243 while (iter.next()) {
244 var kk = iter.get_key().split(" ");
245 if (kk[kk.length-1] == key) {
246 return iter.get_value();
255 public string get_key(string key)
257 var k = this.props.get(key);
262 k = this.props.get("$ " + key);
267 var iter = this.props.map_iterator();
268 while (iter.next()) {
269 var kk = iter.get_key().split(" ");
270 if (kk[kk.length-1] == key) {
271 return iter.get_key();
279 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
281 // key formats : XXXX
283 // string XXX - with type
284 // $ XXX - with flag (no type)
285 // $ string XXX - with flag
287 ktype = ""; // these used to contain '-' ???
288 kflag = ""; // these used to contain '-' ???
289 var kkv = key.strip().split(" ");
291 for (var i = 0; i < kkv.length; i++) {
292 if (kkv[i].length > 0 ) {
296 //print("normalize %s => %s\n", key,string.joinv("=:=",kk));
304 if (kk[0].length > 1) {
316 // everything blank otherwise...
318 public void set(string key, string value) {
319 this.props.set(key,value);
321 public bool has(string key)
323 var k = this.props.get(key);
327 var iter = this.props.map_iterator();
328 while (iter.next()) {
329 var kk = iter.get_key().strip().split(" ");
330 if (kk[kk.length-1] == key) {
341 if (this.parent == null) {
346 var nlist = new Gee.ArrayList<Node>();
347 for (var i =0;i < this.parent.items.size; i++) {
348 if (this.parent.items.get(i) == this) {
351 nlist.add(this.parent.items.get(i));
353 this.parent.items = nlist;
358 /* creates javascript based on the rules */
359 public Node? findProp(string n) {
360 for(var i=0;i< this.items.size;i++) {
361 var p = this.items.get(i).get("* prop");
362 if (this.items.get(i).get("* prop").length < 1) {
366 return this.items.get(i);
376 static Json.Generator gen = null;
378 public string quoteString(string str)
380 if (Node.gen == null) {
381 Node.gen = new Json.Generator();
383 var n = new Json.Node(Json.NodeType.VALUE);
386 Node.gen.set_root (n);
387 return Node.gen.to_data (null);
390 public void loadFromJson(Json.Object obj, int version) {
391 obj.foreach_member((o , key, value) => {
393 if (key == "items") {
394 var ar = value.get_array();
395 ar.foreach_element( (are, ix, el) => {
396 var node = new Node();
398 node.loadFromJson(el.get_object(), version);
399 this.items.add(node);
403 if (key == "listeners") {
404 var li = value.get_object();
405 li.foreach_member((lio , li_key, li_value) => {
406 this.listeners.set(li_key, li_value.get_string());
411 var v = value.get_value();
412 var sv = Value (typeof (string));
417 rkey = this.upgradeKey(key, (string)sv);
421 this.props.set(rkey, (string)sv);
429 public string upgradeKey(string key, string val)
432 if (key.length < 1) {
440 return "* " + key.substring(1);
445 if (key[0] == '.') { // v2 does not start with '.' ?
446 var bits = key.substring(1).split(":");
447 if (bits[0] == "signal") {
448 return "@" + string.joinv(" ", bits).substring(bits[0].length);
450 return "# " + string.joinv(" ", bits);
452 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
455 var bits = key.substring(1).split(":");
456 // two types '$' or '|' << for methods..
458 if (Regex.match_simple ("^function\\s*(", val.strip())) {
459 return "| " + key.substring(1);
463 if (Regex.match_simple ("^\\(", val.strip())) {
465 return "| " + string.joinv(" ", bits);
468 // guessing it's a property..
469 return "$ " + string.joinv(" ", bits);
480 public Node deepClone()
483 n.loadFromJson(this.toJsonObject(), 2);
487 public string toJsonString()
489 if (Node.gen == null) {
490 Node.gen = new Json.Generator();
494 var n = new Json.Node(Json.NodeType.OBJECT);
495 n.set_object(this.toJsonObject () );
496 Node.gen.set_root (n);
497 return Node.gen.to_data (null);
500 public Json.Object toJsonObject()
502 var ret = new Json.Object();
505 if (this.listeners.size > 0) {
506 var li = new Json.Object();
507 ret.set_object_member("listeners", li);
508 var liter = this.listeners.map_iterator();
509 while (liter.next()) {
510 li.set_string_member(liter.get_key(), liter.get_value());
514 if (this.props.size > 0 ) {
515 var iter = this.props.map_iterator();
516 while (iter.next()) {
517 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
520 if (this.items.size > 0) {
521 var ar = new Json.Array();
522 ret.set_array_member("items", ar);
525 for(var i =0;i < this.items.size;i++) {
526 ar.add_object_element(this.items.get(i).toJsonObject());
534 public void jsonObjectsetMember(Json.Object o, string key, string val) {
535 if (Lang.isBoolean(val)) {
536 o.set_boolean_member(key, val.down() == "false" ? false : true);
541 if (Lang.isNumber(val)) {
542 if (val.contains(".")) {
543 //print( "ADD " + key + "=" + val + " as a double?\n");
544 o.set_double_member(key, double.parse (val));
548 //print( "ADD " + key + "=" + val + " as a int?\n") ;
549 o.set_int_member(key,long.parse(val));
552 ///print( "ADD " + key + "=" + val + " as a string?\n");
553 o.set_string_member(key,val);
556 public string nodeTip()
558 var ret = this.nodeTitle(true);
562 var iter = this.props.map_iterator();
563 while (iter.next()) {
564 var i = iter.get_key().strip();
565 var val = iter.get_value().strip();
566 if (val == null || val.length < 1) {
571 GLib.Markup.escape_text(i) +"</b> : " +
572 GLib.Markup.escape_text(val.split("\n")[0]);
577 //if (i == "* init") {
581 if (Regex.match_simple("^\\s*function", val)) {
583 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
584 GLib.Markup.escape_text(val.split("\n")[0]);
587 if (Regex.match_simple("^\\s*\\(", val)) {
588 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
590 GLib.Markup.escape_text(val.split("\n")[0]);
595 iter = this.listeners.map_iterator();
596 while (iter.next()) {
597 var i = iter.get_key().strip();
598 var val = iter.get_value().strip();
599 if (val == null || val.length < 1) {
602 listen += "\n\t<b>" +
603 GLib.Markup.escape_text(i) +"</b> : " +
604 GLib.Markup.escape_text(val.split("\n")[0]);
609 if (props.length > 0) {
610 ret+="\n\nProperties:" + props;
612 if (funcs.length > 0) {
613 ret+="\n\nMethods:" + funcs;
615 if (listen.length > 0) {
616 ret+="\n\nListeners:" + listen;
621 public string nodeTitle(bool for_tip = false) {
624 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
625 //if (sr) txt.push('<s>');
627 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
629 //if (renderfull && c['|xns']) {
630 var fqn = this.fqn();
631 var fqn_ar = fqn.split(".");
632 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
636 //if (c.xtype) { txt.push(c.xtype); }
638 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
639 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
640 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
643 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
644 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
645 if (this.has("html") && this.get("html").length > 0) {
646 var ht = this.get("html").split("\n");
648 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
650 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
653 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
654 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
655 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
656 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
657 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
658 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
659 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
661 // for flat classes...
662 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
663 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
666 //if (sr) txt.push('</s>');
667 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);