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         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 = ""; // these used to contain '-' ???
278                 kflag = ""; // these used to contain '-' ???
279                 var kkv = key.strip().split(" ");
280                 string[] kk = {};
281                 for (var i = 0; i < kkv.length; i++) {
282                         if (kkv[i].length > 0 ) {
283                                 kk+= kkv[i];
284                         }
285                 }
286                 print("normalize %s => %s\n", key,string.joinv("=:=",kk));
287                 
288                 switch(kk.length) {
289                         case 1: 
290                                 kname = kk[0];
291                                 return;
292                         case 2: 
293                                 kname = kk[1];
294                                 if (kk[0].length > 1) {
295                                         ktype = kk[0];
296                                 } else {
297                                         kflag = kk[0];
298                                 }
299                                 return;
300                         case 3:
301                                 kname = kk[2];
302                                 kflag = kk[0];
303                                 ktype = kk[1];
304                                 return;
305                 }
306                 // everything blank otherwise...
307         }
308         public void set(string key, string value) {
309                 this.props.set(key,value);
310         }
311          public bool has(string key)
312         {
313                 var k = this.props.get(key);
314                 if (k != null) {
315                         return true;
316                 }
317                 var iter = this.props.map_iterator();
318                 while (iter.next()) {
319                         var kk = iter.get_key().strip().split(" ");
320                         if (kk[kk.length-1] == key) {
321                                 return true;
322                         }
323                 }
324                 
325                 return false;
326                 
327         }
328
329         public void  remove()
330         {
331                 if (this.parent == null) {
332                         
333                         
334                         return;
335                 }
336                 var nlist = new Gee.ArrayList<Node>();
337                 for (var i =0;i < this.parent.items.size; i++) {
338                         if (this.parent.items.get(i) == this) {
339                                 continue;
340                         }
341                         nlist.add(this.parent.items.get(i));
342                 }
343                 this.parent.items = nlist;
344                 this.parent = null;
345
346         }
347          
348         /* creates javascript based on the rules */
349         public Node? findProp(string n) {
350                 for(var i=0;i< this.items.size;i++) {
351                         var p = this.items.get(i).get("* prop");
352                         if (this.items.get(i).get("* prop").length < 1) {
353                                 continue;
354                         }
355                         if (p == n) {
356                                 return this.items.get(i);
357                         }
358                 }
359                 return null;
360
361         }
362
363         
364         
365          
366         static Json.Generator gen = null;
367         
368         public string quoteString(string str)
369         {
370                 if (Node.gen == null) {
371                         Node.gen = new Json.Generator();
372                 }
373                  var n = new Json.Node(Json.NodeType.VALUE);
374                 n.set_string(str);
375  
376                 Node.gen.set_root (n);
377                 return  Node.gen.to_data (null);   
378         }
379
380         public void loadFromJson(Json.Object obj, int version) {
381                 obj.foreach_member((o , key, value) => {
382                         //print(key+"\n");
383                         if (key == "items") {
384                                 var ar = value.get_array();
385                                 ar.foreach_element( (are, ix, el) => {
386                                         var node = new Node();
387                                         node.parent = this;
388                                         node.loadFromJson(el.get_object(), version);
389                                         this.items.add(node);
390                                 });
391                                 return;
392                         }
393                         if (key == "listeners") {
394                                 var li = value.get_object();
395                                 li.foreach_member((lio , li_key, li_value) => {
396                                         this.listeners.set(li_key, li_value.get_string());
397
398                                 });
399                                 return;
400                         }
401                         var v = value.get_value();
402                         var sv =  Value (typeof (string));
403                         v.transform(ref sv);
404
405                         var rkey = key;
406                         if (version == 1) {
407                                 rkey = this.upgradeKey(key, (string)sv);
408                         }
409
410                         
411                         this.props.set(rkey,  (string)sv);
412                 });
413                 
414
415
416
417         }
418
419         public string upgradeKey(string key, string val)
420         {
421                 // convert V1 to V2
422                 if (key.length < 1) {
423                         return key;
424                 }
425                 switch(key) {
426                         case "*prop":
427                         case "*args":
428                         case ".ctor":
429                         case "|init":
430                                 return "* " + key.substring(1);
431                                 
432                         case "pack":
433                                 return "* " + key;
434                 }
435                 if (key[0] == '.') { // v2 does not start with '.' ?
436                         var bits = key.substring(1).split(":");
437                         if (bits[0] == "signal") {
438                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
439                         }
440                         return "# " + string.joinv(" ", bits);                  
441                 }
442                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
443                         return key;
444                 }
445                 var bits = key.substring(1).split(":");
446                 // two types '$' or '|' << for methods..
447                 // javascript 
448                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
449                         return "| " + key.substring(1);
450                 }
451                 // vala function..
452                 
453                 if  (Regex.match_simple ("^\\(", val.strip())) {
454                 
455                         return "| " + string.joinv(" ", bits);
456                 }
457                 
458                 // guessing it's a property..
459                 return "$ " + string.joinv(" ", bits);
460                 
461                 
462
463         }
464
465
466
467
468
469         
470         public Node  deepClone()
471         {
472                 var n = new Node();
473                 n.loadFromJson(this.toJsonObject(), 2);
474                 return n;
475
476         }
477         public string toJsonString()
478         {
479                 if (Node.gen == null) {
480                         Node.gen = new Json.Generator();
481                         gen.pretty =  true;
482                         gen.indent = 1;
483                 }
484                 var n = new Json.Node(Json.NodeType.OBJECT);
485                 n.set_object(this.toJsonObject () );
486                 Node.gen.set_root (n);
487                 return  Node.gen.to_data (null);   
488         }
489         
490         public Json.Object toJsonObject()
491         {
492                 var ret = new Json.Object();
493
494                 // listeners...
495                 if (this.listeners.size > 0) {
496                         var li = new Json.Object();
497                         ret.set_object_member("listeners", li);
498                         var liter = this.listeners.map_iterator();
499                         while (liter.next()) {
500                                 li.set_string_member(liter.get_key(), liter.get_value());
501                         }
502                 }
503                 //props
504                 if (this.props.size > 0 ) {
505                         var iter = this.props.map_iterator();
506                         while (iter.next()) {
507                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
508                         }
509                 }
510                 if (this.items.size > 0) {
511                         var ar = new Json.Array();
512                         ret.set_array_member("items", ar);
513                 
514                         // children..
515                         for(var i =0;i < this.items.size;i++) {
516                                 ar.add_object_element(this.items.get(i).toJsonObject());
517                         }
518                 }
519                 return ret;
520                 
521  
522         }
523          
524         public void jsonObjectsetMember(Json.Object o, string key, string val) {
525                 if (Lang.isBoolean(val)) {
526                         o.set_boolean_member(key, val.down() == "false" ? false : true);
527                         return;
528                 }
529                 
530                 
531                 if (Lang.isNumber(val)) {
532                         if (val.contains(".")) {
533                                 //print( "ADD " + key + "=" + val + " as a double?\n");
534                                 o.set_double_member(key, double.parse (val));
535                                 return;
536
537                         }
538                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
539                         o.set_int_member(key,long.parse(val));
540                         return;
541                 }
542                 ///print( "ADD " + key + "=" + val + " as a string?\n");
543                 o.set_string_member(key,val);
544                 
545         }
546         public string nodeTip()
547         {
548                 var ret = this.nodeTitle(true);
549                 var funcs = "";
550                 var props = "";
551                 var listen = "";
552                 var iter = this.props.map_iterator();
553                 while (iter.next()) {
554                         var i =  iter.get_key().strip();
555                         var val = iter.get_value().strip();
556                         if (val == null || val.length < 1) {
557                                 continue;
558                         }
559                         if ( i[0] != '|') {
560                                 props += "\n\t<b>" + 
561                                         GLib.Markup.escape_text(i) +"</b> : " + 
562                                         GLib.Markup.escape_text(val.split("\n")[0]);
563                                  
564                                 continue;
565                         }
566                 
567                         //if (i == "* init") { 
568                         //      continue;
569                         //}
570                         
571                         if (Regex.match_simple("^\\s*function", val)) { 
572                                 funcs += "\n\t<b>" + 
573                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
574                                         GLib.Markup.escape_text(val.split("\n")[0]);
575                                 continue;
576                         }
577                         if (Regex.match_simple("^\\s*\\(", val)) {
578                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
579                                         "</b> : " + 
580                                         GLib.Markup.escape_text(val.split("\n")[0]);
581                                 continue;
582                         }
583                         
584                 }
585                 iter = this.listeners.map_iterator();
586                 while (iter.next()) {
587                         var i =  iter.get_key().strip();
588                         var val = iter.get_value().strip();
589                         if (val == null || val.length < 1) {
590                                 continue;
591                         }
592                          listen += "\n\t<b>" + 
593                                         GLib.Markup.escape_text(i) +"</b> : " + 
594                                         GLib.Markup.escape_text(val.split("\n")[0]);
595                         
596                 }
597                 
598                 
599                 if (props.length > 0) {
600                         ret+="\n\nProperties:" + props;
601                 } 
602                 if (funcs.length > 0) {
603                         ret+="\n\nMethods:" + funcs;
604                 } 
605                 if (listen.length > 0) {
606                         ret+="\n\nListeners:" + listen;
607                 } 
608                 return ret;
609
610         }
611         public string nodeTitle(bool for_tip = false) {
612                 string[] txt = {};
613
614                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
615                 //if (sr) txt.push('<s>');
616
617                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
618                 
619                 //if (renderfull && c['|xns']) {
620                 var fqn = this.fqn();
621                 var fqn_ar = fqn.split(".");
622                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
623                         
624                 //}
625                 
626                 //if (c.xtype)    { txt.push(c.xtype); }
627                         
628                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
629                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
630                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
631                 
632                 
633                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
634                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
635                 if (this.has("html") && this.get("html").length > 0)     { 
636                         var ht = this.get("html").split("\n");
637                         if (ht.length > 1) {
638                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
639                         } else { 
640                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
641                         }
642                 }
643                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
644                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
645                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
646                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
647                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
648                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
649                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
650                 
651                 // for flat classes...
652                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
653                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
654                 
655                 
656                 //if (sr) txt.push('</s>');
657                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
658         }
659
660 }