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:....
113 this.items = new Gee.ArrayList<Node>();
114 this.props = new Gee.HashMap<string,string>();
115 this.listeners = new Gee.HashMap<string,string>();
117 this.xvala_xcls = "";
120 this.line_start = -1;
122 this.lines = new Gee.ArrayList<int>();
123 this.line_map = new Gee.HashMap<int,string>();
127 public void setLine(int line, string type, string prop) {
128 this.lines.add(line);
129 this.line_map.set(line, type +":" + prop);
131 public void sortLines() {
132 this.lines.sort((a,b) => {
133 return (int)a-(int)b;
137 return a < b ? -1 : 1;
144 if (this.props.get("id") == null) {
146 return "uid-%d".printf(uid_count);
148 return this.props.get("id");
152 public bool hasChildren()
154 return this.items.size > 0;
156 public bool hasXnsType()
158 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
166 if (!this.hasXnsType ()) {
169 return this.props.get("$ xns") + "." + this.props.get("xtype");
172 public void setFqn(string name)
174 var ar = name.split(".");
175 this.props.set("xtype", ar[ar.length-1]);
176 var l = name.length - (ar[ar.length-1].length +1);
177 this.props.set("$ xns", name.substring(0, l));
178 print("setFQN %s to %s\n", name , this.fqn());
182 // wrapper around get props that returns empty string if not found.
183 public string get(string key)
185 var k = this.props.get(key);
190 k = this.props.get("$ " + key);
195 var iter = this.props.map_iterator();
196 while (iter.next()) {
197 var kk = iter.get_key().split(" ");
198 if (kk[kk.length-1] == key) {
199 return iter.get_value();
208 public string get_key(string key)
210 var k = this.props.get(key);
215 k = this.props.get("$ " + key);
220 var iter = this.props.map_iterator();
221 while (iter.next()) {
222 var kk = iter.get_key().split(" ");
223 if (kk[kk.length-1] == key) {
224 return iter.get_key();
232 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
234 // key formats : XXXX
236 // string XXX - with type
237 // $ XXX - with flag (no type)
238 // $ string XXX - with flag
242 var kk = key.strip().split(" ");
249 if (kk[0].length > 1) {
261 // everything blank otherwise...
263 public void set(string key, string value) {
264 this.props.set(key,value);
266 public bool has(string key)
268 var k = this.props.get(key);
272 var iter = this.props.map_iterator();
273 while (iter.next()) {
274 var kk = iter.get_key().strip().split(" ");
275 if (kk[kk.length-1] == key) {
286 if (this.parent == null) {
291 var nlist = new Gee.ArrayList<Node>();
292 for (var i =0;i < this.parent.items.size; i++) {
293 if (this.parent.items.get(i) == this) {
296 nlist.add(this.parent.items.get(i));
298 this.parent.items = nlist;
303 /* creates javascript based on the rules */
304 public Node? findProp(string n) {
305 for(var i=0;i< this.items.size;i++) {
306 var p = this.items.get(i).get("* prop");
307 if (this.items.get(i).get("* prop").length < 1) {
311 return this.items.get(i);
321 static Json.Generator gen = null;
323 public string quoteString(string str)
325 if (Node.gen == null) {
326 Node.gen = new Json.Generator();
328 var n = new Json.Node(Json.NodeType.VALUE);
331 Node.gen.set_root (n);
332 return Node.gen.to_data (null);
335 public void loadFromJson(Json.Object obj, int version) {
336 obj.foreach_member((o , key, value) => {
338 if (key == "items") {
339 var ar = value.get_array();
340 ar.foreach_element( (are, ix, el) => {
341 var node = new Node();
343 node.loadFromJson(el.get_object(), version);
344 this.items.add(node);
348 if (key == "listeners") {
349 var li = value.get_object();
350 li.foreach_member((lio , li_key, li_value) => {
351 this.listeners.set(li_key, li_value.get_string());
356 var v = value.get_value();
357 var sv = Value (typeof (string));
362 rkey = this.upgradeKey(key, (string)sv);
366 this.props.set(rkey, (string)sv);
374 public string upgradeKey(string key, string val)
377 if (key.length < 1) {
385 return "* " + key.substring(1);
390 if (key[0] == '.') { // v2 does not start with '.' ?
391 var bits = key.substring(1).split(":");
392 if (bits[0] == "signal") {
393 return "@" + string.joinv(" ", bits).substring(bits[0].length);
395 return "# " + string.joinv(" ", bits);
397 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
400 var bits = key.substring(1).split(":");
401 // two types '$' or '|' << for methods..
403 if (Regex.match_simple ("^function\\s*(", val.strip())) {
404 return "| " + key.substring(1);
408 if (Regex.match_simple ("^\\(", val.strip())) {
410 return "| " + string.joinv(" ", bits);
413 // guessing it's a property..
414 return "$ " + string.joinv(" ", bits);
425 public Node deepClone()
428 n.loadFromJson(this.toJsonObject(), 2);
432 public string toJsonString()
434 if (Node.gen == null) {
435 Node.gen = new Json.Generator();
439 var n = new Json.Node(Json.NodeType.OBJECT);
440 n.set_object(this.toJsonObject () );
441 Node.gen.set_root (n);
442 return Node.gen.to_data (null);
445 public Json.Object toJsonObject()
447 var ret = new Json.Object();
450 if (this.listeners.size > 0) {
451 var li = new Json.Object();
452 ret.set_object_member("listeners", li);
453 var liter = this.listeners.map_iterator();
454 while (liter.next()) {
455 li.set_string_member(liter.get_key(), liter.get_value());
459 if (this.props.size > 0 ) {
460 var iter = this.props.map_iterator();
461 while (iter.next()) {
462 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
465 if (this.items.size > 0) {
466 var ar = new Json.Array();
467 ret.set_array_member("items", ar);
470 for(var i =0;i < this.items.size;i++) {
471 ar.add_object_element(this.items.get(i).toJsonObject());
479 public void jsonObjectsetMember(Json.Object o, string key, string val) {
480 if (Lang.isBoolean(val)) {
481 o.set_boolean_member(key, val.down() == "false" ? false : true);
486 if (Lang.isNumber(val)) {
487 if (val.contains(".")) {
488 //print( "ADD " + key + "=" + val + " as a double?\n");
489 o.set_double_member(key, double.parse (val));
493 //print( "ADD " + key + "=" + val + " as a int?\n") ;
494 o.set_int_member(key,long.parse(val));
497 ///print( "ADD " + key + "=" + val + " as a string?\n");
498 o.set_string_member(key,val);
501 public string nodeTip()
503 var ret = this.nodeTitle(true);
507 var iter = this.props.map_iterator();
508 while (iter.next()) {
509 var i = iter.get_key().strip();
510 var val = iter.get_value().strip();
511 if (val == null || val.length < 1) {
516 GLib.Markup.escape_text(i) +"</b> : " +
517 GLib.Markup.escape_text(val.split("\n")[0]);
522 //if (i == "* init") {
526 if (Regex.match_simple("^\\s*function", val)) {
528 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
529 GLib.Markup.escape_text(val.split("\n")[0]);
532 if (Regex.match_simple("^\\s*\\(", val)) {
533 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
535 GLib.Markup.escape_text(val.split("\n")[0]);
540 iter = this.listeners.map_iterator();
541 while (iter.next()) {
542 var i = iter.get_key().strip();
543 var val = iter.get_value().strip();
544 if (val == null || val.length < 1) {
547 listen += "\n\t<b>" +
548 GLib.Markup.escape_text(i) +"</b> : " +
549 GLib.Markup.escape_text(val.split("\n")[0]);
554 if (props.length > 0) {
555 ret+="\n\nProperties:" + props;
557 if (funcs.length > 0) {
558 ret+="\n\nMethods:" + funcs;
560 if (listen.length > 0) {
561 ret+="\n\nListeners:" + listen;
566 public string nodeTitle(bool for_tip = false) {
569 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
570 //if (sr) txt.push('<s>');
572 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
574 //if (renderfull && c['|xns']) {
575 var fqn = this.fqn();
576 var fqn_ar = fqn.split(".");
577 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
581 //if (c.xtype) { txt.push(c.xtype); }
583 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
584 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
585 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
588 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
589 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
590 if (this.has("html") && this.get("html").length > 0) {
591 var ht = this.get("html").split("\n");
593 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
595 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
598 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
599 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
600 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
601 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
602 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
603 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
604 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
606 // for flat classes...
607 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
608 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
611 //if (sr) txt.push('</s>');
612 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);