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();
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);
135 if (this.props.get("id") == null) {
137 return "uid-%d".printf(uid_count);
139 return this.props.get("id");
143 public bool hasChildren()
145 return this.items.size > 0;
147 public bool hasXnsType()
149 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
157 if (!this.hasXnsType ()) {
160 return this.props.get("$ xns") + "." + this.props.get("xtype");
163 public void setFqn(string name)
165 var ar = name.split(".");
166 this.props.set("xtype", ar[ar.length-1]);
167 var l = name.length - (ar[ar.length-1].length +1);
168 this.props.set("$ xns", name.substring(0, l));
169 print("setFQN %s to %s\n", name , this.fqn());
173 // wrapper around get props that returns empty string if not found.
174 public string get(string key)
176 var k = this.props.get(key);
181 k = this.props.get("$ " + key);
186 var iter = this.props.map_iterator();
187 while (iter.next()) {
188 var kk = iter.get_key().split(" ");
189 if (kk[kk.length-1] == key) {
190 return iter.get_value();
199 public string get_key(string key)
201 var k = this.props.get(key);
206 k = this.props.get("$ " + key);
211 var iter = this.props.map_iterator();
212 while (iter.next()) {
213 var kk = iter.get_key().split(" ");
214 if (kk[kk.length-1] == key) {
215 return iter.get_key();
223 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
225 // key formats : XXXX
227 // string XXX - with type
228 // $ XXX - with flag (no type)
229 // $ string XXX - with flag
233 var kk = key.strip().split(" ");
240 if (kk[0].length > 1) {
252 // everything blank otherwise...
254 public void set(string key, string value) {
255 this.props.set(key,value);
257 public bool has(string key)
259 var k = this.props.get(key);
263 var iter = this.props.map_iterator();
264 while (iter.next()) {
265 var kk = iter.get_key().strip().split(" ");
266 if (kk[kk.length-1] == key) {
277 if (this.parent == null) {
282 var nlist = new Gee.ArrayList<Node>();
283 for (var i =0;i < this.parent.items.size; i++) {
284 if (this.parent.items.get(i) == this) {
287 nlist.add(this.parent.items.get(i));
289 this.parent.items = nlist;
294 /* creates javascript based on the rules */
295 public Node? findProp(string n) {
296 for(var i=0;i< this.items.size;i++) {
297 var p = this.items.get(i).get("* prop");
298 if (this.items.get(i).get("* prop").length < 1) {
302 return this.items.get(i);
312 static Json.Generator gen = null;
314 public string quoteString(string str)
316 if (Node.gen == null) {
317 Node.gen = new Json.Generator();
319 var n = new Json.Node(Json.NodeType.VALUE);
322 Node.gen.set_root (n);
323 return Node.gen.to_data (null);
326 public void loadFromJson(Json.Object obj, int version) {
327 obj.foreach_member((o , key, value) => {
329 if (key == "items") {
330 var ar = value.get_array();
331 ar.foreach_element( (are, ix, el) => {
332 var node = new Node();
334 node.loadFromJson(el.get_object(), version);
335 this.items.add(node);
339 if (key == "listeners") {
340 var li = value.get_object();
341 li.foreach_member((lio , li_key, li_value) => {
342 this.listeners.set(li_key, li_value.get_string());
347 var v = value.get_value();
348 var sv = Value (typeof (string));
353 rkey = this.upgradeKey(key, (string)sv);
357 this.props.set(rkey, (string)sv);
365 public string upgradeKey(string key, string val)
368 if (key.length < 1) {
376 return "* " + key.substring(1);
381 if (key[0] == '.') { // v2 does not start with '.' ?
382 var bits = key.substring(1).split(":");
383 if (bits[0] == "signal") {
384 return "@" + string.joinv(" ", bits).substring(bits[0].length);
386 return "# " + string.joinv(" ", bits);
388 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
391 var bits = key.substring(1).split(":");
392 // two types '$' or '|' << for methods..
394 if (Regex.match_simple ("^function\\s*(", val.strip())) {
395 return "| " + key.substring(1);
399 if (Regex.match_simple ("^\\(", val.strip())) {
401 return "| " + string.joinv(" ", bits);
404 // guessing it's a property..
405 return "$ " + string.joinv(" ", bits);
416 public Node deepClone()
419 n.loadFromJson(this.toJsonObject(), 2);
423 public string toJsonString()
425 if (Node.gen == null) {
426 Node.gen = new Json.Generator();
430 var n = new Json.Node(Json.NodeType.OBJECT);
431 n.set_object(this.toJsonObject () );
432 Node.gen.set_root (n);
433 return Node.gen.to_data (null);
436 public Json.Object toJsonObject()
438 var ret = new Json.Object();
441 if (this.listeners.size > 0) {
442 var li = new Json.Object();
443 ret.set_object_member("listeners", li);
444 var liter = this.listeners.map_iterator();
445 while (liter.next()) {
446 li.set_string_member(liter.get_key(), liter.get_value());
450 if (this.props.size > 0 ) {
451 var iter = this.props.map_iterator();
452 while (iter.next()) {
453 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
456 if (this.items.size > 0) {
457 var ar = new Json.Array();
458 ret.set_array_member("items", ar);
461 for(var i =0;i < this.items.size;i++) {
462 ar.add_object_element(this.items.get(i).toJsonObject());
470 public void jsonObjectsetMember(Json.Object o, string key, string val) {
471 if (Lang.isBoolean(val)) {
472 o.set_boolean_member(key, val.down() == "false" ? false : true);
477 if (Lang.isNumber(val)) {
478 if (val.contains(".")) {
479 //print( "ADD " + key + "=" + val + " as a double?\n");
480 o.set_double_member(key, double.parse (val));
484 //print( "ADD " + key + "=" + val + " as a int?\n") ;
485 o.set_int_member(key,long.parse(val));
488 ///print( "ADD " + key + "=" + val + " as a string?\n");
489 o.set_string_member(key,val);
492 public string nodeTip()
494 var ret = this.nodeTitle(true);
498 var iter = this.props.map_iterator();
499 while (iter.next()) {
500 var i = iter.get_key().strip();
501 var val = iter.get_value().strip();
502 if (val == null || val.length < 1) {
507 GLib.Markup.escape_text(i) +"</b> : " +
508 GLib.Markup.escape_text(val.split("\n")[0]);
513 //if (i == "* init") {
517 if (Regex.match_simple("^\\s*function", val)) {
519 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
520 GLib.Markup.escape_text(val.split("\n")[0]);
523 if (Regex.match_simple("^\\s*\\(", val)) {
524 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
526 GLib.Markup.escape_text(val.split("\n")[0]);
531 iter = this.listeners.map_iterator();
532 while (iter.next()) {
533 var i = iter.get_key().strip();
534 var val = iter.get_value().strip();
535 if (val == null || val.length < 1) {
538 listen += "\n\t<b>" +
539 GLib.Markup.escape_text(i) +"</b> : " +
540 GLib.Markup.escape_text(val.split("\n")[0]);
545 if (props.length > 0) {
546 ret+="\n\nProperties:" + props;
548 if (funcs.length > 0) {
549 ret+="\n\nMethods:" + funcs;
551 if (listen.length > 0) {
552 ret+="\n\nListeners:" + listen;
557 public string nodeTitle(bool for_tip = false) {
560 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
561 //if (sr) txt.push('<s>');
563 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
565 //if (renderfull && c['|xns']) {
566 var fqn = this.fqn();
567 var fqn_ar = fqn.split(".");
568 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
572 //if (c.xtype) { txt.push(c.xtype); }
574 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
575 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
576 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
579 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
580 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
581 if (this.has("html") && this.get("html").length > 0) {
582 var ht = this.get("html").split("\n");
584 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
586 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
589 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
590 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
591 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
592 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
593 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
594 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
595 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
597 // for flat classes...
598 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
599 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
602 //if (sr) txt.push('</s>');
603 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);