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