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 ""
105 public int line_start;
107 public Gee.ArrayList<int> lines;
108 public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....
109 public Gee.ArrayList<int> node_lines;
110 public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
115 this.items = new Gee.ArrayList<Node>();
116 this.props = new Gee.HashMap<string,string>();
117 this.listeners = new Gee.HashMap<string,string>();
119 this.xvala_xcls = "";
122 this.line_start = -1;
124 this.lines = new Gee.ArrayList<int>();
125 this.line_map = new Gee.HashMap<int,string>();
126 this.node_lines = new Gee.ArrayList<int>();
127 this.node_lines_map = new Gee.HashMap<int,Node>();
131 public void setNodeLine(int line, Node node) {
132 this.node_lines.add(line);
133 this.node_lines_map.set(line, node);
136 public void setLine(int line, string type, string prop) {
137 this.lines.add(line);
138 this.line_map.set(line, type +":" + prop);
140 public void sortLines() {
141 this.lines.sort((a,b) => {
142 return (int)b-(int)a;
144 this.node_lines.sort((a,b) => {
145 return (int)b-(int)a;
148 public Node? lineToNode(int line)
150 print("Searching for line %d\n",line);
152 foreach(int el in this.node_lines) {
153 print("?match %d\n", el);
165 print("RETURNING NODE ON LINE %d", l);
166 return this.node_lines_map.get(l);
172 print("RETURNING NODE ON LINE %d", l);
173 return this.node_lines_map.get(l);
181 if (this.props.get("id") == null) {
183 return "uid-%d".printf(uid_count);
185 return this.props.get("id");
189 public bool hasChildren()
191 return this.items.size > 0;
193 public bool hasXnsType()
195 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
203 if (!this.hasXnsType ()) {
206 return this.props.get("$ xns") + "." + this.props.get("xtype");
209 public void setFqn(string name)
211 var ar = name.split(".");
212 this.props.set("xtype", ar[ar.length-1]);
213 var l = name.length - (ar[ar.length-1].length +1);
214 this.props.set("$ xns", name.substring(0, l));
215 print("setFQN %s to %s\n", name , this.fqn());
219 // wrapper around get props that returns empty string if not found.
220 public string get(string key)
222 var k = this.props.get(key);
227 k = this.props.get("$ " + key);
232 var iter = this.props.map_iterator();
233 while (iter.next()) {
234 var kk = iter.get_key().split(" ");
235 if (kk[kk.length-1] == key) {
236 return iter.get_value();
245 public string get_key(string key)
247 var k = this.props.get(key);
252 k = this.props.get("$ " + key);
257 var iter = this.props.map_iterator();
258 while (iter.next()) {
259 var kk = iter.get_key().split(" ");
260 if (kk[kk.length-1] == key) {
261 return iter.get_key();
269 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
271 // key formats : XXXX
273 // string XXX - with type
274 // $ XXX - with flag (no type)
275 // $ string XXX - with flag
279 var kkv = key.strip().split(" ");
281 for (var i = 0; i < kkv.length; i++) {
282 if (kkv[i].length > 0 ) {
292 if (kk[0].length > 1) {
304 // everything blank otherwise...
306 public void set(string key, string value) {
307 this.props.set(key,value);
309 public bool has(string key)
311 var k = this.props.get(key);
315 var iter = this.props.map_iterator();
316 while (iter.next()) {
317 var kk = iter.get_key().strip().split(" ");
318 if (kk[kk.length-1] == key) {
329 if (this.parent == null) {
334 var nlist = new Gee.ArrayList<Node>();
335 for (var i =0;i < this.parent.items.size; i++) {
336 if (this.parent.items.get(i) == this) {
339 nlist.add(this.parent.items.get(i));
341 this.parent.items = nlist;
346 /* creates javascript based on the rules */
347 public Node? findProp(string n) {
348 for(var i=0;i< this.items.size;i++) {
349 var p = this.items.get(i).get("* prop");
350 if (this.items.get(i).get("* prop").length < 1) {
354 return this.items.get(i);
364 static Json.Generator gen = null;
366 public string quoteString(string str)
368 if (Node.gen == null) {
369 Node.gen = new Json.Generator();
371 var n = new Json.Node(Json.NodeType.VALUE);
374 Node.gen.set_root (n);
375 return Node.gen.to_data (null);
378 public void loadFromJson(Json.Object obj, int version) {
379 obj.foreach_member((o , key, value) => {
381 if (key == "items") {
382 var ar = value.get_array();
383 ar.foreach_element( (are, ix, el) => {
384 var node = new Node();
386 node.loadFromJson(el.get_object(), version);
387 this.items.add(node);
391 if (key == "listeners") {
392 var li = value.get_object();
393 li.foreach_member((lio , li_key, li_value) => {
394 this.listeners.set(li_key, li_value.get_string());
399 var v = value.get_value();
400 var sv = Value (typeof (string));
405 rkey = this.upgradeKey(key, (string)sv);
409 this.props.set(rkey, (string)sv);
417 public string upgradeKey(string key, string val)
420 if (key.length < 1) {
428 return "* " + key.substring(1);
433 if (key[0] == '.') { // v2 does not start with '.' ?
434 var bits = key.substring(1).split(":");
435 if (bits[0] == "signal") {
436 return "@" + string.joinv(" ", bits).substring(bits[0].length);
438 return "# " + string.joinv(" ", bits);
440 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
443 var bits = key.substring(1).split(":");
444 // two types '$' or '|' << for methods..
446 if (Regex.match_simple ("^function\\s*(", val.strip())) {
447 return "| " + key.substring(1);
451 if (Regex.match_simple ("^\\(", val.strip())) {
453 return "| " + string.joinv(" ", bits);
456 // guessing it's a property..
457 return "$ " + string.joinv(" ", bits);
468 public Node deepClone()
471 n.loadFromJson(this.toJsonObject(), 2);
475 public string toJsonString()
477 if (Node.gen == null) {
478 Node.gen = new Json.Generator();
482 var n = new Json.Node(Json.NodeType.OBJECT);
483 n.set_object(this.toJsonObject () );
484 Node.gen.set_root (n);
485 return Node.gen.to_data (null);
488 public Json.Object toJsonObject()
490 var ret = new Json.Object();
493 if (this.listeners.size > 0) {
494 var li = new Json.Object();
495 ret.set_object_member("listeners", li);
496 var liter = this.listeners.map_iterator();
497 while (liter.next()) {
498 li.set_string_member(liter.get_key(), liter.get_value());
502 if (this.props.size > 0 ) {
503 var iter = this.props.map_iterator();
504 while (iter.next()) {
505 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
508 if (this.items.size > 0) {
509 var ar = new Json.Array();
510 ret.set_array_member("items", ar);
513 for(var i =0;i < this.items.size;i++) {
514 ar.add_object_element(this.items.get(i).toJsonObject());
522 public void jsonObjectsetMember(Json.Object o, string key, string val) {
523 if (Lang.isBoolean(val)) {
524 o.set_boolean_member(key, val.down() == "false" ? false : true);
529 if (Lang.isNumber(val)) {
530 if (val.contains(".")) {
531 //print( "ADD " + key + "=" + val + " as a double?\n");
532 o.set_double_member(key, double.parse (val));
536 //print( "ADD " + key + "=" + val + " as a int?\n") ;
537 o.set_int_member(key,long.parse(val));
540 ///print( "ADD " + key + "=" + val + " as a string?\n");
541 o.set_string_member(key,val);
544 public string nodeTip()
546 var ret = this.nodeTitle(true);
550 var iter = this.props.map_iterator();
551 while (iter.next()) {
552 var i = iter.get_key().strip();
553 var val = iter.get_value().strip();
554 if (val == null || val.length < 1) {
559 GLib.Markup.escape_text(i) +"</b> : " +
560 GLib.Markup.escape_text(val.split("\n")[0]);
565 //if (i == "* init") {
569 if (Regex.match_simple("^\\s*function", val)) {
571 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
572 GLib.Markup.escape_text(val.split("\n")[0]);
575 if (Regex.match_simple("^\\s*\\(", val)) {
576 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
578 GLib.Markup.escape_text(val.split("\n")[0]);
583 iter = this.listeners.map_iterator();
584 while (iter.next()) {
585 var i = iter.get_key().strip();
586 var val = iter.get_value().strip();
587 if (val == null || val.length < 1) {
590 listen += "\n\t<b>" +
591 GLib.Markup.escape_text(i) +"</b> : " +
592 GLib.Markup.escape_text(val.split("\n")[0]);
597 if (props.length > 0) {
598 ret+="\n\nProperties:" + props;
600 if (funcs.length > 0) {
601 ret+="\n\nMethods:" + funcs;
603 if (listen.length > 0) {
604 ret+="\n\nListeners:" + listen;
609 public string nodeTitle(bool for_tip = false) {
612 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
613 //if (sr) txt.push('<s>');
615 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
617 //if (renderfull && c['|xns']) {
618 var fqn = this.fqn();
619 var fqn_ar = fqn.split(".");
620 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
624 //if (c.xtype) { txt.push(c.xtype); }
626 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
627 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
628 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
631 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
632 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
633 if (this.has("html") && this.get("html").length > 0) {
634 var ht = this.get("html").split("\n");
636 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
638 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
641 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
642 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
643 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
644 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
645 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
646 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
647 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
649 // for flat classes...
650 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
651 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
654 //if (sr) txt.push('</s>');
655 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);