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