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