tests/enum.vala.c
[app.Builder.js] / src / JsRender / Node.vala
1
2 // test..
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
4
5
6 /*
7  * 
8  * props:
9  * 
10  * key value view of properties.
11  * 
12  * Old standard..
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...  
19  * 
20  * Newer code
21  * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type  name 
22  * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23  *  "|void:clearFiles": "() .... some code...."  | type name
24  *
25  * 
26  * 
27  * 
28  * 
29  * Standardize this crap...
30  * 
31  * standard properties (use to set)
32  *          If they are long values show the dialog..
33  * 
34  * bool is_xxx  :: can show a pulldown.. (true/false)
35  * string html  
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... 
39  * 
40  * object properties (not part of the GOjbect being wrapped?
41  * # Gee.ArrayList<Xcls_fileitem> fileitems
42  * 
43  * signals
44  * @ void open 
45  * 
46  * methods -- always text editor..
47  * | void clearFiles
48  * | someJSmethod
49  * 
50  * specials
51  * * prop -- string
52  * * args  -- string
53  * * ctor -- string
54  * * init -- big string?
55  * 
56  * event handlers (listeners)
57  *   just shown 
58  * 
59  * -----------------
60  * special ID values
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.
64  * 
65  * 
66  *  FORMATING?
67 .method {
68          color : green;
69          font-weight: bold;      
70 }
71 .prop {
72         color : #333;
73 }
74 .prop-code {
75     font-style: italic;
76  }
77 .listener {
78     color: #600;
79     font-weight: bold;   
80 }
81 .special { 
82   color : #00c;    font-weight: bold;    
83
84
85 */
86
87
88
89
90
91
92 public class JsRender.Node : Object {
93         
94
95         public static int uid_count = 0;
96         
97         public Node parent;
98         public Gee.ArrayList<Node> items; // child items..
99         
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;
106         public int line_end;
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> node_lines_map; // store of l:xxx or p:....
111         
112
113         public Node()
114         {
115                 this.items = new Gee.ArrayList<Node>();
116                 this.props = new Gee.HashMap<string,string>();
117                 this.listeners = new Gee.HashMap<string,string>();
118                 this.xvala_cls = "";
119                 this.xvala_xcls = "";
120                 this.xvala_id = "";
121                 this.parent = null;
122                 this.line_start = -1;
123                 this.line_end = -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_lines_map = new Gee.HashMap<int,Node>();
128                 
129         }
130         
131         public void setNodeLine(int line, Node node) {
132                 this.node_lines.add(line);
133                 this.node_lines_map.set(line, node);
134         }
135         
136         public void setLine(int line, string type, string prop) {
137                 this.lines.add(line);
138                 this.line_map.set(line, type +":" + prop);
139         }
140         public void sortLines() {
141                 this.lines.sort((a,b) => {   
142                         return (int)b-(int)a;
143                 });
144                 this.node_lines.sort((a,b) => {   
145                         return (int)b-(int)a;
146                 });
147         }
148         public Node? lineToNode(int line)
149         {
150                 print("Searching for line %d\n",line);
151                 var l = -1;
152                 foreach(int el in this.node_lines) {
153                         print("?match %d\n", el);
154                         if (el < line) {
155                                 
156                                 l = el;
157                                 print("LESS\n");
158                                 continue;
159                         }
160                         if (el == line) {
161                                 print("SAME\n");
162                                 l = el;
163                         }
164                         if (l > -1) {
165                                 print("RETURNING NODE ON LINE %d", l);
166                                 return this.node_lines_map.get(l);
167                         }
168                         return null;
169                         
170                 }
171                 if (l > -1) {
172                         print("RETURNING NODE ON LINE %d", l);
173                         return this.node_lines_map.get(l);
174                 }
175                 return null;
176                 
177         }
178         
179         public string uid()
180         {
181                 if (this.props.get("id") == null) {
182                         uid_count++;
183                         return "uid-%d".printf(uid_count);
184                 }
185                 return this.props.get("id");
186         }
187         
188         
189         public bool hasChildren()
190         {
191                 return this.items.size > 0;
192         }
193         public bool hasXnsType()
194         {
195                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
196                         return true;
197                         
198                 }
199                 return false;
200         }
201         public string fqn()
202         {
203                 if (!this.hasXnsType ()) {
204                         return "";
205                 }
206                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
207
208         }
209         public void setFqn(string name)
210         {
211                 var ar = name.split(".");
212                 this.props.set("xtype", ar[ar.length-1]);
213                 var l = name.length - (ar[ar.length-1].length +1);
214                 this.props.set("$ xns", name.substring(0, l));
215                 print("setFQN %s to %s\n", name , this.fqn());
216                                
217
218         }
219         // wrapper around get props that returns empty string if not found.
220         public string get(string key)
221         {
222                 var k = this.props.get(key);
223                 if (k != null) {
224                         return k;
225                 }
226                 
227                 k = this.props.get("$ " + key);
228                 if (k != null) {
229                         return k;
230                 }
231                 
232                 var iter = this.props.map_iterator();
233                 while (iter.next()) {
234                         var kk = iter.get_key().split(" ");
235                         if (kk[kk.length-1] == key) {
236                                 return iter.get_value();
237                         }
238                 }
239                 
240                 
241                 return "";
242                 
243         }
244         
245         public string get_key(string key)
246         {
247                 var k = this.props.get(key);
248                 if (k != null) {
249                         return key;
250                 }
251                 
252                 k = this.props.get("$ " + key);
253                 if (k != null) {
254                         return "$ " + key;
255                 }
256                 
257                 var iter = this.props.map_iterator();
258                 while (iter.next()) {
259                         var kk = iter.get_key().split(" ");
260                         if (kk[kk.length-1] == key) {
261                                 return iter.get_key();
262                         }
263                 }
264                 
265                 
266                 return "";
267                 
268         }
269         public void normalize_key(string key, out string kname, out string kflag, out string ktype)
270         {
271                 // key formats : XXXX
272                 // XXX - plain
273                 // string XXX - with type
274                 // $ XXX - with flag (no type)
275                 // $ string XXX - with flag
276                 kname = "";
277                 ktype = "-";
278                 kflag = "-";
279                 var kk = key.strip().split(" ");
280                 switch(kk.length) {
281                         case 1: 
282                                 kname = kk[0];
283                                 return;
284                         case 2: 
285                                 kname = kk[1];
286                                 if (kk[0].length > 1) {
287                                         ktype = kk[0];
288                                 } else {
289                                         kflag = kk[0];
290                                 }
291                                 return;
292                         case 3:
293                                 kname = kk[2];
294                                 kflag = kk[0];
295                                 ktype = kk[1];
296                                 return;
297                 }
298                 // everything blank otherwise...
299         }
300         public void set(string key, string value) {
301                 this.props.set(key,value);
302         }
303          public bool has(string key)
304         {
305                 var k = this.props.get(key);
306                 if (k != null) {
307                         return true;
308                 }
309                 var iter = this.props.map_iterator();
310                 while (iter.next()) {
311                         var kk = iter.get_key().strip().split(" ");
312                         if (kk[kk.length-1] == key) {
313                                 return true;
314                         }
315                 }
316                 
317                 return false;
318                 
319         }
320
321         public void  remove()
322         {
323                 if (this.parent == null) {
324                         
325                         
326                         return;
327                 }
328                 var nlist = new Gee.ArrayList<Node>();
329                 for (var i =0;i < this.parent.items.size; i++) {
330                         if (this.parent.items.get(i) == this) {
331                                 continue;
332                         }
333                         nlist.add(this.parent.items.get(i));
334                 }
335                 this.parent.items = nlist;
336                 this.parent = null;
337
338         }
339          
340         /* creates javascript based on the rules */
341         public Node? findProp(string n) {
342                 for(var i=0;i< this.items.size;i++) {
343                         var p = this.items.get(i).get("* prop");
344                         if (this.items.get(i).get("* prop").length < 1) {
345                                 continue;
346                         }
347                         if (p == n) {
348                                 return this.items.get(i);
349                         }
350                 }
351                 return null;
352
353         }
354
355         
356         
357          
358         static Json.Generator gen = null;
359         
360         public string quoteString(string str)
361         {
362                 if (Node.gen == null) {
363                         Node.gen = new Json.Generator();
364                 }
365                  var n = new Json.Node(Json.NodeType.VALUE);
366                 n.set_string(str);
367  
368                 Node.gen.set_root (n);
369                 return  Node.gen.to_data (null);   
370         }
371
372         public void loadFromJson(Json.Object obj, int version) {
373                 obj.foreach_member((o , key, value) => {
374                         //print(key+"\n");
375                         if (key == "items") {
376                                 var ar = value.get_array();
377                                 ar.foreach_element( (are, ix, el) => {
378                                         var node = new Node();
379                                         node.parent = this;
380                                         node.loadFromJson(el.get_object(), version);
381                                         this.items.add(node);
382                                 });
383                                 return;
384                         }
385                         if (key == "listeners") {
386                                 var li = value.get_object();
387                                 li.foreach_member((lio , li_key, li_value) => {
388                                         this.listeners.set(li_key, li_value.get_string());
389
390                                 });
391                                 return;
392                         }
393                         var v = value.get_value();
394                         var sv =  Value (typeof (string));
395                         v.transform(ref sv);
396
397                         var rkey = key;
398                         if (version == 1) {
399                                 rkey = this.upgradeKey(key, (string)sv);
400                         }
401
402                         
403                         this.props.set(rkey,  (string)sv);
404                 });
405                 
406
407
408
409         }
410
411         public string upgradeKey(string key, string val)
412         {
413                 // convert V1 to V2
414                 if (key.length < 1) {
415                         return key;
416                 }
417                 switch(key) {
418                         case "*prop":
419                         case "*args":
420                         case ".ctor":
421                         case "|init":
422                                 return "* " + key.substring(1);
423                                 
424                         case "pack":
425                                 return "* " + key;
426                 }
427                 if (key[0] == '.') { // v2 does not start with '.' ?
428                         var bits = key.substring(1).split(":");
429                         if (bits[0] == "signal") {
430                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
431                         }
432                         return "# " + string.joinv(" ", bits);                  
433                 }
434                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
435                         return key;
436                 }
437                 var bits = key.substring(1).split(":");
438                 // two types '$' or '|' << for methods..
439                 // javascript 
440                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
441                         return "| " + key.substring(1);
442                 }
443                 // vala function..
444                 
445                 if  (Regex.match_simple ("^\\(", val.strip())) {
446                 
447                         return "| " + string.joinv(" ", bits);
448                 }
449                 
450                 // guessing it's a property..
451                 return "$ " + string.joinv(" ", bits);
452                 
453                 
454
455         }
456
457
458
459
460
461         
462         public Node  deepClone()
463         {
464                 var n = new Node();
465                 n.loadFromJson(this.toJsonObject(), 2);
466                 return n;
467
468         }
469         public string toJsonString()
470         {
471                 if (Node.gen == null) {
472                         Node.gen = new Json.Generator();
473                         gen.pretty =  true;
474                         gen.indent = 1;
475                 }
476                 var n = new Json.Node(Json.NodeType.OBJECT);
477                 n.set_object(this.toJsonObject () );
478                 Node.gen.set_root (n);
479                 return  Node.gen.to_data (null);   
480         }
481         
482         public Json.Object toJsonObject()
483         {
484                 var ret = new Json.Object();
485
486                 // listeners...
487                 if (this.listeners.size > 0) {
488                         var li = new Json.Object();
489                         ret.set_object_member("listeners", li);
490                         var liter = this.listeners.map_iterator();
491                         while (liter.next()) {
492                                 li.set_string_member(liter.get_key(), liter.get_value());
493                         }
494                 }
495                 //props
496                 if (this.props.size > 0 ) {
497                         var iter = this.props.map_iterator();
498                         while (iter.next()) {
499                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
500                         }
501                 }
502                 if (this.items.size > 0) {
503                         var ar = new Json.Array();
504                         ret.set_array_member("items", ar);
505                 
506                         // children..
507                         for(var i =0;i < this.items.size;i++) {
508                                 ar.add_object_element(this.items.get(i).toJsonObject());
509                         }
510                 }
511                 return ret;
512                 
513  
514         }
515          
516         public void jsonObjectsetMember(Json.Object o, string key, string val) {
517                 if (Lang.isBoolean(val)) {
518                         o.set_boolean_member(key, val.down() == "false" ? false : true);
519                         return;
520                 }
521                 
522                 
523                 if (Lang.isNumber(val)) {
524                         if (val.contains(".")) {
525                                 //print( "ADD " + key + "=" + val + " as a double?\n");
526                                 o.set_double_member(key, double.parse (val));
527                                 return;
528
529                         }
530                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
531                         o.set_int_member(key,long.parse(val));
532                         return;
533                 }
534                 ///print( "ADD " + key + "=" + val + " as a string?\n");
535                 o.set_string_member(key,val);
536                 
537         }
538         public string nodeTip()
539         {
540                 var ret = this.nodeTitle(true);
541                 var funcs = "";
542                 var props = "";
543                 var listen = "";
544                 var iter = this.props.map_iterator();
545                 while (iter.next()) {
546                         var i =  iter.get_key().strip();
547                         var val = iter.get_value().strip();
548                         if (val == null || val.length < 1) {
549                                 continue;
550                         }
551                         if ( i[0] != '|') {
552                                 props += "\n\t<b>" + 
553                                         GLib.Markup.escape_text(i) +"</b> : " + 
554                                         GLib.Markup.escape_text(val.split("\n")[0]);
555                                  
556                                 continue;
557                         }
558                 
559                         //if (i == "* init") { 
560                         //      continue;
561                         //}
562                         
563                         if (Regex.match_simple("^\\s*function", val)) { 
564                                 funcs += "\n\t<b>" + 
565                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
566                                         GLib.Markup.escape_text(val.split("\n")[0]);
567                                 continue;
568                         }
569                         if (Regex.match_simple("^\\s*\\(", val)) {
570                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
571                                         "</b> : " + 
572                                         GLib.Markup.escape_text(val.split("\n")[0]);
573                                 continue;
574                         }
575                         
576                 }
577                 iter = this.listeners.map_iterator();
578                 while (iter.next()) {
579                         var i =  iter.get_key().strip();
580                         var val = iter.get_value().strip();
581                         if (val == null || val.length < 1) {
582                                 continue;
583                         }
584                          listen += "\n\t<b>" + 
585                                         GLib.Markup.escape_text(i) +"</b> : " + 
586                                         GLib.Markup.escape_text(val.split("\n")[0]);
587                         
588                 }
589                 
590                 
591                 if (props.length > 0) {
592                         ret+="\n\nProperties:" + props;
593                 } 
594                 if (funcs.length > 0) {
595                         ret+="\n\nMethods:" + funcs;
596                 } 
597                 if (listen.length > 0) {
598                         ret+="\n\nListeners:" + listen;
599                 } 
600                 return ret;
601
602         }
603         public string nodeTitle(bool for_tip = false) {
604                 string[] txt = {};
605
606                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
607                 //if (sr) txt.push('<s>');
608
609                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
610                 
611                 //if (renderfull && c['|xns']) {
612                 var fqn = this.fqn();
613                 var fqn_ar = fqn.split(".");
614                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
615                         
616                 //}
617                 
618                 //if (c.xtype)    { txt.push(c.xtype); }
619                         
620                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
621                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
622                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
623                 
624                 
625                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
626                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
627                 if (this.has("html") && this.get("html").length > 0)     { 
628                         var ht = this.get("html").split("\n");
629                         if (ht.length > 1) {
630                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
631                         } else { 
632                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
633                         }
634                 }
635                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
636                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
637                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
638                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
639                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
640                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
641                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
642                 
643                 // for flat classes...
644                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
645                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
646                 
647                 
648                 //if (sr) txt.push('</s>');
649                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
650         }
651
652 }