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