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;
108 public Gee.HashMap<string,int> proplines;
109 public Gee.HashMap<string,int> listenlines;
114 this.items = new Gee.ArrayList<Node>();
115 this.props = new Gee.HashMap<string,string>();
116 this.listeners = new Gee.HashMap<string,string>();
118 this.xvala_xcls = "";
121 this.line_start = -1;
123 this.proplines = new Gee.HashMap<string,int>();
124 this.listenlines = Gee.HashMap<string,int>();
131 if (this.props.get("id") == null) {
133 return "uid-%d".printf(uid_count);
135 return this.props.get("id");
139 public bool hasChildren()
141 return this.items.size > 0;
143 public bool hasXnsType()
145 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
153 if (!this.hasXnsType ()) {
156 return this.props.get("$ xns") + "." + this.props.get("xtype");
159 public void setFqn(string name)
161 var ar = name.split(".");
162 this.props.set("xtype", ar[ar.length-1]);
163 var l = name.length - (ar[ar.length-1].length +1);
164 this.props.set("$ xns", name.substring(0, l));
165 print("setFQN %s to %s\n", name , this.fqn());
169 // wrapper around get props that returns empty string if not found.
170 public string get(string key)
172 var k = this.props.get(key);
177 k = this.props.get("$ " + key);
182 var iter = this.props.map_iterator();
183 while (iter.next()) {
184 var kk = iter.get_key().split(" ");
185 if (kk[kk.length-1] == key) {
186 return iter.get_value();
195 public string get_key(string key)
197 var k = this.props.get(key);
202 k = this.props.get("$ " + key);
207 var iter = this.props.map_iterator();
208 while (iter.next()) {
209 var kk = iter.get_key().split(" ");
210 if (kk[kk.length-1] == key) {
211 return iter.get_key();
219 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
221 // key formats : XXXX
223 // string XXX - with type
224 // $ XXX - with flag (no type)
225 // $ string XXX - with flag
229 var kk = key.strip().split(" ");
236 if (kk[0].length > 1) {
248 // everything blank otherwise...
250 public void set(string key, string value) {
251 this.props.set(key,value);
253 public bool has(string key)
255 var k = this.props.get(key);
259 var iter = this.props.map_iterator();
260 while (iter.next()) {
261 var kk = iter.get_key().strip().split(" ");
262 if (kk[kk.length-1] == key) {
273 if (this.parent == null) {
278 var nlist = new Gee.ArrayList<Node>();
279 for (var i =0;i < this.parent.items.size; i++) {
280 if (this.parent.items.get(i) == this) {
283 nlist.add(this.parent.items.get(i));
285 this.parent.items = nlist;
290 /* creates javascript based on the rules */
291 public Node? findProp(string n) {
292 for(var i=0;i< this.items.size;i++) {
293 var p = this.items.get(i).get("* prop");
294 if (this.items.get(i).get("* prop").length < 1) {
298 return this.items.get(i);
308 static Json.Generator gen = null;
310 public string quoteString(string str)
312 if (Node.gen == null) {
313 Node.gen = new Json.Generator();
315 var n = new Json.Node(Json.NodeType.VALUE);
318 Node.gen.set_root (n);
319 return Node.gen.to_data (null);
322 public void loadFromJson(Json.Object obj, int version) {
323 obj.foreach_member((o , key, value) => {
325 if (key == "items") {
326 var ar = value.get_array();
327 ar.foreach_element( (are, ix, el) => {
328 var node = new Node();
330 node.loadFromJson(el.get_object(), version);
331 this.items.add(node);
335 if (key == "listeners") {
336 var li = value.get_object();
337 li.foreach_member((lio , li_key, li_value) => {
338 this.listeners.set(li_key, li_value.get_string());
343 var v = value.get_value();
344 var sv = Value (typeof (string));
349 rkey = this.upgradeKey(key, (string)sv);
353 this.props.set(rkey, (string)sv);
361 public string upgradeKey(string key, string val)
364 if (key.length < 1) {
372 return "* " + key.substring(1);
377 if (key[0] == '.') { // v2 does not start with '.' ?
378 var bits = key.substring(1).split(":");
379 if (bits[0] == "signal") {
380 return "@" + string.joinv(" ", bits).substring(bits[0].length);
382 return "# " + string.joinv(" ", bits);
384 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
387 var bits = key.substring(1).split(":");
388 // two types '$' or '|' << for methods..
390 if (Regex.match_simple ("^function\\s*(", val.strip())) {
391 return "| " + key.substring(1);
395 if (Regex.match_simple ("^\\(", val.strip())) {
397 return "| " + string.joinv(" ", bits);
400 // guessing it's a property..
401 return "$ " + string.joinv(" ", bits);
412 public Node deepClone()
415 n.loadFromJson(this.toJsonObject(), 2);
419 public string toJsonString()
421 if (Node.gen == null) {
422 Node.gen = new Json.Generator();
426 var n = new Json.Node(Json.NodeType.OBJECT);
427 n.set_object(this.toJsonObject () );
428 Node.gen.set_root (n);
429 return Node.gen.to_data (null);
432 public Json.Object toJsonObject()
434 var ret = new Json.Object();
437 if (this.listeners.size > 0) {
438 var li = new Json.Object();
439 ret.set_object_member("listeners", li);
440 var liter = this.listeners.map_iterator();
441 while (liter.next()) {
442 li.set_string_member(liter.get_key(), liter.get_value());
446 if (this.props.size > 0 ) {
447 var iter = this.props.map_iterator();
448 while (iter.next()) {
449 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
452 if (this.items.size > 0) {
453 var ar = new Json.Array();
454 ret.set_array_member("items", ar);
457 for(var i =0;i < this.items.size;i++) {
458 ar.add_object_element(this.items.get(i).toJsonObject());
466 public void jsonObjectsetMember(Json.Object o, string key, string val) {
467 if (Lang.isBoolean(val)) {
468 o.set_boolean_member(key, val.down() == "false" ? false : true);
473 if (Lang.isNumber(val)) {
474 if (val.contains(".")) {
475 //print( "ADD " + key + "=" + val + " as a double?\n");
476 o.set_double_member(key, double.parse (val));
480 //print( "ADD " + key + "=" + val + " as a int?\n") ;
481 o.set_int_member(key,long.parse(val));
484 ///print( "ADD " + key + "=" + val + " as a string?\n");
485 o.set_string_member(key,val);
488 public string nodeTip()
490 var ret = this.nodeTitle(true);
494 var iter = this.props.map_iterator();
495 while (iter.next()) {
496 var i = iter.get_key().strip();
497 var val = iter.get_value().strip();
498 if (val == null || val.length < 1) {
503 GLib.Markup.escape_text(i) +"</b> : " +
504 GLib.Markup.escape_text(val.split("\n")[0]);
509 //if (i == "* init") {
513 if (Regex.match_simple("^\\s*function", val)) {
515 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
516 GLib.Markup.escape_text(val.split("\n")[0]);
519 if (Regex.match_simple("^\\s*\\(", val)) {
520 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
522 GLib.Markup.escape_text(val.split("\n")[0]);
527 iter = this.listeners.map_iterator();
528 while (iter.next()) {
529 var i = iter.get_key().strip();
530 var val = iter.get_value().strip();
531 if (val == null || val.length < 1) {
534 listen += "\n\t<b>" +
535 GLib.Markup.escape_text(i) +"</b> : " +
536 GLib.Markup.escape_text(val.split("\n")[0]);
541 if (props.length > 0) {
542 ret+="\n\nProperties:" + props;
544 if (funcs.length > 0) {
545 ret+="\n\nMethods:" + funcs;
547 if (listen.length > 0) {
548 ret+="\n\nListeners:" + listen;
553 public string nodeTitle(bool for_tip = false) {
556 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
557 //if (sr) txt.push('<s>');
559 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
561 //if (renderfull && c['|xns']) {
562 var fqn = this.fqn();
563 var fqn_ar = fqn.split(".");
564 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
568 //if (c.xtype) { txt.push(c.xtype); }
570 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
571 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
572 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
575 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
576 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
577 if (this.has("html") && this.get("html").length > 0) {
578 var ht = this.get("html").split("\n");
580 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
582 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
585 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
586 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
587 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
588 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
589 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
590 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
591 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
593 // for flat classes...
594 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
595 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
598 //if (sr) txt.push('</s>');
599 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);