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