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