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