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