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