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 ""
106 public Gee.HashMap<string,int> proplines;
107 public Gee.HashMap<string,int> listenlines;
112 this.items = new Gee.ArrayList<Node>();
113 this.props = new Gee.HashMap<string,string>();
114 this.listeners = new Gee.HashMap<string,string>();
116 this.xvala_xcls = "";
120 this.proplines = new Gee.HashMap<string,int>();
121 this.listenlines = Gee.HashMap<string,int>();
128 if (this.props.get("id") == null) {
130 return "uid-%d".printf(uid_count);
132 return this.props.get("id");
136 public bool hasChildren()
138 return this.items.size > 0;
140 public bool hasXnsType()
142 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
150 if (!this.hasXnsType ()) {
153 return this.props.get("$ xns") + "." + this.props.get("xtype");
156 public void setFqn(string name)
158 var ar = name.split(".");
159 this.props.set("xtype", ar[ar.length-1]);
160 var l = name.length - (ar[ar.length-1].length +1);
161 this.props.set("$ xns", name.substring(0, l));
162 print("setFQN %s to %s\n", name , this.fqn());
166 // wrapper around get props that returns empty string if not found.
167 public string get(string key)
169 var k = this.props.get(key);
174 k = this.props.get("$ " + key);
179 var iter = this.props.map_iterator();
180 while (iter.next()) {
181 var kk = iter.get_key().split(" ");
182 if (kk[kk.length-1] == key) {
183 return iter.get_value();
192 public string get_key(string key)
194 var k = this.props.get(key);
199 k = this.props.get("$ " + key);
204 var iter = this.props.map_iterator();
205 while (iter.next()) {
206 var kk = iter.get_key().split(" ");
207 if (kk[kk.length-1] == key) {
208 return iter.get_key();
216 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
218 // key formats : XXXX
220 // string XXX - with type
221 // $ XXX - with flag (no type)
222 // $ string XXX - with flag
226 var kk = key.strip().split(" ");
233 if (kk[0].length > 1) {
245 // everything blank otherwise...
247 public void set(string key, string value) {
248 this.props.set(key,value);
250 public bool has(string key)
252 var k = this.props.get(key);
256 var iter = this.props.map_iterator();
257 while (iter.next()) {
258 var kk = iter.get_key().strip().split(" ");
259 if (kk[kk.length-1] == key) {
270 if (this.parent == null) {
275 var nlist = new Gee.ArrayList<Node>();
276 for (var i =0;i < this.parent.items.size; i++) {
277 if (this.parent.items.get(i) == this) {
280 nlist.add(this.parent.items.get(i));
282 this.parent.items = nlist;
287 /* creates javascript based on the rules */
288 public Node? findProp(string n) {
289 for(var i=0;i< this.items.size;i++) {
290 var p = this.items.get(i).get("* prop");
291 if (this.items.get(i).get("* prop").length < 1) {
295 return this.items.get(i);
305 static Json.Generator gen = null;
307 public string quoteString(string str)
309 if (Node.gen == null) {
310 Node.gen = new Json.Generator();
312 var n = new Json.Node(Json.NodeType.VALUE);
315 Node.gen.set_root (n);
316 return Node.gen.to_data (null);
319 public void loadFromJson(Json.Object obj, int version) {
320 obj.foreach_member((o , key, value) => {
322 if (key == "items") {
323 var ar = value.get_array();
324 ar.foreach_element( (are, ix, el) => {
325 var node = new Node();
327 node.loadFromJson(el.get_object(), version);
328 this.items.add(node);
332 if (key == "listeners") {
333 var li = value.get_object();
334 li.foreach_member((lio , li_key, li_value) => {
335 this.listeners.set(li_key, li_value.get_string());
340 var v = value.get_value();
341 var sv = Value (typeof (string));
346 rkey = this.upgradeKey(key, (string)sv);
350 this.props.set(rkey, (string)sv);
358 public string upgradeKey(string key, string val)
361 if (key.length < 1) {
369 return "* " + key.substring(1);
374 if (key[0] == '.') { // v2 does not start with '.' ?
375 var bits = key.substring(1).split(":");
376 if (bits[0] == "signal") {
377 return "@" + string.joinv(" ", bits).substring(bits[0].length);
379 return "# " + string.joinv(" ", bits);
381 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
384 var bits = key.substring(1).split(":");
385 // two types '$' or '|' << for methods..
387 if (Regex.match_simple ("^function\\s*(", val.strip())) {
388 return "| " + key.substring(1);
392 if (Regex.match_simple ("^\\(", val.strip())) {
394 return "| " + string.joinv(" ", bits);
397 // guessing it's a property..
398 return "$ " + string.joinv(" ", bits);
409 public Node deepClone()
412 n.loadFromJson(this.toJsonObject(), 2);
416 public string toJsonString()
418 if (Node.gen == null) {
419 Node.gen = new Json.Generator();
423 var n = new Json.Node(Json.NodeType.OBJECT);
424 n.set_object(this.toJsonObject () );
425 Node.gen.set_root (n);
426 return Node.gen.to_data (null);
429 public Json.Object toJsonObject()
431 var ret = new Json.Object();
434 if (this.listeners.size > 0) {
435 var li = new Json.Object();
436 ret.set_object_member("listeners", li);
437 var liter = this.listeners.map_iterator();
438 while (liter.next()) {
439 li.set_string_member(liter.get_key(), liter.get_value());
443 if (this.props.size > 0 ) {
444 var iter = this.props.map_iterator();
445 while (iter.next()) {
446 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
449 if (this.items.size > 0) {
450 var ar = new Json.Array();
451 ret.set_array_member("items", ar);
454 for(var i =0;i < this.items.size;i++) {
455 ar.add_object_element(this.items.get(i).toJsonObject());
463 public void jsonObjectsetMember(Json.Object o, string key, string val) {
464 if (Lang.isBoolean(val)) {
465 o.set_boolean_member(key, val.down() == "false" ? false : true);
470 if (Lang.isNumber(val)) {
471 if (val.contains(".")) {
472 //print( "ADD " + key + "=" + val + " as a double?\n");
473 o.set_double_member(key, double.parse (val));
477 //print( "ADD " + key + "=" + val + " as a int?\n") ;
478 o.set_int_member(key,long.parse(val));
481 ///print( "ADD " + key + "=" + val + " as a string?\n");
482 o.set_string_member(key,val);
485 public string nodeTip()
487 var ret = this.nodeTitle(true);
491 var iter = this.props.map_iterator();
492 while (iter.next()) {
493 var i = iter.get_key().strip();
494 var val = iter.get_value().strip();
495 if (val == null || val.length < 1) {
500 GLib.Markup.escape_text(i) +"</b> : " +
501 GLib.Markup.escape_text(val.split("\n")[0]);
506 //if (i == "* init") {
510 if (Regex.match_simple("^\\s*function", val)) {
512 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
513 GLib.Markup.escape_text(val.split("\n")[0]);
516 if (Regex.match_simple("^\\s*\\(", val)) {
517 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
519 GLib.Markup.escape_text(val.split("\n")[0]);
524 iter = this.listeners.map_iterator();
525 while (iter.next()) {
526 var i = iter.get_key().strip();
527 var val = iter.get_value().strip();
528 if (val == null || val.length < 1) {
531 listen += "\n\t<b>" +
532 GLib.Markup.escape_text(i) +"</b> : " +
533 GLib.Markup.escape_text(val.split("\n")[0]);
538 if (props.length > 0) {
539 ret+="\n\nProperties:" + props;
541 if (funcs.length > 0) {
542 ret+="\n\nMethods:" + funcs;
544 if (listen.length > 0) {
545 ret+="\n\nListeners:" + listen;
550 public string nodeTitle(bool for_tip = false) {
553 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
554 //if (sr) txt.push('<s>');
556 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
558 //if (renderfull && c['|xns']) {
559 var fqn = this.fqn();
560 var fqn_ar = fqn.split(".");
561 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
565 //if (c.xtype) { txt.push(c.xtype); }
567 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
568 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
569 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
572 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
573 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
574 if (this.has("html") && this.get("html").length > 0) {
575 var ht = this.get("html").split("\n");
577 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
579 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
582 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
583 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
584 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
585 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
586 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
587 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
588 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
590 // for flat classes...
591 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
592 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
595 //if (sr) txt.push('</s>');
596 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);