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