src/JsRender/NodeToJs.vala
[app.Builder.js] / src / JsRender / NodeToJs.vala
1 /**
2  * 
3  * Code to convert node tree to Javascript...
4  * 
5  * usage : x = (new JsRender.NodeToJs(node)).munge();
6  * 
7 */
8
9
10
11
12 public class JsRender.NodeToJs : Object {
13
14         static uint indent = 1;
15         static string indent_str = " ";
16         
17         Node node;
18         Gee.ArrayList<string>  doubleStringProps;  // need to think if this is a good idea like this
19         string pad;
20         
21         Gee.ArrayList<string> els;
22         //Gee.ArrayList<string> skip;
23         Gee.HashMap<string,string> ar_props;
24         
25         Gee.HashMap<string,string> out_props;
26         Gee.HashMap<string,string> out_listeners;       
27         Gee.HashMap<string,Node> out_nodeprops;
28         Gee.ArrayList<Node> out_children;
29         Gee.HashMap<string,Gee.ArrayList<Node>> out_props_array;
30         Gee.HashMap<string,Gee.ArrayList<string>> out_props_array_plain;        
31         
32         NodeToJs top;
33         public string ret;
34         
35         public int cur_line;
36
37         
38         public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad, NodeToJs? parent) 
39         {
40                 this.node = node;
41                 this.doubleStringProps = doubleStringProps;
42                 this.pad = pad;
43                 
44                 this.els = new Gee.ArrayList<string>(); 
45                 this.ar_props = new Gee.HashMap<string,string>();
46                 
47                 
48                 this.out_props = new Gee.HashMap<string,string>();
49                 this.out_listeners = new Gee.HashMap<string,string>();  
50                 this.out_nodeprops = new Gee.HashMap<string,Node>() ;
51                 this.out_children = new Gee.ArrayList<Node> ();
52                 this.out_props_array = new Gee.HashMap<string,Gee.ArrayList<Node>>() ;
53                 this.out_props_array_plain = new Gee.HashMap<string,Gee.ArrayList<string>>() ;
54         
55                 
56                 
57                 this.cur_line = parent == null ? 0 : parent.cur_line  ; //-1 as we usuall concat onto the existin gline?
58                 this.ret = "";
59                 this.top = parent == null ? this : parent.top;
60  
61         }
62         
63         
64         
65         
66         
67         
68         public string munge ( )
69         {
70                 //return this.mungeToString(this.node);
71
72                 this.node.line_start = this.cur_line;
73                 
74                 this.checkChildren();
75                 this.readProps();
76                 this.readArrayProps();
77                 this.readListeners();
78
79                 if (!this.node.props.has_key("* xinclude")) {
80                         this.iterChildren();
81                 }
82                 
83                 
84                 
85                 // no properties to output...
86                 if (this.els.size < 1) {
87                         return "";
88                 }
89
90                 this.mungeOut();
91                 
92                 // oprops...    
93                         
94                 var spad = this.pad.substring(0, this.pad.length-indent);
95                 
96                 
97                 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
98                 //print ("STR PROPS: " + str_props);
99                 
100                 
101                 
102                 if (!this.node.props.has_key("* xinclude")) {
103                         return   "{\n" +
104                                 this.pad  + str_props + 
105                                 "\n" + spad +  "}";
106                 }
107                 // xinclude...
108
109
110                 return "Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), "+
111                          "{\n" +
112                                 this.pad  + str_props + 
113                                 "\n" + spad +  "})";
114                      
115         } 
116                 /**
117         
118         This currently works by creating a key/value array of this.els, which is just an array of properties..
119         this is so that join() works...
120         
121         how this could work:
122         a) output header
123         b) output plan properties.
124         c) output listeners..
125         c) output *prop
126         g) output prop_arrays..
127         d) output children
128         e) 
129         
130         
131         
132         */
133         
134         public Gee.ArrayList<string> orderedPropKeys() {
135         
136                 var ret = new Gee.ArrayList<string> ();
137                 var niter = this.out_props.map_iterator();
138                 while(niter.next()) {
139                         ret.add(niter.get_key());
140                 }
141                 
142                 ret.sort((  a,  b) => {
143                         return ((string)a).collate((string)b);
144                         //if (a == b) return 0;
145                         //return a < b ? -1 : 1;
146                 });
147                 return ret;
148         }
149         public Gee.ArrayList<string> orderedListenerKeys() {
150         
151                 var ret = new Gee.ArrayList<string> ();
152                 var niter = this.out_listeners.map_iterator();
153                 while(niter.next()) {
154                         ret.add(niter.get_key());
155                 }
156                 
157                 ret.sort((  a,  b) => {
158                         return ((string)a).collate((string)b);
159                         //if (a == b) return 0;
160                         //return a < b ? -1 : 1;
161                 });
162                 return ret;
163         }
164         
165
166         public string mungeOut()
167         {
168                 this.node.line_start = this.cur_line;
169                 this.top.node.setNodeLine(this.cur_line, this.node);
170                 var spad = this.pad.substring(0, this.pad.length-indent);
171                 
172                 if (this.node.props.has_key("* xinclude")) {
173                         this.addLine("Roo.apply(" + this.node.props.get("* xinclude") + "._tree(), {");
174          
175                 } else {
176                         this.addLine("{");
177                 }
178                 var suffix = "";
179                 // output the items...
180                 // work out remaining items...
181                 var  total_nodes = this.out_props.size + 
182                                 this.out_props_array_plain.size + 
183                                 (this.out_listeners.size > 0 ? 1 : 0) +
184                                 this.out_nodeprops.size +
185                                 this.out_props_array.size +
186                                 (this.out_children.size > 0 ? 1 : 0);
187                 
188                 
189                 // * prop
190
191                 var niter = this.out_nodeprops.map_iterator();
192
193                 while(niter.next()) {
194                         total_nodes--;
195                         suffix = total_nodes > 0 ? "," : "";
196                         var l = this.pad + niter.get_key() + " : " + 
197                                         this.mungeChildNew(this.pad + indent_str, niter.get_value()) + suffix;
198                         this.addMultiLine(l);
199                 }       
200                 
201                 // plain properties.
202                 var iter = this.orderedPropKeys().list_iterator();
203                 while(iter.next()) {
204                         total_nodes--;
205                         suffix = total_nodes > 0 ? "," : "";
206                         var k = iter.get();
207                         var v = this.out_props.get(k);
208                         
209                         this.addMultiLine(this.pad + k + " : " + v + suffix);
210                 }
211                 /*
212                 //                              out_props_array_plain -- not used?
213                 var paiter = this.out_props_array_plain.map_iterator();
214
215                 while(paiter.next()) {
216                         total_nodes--;
217
218                         this.addLine(this.pad + paiter.get_key() + " : [");
219                         var paliter = paiter.get_value().list_iterator();
220                         while (paliter.next()) {
221                                 suffix = paliter.has_next()  ? "," : "";
222                                 this.addMultiLine(this.pad + indent_str +   paliter.get() + suffix);
223                         }
224
225                         suffix = total_nodes > 0 ? "," : "";
226 //                                      this.mungeChild(this.pad + indent_str, niter.get_value())
227                         this.addLine(this.pad + "]" + suffix);                  
228                 }       
229                 */
230                 
231                 
232                                  
233                 // prop arrays...
234                 
235                 var piter = this.out_props_array.map_iterator();
236
237                 while(piter.next()) {
238                         total_nodes--;
239
240                         this.addLine(this.pad + piter.get_key() + " : [");
241                         var pliter = piter.get_value().list_iterator();
242                         while (pliter.next()) {
243                                 suffix = pliter.has_next()  ? "," : "";
244                                 this.addMultiLine(this.pad + indent_str + 
245                                         this.mungeChildNew(this.pad + indent_str  + indent_str, pliter.get()) + suffix);
246                         }
247
248                         suffix = total_nodes > 0 ? "," : "";
249  
250                         this.addLine(this.pad + "]" + suffix);                  
251                 }       
252                 // listeners..
253                 
254                 if (this.out_listeners.size > 0 ) { 
255                         total_nodes--;
256                         this.addLine(this.pad + "listeners : {");
257                         iter = this.orderedListenerKeys().list_iterator();
258                          
259                         var sz = this.out_listeners.size;
260                         while(iter.next()) {
261                                 sz--;
262                                 suffix = sz > 0 ? "," : "";
263                                 var k = iter.get();
264                                 var v = this.out_listeners.get(k);
265                                 this.addMultiLine(this.pad + indent_str + k + " : " + v + suffix);
266                         }
267                         suffix = total_nodes > 0 ? "," : "";
268                         this.addLine(this.pad + "}" + suffix);                  
269                         
270                 }
271                 // children..
272                 if (this.out_children.size > 0) {
273                         this.addLine(this.pad + "items  : [" );
274                         var cniter = this.out_children.list_iterator();
275                         while (cniter.next()) {
276                                 suffix = cniter.has_next()  ? "," : "";
277                                 this.addMultiLine(this.pad + indent_str +
278                                         this.mungeChildNew(this.pad + indent_str  + indent_str, cniter.get()) + suffix
279                                 );
280                                 
281                         }
282                         
283                         this.addLine(this.pad +   "]");
284                 }
285                 
286                 if (this.node.props.has_key("* xinclude")) {
287                         this.ret += spad + "})";
288          
289                 } else {
290                         this.ret += spad + "}";
291                 }
292                 this.node.line_end = this.cur_line;
293                 
294                 return this.ret;
295         
296         }
297         
298  
299         
300         
301         
302         public void addLine(string str= "")
303         {
304                 this.cur_line ++;
305                 this.ret += str+ "\n";
306                 //this.ret +=  "/*%d*/ ".printf(this.cur_line -1) + str + "\n";
307                 
308                 
309         }
310         
311         public void addMultiLine(string str= "")
312         {
313                 //var l = cur_line;
314                 this.cur_line += str.split("\n").length;
315                 //this.ret +=  "/*%d*/ ".printf(l) + str + "\n";
316                 this.ret +=   str + "\n";
317         }
318
319         string gLibStringListJoin( string sep, Gee.ArrayList<string> ar) 
320         {
321                 var ret = "";
322                 for (var i = 0; i < ar.size; i++) {
323                         ret += i>0 ? sep : "";
324                         ret += ar.get(i);
325                 }
326                 return ret;
327
328         }
329         public string mungeChild(string pad ,  Node cnode)
330         {
331                 var x = new  NodeToJs(cnode, this.doubleStringProps, pad, this);
332                 return x.munge();
333         }
334         
335         public string mungeChildNew(string pad ,  Node cnode )
336         {
337                 var x = new  NodeToJs(cnode, this.doubleStringProps, pad, this);
338          
339                 x.munge();
340                 return x.ret;
341         }
342         
343
344         
345         public void checkChildren () 
346         {
347                 
348                  
349                 // look throught he chilren == looking for * prop.. -- fixme might not work..
350                 
351                 
352                 if (!this.node.hasChildren()) {
353                         return;
354                 }
355                 // look for '*props'
356            
357                 for (var ii =0; ii< this.node.items.size; ii++) {
358                         var pl = this.node.items.get(ii);
359                         if (!pl.props.has_key("* prop")) {
360                                 //newitems.add(pl);
361                                 continue;
362                         }
363                         
364                         //print(JSON.stringify(pl,null,4));
365                         // we have a prop...
366                         //var prop = pl['*prop'] + '';
367                         //delete pl['*prop'];
368                         var prop = pl.get("* prop");
369                         print("got prop "+ prop + "\n");
370                         
371                         // name ends in [];
372                         if (! Regex.match_simple("\\[\\]$", prop)) {
373                                 // it's a standard prop..
374                                 
375                                 // munge property..??
376                                 
377                                 this.out_nodeprops.set(prop, pl);
378                                 
379                                 this.els.add( prop  + " : " + this.mungeChild (  this.pad + indent_str,  pl));
380                                 
381                                 
382                                 //keys.push(prop);
383                                 continue;
384                         }
385
386
387
388                         
389                         var sprop  = prop.replace("[]", "");
390                         print("sprop is : " + sprop + "\n");
391                         
392                         // it's an array type..
393                         var old = "";
394                         if (!this.ar_props.has_key(sprop)) {
395                                 
396                                 this.ar_props.set(sprop, "");
397                                 this.out_props_array.set(sprop, new Gee.ArrayList<Node>());
398                         } else {
399                                 old = this.ar_props.get(sprop);
400                         }
401                         var nstr  = old += old.length > 0 ? ",\n" : "";
402                         nstr += this.mungeChild( this.pad + indent_str + indent_str + indent_str ,   pl);
403                         this.out_props_array.get(sprop).add( pl);
404                         this.ar_props.set(sprop, nstr);
405                          
406                         
407                 }
408                  
409         }
410         /*
411  * Standardize this crap...
412  * 
413  * standard properties (use to set)
414  *          If they are long values show the dialog..
415  *
416  * someprop : ....
417  * bool is_xxx  :: can show a pulldown.. (true/false)
418  * string html  
419  * $ string html  = string with value interpolated eg. baseURL + ".." 
420  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
421  * $ untypedvalue = javascript untyped value...  
422  * _ string html ... = translatable..
423
424  * 
425  * object properties (not part of the GOjbect being wrapped?
426  * # Gee.ArrayList<Xcls_fileitem> fileitems
427  * 
428  * signals
429  * @ void open 
430  * 
431  * methods -- always text editor..
432  * | void clearFiles
433  * | someJSmethod
434  * 
435  * specials
436  * * prop -- string
437  * * args  -- string
438  * * ctor -- string
439  * * init -- big string?
440  * 
441  * event handlers (listeners)
442  *   just shown 
443  * 
444  * -----------------
445  * special ID values
446  *  +XXXX -- indicates it's a instance property / not glob...
447  *  *XXXX -- skip writing glob property (used as classes that can be created...)
448  * 
449  * 
450  */
451         public void readProps()
452         {
453                 string left;
454                 Regex func_regex ;
455
456                 if (this.node.props.has_key("$ xns")) {
457                         this.out_props.set("'|xns'", "'" +  this.node.props.get("$ xns") + "'" );
458                         
459                         this.els.add("'|xns' : '" + this.node.props.get("$ xns") + "'");
460
461                 }
462
463                 
464                 try {
465                         func_regex = new Regex("^\\s+|\\s+$");
466                 } catch (Error e) {
467                         print("failed to build regex");
468                         return;
469                 }
470                 // sort the key's so they always get rendered in the same order..
471                 
472                 var keys = new Gee.ArrayList<string>();
473                 var piter = this.node.props.map_iterator();
474                 while (piter.next() ) {
475                         string k;
476                         string ktype;
477                         string kflag;
478                         this.node.normalize_key(piter.get_key(), out k, out kflag, out ktype);
479                         
480                         keys.add(k);
481                 }
482                 
483                 keys.sort((  a,  b) => {
484                         return ((string)a).collate((string)b);
485                         //if (a == b) return 0;
486                         //return a < b ? -1 : 1;
487                 });
488                 
489                 
490                 
491                 for (var i = 0; i< keys.size; i++) {
492                         var key = this.node.get_key(keys.get(i));
493                         print("ADD KEY %s\n", key);
494                         string k;
495                         string ktype;
496                         string kflag;
497                         
498                         this.node.normalize_key(key, out k, out kflag, out ktype);
499                         
500                         
501                         var v = this.node.get(key);
502                          
503                         
504                         //if (this.skip.contains(k) ) {
505                         //      continue;
506                         //}
507                         if (  Regex.match_simple("\\[\\]$", k)) {
508                                 // array .. not supported... here?
509                                 
510
511                         }
512                         
513                         string leftv = k;
514                         // skip builder stuff. prefixed with  '.' .. just like unix fs..
515                         if (kflag == ".") { // |. or . -- do not output..
516                                 continue;
517                         }
518                          if (kflag == "*") {
519                                 // ignore '* prop'; ??? 
520                                 continue;
521                          }
522                                 
523                         
524                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
525                                 left = "'" + leftv + "'";
526                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
527                                 var val = this.node.quoteString(leftv);
528                                 
529                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
530                         } else {
531                                 left = leftv;
532                         }
533                          
534                          
535                         // next.. is it a function.. or a raw string..
536                         if (
537                                 kflag == "|" 
538                                 || 
539                                 kflag == "$" 
540                                 || 
541                                 ktype == "function"
542                                
543                                 // ??? any others that are raw output..
544                                 ) {
545                                 // does not hapepnd with arrays.. 
546                                 if (v.length < 1) {  //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
547                                         continue;
548                                 }
549                                 /*
550                                 print(v);
551                                 string str = "";
552                                 try {
553                                         str = func_regex.replace(v,v.length, 0, "");
554                                 } catch(Error e) {
555                                         print("regex failed");
556                                         return "";
557                                 }
558                                 */
559                                 var str = v.strip();
560                                   
561                                 var lines = str.split("\n");
562                                 var nstr = "" + str;
563                                 if (lines.length > 0) {
564                                         nstr =  string.joinv("\n" + this.pad, lines);
565                                         //nstr =  string.joinv("\n", lines);
566                                 }
567                                 this.out_props.set(left, nstr);
568                                 //print("==> " +  str + "\n");
569                                 this.els.add(left + " : "+  nstr);
570                                 continue;
571                         }
572                         // standard..
573                         
574                         
575                         if (
576                                 Lang.isNumber(v) 
577                                 || 
578                                 Lang.isBoolean(v)
579                                 ||
580                                 ktype.down() == "boolean"
581                                 || 
582                                 ktype.down() == "bool"
583                                 || 
584                                 ktype.down() == "number"
585                                 || 
586                                 ktype.down() == "int"
587                             ) { // boolean or number...?
588                             this.out_props.set(left, v.down());
589                                 this.els.add(left + " : " + v.down() );
590                                 continue;
591                         }
592                         
593                         // is it a translated string?
594                         
595                         
596                         
597                         
598                         // strings..
599                         //if (this.doubleStringProps.size < 1) {
600                         //      this.els.add(left + this.node.quoteString(v));
601                         //      continue;
602                         //}
603                    
604                         if ((this.doubleStringProps.index_of(k) > -1) || 
605                                 (ktype.down() == "string" && k[0] == '_')
606                         
607                         ) {
608                                 // then use the translated version...
609                                 
610                                 var com = " /* " + 
611                                         (v.split("\n").length > 1 ?
612                                                 ("\n" + string.joinv(this.pad +  "\n", v.split("\n")).replace("*/", "* - /") + "\n" + this.pad + "*/ ") :
613                                                 (v.replace("*/", "* - /") + " */")
614                                         );
615                                 
616                                 this.els.add(left + " : _this._strings['" + 
617                                         GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
618                                         "']"
619                                 );
620                                 this.out_props.set(left, "_this._strings['" + 
621                                         GLib.Checksum.compute_for_string (ChecksumType.MD5, v) +
622                                         "']" + com);
623                                 continue;
624                         }
625                  
626                         // otherwise it needs to be encapsulated.. as single quotes..
627                         
628                         var vv = this.node.quoteString(v);
629                         // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
630                         this.els.add(left + " : " +  "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
631                         this.out_props.set(left,  "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
632
633                    
634                    
635                    
636                 }
637         }
638         public void readArrayProps()
639         {
640         
641                 // this is not needed in the new version
642                 // as array props are handled more directly..
643                 
644                 // handle the childitems  that are arrays.. eg. button[] = {  }...
645                 
646                 // note this does not handle a mix of nodes and properties with the same 
647                 
648                 string left;
649                 
650                 var iter = this.ar_props.map_iterator();
651                 while (iter.next()) {
652                         var k = iter.get_key();
653                         var right = iter.get_value();
654                         
655                         string leftv = k[0] == '|' ? k.substring(1) : k;
656                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
657                                 left = "'" + leftv + "'";
658                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
659                                 var val = this.node.quoteString(leftv);
660                                 
661                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
662                         } else {
663                                 left = leftv;
664                         }
665
666                         
667                         if (right.length > 0){
668                                 //if (this.out_props_array_plain.has_key(left)) {
669                                 //      this.out_props_array_plain.set(left, new Gee.ArrayList<string>());
670                                 //}
671                                 //this.out_props_array_plain.get(left).add(right);
672                         
673                                 this.els.add(left + " : [\n" +  this.pad + indent_str + indent_str +  
674                                              right + "\n" + this.pad + "]");
675                         }
676                 
677                         
678                 }
679
680         }
681         public void readListeners()
682         {
683                 
684                 if (this.node.listeners.size < 1) {
685                         return;
686                 }
687                 // munge the listeners.
688                 //print("ADDING listeners?");
689         
690  
691         
692         
693                 var keys = new Gee.ArrayList<string>();
694                 var piter = this.node.listeners.map_iterator();
695                 while (piter.next() ) {
696                          
697                         keys.add(piter.get_key());
698                 }
699                 keys.sort((  a,  b) => {
700                         return ((string)a).collate((string)b);
701                         //if (a == b) return 0;
702                         //return a < b ? -1 : 1;
703                 });
704         
705                 var itms = "listeners : {\n";
706                 
707                 for (var i = 0; i< keys.size; i++) {
708                         var key = keys.get(i);
709                         var val = this.node.listeners.get(key);
710                 
711         
712                         itms += i >0 ? ",\n" : "";      
713                         // 
714                         var str = val.strip();
715                         var lines = str.split("\n");
716                         if (lines.length > 0) {
717                                 //str = string.joinv("\n" + this.pad + "           ", lines);
718                                 str = string.joinv("\n" + this.pad + indent_str + indent_str , lines);
719                         }
720                         
721                         itms +=  this.pad + indent_str  + key.replace("|", "")  + " : " + str;
722                         this.out_listeners.set(key.replace("|", "") ,str);
723                 
724                         
725                 }
726                 itms += "\n" + this.pad + "}";
727                 //print ( "ADD " + itms); 
728                 this.els.add(itms);
729
730         }
731
732         public void iterChildren()
733         {
734                 
735                 
736                 // finally munge the children...
737                 if (this.node.items.size < 1) {
738                         return;
739                 }
740                 var itms = "items : [\n";
741                 var n = 0;
742                 for(var i = 0; i < this.node.items.size;i++) {
743                         var ele = this.node.items.get(i);
744                         if (ele.props.has_key("* prop")) {
745                                 continue;
746                         }
747                         if (n > 0) {
748                                  itms += ",\n";
749                         }
750                         n++;
751                         itms += this.pad + indent_str  +
752                                 this.mungeChild( this.pad + indent_str + indent_str ,  ele);
753                         this.out_children.add(ele);
754                         
755                 }
756                 itms +=  "\n"+  this.pad + "]"  + "\n";
757                 this.els.add(itms);
758         }
759
760                 // finally output listeners...
761                 
762         public void xIncludeToString()
763         {
764                 
765
766         }
767
768 }
769         
770          
771         
772