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