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