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