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