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 @ %s\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 this.lines.sort((a,b) => {
145 return (int)b-(int)a;
147 this.node_lines.sort((a,b) => {
148 return (int)b-(int)a;
151 public Node? lineToNode(int line)
153 print("Searching for line %d\n",line);
155 foreach(int el in this.node_lines) {
156 print("?match %d\n", el);
168 print("RETURNING NODE ON LINE %d", l);
169 return this.node_lines_map.get(l);
175 print("RETURNING NODE ON LINE %d", l);
176 return this.node_lines_map.get(l);
184 if (this.props.get("id") == null) {
186 return "uid-%d".printf(uid_count);
188 return this.props.get("id");
192 public bool hasChildren()
194 return this.items.size > 0;
196 public bool hasXnsType()
198 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
206 if (!this.hasXnsType ()) {
209 return this.props.get("$ xns") + "." + this.props.get("xtype");
212 public void setFqn(string name)
214 var ar = name.split(".");
215 this.props.set("xtype", ar[ar.length-1]);
216 var l = name.length - (ar[ar.length-1].length +1);
217 this.props.set("$ xns", name.substring(0, l));
218 print("setFQN %s to %s\n", name , this.fqn());
222 // wrapper around get props that returns empty string if not found.
223 public string get(string key)
225 var k = this.props.get(key);
230 k = this.props.get("$ " + key);
235 var iter = this.props.map_iterator();
236 while (iter.next()) {
237 var kk = iter.get_key().split(" ");
238 if (kk[kk.length-1] == key) {
239 return iter.get_value();
248 public string get_key(string key)
250 var k = this.props.get(key);
255 k = this.props.get("$ " + key);
260 var iter = this.props.map_iterator();
261 while (iter.next()) {
262 var kk = iter.get_key().split(" ");
263 if (kk[kk.length-1] == key) {
264 return iter.get_key();
272 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
274 // key formats : XXXX
276 // string XXX - with type
277 // $ XXX - with flag (no type)
278 // $ string XXX - with flag
280 ktype = ""; // these used to contain '-' ???
281 kflag = ""; // these used to contain '-' ???
282 var kkv = key.strip().split(" ");
284 for (var i = 0; i < kkv.length; i++) {
285 if (kkv[i].length > 0 ) {
289 print("normalize %s => %s\n", key,string.joinv("=:=",kk));
297 if (kk[0].length > 1) {
309 // everything blank otherwise...
311 public void set(string key, string value) {
312 this.props.set(key,value);
314 public bool has(string key)
316 var k = this.props.get(key);
320 var iter = this.props.map_iterator();
321 while (iter.next()) {
322 var kk = iter.get_key().strip().split(" ");
323 if (kk[kk.length-1] == key) {
334 if (this.parent == null) {
339 var nlist = new Gee.ArrayList<Node>();
340 for (var i =0;i < this.parent.items.size; i++) {
341 if (this.parent.items.get(i) == this) {
344 nlist.add(this.parent.items.get(i));
346 this.parent.items = nlist;
351 /* creates javascript based on the rules */
352 public Node? findProp(string n) {
353 for(var i=0;i< this.items.size;i++) {
354 var p = this.items.get(i).get("* prop");
355 if (this.items.get(i).get("* prop").length < 1) {
359 return this.items.get(i);
369 static Json.Generator gen = null;
371 public string quoteString(string str)
373 if (Node.gen == null) {
374 Node.gen = new Json.Generator();
376 var n = new Json.Node(Json.NodeType.VALUE);
379 Node.gen.set_root (n);
380 return Node.gen.to_data (null);
383 public void loadFromJson(Json.Object obj, int version) {
384 obj.foreach_member((o , key, value) => {
386 if (key == "items") {
387 var ar = value.get_array();
388 ar.foreach_element( (are, ix, el) => {
389 var node = new Node();
391 node.loadFromJson(el.get_object(), version);
392 this.items.add(node);
396 if (key == "listeners") {
397 var li = value.get_object();
398 li.foreach_member((lio , li_key, li_value) => {
399 this.listeners.set(li_key, li_value.get_string());
404 var v = value.get_value();
405 var sv = Value (typeof (string));
410 rkey = this.upgradeKey(key, (string)sv);
414 this.props.set(rkey, (string)sv);
422 public string upgradeKey(string key, string val)
425 if (key.length < 1) {
433 return "* " + key.substring(1);
438 if (key[0] == '.') { // v2 does not start with '.' ?
439 var bits = key.substring(1).split(":");
440 if (bits[0] == "signal") {
441 return "@" + string.joinv(" ", bits).substring(bits[0].length);
443 return "# " + string.joinv(" ", bits);
445 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
448 var bits = key.substring(1).split(":");
449 // two types '$' or '|' << for methods..
451 if (Regex.match_simple ("^function\\s*(", val.strip())) {
452 return "| " + key.substring(1);
456 if (Regex.match_simple ("^\\(", val.strip())) {
458 return "| " + string.joinv(" ", bits);
461 // guessing it's a property..
462 return "$ " + string.joinv(" ", bits);
473 public Node deepClone()
476 n.loadFromJson(this.toJsonObject(), 2);
480 public string toJsonString()
482 if (Node.gen == null) {
483 Node.gen = new Json.Generator();
487 var n = new Json.Node(Json.NodeType.OBJECT);
488 n.set_object(this.toJsonObject () );
489 Node.gen.set_root (n);
490 return Node.gen.to_data (null);
493 public Json.Object toJsonObject()
495 var ret = new Json.Object();
498 if (this.listeners.size > 0) {
499 var li = new Json.Object();
500 ret.set_object_member("listeners", li);
501 var liter = this.listeners.map_iterator();
502 while (liter.next()) {
503 li.set_string_member(liter.get_key(), liter.get_value());
507 if (this.props.size > 0 ) {
508 var iter = this.props.map_iterator();
509 while (iter.next()) {
510 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
513 if (this.items.size > 0) {
514 var ar = new Json.Array();
515 ret.set_array_member("items", ar);
518 for(var i =0;i < this.items.size;i++) {
519 ar.add_object_element(this.items.get(i).toJsonObject());
527 public void jsonObjectsetMember(Json.Object o, string key, string val) {
528 if (Lang.isBoolean(val)) {
529 o.set_boolean_member(key, val.down() == "false" ? false : true);
534 if (Lang.isNumber(val)) {
535 if (val.contains(".")) {
536 //print( "ADD " + key + "=" + val + " as a double?\n");
537 o.set_double_member(key, double.parse (val));
541 //print( "ADD " + key + "=" + val + " as a int?\n") ;
542 o.set_int_member(key,long.parse(val));
545 ///print( "ADD " + key + "=" + val + " as a string?\n");
546 o.set_string_member(key,val);
549 public string nodeTip()
551 var ret = this.nodeTitle(true);
555 var iter = this.props.map_iterator();
556 while (iter.next()) {
557 var i = iter.get_key().strip();
558 var val = iter.get_value().strip();
559 if (val == null || val.length < 1) {
564 GLib.Markup.escape_text(i) +"</b> : " +
565 GLib.Markup.escape_text(val.split("\n")[0]);
570 //if (i == "* init") {
574 if (Regex.match_simple("^\\s*function", val)) {
576 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
577 GLib.Markup.escape_text(val.split("\n")[0]);
580 if (Regex.match_simple("^\\s*\\(", val)) {
581 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
583 GLib.Markup.escape_text(val.split("\n")[0]);
588 iter = this.listeners.map_iterator();
589 while (iter.next()) {
590 var i = iter.get_key().strip();
591 var val = iter.get_value().strip();
592 if (val == null || val.length < 1) {
595 listen += "\n\t<b>" +
596 GLib.Markup.escape_text(i) +"</b> : " +
597 GLib.Markup.escape_text(val.split("\n")[0]);
602 if (props.length > 0) {
603 ret+="\n\nProperties:" + props;
605 if (funcs.length > 0) {
606 ret+="\n\nMethods:" + funcs;
608 if (listen.length > 0) {
609 ret+="\n\nListeners:" + listen;
614 public string nodeTitle(bool for_tip = false) {
617 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
618 //if (sr) txt.push('<s>');
620 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
622 //if (renderfull && c['|xns']) {
623 var fqn = this.fqn();
624 var fqn_ar = fqn.split(".");
625 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
629 //if (c.xtype) { txt.push(c.xtype); }
631 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
632 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
633 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
636 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
637 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
638 if (this.has("html") && this.get("html").length > 0) {
639 var ht = this.get("html").split("\n");
641 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
643 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
646 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
647 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
648 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
649 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
650 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
651 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
652 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
654 // for flat classes...
655 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
656 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
659 //if (sr) txt.push('</s>');
660 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);