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