src/JsRender/Node.vala
[app.Builder.js] / src / JsRender / Node.vala
1
2 // test..
3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
4
5
6 /*
7  * 
8  * props:
9  * 
10  * key value view of properties.
11  * 
12  * Old standard..
13  * XXXXX : YYYYY  -- standard - should be rendered as XXXX : "YYYY" usually.
14  * |XXXXX : YYYYY  -- standard - should be rendered as XXXX : YYYY usually.
15  * |init  -- the initialization...
16  * *prop : a property which is actually an object definition... 
17  * *args : contructor args
18  * .ctor : Full contruct line...  
19  * 
20  * Newer code
21  * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type  name 
22  * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23  *  "|void:clearFiles": "() .... some code...."  | type name
24  *
25  * 
26  * 
27  * 
28  * 
29  * Standardize this crap...
30  * 
31  * standard properties (use to set)
32  *          If they are long values show the dialog..
33  * 
34  * bool is_xxx  :: can show a pulldown.. (true/false)
35  * string html  
36  * $ string html  = string with value interpolated eg. baseURL + ".." 
37  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
38  * $ untypedvalue = javascript untyped value... 
39  * 
40  * object properties (not part of the GOjbect being wrapped?
41  * # Gee.ArrayList<Xcls_fileitem> fileitems
42  * 
43  * signals
44  * @ void open 
45  * 
46  * methods -- always text editor..
47  * | void clearFiles
48  * | someJSmethod
49  * 
50  * specials
51  * * prop -- string
52  * * args  -- string
53  * * ctor -- string
54  * * init -- big string?
55  * 
56  * event handlers (listeners)
57  *   just shown 
58  * 
59  * -----------------
60  * special ID values
61  *  +XXXX -- indicates it's a instance property / not glob...
62  *  *XXXX -- skip writing glob property (used as classes that can be created...)
63  *  _XXXX -- (string) a translatable string.
64  * 
65  * 
66  *  FORMATING?
67 .method {
68          color : green;
69          font-weight: bold;      
70 }
71 .prop {
72         color : #333;
73 }
74 .prop-code {
75     font-style: italic;
76  }
77 .listener {
78     color: #600;
79     font-weight: bold;   
80 }
81 .special { 
82   color : #00c;    font-weight: bold;    
83
84
85 */
86
87
88
89
90
91
92 public class JsRender.Node : Object {
93         
94
95         public static int uid_count = 0;
96         
97         public Node parent;
98         public Gee.ArrayList<Node> items; // child items..
99         
100         public Gee.HashMap<string,string> props; // the properties..
101         public Gee.HashMap<string,string> listeners; // the listeners..
102         public string  xvala_cls;
103         public string xvala_xcls; // 'Xcls_' + id;
104         public string xvala_id; // item id or ""
105         public int line_start;
106         public int line_end;
107         public Gee.ArrayList<int> lines;
108         public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....
109         public Gee.ArrayList<int> node_lines;
110         public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
111         
112
113         public Node()
114         {
115                 this.items = new Gee.ArrayList<Node>();
116                 this.props = new Gee.HashMap<string,string>();
117                 this.listeners = new Gee.HashMap<string,string>();
118                 this.xvala_cls = "";
119                 this.xvala_xcls = "";
120                 this.xvala_id = "";
121                 this.parent = null;
122                 this.line_start = -1;
123                 this.line_end = -1;             
124                 this.lines = new Gee.ArrayList<int>();
125                 this.line_map = new Gee.HashMap<int,string>();
126                 this.node_lines = new Gee.ArrayList<int>();
127                 this.node_lines_map = new Gee.HashMap<int,Node>();
128                 
129         }
130         
131         public void setNodeLine(int line, Node node) {
132                 this.node_lines.add(line);
133                 this.node_lines_map.set(line, node);
134         }
135         
136         public void setLine(int line, string type, string prop) {
137                 this.lines.add(line);
138                 this.line_map.set(line, type +":" + prop);
139         }
140         public void sortLines() {
141                 this.lines.sort((a,b) => {   
142                         return (int)a-(int)b;
143                 });
144                 this.node_lines.sort((a,b) => {   
145                         return (int)a-(int)b;
146                 });
147         }
148         public Node lineToNode(int line)
149         {
150                 var l = -1;
151                 foreach(int el in this.node_lines) {
152                         if (el < line) {
153                                 l = el;
154                                 continue;
155                         }
156                         if (el == line) {
157                                 l = el;
158                         }
159                         if (l > -1) {
160                                 return this.node_lines_map.get(l);
161                         }
162                         return null;
163                         
164                 }
165                 if (l > -1) {
166                         return this.node_lines_map.get(l);
167                 
168                 return null;
169                 
170         }
171         
172         public string uid()
173         {
174                 if (this.props.get("id") == null) {
175                         uid_count++;
176                         return "uid-%d".printf(uid_count);
177                 }
178                 return this.props.get("id");
179         }
180         
181         
182         public bool hasChildren()
183         {
184                 return this.items.size > 0;
185         }
186         public bool hasXnsType()
187         {
188                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
189                         return true;
190                         
191                 }
192                 return false;
193         }
194         public string fqn()
195         {
196                 if (!this.hasXnsType ()) {
197                         return "";
198                 }
199                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
200
201         }
202         public void setFqn(string name)
203         {
204                 var ar = name.split(".");
205                 this.props.set("xtype", ar[ar.length-1]);
206                 var l = name.length - (ar[ar.length-1].length +1);
207                 this.props.set("$ xns", name.substring(0, l));
208                 print("setFQN %s to %s\n", name , this.fqn());
209                                
210
211         }
212         // wrapper around get props that returns empty string if not found.
213         public string get(string key)
214         {
215                 var k = this.props.get(key);
216                 if (k != null) {
217                         return k;
218                 }
219                 
220                 k = this.props.get("$ " + key);
221                 if (k != null) {
222                         return k;
223                 }
224                 
225                 var iter = this.props.map_iterator();
226                 while (iter.next()) {
227                         var kk = iter.get_key().split(" ");
228                         if (kk[kk.length-1] == key) {
229                                 return iter.get_value();
230                         }
231                 }
232                 
233                 
234                 return "";
235                 
236         }
237         
238         public string get_key(string key)
239         {
240                 var k = this.props.get(key);
241                 if (k != null) {
242                         return key;
243                 }
244                 
245                 k = this.props.get("$ " + key);
246                 if (k != null) {
247                         return "$ " + key;
248                 }
249                 
250                 var iter = this.props.map_iterator();
251                 while (iter.next()) {
252                         var kk = iter.get_key().split(" ");
253                         if (kk[kk.length-1] == key) {
254                                 return iter.get_key();
255                         }
256                 }
257                 
258                 
259                 return "";
260                 
261         }
262         public void normalize_key(string key, out string kname, out string kflag, out string ktype)
263         {
264                 // key formats : XXXX
265                 // XXX - plain
266                 // string XXX - with type
267                 // $ XXX - with flag (no type)
268                 // $ string XXX - with flag
269                 kname = "";
270                 ktype = "-";
271                 kflag = "-";
272                 var kk = key.strip().split(" ");
273                 switch(kk.length) {
274                         case 1: 
275                                 kname = kk[0];
276                                 return;
277                         case 2: 
278                                 kname = kk[1];
279                                 if (kk[0].length > 1) {
280                                         ktype = kk[0];
281                                 } else {
282                                         kflag = kk[0];
283                                 }
284                                 return;
285                         case 3:
286                                 kname = kk[2];
287                                 kflag = kk[0];
288                                 ktype = kk[1];
289                                 return;
290                 }
291                 // everything blank otherwise...
292         }
293         public void set(string key, string value) {
294                 this.props.set(key,value);
295         }
296          public bool has(string key)
297         {
298                 var k = this.props.get(key);
299                 if (k != null) {
300                         return true;
301                 }
302                 var iter = this.props.map_iterator();
303                 while (iter.next()) {
304                         var kk = iter.get_key().strip().split(" ");
305                         if (kk[kk.length-1] == key) {
306                                 return true;
307                         }
308                 }
309                 
310                 return false;
311                 
312         }
313
314         public void  remove()
315         {
316                 if (this.parent == null) {
317                         
318                         
319                         return;
320                 }
321                 var nlist = new Gee.ArrayList<Node>();
322                 for (var i =0;i < this.parent.items.size; i++) {
323                         if (this.parent.items.get(i) == this) {
324                                 continue;
325                         }
326                         nlist.add(this.parent.items.get(i));
327                 }
328                 this.parent.items = nlist;
329                 this.parent = null;
330
331         }
332          
333         /* creates javascript based on the rules */
334         public Node? findProp(string n) {
335                 for(var i=0;i< this.items.size;i++) {
336                         var p = this.items.get(i).get("* prop");
337                         if (this.items.get(i).get("* prop").length < 1) {
338                                 continue;
339                         }
340                         if (p == n) {
341                                 return this.items.get(i);
342                         }
343                 }
344                 return null;
345
346         }
347
348         
349         
350          
351         static Json.Generator gen = null;
352         
353         public string quoteString(string str)
354         {
355                 if (Node.gen == null) {
356                         Node.gen = new Json.Generator();
357                 }
358                  var n = new Json.Node(Json.NodeType.VALUE);
359                 n.set_string(str);
360  
361                 Node.gen.set_root (n);
362                 return  Node.gen.to_data (null);   
363         }
364
365         public void loadFromJson(Json.Object obj, int version) {
366                 obj.foreach_member((o , key, value) => {
367                         //print(key+"\n");
368                         if (key == "items") {
369                                 var ar = value.get_array();
370                                 ar.foreach_element( (are, ix, el) => {
371                                         var node = new Node();
372                                         node.parent = this;
373                                         node.loadFromJson(el.get_object(), version);
374                                         this.items.add(node);
375                                 });
376                                 return;
377                         }
378                         if (key == "listeners") {
379                                 var li = value.get_object();
380                                 li.foreach_member((lio , li_key, li_value) => {
381                                         this.listeners.set(li_key, li_value.get_string());
382
383                                 });
384                                 return;
385                         }
386                         var v = value.get_value();
387                         var sv =  Value (typeof (string));
388                         v.transform(ref sv);
389
390                         var rkey = key;
391                         if (version == 1) {
392                                 rkey = this.upgradeKey(key, (string)sv);
393                         }
394
395                         
396                         this.props.set(rkey,  (string)sv);
397                 });
398                 
399
400
401
402         }
403
404         public string upgradeKey(string key, string val)
405         {
406                 // convert V1 to V2
407                 if (key.length < 1) {
408                         return key;
409                 }
410                 switch(key) {
411                         case "*prop":
412                         case "*args":
413                         case ".ctor":
414                         case "|init":
415                                 return "* " + key.substring(1);
416                                 
417                         case "pack":
418                                 return "* " + key;
419                 }
420                 if (key[0] == '.') { // v2 does not start with '.' ?
421                         var bits = key.substring(1).split(":");
422                         if (bits[0] == "signal") {
423                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
424                         }
425                         return "# " + string.joinv(" ", bits);                  
426                 }
427                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
428                         return key;
429                 }
430                 var bits = key.substring(1).split(":");
431                 // two types '$' or '|' << for methods..
432                 // javascript 
433                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
434                         return "| " + key.substring(1);
435                 }
436                 // vala function..
437                 
438                 if  (Regex.match_simple ("^\\(", val.strip())) {
439                 
440                         return "| " + string.joinv(" ", bits);
441                 }
442                 
443                 // guessing it's a property..
444                 return "$ " + string.joinv(" ", bits);
445                 
446                 
447
448         }
449
450
451
452
453
454         
455         public Node  deepClone()
456         {
457                 var n = new Node();
458                 n.loadFromJson(this.toJsonObject(), 2);
459                 return n;
460
461         }
462         public string toJsonString()
463         {
464                 if (Node.gen == null) {
465                         Node.gen = new Json.Generator();
466                         gen.pretty =  true;
467                         gen.indent = 1;
468                 }
469                 var n = new Json.Node(Json.NodeType.OBJECT);
470                 n.set_object(this.toJsonObject () );
471                 Node.gen.set_root (n);
472                 return  Node.gen.to_data (null);   
473         }
474         
475         public Json.Object toJsonObject()
476         {
477                 var ret = new Json.Object();
478
479                 // listeners...
480                 if (this.listeners.size > 0) {
481                         var li = new Json.Object();
482                         ret.set_object_member("listeners", li);
483                         var liter = this.listeners.map_iterator();
484                         while (liter.next()) {
485                                 li.set_string_member(liter.get_key(), liter.get_value());
486                         }
487                 }
488                 //props
489                 if (this.props.size > 0 ) {
490                         var iter = this.props.map_iterator();
491                         while (iter.next()) {
492                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
493                         }
494                 }
495                 if (this.items.size > 0) {
496                         var ar = new Json.Array();
497                         ret.set_array_member("items", ar);
498                 
499                         // children..
500                         for(var i =0;i < this.items.size;i++) {
501                                 ar.add_object_element(this.items.get(i).toJsonObject());
502                         }
503                 }
504                 return ret;
505                 
506  
507         }
508          
509         public void jsonObjectsetMember(Json.Object o, string key, string val) {
510                 if (Lang.isBoolean(val)) {
511                         o.set_boolean_member(key, val.down() == "false" ? false : true);
512                         return;
513                 }
514                 
515                 
516                 if (Lang.isNumber(val)) {
517                         if (val.contains(".")) {
518                                 //print( "ADD " + key + "=" + val + " as a double?\n");
519                                 o.set_double_member(key, double.parse (val));
520                                 return;
521
522                         }
523                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
524                         o.set_int_member(key,long.parse(val));
525                         return;
526                 }
527                 ///print( "ADD " + key + "=" + val + " as a string?\n");
528                 o.set_string_member(key,val);
529                 
530         }
531         public string nodeTip()
532         {
533                 var ret = this.nodeTitle(true);
534                 var funcs = "";
535                 var props = "";
536                 var listen = "";
537                 var iter = this.props.map_iterator();
538                 while (iter.next()) {
539                         var i =  iter.get_key().strip();
540                         var val = iter.get_value().strip();
541                         if (val == null || val.length < 1) {
542                                 continue;
543                         }
544                         if ( i[0] != '|') {
545                                 props += "\n\t<b>" + 
546                                         GLib.Markup.escape_text(i) +"</b> : " + 
547                                         GLib.Markup.escape_text(val.split("\n")[0]);
548                                  
549                                 continue;
550                         }
551                 
552                         //if (i == "* init") { 
553                         //      continue;
554                         //}
555                         
556                         if (Regex.match_simple("^\\s*function", val)) { 
557                                 funcs += "\n\t<b>" + 
558                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
559                                         GLib.Markup.escape_text(val.split("\n")[0]);
560                                 continue;
561                         }
562                         if (Regex.match_simple("^\\s*\\(", val)) {
563                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
564                                         "</b> : " + 
565                                         GLib.Markup.escape_text(val.split("\n")[0]);
566                                 continue;
567                         }
568                         
569                 }
570                 iter = this.listeners.map_iterator();
571                 while (iter.next()) {
572                         var i =  iter.get_key().strip();
573                         var val = iter.get_value().strip();
574                         if (val == null || val.length < 1) {
575                                 continue;
576                         }
577                          listen += "\n\t<b>" + 
578                                         GLib.Markup.escape_text(i) +"</b> : " + 
579                                         GLib.Markup.escape_text(val.split("\n")[0]);
580                         
581                 }
582                 
583                 
584                 if (props.length > 0) {
585                         ret+="\n\nProperties:" + props;
586                 } 
587                 if (funcs.length > 0) {
588                         ret+="\n\nMethods:" + funcs;
589                 } 
590                 if (listen.length > 0) {
591                         ret+="\n\nListeners:" + listen;
592                 } 
593                 return ret;
594
595         }
596         public string nodeTitle(bool for_tip = false) {
597                 string[] txt = {};
598
599                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
600                 //if (sr) txt.push('<s>');
601
602                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
603                 
604                 //if (renderfull && c['|xns']) {
605                 var fqn = this.fqn();
606                 var fqn_ar = fqn.split(".");
607                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
608                         
609                 //}
610                 
611                 //if (c.xtype)    { txt.push(c.xtype); }
612                         
613                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
614                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
615                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
616                 
617                 
618                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
619                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
620                 if (this.has("html") && this.get("html").length > 0)     { 
621                         var ht = this.get("html").split("\n");
622                         if (ht.length > 1) {
623                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
624                         } else { 
625                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
626                         }
627                 }
628                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
629                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
630                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
631                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
632                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
633                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
634                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
635                 
636                 // for flat classes...
637                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
638                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
639                 
640                 
641                 //if (sr) txt.push('</s>');
642                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
643         }
644
645 }