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 = "";
121 this.proplines = new Gee.HashMap<string,int>();
122 this.listenlines = Gee.HashMap<string,int>();
129 if (this.props.get("id") == null) {
131 return "uid-%d".printf(uid_count);
133 return this.props.get("id");
137 public bool hasChildren()
139 return this.items.size > 0;
141 public bool hasXnsType()
143 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
151 if (!this.hasXnsType ()) {
154 return this.props.get("$ xns") + "." + this.props.get("xtype");
157 public void setFqn(string name)
159 var ar = name.split(".");
160 this.props.set("xtype", ar[ar.length-1]);
161 var l = name.length - (ar[ar.length-1].length +1);
162 this.props.set("$ xns", name.substring(0, l));
163 print("setFQN %s to %s\n", name , this.fqn());
167 // wrapper around get props that returns empty string if not found.
168 public string get(string key)
170 var k = this.props.get(key);
175 k = this.props.get("$ " + key);
180 var iter = this.props.map_iterator();
181 while (iter.next()) {
182 var kk = iter.get_key().split(" ");
183 if (kk[kk.length-1] == key) {
184 return iter.get_value();
193 public string get_key(string key)
195 var k = this.props.get(key);
200 k = this.props.get("$ " + key);
205 var iter = this.props.map_iterator();
206 while (iter.next()) {
207 var kk = iter.get_key().split(" ");
208 if (kk[kk.length-1] == key) {
209 return iter.get_key();
217 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
219 // key formats : XXXX
221 // string XXX - with type
222 // $ XXX - with flag (no type)
223 // $ string XXX - with flag
227 var kk = key.strip().split(" ");
234 if (kk[0].length > 1) {
246 // everything blank otherwise...
248 public void set(string key, string value) {
249 this.props.set(key,value);
251 public bool has(string key)
253 var k = this.props.get(key);
257 var iter = this.props.map_iterator();
258 while (iter.next()) {
259 var kk = iter.get_key().strip().split(" ");
260 if (kk[kk.length-1] == key) {
271 if (this.parent == null) {
276 var nlist = new Gee.ArrayList<Node>();
277 for (var i =0;i < this.parent.items.size; i++) {
278 if (this.parent.items.get(i) == this) {
281 nlist.add(this.parent.items.get(i));
283 this.parent.items = nlist;
288 /* creates javascript based on the rules */
289 public Node? findProp(string n) {
290 for(var i=0;i< this.items.size;i++) {
291 var p = this.items.get(i).get("* prop");
292 if (this.items.get(i).get("* prop").length < 1) {
296 return this.items.get(i);
306 static Json.Generator gen = null;
308 public string quoteString(string str)
310 if (Node.gen == null) {
311 Node.gen = new Json.Generator();
313 var n = new Json.Node(Json.NodeType.VALUE);
316 Node.gen.set_root (n);
317 return Node.gen.to_data (null);
320 public void loadFromJson(Json.Object obj, int version) {
321 obj.foreach_member((o , key, value) => {
323 if (key == "items") {
324 var ar = value.get_array();
325 ar.foreach_element( (are, ix, el) => {
326 var node = new Node();
328 node.loadFromJson(el.get_object(), version);
329 this.items.add(node);
333 if (key == "listeners") {
334 var li = value.get_object();
335 li.foreach_member((lio , li_key, li_value) => {
336 this.listeners.set(li_key, li_value.get_string());
341 var v = value.get_value();
342 var sv = Value (typeof (string));
347 rkey = this.upgradeKey(key, (string)sv);
351 this.props.set(rkey, (string)sv);
359 public string upgradeKey(string key, string val)
362 if (key.length < 1) {
370 return "* " + key.substring(1);
375 if (key[0] == '.') { // v2 does not start with '.' ?
376 var bits = key.substring(1).split(":");
377 if (bits[0] == "signal") {
378 return "@" + string.joinv(" ", bits).substring(bits[0].length);
380 return "# " + string.joinv(" ", bits);
382 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
385 var bits = key.substring(1).split(":");
386 // two types '$' or '|' << for methods..
388 if (Regex.match_simple ("^function\\s*(", val.strip())) {
389 return "| " + key.substring(1);
393 if (Regex.match_simple ("^\\(", val.strip())) {
395 return "| " + string.joinv(" ", bits);
398 // guessing it's a property..
399 return "$ " + string.joinv(" ", bits);
410 public Node deepClone()
413 n.loadFromJson(this.toJsonObject(), 2);
417 public string toJsonString()
419 if (Node.gen == null) {
420 Node.gen = new Json.Generator();
424 var n = new Json.Node(Json.NodeType.OBJECT);
425 n.set_object(this.toJsonObject () );
426 Node.gen.set_root (n);
427 return Node.gen.to_data (null);
430 public Json.Object toJsonObject()
432 var ret = new Json.Object();
435 if (this.listeners.size > 0) {
436 var li = new Json.Object();
437 ret.set_object_member("listeners", li);
438 var liter = this.listeners.map_iterator();
439 while (liter.next()) {
440 li.set_string_member(liter.get_key(), liter.get_value());
444 if (this.props.size > 0 ) {
445 var iter = this.props.map_iterator();
446 while (iter.next()) {
447 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
450 if (this.items.size > 0) {
451 var ar = new Json.Array();
452 ret.set_array_member("items", ar);
455 for(var i =0;i < this.items.size;i++) {
456 ar.add_object_element(this.items.get(i).toJsonObject());
464 public void jsonObjectsetMember(Json.Object o, string key, string val) {
465 if (Lang.isBoolean(val)) {
466 o.set_boolean_member(key, val.down() == "false" ? false : true);
471 if (Lang.isNumber(val)) {
472 if (val.contains(".")) {
473 //print( "ADD " + key + "=" + val + " as a double?\n");
474 o.set_double_member(key, double.parse (val));
478 //print( "ADD " + key + "=" + val + " as a int?\n") ;
479 o.set_int_member(key,long.parse(val));
482 ///print( "ADD " + key + "=" + val + " as a string?\n");
483 o.set_string_member(key,val);
486 public string nodeTip()
488 var ret = this.nodeTitle(true);
492 var iter = this.props.map_iterator();
493 while (iter.next()) {
494 var i = iter.get_key().strip();
495 var val = iter.get_value().strip();
496 if (val == null || val.length < 1) {
501 GLib.Markup.escape_text(i) +"</b> : " +
502 GLib.Markup.escape_text(val.split("\n")[0]);
507 //if (i == "* init") {
511 if (Regex.match_simple("^\\s*function", val)) {
513 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
514 GLib.Markup.escape_text(val.split("\n")[0]);
517 if (Regex.match_simple("^\\s*\\(", val)) {
518 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
520 GLib.Markup.escape_text(val.split("\n")[0]);
525 iter = this.listeners.map_iterator();
526 while (iter.next()) {
527 var i = iter.get_key().strip();
528 var val = iter.get_value().strip();
529 if (val == null || val.length < 1) {
532 listen += "\n\t<b>" +
533 GLib.Markup.escape_text(i) +"</b> : " +
534 GLib.Markup.escape_text(val.split("\n")[0]);
539 if (props.length > 0) {
540 ret+="\n\nProperties:" + props;
542 if (funcs.length > 0) {
543 ret+="\n\nMethods:" + funcs;
545 if (listen.length > 0) {
546 ret+="\n\nListeners:" + listen;
551 public string nodeTitle(bool for_tip = false) {
554 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
555 //if (sr) txt.push('<s>');
557 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
559 //if (renderfull && c['|xns']) {
560 var fqn = this.fqn();
561 var fqn_ar = fqn.split(".");
562 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
566 //if (c.xtype) { txt.push(c.xtype); }
568 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
569 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
570 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
573 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
574 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
575 if (this.has("html") && this.get("html").length > 0) {
576 var ht = this.get("html").split("\n");
578 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
580 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
583 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
584 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
585 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
586 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
587 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
588 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
589 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
591 // for flat classes...
592 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
593 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
596 //if (sr) txt.push('</s>');
597 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);