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 ""
110 this.items = new Gee.ArrayList<Node>();
111 this.props = new Gee.HashMap<string,string>();
112 this.listeners = new Gee.HashMap<string,string>();
114 this.xvala_xcls = "";
122 if (this.props.get("id") == null) {
124 return "uid-%d".printf(uid_count);
126 return this.props.get("id");
130 public bool hasChildren()
132 return this.items.size > 0;
134 public bool hasXnsType()
136 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
144 if (!this.hasXnsType ()) {
147 return this.props.get("$ xns") + "." + this.props.get("xtype");
150 public void setFqn(string name)
152 var ar = name.split(".");
153 this.props.set("xtype", ar[ar.length-1]);
154 var l = name.length - (ar[ar.length-1].length +1);
155 this.props.set("$ xns", name.substring(0, l));
156 print("setFQN %s to %s\n", name , this.fqn());
160 // wrapper around get props that returns empty string if not found.
161 public string get(string key)
163 var k = this.props.get(key);
168 k = this.props.get("$ " + key);
173 var iter = this.props.map_iterator();
174 while (iter.next()) {
175 var kk = iter.get_key().split(" ");
176 if (kk[kk.length-1] == key) {
177 return iter.get_value();
186 public string get_key(string key)
188 var k = this.props.get(key);
193 k = this.props.get("$ " + key);
198 var iter = this.props.map_iterator();
199 while (iter.next()) {
200 var kk = iter.get_key().split(" ");
201 if (kk[kk.length-1] == key) {
202 return iter.get_key();
210 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
212 // key formats : XXXX
214 // string XXX - with type
215 // $ XXX - with flag (no type)
216 // $ string XXX - with flag
220 var kk = key.strip().split(" ");
227 if (kk[0].length > 1) {
239 // everything blank otherwise...
241 public void set(string key, string value) {
242 this.props.set(key,value);
244 public bool has(string key)
246 var k = this.props.get(key);
250 var iter = this.props.map_iterator();
251 while (iter.next()) {
252 var kk = iter.get_key().strip().split(" ");
253 if (kk[kk.length-1] == key) {
264 if (this.parent == null) {
269 var nlist = new Gee.ArrayList<Node>();
270 for (var i =0;i < this.parent.items.size; i++) {
271 if (this.parent.items.get(i) == this) {
274 nlist.add(this.parent.items.get(i));
276 this.parent.items = nlist;
281 /* creates javascript based on the rules */
282 public Node? findProp(string n) {
283 for(var i=0;i< this.items.size;i++) {
284 var p = this.items.get(i).get("* prop");
285 if (this.items.get(i).get("* prop").length < 1) {
289 return this.items.get(i);
299 static Json.Generator gen = null;
301 public string quoteString(string str)
303 if (Node.gen == null) {
304 Node.gen = new Json.Generator();
306 var n = new Json.Node(Json.NodeType.VALUE);
309 Node.gen.set_root (n);
310 return Node.gen.to_data (null);
313 public void loadFromJson(Json.Object obj, int version) {
314 obj.foreach_member((o , key, value) => {
316 if (key == "items") {
317 var ar = value.get_array();
318 ar.foreach_element( (are, ix, el) => {
319 var node = new Node();
321 node.loadFromJson(el.get_object(), version);
322 this.items.add(node);
326 if (key == "listeners") {
327 var li = value.get_object();
328 li.foreach_member((lio , li_key, li_value) => {
329 this.listeners.set(li_key, li_value.get_string());
334 var v = value.get_value();
335 var sv = Value (typeof (string));
340 rkey = this.upgradeKey(key, (string)sv);
344 this.props.set(rkey, (string)sv);
352 public string upgradeKey(string key, string val)
355 if (key.length < 1) {
363 return "* " + key.substring(1);
368 if (key[0] == '.') { // v2 does not start with '.' ?
369 var bits = key.substring(1).split(":");
370 if (bits[0] == "signal") {
371 return "@" + string.joinv(" ", bits).substring(bits[0].length);
373 return "# " + string.joinv(" ", bits);
375 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
378 var bits = key.substring(1).split(":");
379 // two types '$' or '|' << for methods..
381 if (Regex.match_simple ("^function\\s*(", val.strip())) {
382 return "| " + key.substring(1);
386 if (Regex.match_simple ("^\\(", val.strip())) {
388 return "| " + string.joinv(" ", bits);
391 // guessing it's a property..
392 return "$ " + string.joinv(" ", bits);
403 public Node deepClone()
406 n.loadFromJson(this.toJsonObject(), 2);
410 public string toJsonString()
412 if (Node.gen == null) {
413 Node.gen = new Json.Generator();
417 var n = new Json.Node(Json.NodeType.OBJECT);
418 n.set_object(this.toJsonObject () );
419 Node.gen.set_root (n);
420 return Node.gen.to_data (null);
423 public Json.Object toJsonObject()
425 var ret = new Json.Object();
428 if (this.listeners.size > 0) {
429 var li = new Json.Object();
430 ret.set_object_member("listeners", li);
431 var liter = this.listeners.map_iterator();
432 while (liter.next()) {
433 li.set_string_member(liter.get_key(), liter.get_value());
437 if (this.props.size > 0 ) {
438 var iter = this.props.map_iterator();
439 while (iter.next()) {
440 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
443 if (this.items.size > 0) {
444 var ar = new Json.Array();
445 ret.set_array_member("items", ar);
448 for(var i =0;i < this.items.size;i++) {
449 ar.add_object_element(this.items.get(i).toJsonObject());
457 public void jsonObjectsetMember(Json.Object o, string key, string val) {
458 if (Lang.isBoolean(val)) {
459 o.set_boolean_member(key, val.down() == "false" ? false : true);
464 if (Lang.isNumber(val)) {
465 if (val.contains(".")) {
466 //print( "ADD " + key + "=" + val + " as a double?\n");
467 o.set_double_member(key, double.parse (val));
471 //print( "ADD " + key + "=" + val + " as a int?\n") ;
472 o.set_int_member(key,long.parse(val));
475 ///print( "ADD " + key + "=" + val + " as a string?\n");
476 o.set_string_member(key,val);
479 public string nodeTip()
481 var ret = this.nodeTitle(true);
485 var iter = this.props.map_iterator();
486 while (iter.next()) {
487 var i = iter.get_key();
488 var val = iter.get_value();
489 if (val == null || val.length < 1) {
494 GLib.Markup.escape_text(i) +"</b> : " +
495 GLib.Markup.escape_text(val.split("\n")[0]);
500 //if (i == "* init") {
504 if (Regex.match_simple("^\\s*function", val)) {
506 GLib.Markup.escape_text(i.substring(1)) +"</b> : " +
507 GLib.Markup.escape_text(val.split("\n")[0]);
510 if (Regex.match_simple("^\\s*\\(", val)) {
511 funcs += "\n<b>" + GLib.Markup.escape_text(i.substring(1)) +
513 GLib.Markup.escape_text(val.split("\n")[0]);
518 iter = this.listeners.map_iterator();
519 while (iter.next()) {
520 var i = iter.get_key();
521 var val = iter.get_value();
524 GLib.Markup.escape_text(i) +"</b> : " +
525 GLib.Markup.escape_text(val.split("\n")[0]);
530 if (props.length > 0) {
531 ret+="\n\nProperties:" + props;
533 if (funcs.length > 0) {
534 ret+="\n\nMethods:" + funcs;
536 if (listen.length > 0) {
537 ret+="\n\nListeners:" + listen;
542 public string nodeTitle(bool for_tip = false) {
545 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
546 //if (sr) txt.push('<s>');
548 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
550 //if (renderfull && c['|xns']) {
551 var fqn = this.fqn();
552 var fqn_ar = fqn.split(".");
553 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
557 //if (c.xtype) { txt.push(c.xtype); }
559 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
560 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
561 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
564 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
565 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
566 if (this.has("html") && this.get("html").length > 0) {
567 var ht = this.get("html").split("\n");
569 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
571 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
574 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
575 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
576 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
577 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
578 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
579 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
580 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
582 // for flat classes...
583 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
584 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
587 //if (sr) txt.push('</s>');
588 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);