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