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