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