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