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