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