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