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 ""
107 public int line_start;
109 public Gee.ArrayList<int> lines;
110 public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....
111 public Gee.ArrayList<int> node_lines;
112 public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
117 this.items = new Gee.ArrayList<Node>();
118 this.props = new Gee.HashMap<string,string>();
119 this.listeners = new Gee.HashMap<string,string>();
121 this.xvala_xcls = "";
124 this.line_start = -1;
126 this.lines = new Gee.ArrayList<int>();
127 this.line_map = new Gee.HashMap<int,string>();
128 this.node_lines = new Gee.ArrayList<int>();
129 this.node_lines_map = new Gee.HashMap<int,Node>();
133 public void setNodeLine(int line, Node node) {
134 print("Add node @ %d\n", line);
135 this.node_lines.add(line);
136 this.node_lines_map.set(line, node);
139 public void setLine(int line, string type, string prop) {
140 this.lines.add(line);
141 this.line_map.set(line, type + ":" + prop);
143 public void sortLines() {
144 print("sortLines\n");
145 this.lines.sort((a,b) => {
146 return (int)b-(int)a;
148 this.node_lines.sort((a,b) => {
149 return (int)b-(int)a;
152 public Node? lineToNode(int line)
154 print("Searching for line %d\n",line);
156 foreach(int el in this.node_lines) {
157 print("all lines %d\n", el);
161 foreach(int el in this.node_lines) {
162 print("?match %d\n", el);
174 print("RETURNING NODE ON LINE %d", l);
175 return this.node_lines_map.get(l);
181 print("RETURNING NODE ON LINE %d", l);
182 return this.node_lines_map.get(l);
190 if (this.props.get("id") == null) {
192 return "uid-%d".printf(uid_count);
194 return this.props.get("id");
198 public bool hasChildren()
200 return this.items.size > 0;
202 public bool hasXnsType()
204 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
212 if (!this.hasXnsType ()) {
215 return this.props.get("$ xns") + "." + this.props.get("xtype");
218 public void setFqn(string name)
220 var ar = name.split(".");
221 this.props.set("xtype", ar[ar.length-1]);
222 var l = name.length - (ar[ar.length-1].length +1);
223 this.props.set("$ xns", name.substring(0, l));
224 print("setFQN %s to %s\n", name , this.fqn());
228 // wrapper around get props that returns empty string if not found.
229 public string get(string key)
231 var k = this.props.get(key);
236 k = this.props.get("$ " + key);
241 var iter = this.props.map_iterator();
242 while (iter.next()) {
243 var kk = iter.get_key().split(" ");
244 if (kk[kk.length-1] == key) {
245 return iter.get_value();
254 public string get_key(string key)
256 var k = this.props.get(key);
261 k = this.props.get("$ " + key);
266 var iter = this.props.map_iterator();
267 while (iter.next()) {
268 var kk = iter.get_key().split(" ");
269 if (kk[kk.length-1] == key) {
270 return iter.get_key();
278 public void normalize_key(string key, out string kname, out string kflag, out string ktype)
280 // key formats : XXXX
282 // string XXX - with type
283 // $ XXX - with flag (no type)
284 // $ string XXX - with flag
286 ktype = ""; // these used to contain '-' ???
287 kflag = ""; // these used to contain '-' ???
288 var kkv = key.strip().split(" ");
290 for (var i = 0; i < kkv.length; i++) {
291 if (kkv[i].length > 0 ) {
295 print("normalize %s => %s\n", key,string.joinv("=:=",kk));
303 if (kk[0].length > 1) {
315 // everything blank otherwise...
317 public void set(string key, string value) {
318 this.props.set(key,value);
320 public bool has(string key)
322 var k = this.props.get(key);
326 var iter = this.props.map_iterator();
327 while (iter.next()) {
328 var kk = iter.get_key().strip().split(" ");
329 if (kk[kk.length-1] == key) {
340 if (this.parent == null) {
345 var nlist = new Gee.ArrayList<Node>();
346 for (var i =0;i < this.parent.items.size; i++) {
347 if (this.parent.items.get(i) == this) {
350 nlist.add(this.parent.items.get(i));
352 this.parent.items = nlist;
357 /* creates javascript based on the rules */
358 public Node? findProp(string n) {
359 for(var i=0;i< this.items.size;i++) {
360 var p = this.items.get(i).get("* prop");
361 if (this.items.get(i).get("* prop").length < 1) {
365 return this.items.get(i);
375 static Json.Generator gen = null;
377 public string quoteString(string str)
379 if (Node.gen == null) {
380 Node.gen = new Json.Generator();
382 var n = new Json.Node(Json.NodeType.VALUE);
385 Node.gen.set_root (n);
386 return Node.gen.to_data (null);
389 public void loadFromJson(Json.Object obj, int version) {
390 obj.foreach_member((o , key, value) => {
392 if (key == "items") {
393 var ar = value.get_array();
394 ar.foreach_element( (are, ix, el) => {
395 var node = new Node();
397 node.loadFromJson(el.get_object(), version);
398 this.items.add(node);
402 if (key == "listeners") {
403 var li = value.get_object();
404 li.foreach_member((lio , li_key, li_value) => {
405 this.listeners.set(li_key, li_value.get_string());
410 var v = value.get_value();
411 var sv = Value (typeof (string));
416 rkey = this.upgradeKey(key, (string)sv);
420 this.props.set(rkey, (string)sv);
428 public string upgradeKey(string key, string val)
431 if (key.length < 1) {
439 return "* " + key.substring(1);
444 if (key[0] == '.') { // v2 does not start with '.' ?
445 var bits = key.substring(1).split(":");
446 if (bits[0] == "signal") {
447 return "@" + string.joinv(" ", bits).substring(bits[0].length);
449 return "# " + string.joinv(" ", bits);
451 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
454 var bits = key.substring(1).split(":");
455 // two types '$' or '|' << for methods..
457 if (Regex.match_simple ("^function\\s*(", val.strip())) {
458 return "| " + key.substring(1);
462 if (Regex.match_simple ("^\\(", val.strip())) {
464 return "| " + string.joinv(" ", bits);
467 // guessing it's a property..
468 return "$ " + string.joinv(" ", bits);
479 public Node deepClone()
482 n.loadFromJson(this.toJsonObject(), 2);
486 public string toJsonString()
488 if (Node.gen == null) {
489 Node.gen = new Json.Generator();
493 var n = new Json.Node(Json.NodeType.OBJECT);
494 n.set_object(this.toJsonObject () );
495 Node.gen.set_root (n);
496 return Node.gen.to_data (null);
499 public Json.Object toJsonObject()
501 var ret = new Json.Object();
504 if (this.listeners.size > 0) {
505 var li = new Json.Object();
506 ret.set_object_member("listeners", li);
507 var liter = this.listeners.map_iterator();
508 while (liter.next()) {
509 li.set_string_member(liter.get_key(), liter.get_value());
513 if (this.props.size > 0 ) {
514 var iter = this.props.map_iterator();
515 while (iter.next()) {
516 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
519 if (this.items.size > 0) {
520 var ar = new Json.Array();
521 ret.set_array_member("items", ar);
524 for(var i =0;i < this.items.size;i++) {
525 ar.add_object_element(this.items.get(i).toJsonObject());
533 public void jsonObjectsetMember(Json.Object o, string key, string val) {
534 if (Lang.isBoolean(val)) {
535 o.set_boolean_member(key, val.down() == "false" ? false : true);
540 if (Lang.isNumber(val)) {
541 if (val.contains(".")) {
542 //print( "ADD " + key + "=" + val + " as a double?\n");
543 o.set_double_member(key, double.parse (val));
547 //print( "ADD " + key + "=" + val + " as a int?\n") ;
548 o.set_int_member(key,long.parse(val));
551 ///print( "ADD " + key + "=" + val + " as a string?\n");
552 o.set_string_member(key,val);
555 public string nodeTip()
557 var ret = this.nodeTitle(true);
561 var iter = this.props.map_iterator();
562 while (iter.next()) {
563 var i = iter.get_key().strip();
564 var val = iter.get_value().strip();
565 if (val == null || val.length < 1) {
570 GLib.Markup.escape_text(i) +"</b> : " +
571 GLib.Markup.escape_text(val.split("\n")[0]);
576 //if (i == "* init") {
580 if (Regex.match_simple("^\\s*function", val)) {
582 GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " +
583 GLib.Markup.escape_text(val.split("\n")[0]);
586 if (Regex.match_simple("^\\s*\\(", val)) {
587 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
589 GLib.Markup.escape_text(val.split("\n")[0]);
594 iter = this.listeners.map_iterator();
595 while (iter.next()) {
596 var i = iter.get_key().strip();
597 var val = iter.get_value().strip();
598 if (val == null || val.length < 1) {
601 listen += "\n\t<b>" +
602 GLib.Markup.escape_text(i) +"</b> : " +
603 GLib.Markup.escape_text(val.split("\n")[0]);
608 if (props.length > 0) {
609 ret+="\n\nProperties:" + props;
611 if (funcs.length > 0) {
612 ret+="\n\nMethods:" + funcs;
614 if (listen.length > 0) {
615 ret+="\n\nListeners:" + listen;
620 public string nodeTitle(bool for_tip = false) {
623 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
624 //if (sr) txt.push('<s>');
626 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
628 //if (renderfull && c['|xns']) {
629 var fqn = this.fqn();
630 var fqn_ar = fqn.split(".");
631 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
635 //if (c.xtype) { txt.push(c.xtype); }
637 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
638 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
639 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
642 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
643 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
644 if (this.has("html") && this.get("html").length > 0) {
645 var ht = this.get("html").split("\n");
647 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
649 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
652 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
653 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
654 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
655 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
656 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
657 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
658 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
660 // for flat classes...
661 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
662 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
665 //if (sr) txt.push('</s>');
666 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);