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