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