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