resources/RooUsage.txt
[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         
106         // line markers..
107         public int line_start;
108         public int line_end;
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:....
113         
114
115         public Node()
116         {
117                 this.items = new Gee.ArrayList<Node>();
118                 this.props = new Gee.HashMap<string,string>();
119                 this.listeners = new Gee.HashMap<string,string>();
120                 this.xvala_cls = "";
121                 this.xvala_xcls = "";
122                 this.xvala_id = "";
123                 this.parent = null;
124                 this.line_start = -1;
125                 this.line_end = -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>();
130                 
131         }
132         
133         public void setNodeLine(int line, Node node) {
134                 //print("Add node @ %d\n", line);
135                 if (this.node_lines_map.has_key(line)) {
136                         return;
137                 }
138                 this.node_lines.add(line);
139                 this.node_lines_map.set(line, node);
140                 
141         }
142         
143         public void setLine(int line, string type, string prop) {
144                 if (this.line_map.has_key(line)) {
145                         if  (this.line_map.get(line) != "e:"  ) {
146                                 return;
147                         }
148                 } else {
149                         this.lines.add(line);
150                 }
151                 this.line_map.set(line, type + ":" + prop);
152                 GLib.debug("setLine %d, %s", line, type + ":" + prop);
153         }
154         public void sortLines() {
155                 //print("sortLines\n");
156                 this.lines.sort((a,b) => {   
157                         return (int)a-(int)b;
158                 });
159                 this.node_lines.sort((a,b) => {   
160                         return (int)a-(int)b;
161                 });
162         }
163         public Node? lineToNode(int line)
164         {
165                 //print("Searching for line %d\n",line);
166                 var l = -1;
167                 //foreach(int el in this.node_lines) {
168                         //print("all lines %d\n", el);
169                 //}
170                 
171                 
172                 foreach(int el in this.node_lines) {
173                         //print("?match %d\n", el);
174                         if (el < line) {
175                                 
176                                 l = el;
177                                 //print("LESS\n");
178                                 continue;
179                         }
180                         if (el == line) {
181                                 //print("SAME\n");
182                                 l = el;
183                                 break;
184                         }
185                         if (l > -1) {
186                                 var ret = this.node_lines_map.get(l);
187                                 if (line > ret.line_end) {
188                                         return null;
189                                 }
190                                 //print("RETURNING NODE ON LINE %d", l);
191                                 return ret;
192                         }
193                         return null;
194                         
195                 }
196                 if (l > -1) {
197                         var ret = this.node_lines_map.get(l);
198                         if (line > ret.line_end) {
199                                 return null;
200                         }
201                         //print("RETURNING NODE ON LINE %d", l);
202                         return ret;
203
204                 }
205                 return null;
206                 
207         }
208         public string lineToProp(int line)
209         {
210                 // assume lineToNode called first...
211                 var l = -1;
212                 //foreach(int el in this.lines) {
213                 //      //print("all lines %d\n", el);
214                 //
215                 
216                 
217                 foreach(int el in this.lines) {
218                         //print("?match %d\n", el);
219                         if (el < line) {
220                                 
221                                 l = el;
222                                 //print("LESS\n");
223                                 continue;
224                         }
225                         if (el == line) {
226                                 //print("SAME\n");
227                                 l = el;
228                                 break;
229                         }
230                         if (l > -1) {
231                                 //print("RETURNING NODE ON LINE %d", l);
232                                 return this.line_map.get(l);
233                         }
234                         return null;
235                         
236                 }
237                 if (l > -1) {
238                         //print("RETURNING NODE ON LINE %d", l);
239                         return this.line_map.get(l);
240                 }
241                 return null;
242         
243         }
244         
245         public bool getPropertyRange(string prop, out int start, out int end)
246         {
247                 start = -1;
248                 foreach(int el in this.lines) {
249                         if (start < 0) {
250                                 if (this.line_map.get(el) == prop) {
251                                         start = el;
252                                         end = el;
253                                 }
254                                 continue;
255                         }
256                         end = el -1;
257                         break;
258                 }
259                 return start > -1;
260         
261         
262         }
263         
264         public void dumpProps(string indent = "")
265         {
266                 print("%s:\n" , this.fqn());
267                 foreach(int el in this.lines) {
268                         print("%d: %s%s\n", el, indent, this.line_map.get(el));
269                 }
270                 foreach(Node n in this.items) {
271                         n.dumpProps(indent + "  ");
272                 }
273         }
274         
275         
276         
277         public string uid()
278         {
279                 if (this.props.get("id") == null) {
280                         uid_count++;
281                         return "uid-%d".printf(uid_count);
282                 }
283                 return this.props.get("id");
284         }
285         
286         
287         public bool hasChildren()
288         {
289                 return this.items.size > 0;
290         }
291         public bool hasXnsType()
292         {
293                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
294                         return true;
295                         
296                 }
297                 return false;
298         }
299         public string fqn()
300         {
301                 if (!this.hasXnsType ()) {
302                         return "";
303                 }
304                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
305
306         }
307         public void setFqn(string name)
308         {
309                 var ar = name.split(".");
310                 this.props.set("xtype", ar[ar.length-1]);
311                 var l = name.length - (ar[ar.length-1].length +1);
312                 this.props.set("$ xns", name.substring(0, l));
313                 //print("setFQN %s to %s\n", name , this.fqn());
314                                
315
316         }
317         // wrapper around get props that returns empty string if not found.
318         public string get(string key)
319         {
320                 var k = this.props.get(key);
321                 if (k != null) {
322                         return k;
323                 }
324                 
325                 k = this.props.get("$ " + key);
326                 if (k != null) {
327                         return k;
328                 }
329                 
330                 var iter = this.props.map_iterator();
331                 while (iter.next()) {
332                         var kk = iter.get_key().split(" ");
333                         if (kk[kk.length-1] == key) {
334                                 return iter.get_value();
335                         }
336                 }
337                 
338                 
339                 return "";
340                 
341         }
342         
343         public string get_key(string key)
344         {
345                 var k = this.props.get(key);
346                 if (k != null) {
347                         return key;
348                 }
349                 
350                 k = this.props.get("$ " + key);
351                 if (k != null) {
352                         return "$ " + key;
353                 }
354                 
355                 var iter = this.props.map_iterator();
356                 while (iter.next()) {
357                         var kk = iter.get_key().split(" ");
358                         if (kk[kk.length-1] == key) {
359                                 return iter.get_key();
360                         }
361                 }
362                 
363                 
364                 return "";
365                 
366         }
367         public void normalize_key(string key, out string kname, out string kflag, out string ktype)
368         {
369                 // key formats : XXXX
370                 // XXX - plain
371                 // string XXX - with type
372                 // $ XXX - with flag (no type)
373                 // $ string XXX - with flag
374                 kname = "";
375                 ktype = ""; // these used to contain '-' ???
376                 kflag = ""; // these used to contain '-' ???
377                 var kkv = key.strip().split(" ");
378                 string[] kk = {};
379                 for (var i = 0; i < kkv.length; i++) {
380                         if (kkv[i].length > 0 ) {
381                                 kk+= kkv[i];
382                         }
383                 }
384                 //print("normalize %s => %s\n", key,string.joinv("=:=",kk));
385                 
386                 switch(kk.length) {
387                         case 1: 
388                                 kname = kk[0];
389                                 return;
390                         case 2: 
391                                 kname = kk[1];
392                                 if (kk[0].length > 1) {
393                                         ktype = kk[0];
394                                 } else {
395                                         kflag = kk[0];
396                                 }
397                                 return;
398                         case 3:
399                                 kname = kk[2];
400                                 kflag = kk[0];
401                                 ktype = kk[1];
402                                 return;
403                 }
404                 // everything blank otherwise...
405         }
406         public void set(string key, string value) {
407                 this.props.set(key,value);
408         }
409          public bool has(string key)
410         {
411                 var k = this.props.get(key);
412                 if (k != null) {
413                         return true;
414                 }
415                 var iter = this.props.map_iterator();
416                 while (iter.next()) {
417                         var kk = iter.get_key().strip().split(" ");
418                         if (kk[kk.length-1] == key) {
419                                 return true;
420                         }
421                 }
422                 
423                 return false;
424                 
425         }
426
427         public void  remove()
428         {
429                 if (this.parent == null) {
430                         
431                         
432                         return;
433                 }
434                 var nlist = new Gee.ArrayList<Node>();
435                 for (var i =0;i < this.parent.items.size; i++) {
436                         if (this.parent.items.get(i) == this) {
437                                 continue;
438                         }
439                         nlist.add(this.parent.items.get(i));
440                 }
441                 this.parent.items = nlist;
442                 this.parent = null;
443
444         }
445          
446         /* creates javascript based on the rules */
447         public Node? findProp(string n) {
448                 for(var i=0;i< this.items.size;i++) {
449                         var p = this.items.get(i).get("* prop");
450                         if (this.items.get(i).get("* prop").length < 1) {
451                                 continue;
452                         }
453                         if (p == n) {
454                                 return this.items.get(i);
455                         }
456                 }
457                 return null;
458
459         }
460
461         
462         
463          
464         static Json.Generator gen = null;
465         
466         public string quoteString(string str)
467         {
468                 if (Node.gen == null) {
469                         Node.gen = new Json.Generator();
470                 }
471                  var n = new Json.Node(Json.NodeType.VALUE);
472                 n.set_string(str);
473  
474                 Node.gen.set_root (n);
475                 return  Node.gen.to_data (null);   
476         }
477
478         public void loadFromJson(Json.Object obj, int version) {
479                 obj.foreach_member((o , key, value) => {
480                         //print(key+"\n");
481                         if (key == "items") {
482                                 var ar = value.get_array();
483                                 ar.foreach_element( (are, ix, el) => {
484                                         var node = new Node();
485                                         node.parent = this;
486                                         node.loadFromJson(el.get_object(), version);
487                                         this.items.add(node);
488                                 });
489                                 return;
490                         }
491                         if (key == "listeners") {
492                                 var li = value.get_object();
493                                 li.foreach_member((lio , li_key, li_value) => {
494                                         this.listeners.set(li_key, li_value.get_string());
495
496                                 });
497                                 return;
498                         }
499                         var v = value.get_value();
500                         var sv =  Value (typeof (string));
501                         v.transform(ref sv);
502
503                         var rkey = key;
504                         if (version == 1) {
505                                 rkey = this.upgradeKey(key, (string)sv);
506                         }
507
508                         
509                         this.props.set(rkey,  (string)sv);
510                 });
511                 
512
513
514
515         }
516
517         public string upgradeKey(string key, string val)
518         {
519                 // convert V1 to V2
520                 if (key.length < 1) {
521                         return key;
522                 }
523                 switch(key) {
524                         case "*prop":
525                         case "*args":
526                         case ".ctor":
527                         case "|init":
528                                 return "* " + key.substring(1);
529                                 
530                         case "pack":
531                                 return "* " + key;
532                 }
533                 if (key[0] == '.') { // v2 does not start with '.' ?
534                         var bits = key.substring(1).split(":");
535                         if (bits[0] == "signal") {
536                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
537                         }
538                         return "# " + string.joinv(" ", bits);                  
539                 }
540                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
541                         return key;
542                 }
543                 var bits = key.substring(1).split(":");
544                 // two types '$' or '|' << for methods..
545                 // javascript 
546                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
547                         return "| " + key.substring(1);
548                 }
549                 // vala function..
550                 
551                 if  (Regex.match_simple ("^\\(", val.strip())) {
552                 
553                         return "| " + string.joinv(" ", bits);
554                 }
555                 
556                 // guessing it's a property..
557                 return "$ " + string.joinv(" ", bits);
558                 
559                 
560
561         }
562
563
564
565
566
567         
568         public Node  deepClone()
569         {
570                 var n = new Node();
571                 n.loadFromJson(this.toJsonObject(), 2);
572                 return n;
573
574         }
575         public string toJsonString()
576         {
577                 if (Node.gen == null) {
578                         Node.gen = new Json.Generator();
579                         gen.pretty =  true;
580                         gen.indent = 1;
581                 }
582                 var n = new Json.Node(Json.NodeType.OBJECT);
583                 n.set_object(this.toJsonObject () );
584                 Node.gen.set_root (n);
585                 return  Node.gen.to_data (null);   
586         }
587         
588         public Json.Object toJsonObject()
589         {
590                 var ret = new Json.Object();
591
592                 // listeners...
593                 if (this.listeners.size > 0) {
594                         var li = new Json.Object();
595                         ret.set_object_member("listeners", li);
596                         var liter = this.listeners.map_iterator();
597                         while (liter.next()) {
598                                 li.set_string_member(liter.get_key(), liter.get_value());
599                         }
600                 }
601                 //props
602                 if (this.props.size > 0 ) {
603                         var iter = this.props.map_iterator();
604                         while (iter.next()) {
605                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
606                         }
607                 }
608                 if (this.items.size > 0) {
609                         var ar = new Json.Array();
610                         ret.set_array_member("items", ar);
611                 
612                         // children..
613                         for(var i =0;i < this.items.size;i++) {
614                                 ar.add_object_element(this.items.get(i).toJsonObject());
615                         }
616                 }
617                 return ret;
618                 
619  
620         }
621          
622         public void jsonObjectsetMember(Json.Object o, string key, string val) {
623                 if (Lang.isBoolean(val)) {
624                         o.set_boolean_member(key, val.down() == "false" ? false : true);
625                         return;
626                 }
627                 
628                 
629                 if (Lang.isNumber(val)) {
630                         if (val.contains(".")) {
631                                 //print( "ADD " + key + "=" + val + " as a double?\n");
632                                 o.set_double_member(key, double.parse (val));
633                                 return;
634
635                         }
636                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
637                         o.set_int_member(key,long.parse(val));
638                         return;
639                 }
640                 ///print( "ADD " + key + "=" + val + " as a string?\n");
641                 o.set_string_member(key,val);
642                 
643         }
644         public string nodeTip()
645         {
646                 var ret = this.nodeTitle(true);
647                 var funcs = "";
648                 var props = "";
649                 var listen = "";
650                 var iter = this.props.map_iterator();
651                 while (iter.next()) {
652                         var i =  iter.get_key().strip();
653                         var val = iter.get_value().strip();
654                         if (val == null || val.length < 1) {
655                                 continue;
656                         }
657                         if ( i[0] != '|') {
658                                 props += "\n\t<b>" + 
659                                         GLib.Markup.escape_text(i) +"</b> : " + 
660                                         GLib.Markup.escape_text(val.split("\n")[0]);
661                                  
662                                 continue;
663                         }
664                 
665                         //if (i == "* init") { 
666                         //      continue;
667                         //}
668                         
669                         if (Regex.match_simple("^\\s*function", val)) { 
670                                 funcs += "\n\t<b>" + 
671                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
672                                         GLib.Markup.escape_text(val.split("\n")[0]);
673                                 continue;
674                         }
675                         if (Regex.match_simple("^\\s*\\(", val)) {
676                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
677                                         "</b> : " + 
678                                         GLib.Markup.escape_text(val.split("\n")[0]);
679                                 continue;
680                         }
681                         
682                 }
683                 iter = this.listeners.map_iterator();
684                 while (iter.next()) {
685                         var i =  iter.get_key().strip();
686                         var val = iter.get_value().strip();
687                         if (val == null || val.length < 1) {
688                                 continue;
689                         }
690                          listen += "\n\t<b>" + 
691                                         GLib.Markup.escape_text(i) +"</b> : " + 
692                                         GLib.Markup.escape_text(val.split("\n")[0]);
693                         
694                 }
695                 
696                 
697                 if (props.length > 0) {
698                         ret+="\n\nProperties:" + props;
699                 } 
700                 if (funcs.length > 0) {
701                         ret+="\n\nMethods:" + funcs;
702                 } 
703                 if (listen.length > 0) {
704                         ret+="\n\nListeners:" + listen;
705                 } 
706                 return ret;
707
708         }
709         public string nodeTitle(bool for_tip = false) {
710                 string[] txt = {};
711
712                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
713                 //if (sr) txt.push('<s>');
714
715                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
716                 
717                 //if (renderfull && c['|xns']) {
718                 var fqn = this.fqn();
719                 var fqn_ar = fqn.split(".");
720                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
721                         
722                 //}
723                 
724                 //if (c.xtype)    { txt.push(c.xtype); }
725                         
726                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
727                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
728                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
729                 
730                 
731                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
732                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
733                 if (this.has("html") && this.get("html").length > 0)     { 
734                         var ht = this.get("html").split("\n");
735                         if (ht.length > 1) {
736                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
737                         } else { 
738                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
739                         }
740                 }
741                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
742                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
743                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
744                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
745                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
746                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
747                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
748                 
749                 // for flat classes...
750                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
751                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
752                 
753                 
754                 //if (sr) txt.push('</s>');
755                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
756         }
757
758 }