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:....
109 public Gee.ArrayList<int> node_lines;
110 public Gee.HashMap<int,Node> nodes_line_map; // store of l:xxx or p:....
115 this.items = new Gee.ArrayList<Node>();
116 this.props = new Gee.HashMap<string,string>();
117 this.listeners = new Gee.HashMap<string,string>();
119 this.xvala_xcls = "";
122 this.line_start = -1;
124 this.lines = new Gee.ArrayList<int>();
125 this.line_map = new Gee.HashMap<int,string>();
126 this.node_lines = new Gee.ArrayList<int>();
127 this.node_line_map = new Gee.HashMap<int,Node>();
131 public void setNodeLine(int line, Node node) {
132 this.node_lines.add(line);
133 this.node_lines_map.set(line, node);
136 public void setLine(int line, string type, string prop) {
137 this.lines.add(line);
138 this.line_map.set(line, type +":" + prop);
140 public void sortLines() {
141 this.lines.sort((a,b) => {
142 return (int)a-(int)b;
144 this.node_lines.sort((a,b) => {
145 return (int)a-(int)b;
151 if (this.props.get("id") == null) {
153 return "uid-%d".printf(uid_count);
155 return this.props.get("id");
159 public bool hasChildren()
161 return this.items.size > 0;
163 public bool hasXnsType()
165 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
173 if (!this.hasXnsType ()) {
176 return this.props.get("$ xns") + "." + this.props.get("xtype");
179 public void setFqn(string name)
181 var ar = name.split(".");
182 this.props.set("xtype", ar[ar.length-1]);
183 var l = name.length - (ar[ar.length-1].length +1);
184 this.props.set("$ xns", name.substring(0, l));
185 print("setFQN %s to %s\n", name , this.fqn());
189 // wrapper around get props that returns empty string if not found.
190 public string get(string key)
192 var k = this.props.get(key);
197 k = this.props.get("$ " + key);
202 var iter = this.props.map_iterator();
203 while (iter.next()) {
204 var kk = iter.get_key().split(" ");
205 if (kk[kk.length-1] == key) {
206 return iter.get_value();
215 public string get_key(string key)
217 var k = this.props.get(key);
222 k = this.props.get("$ " + key);
227 var iter = this.props.map_iterator();
228 while (iter.next()) {
229 var kk = iter.get_key().split(" ");
230 if (kk[kk.length-1] == key) {
231 return iter.get_key();
239 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
241 // key formats : XXXX
243 // string XXX - with type
244 // $ XXX - with flag (no type)
245 // $ string XXX - with flag
249 var kk = key.strip().split(" ");
256 if (kk[0].length > 1) {
268 // everything blank otherwise...
270 public void set(string key, string value) {
271 this.props.set(key,value);
273 public bool has(string key)
275 var k = this.props.get(key);
279 var iter = this.props.map_iterator();
280 while (iter.next()) {
281 var kk = iter.get_key().strip().split(" ");
282 if (kk[kk.length-1] == key) {
293 if (this.parent == null) {
298 var nlist = new Gee.ArrayList<Node>();
299 for (var i =0;i < this.parent.items.size; i++) {
300 if (this.parent.items.get(i) == this) {
303 nlist.add(this.parent.items.get(i));
305 this.parent.items = nlist;
310 /* creates javascript based on the rules */
311 public Node? findProp(string n) {
312 for(var i=0;i< this.items.size;i++) {
313 var p = this.items.get(i).get("* prop");
314 if (this.items.get(i).get("* prop").length < 1) {
318 return this.items.get(i);
328 static Json.Generator gen = null;
330 public string quoteString(string str)
332 if (Node.gen == null) {
333 Node.gen = new Json.Generator();
335 var n = new Json.Node(Json.NodeType.VALUE);
338 Node.gen.set_root (n);
339 return Node.gen.to_data (null);
342 public void loadFromJson(Json.Object obj, int version) {
343 obj.foreach_member((o , key, value) => {
345 if (key == "items") {
346 var ar = value.get_array();
347 ar.foreach_element( (are, ix, el) => {
348 var node = new Node();
350 node.loadFromJson(el.get_object(), version);
351 this.items.add(node);
355 if (key == "listeners") {
356 var li = value.get_object();
357 li.foreach_member((lio , li_key, li_value) => {
358 this.listeners.set(li_key, li_value.get_string());
363 var v = value.get_value();
364 var sv = Value (typeof (string));
369 rkey = this.upgradeKey(key, (string)sv);
373 this.props.set(rkey, (string)sv);
381 public string upgradeKey(string key, string val)
384 if (key.length < 1) {
392 return "* " + key.substring(1);
397 if (key[0] == '.') { // v2 does not start with '.' ?
398 var bits = key.substring(1).split(":");
399 if (bits[0] == "signal") {
400 return "@" + string.joinv(" ", bits).substring(bits[0].length);
402 return "# " + string.joinv(" ", bits);
404 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
407 var bits = key.substring(1).split(":");
408 // two types '$' or '|' << for methods..
410 if (Regex.match_simple ("^function\\s*(", val.strip())) {
411 return "| " + key.substring(1);
415 if (Regex.match_simple ("^\\(", val.strip())) {
417 return "| " + string.joinv(" ", bits);
420 // guessing it's a property..
421 return "$ " + string.joinv(" ", bits);
432 public Node deepClone()
435 n.loadFromJson(this.toJsonObject(), 2);
439 public string toJsonString()
441 if (Node.gen == null) {
442 Node.gen = new Json.Generator();
446 var n = new Json.Node(Json.NodeType.OBJECT);
447 n.set_object(this.toJsonObject () );
448 Node.gen.set_root (n);
449 return Node.gen.to_data (null);
452 public Json.Object toJsonObject()
454 var ret = new Json.Object();
457 if (this.listeners.size > 0) {
458 var li = new Json.Object();
459 ret.set_object_member("listeners", li);
460 var liter = this.listeners.map_iterator();
461 while (liter.next()) {
462 li.set_string_member(liter.get_key(), liter.get_value());
466 if (this.props.size > 0 ) {
467 var iter = this.props.map_iterator();
468 while (iter.next()) {
469 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
472 if (this.items.size > 0) {
473 var ar = new Json.Array();
474 ret.set_array_member("items", ar);
477 for(var i =0;i < this.items.size;i++) {
478 ar.add_object_element(this.items.get(i).toJsonObject());
486 public void jsonObjectsetMember(Json.Object o, string key, string val) {
487 if (Lang.isBoolean(val)) {
488 o.set_boolean_member(key, val.down() == "false" ? false : true);
493 if (Lang.isNumber(val)) {
494 if (val.contains(".")) {
495 //print( "ADD " + key + "=" + val + " as a double?\n");
496 o.set_double_member(key, double.parse (val));
500 //print( "ADD " + key + "=" + val + " as a int?\n") ;
501 o.set_int_member(key,long.parse(val));
504 ///print( "ADD " + key + "=" + val + " as a string?\n");
505 o.set_string_member(key,val);
508 public string nodeTip()
510 var ret = this.nodeTitle(true);
514 var iter = this.props.map_iterator();
515 while (iter.next()) {
516 var i = iter.get_key().strip();
517 var val = iter.get_value().strip();
518 if (val == null || val.length < 1) {
523 GLib.Markup.escape_text(i) +"</b> : " +
524 GLib.Markup.escape_text(val.split("\n")[0]);
529 //if (i == "* init") {
533 if (Regex.match_simple("^\\s*function", val)) {
535 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
536 GLib.Markup.escape_text(val.split("\n")[0]);
539 if (Regex.match_simple("^\\s*\\(", val)) {
540 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
542 GLib.Markup.escape_text(val.split("\n")[0]);
547 iter = this.listeners.map_iterator();
548 while (iter.next()) {
549 var i = iter.get_key().strip();
550 var val = iter.get_value().strip();
551 if (val == null || val.length < 1) {
554 listen += "\n\t<b>" +
555 GLib.Markup.escape_text(i) +"</b> : " +
556 GLib.Markup.escape_text(val.split("\n")[0]);
561 if (props.length > 0) {
562 ret+="\n\nProperties:" + props;
564 if (funcs.length > 0) {
565 ret+="\n\nMethods:" + funcs;
567 if (listen.length > 0) {
568 ret+="\n\nListeners:" + listen;
573 public string nodeTitle(bool for_tip = false) {
576 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
577 //if (sr) txt.push('<s>');
579 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
581 //if (renderfull && c['|xns']) {
582 var fqn = this.fqn();
583 var fqn_ar = fqn.split(".");
584 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
588 //if (c.xtype) { txt.push(c.xtype); }
590 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
591 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
592 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
595 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
596 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
597 if (this.has("html") && this.get("html").length > 0) {
598 var ht = this.get("html").split("\n");
600 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
602 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
605 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
606 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
607 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
608 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
609 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
610 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
611 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
613 // for flat classes...
614 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
615 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
618 //if (sr) txt.push('</s>');
619 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);