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