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