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         
110
111         public Node()
112         {
113                 this.items = new Gee.ArrayList<Node>();
114                 this.props = new Gee.HashMap<string,string>();
115                 this.listeners = new Gee.HashMap<string,string>();
116                 this.xvala_cls = "";
117                 this.xvala_xcls = "";
118                 this.xvala_id = "";
119                 this.parent = null;
120                 this.line_start = -1;
121                 this.line_end = -1;             
122                 this.lines = new Gee.ArrayList<int>();
123                 this.line_map = new Gee.HashMap<int,string>();
124                 
125         }
126
127         public void setLine(int line, string type, string prop) {
128                 this.lines.add(line);
129                 this.line_map.set(line, type +":" + prop);
130         }
131         public void sortLines() {
132                 this.lines.sort((a,b) => { // probably simpler way to do this... return a-b ?
133                         if (a == b) {
134                                 return 0;
135                         }
136                         return a < b ? -1 : 1;
137                 });
138         }
139         
140         public string uid()
141         {
142                 if (this.props.get("id") == null) {
143                         uid_count++;
144                         return "uid-%d".printf(uid_count);
145                 }
146                 return this.props.get("id");
147         }
148         
149         
150         public bool hasChildren()
151         {
152                 return this.items.size > 0;
153         }
154         public bool hasXnsType()
155         {
156                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
157                         return true;
158                         
159                 }
160                 return false;
161         }
162         public string fqn()
163         {
164                 if (!this.hasXnsType ()) {
165                         return "";
166                 }
167                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
168
169         }
170         public void setFqn(string name)
171         {
172                 var ar = name.split(".");
173                 this.props.set("xtype", ar[ar.length-1]);
174                 var l = name.length - (ar[ar.length-1].length +1);
175                 this.props.set("$ xns", name.substring(0, l));
176                 print("setFQN %s to %s\n", name , this.fqn());
177                                
178
179         }
180         // wrapper around get props that returns empty string if not found.
181         public string get(string key)
182         {
183                 var k = this.props.get(key);
184                 if (k != null) {
185                         return k;
186                 }
187                 
188                 k = this.props.get("$ " + key);
189                 if (k != null) {
190                         return k;
191                 }
192                 
193                 var iter = this.props.map_iterator();
194                 while (iter.next()) {
195                         var kk = iter.get_key().split(" ");
196                         if (kk[kk.length-1] == key) {
197                                 return iter.get_value();
198                         }
199                 }
200                 
201                 
202                 return "";
203                 
204         }
205         
206         public string get_key(string key)
207         {
208                 var k = this.props.get(key);
209                 if (k != null) {
210                         return key;
211                 }
212                 
213                 k = this.props.get("$ " + key);
214                 if (k != null) {
215                         return "$ " + key;
216                 }
217                 
218                 var iter = this.props.map_iterator();
219                 while (iter.next()) {
220                         var kk = iter.get_key().split(" ");
221                         if (kk[kk.length-1] == key) {
222                                 return iter.get_key();
223                         }
224                 }
225                 
226                 
227                 return "";
228                 
229         }
230         public void normalize_key(string key, out string kname, out string kflag, out string ktype)
231         {
232                 // key formats : XXXX
233                 // XXX - plain
234                 // string XXX - with type
235                 // $ XXX - with flag (no type)
236                 // $ string XXX - with flag
237                 kname = "";
238                 ktype = "-";
239                 kflag = "-";
240                 var kk = key.strip().split(" ");
241                 switch(kk.length) {
242                         case 1: 
243                                 kname = kk[0];
244                                 return;
245                         case 2: 
246                                 kname = kk[1];
247                                 if (kk[0].length > 1) {
248                                         ktype = kk[0];
249                                 } else {
250                                         kflag = kk[0];
251                                 }
252                                 return;
253                         case 3:
254                                 kname = kk[2];
255                                 kflag = kk[0];
256                                 ktype = kk[1];
257                                 return;
258                 }
259                 // everything blank otherwise...
260         }
261         public void set(string key, string value) {
262                 this.props.set(key,value);
263         }
264          public bool has(string key)
265         {
266                 var k = this.props.get(key);
267                 if (k != null) {
268                         return true;
269                 }
270                 var iter = this.props.map_iterator();
271                 while (iter.next()) {
272                         var kk = iter.get_key().strip().split(" ");
273                         if (kk[kk.length-1] == key) {
274                                 return true;
275                         }
276                 }
277                 
278                 return false;
279                 
280         }
281
282         public void  remove()
283         {
284                 if (this.parent == null) {
285                         
286                         
287                         return;
288                 }
289                 var nlist = new Gee.ArrayList<Node>();
290                 for (var i =0;i < this.parent.items.size; i++) {
291                         if (this.parent.items.get(i) == this) {
292                                 continue;
293                         }
294                         nlist.add(this.parent.items.get(i));
295                 }
296                 this.parent.items = nlist;
297                 this.parent = null;
298
299         }
300          
301         /* creates javascript based on the rules */
302         public Node? findProp(string n) {
303                 for(var i=0;i< this.items.size;i++) {
304                         var p = this.items.get(i).get("* prop");
305                         if (this.items.get(i).get("* prop").length < 1) {
306                                 continue;
307                         }
308                         if (p == n) {
309                                 return this.items.get(i);
310                         }
311                 }
312                 return null;
313
314         }
315
316         
317         
318          
319         static Json.Generator gen = null;
320         
321         public string quoteString(string str)
322         {
323                 if (Node.gen == null) {
324                         Node.gen = new Json.Generator();
325                 }
326                  var n = new Json.Node(Json.NodeType.VALUE);
327                 n.set_string(str);
328  
329                 Node.gen.set_root (n);
330                 return  Node.gen.to_data (null);   
331         }
332
333         public void loadFromJson(Json.Object obj, int version) {
334                 obj.foreach_member((o , key, value) => {
335                         //print(key+"\n");
336                         if (key == "items") {
337                                 var ar = value.get_array();
338                                 ar.foreach_element( (are, ix, el) => {
339                                         var node = new Node();
340                                         node.parent = this;
341                                         node.loadFromJson(el.get_object(), version);
342                                         this.items.add(node);
343                                 });
344                                 return;
345                         }
346                         if (key == "listeners") {
347                                 var li = value.get_object();
348                                 li.foreach_member((lio , li_key, li_value) => {
349                                         this.listeners.set(li_key, li_value.get_string());
350
351                                 });
352                                 return;
353                         }
354                         var v = value.get_value();
355                         var sv =  Value (typeof (string));
356                         v.transform(ref sv);
357
358                         var rkey = key;
359                         if (version == 1) {
360                                 rkey = this.upgradeKey(key, (string)sv);
361                         }
362
363                         
364                         this.props.set(rkey,  (string)sv);
365                 });
366                 
367
368
369
370         }
371
372         public string upgradeKey(string key, string val)
373         {
374                 // convert V1 to V2
375                 if (key.length < 1) {
376                         return key;
377                 }
378                 switch(key) {
379                         case "*prop":
380                         case "*args":
381                         case ".ctor":
382                         case "|init":
383                                 return "* " + key.substring(1);
384                                 
385                         case "pack":
386                                 return "* " + key;
387                 }
388                 if (key[0] == '.') { // v2 does not start with '.' ?
389                         var bits = key.substring(1).split(":");
390                         if (bits[0] == "signal") {
391                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
392                         }
393                         return "# " + string.joinv(" ", bits);                  
394                 }
395                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
396                         return key;
397                 }
398                 var bits = key.substring(1).split(":");
399                 // two types '$' or '|' << for methods..
400                 // javascript 
401                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
402                         return "| " + key.substring(1);
403                 }
404                 // vala function..
405                 
406                 if  (Regex.match_simple ("^\\(", val.strip())) {
407                 
408                         return "| " + string.joinv(" ", bits);
409                 }
410                 
411                 // guessing it's a property..
412                 return "$ " + string.joinv(" ", bits);
413                 
414                 
415
416         }
417
418
419
420
421
422         
423         public Node  deepClone()
424         {
425                 var n = new Node();
426                 n.loadFromJson(this.toJsonObject(), 2);
427                 return n;
428
429         }
430         public string toJsonString()
431         {
432                 if (Node.gen == null) {
433                         Node.gen = new Json.Generator();
434                         gen.pretty =  true;
435                         gen.indent = 1;
436                 }
437                 var n = new Json.Node(Json.NodeType.OBJECT);
438                 n.set_object(this.toJsonObject () );
439                 Node.gen.set_root (n);
440                 return  Node.gen.to_data (null);   
441         }
442         
443         public Json.Object toJsonObject()
444         {
445                 var ret = new Json.Object();
446
447                 // listeners...
448                 if (this.listeners.size > 0) {
449                         var li = new Json.Object();
450                         ret.set_object_member("listeners", li);
451                         var liter = this.listeners.map_iterator();
452                         while (liter.next()) {
453                                 li.set_string_member(liter.get_key(), liter.get_value());
454                         }
455                 }
456                 //props
457                 if (this.props.size > 0 ) {
458                         var iter = this.props.map_iterator();
459                         while (iter.next()) {
460                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
461                         }
462                 }
463                 if (this.items.size > 0) {
464                         var ar = new Json.Array();
465                         ret.set_array_member("items", ar);
466                 
467                         // children..
468                         for(var i =0;i < this.items.size;i++) {
469                                 ar.add_object_element(this.items.get(i).toJsonObject());
470                         }
471                 }
472                 return ret;
473                 
474  
475         }
476          
477         public void jsonObjectsetMember(Json.Object o, string key, string val) {
478                 if (Lang.isBoolean(val)) {
479                         o.set_boolean_member(key, val.down() == "false" ? false : true);
480                         return;
481                 }
482                 
483                 
484                 if (Lang.isNumber(val)) {
485                         if (val.contains(".")) {
486                                 //print( "ADD " + key + "=" + val + " as a double?\n");
487                                 o.set_double_member(key, double.parse (val));
488                                 return;
489
490                         }
491                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
492                         o.set_int_member(key,long.parse(val));
493                         return;
494                 }
495                 ///print( "ADD " + key + "=" + val + " as a string?\n");
496                 o.set_string_member(key,val);
497                 
498         }
499         public string nodeTip()
500         {
501                 var ret = this.nodeTitle(true);
502                 var funcs = "";
503                 var props = "";
504                 var listen = "";
505                 var iter = this.props.map_iterator();
506                 while (iter.next()) {
507                         var i =  iter.get_key().strip();
508                         var val = iter.get_value().strip();
509                         if (val == null || val.length < 1) {
510                                 continue;
511                         }
512                         if ( i[0] != '|') {
513                                 props += "\n\t<b>" + 
514                                         GLib.Markup.escape_text(i) +"</b> : " + 
515                                         GLib.Markup.escape_text(val.split("\n")[0]);
516                                  
517                                 continue;
518                         }
519                 
520                         //if (i == "* init") { 
521                         //      continue;
522                         //}
523                         
524                         if (Regex.match_simple("^\\s*function", val)) { 
525                                 funcs += "\n\t<b>" + 
526                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
527                                         GLib.Markup.escape_text(val.split("\n")[0]);
528                                 continue;
529                         }
530                         if (Regex.match_simple("^\\s*\\(", val)) {
531                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
532                                         "</b> : " + 
533                                         GLib.Markup.escape_text(val.split("\n")[0]);
534                                 continue;
535                         }
536                         
537                 }
538                 iter = this.listeners.map_iterator();
539                 while (iter.next()) {
540                         var i =  iter.get_key().strip();
541                         var val = iter.get_value().strip();
542                         if (val == null || val.length < 1) {
543                                 continue;
544                         }
545                          listen += "\n\t<b>" + 
546                                         GLib.Markup.escape_text(i) +"</b> : " + 
547                                         GLib.Markup.escape_text(val.split("\n")[0]);
548                         
549                 }
550                 
551                 
552                 if (props.length > 0) {
553                         ret+="\n\nProperties:" + props;
554                 } 
555                 if (funcs.length > 0) {
556                         ret+="\n\nMethods:" + funcs;
557                 } 
558                 if (listen.length > 0) {
559                         ret+="\n\nListeners:" + listen;
560                 } 
561                 return ret;
562
563         }
564         public string nodeTitle(bool for_tip = false) {
565                 string[] txt = {};
566
567                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
568                 //if (sr) txt.push('<s>');
569
570                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
571                 
572                 //if (renderfull && c['|xns']) {
573                 var fqn = this.fqn();
574                 var fqn_ar = fqn.split(".");
575                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
576                         
577                 //}
578                 
579                 //if (c.xtype)    { txt.push(c.xtype); }
580                         
581                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
582                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
583                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
584                 
585                 
586                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
587                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
588                 if (this.has("html") && this.get("html").length > 0)     { 
589                         var ht = this.get("html").split("\n");
590                         if (ht.length > 1) {
591                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
592                         } else { 
593                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
594                         }
595                 }
596                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
597                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
598                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
599                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
600                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
601                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
602                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
603                 
604                 // for flat classes...
605                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
606                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
607                 
608                 
609                 //if (sr) txt.push('</s>');
610                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
611         }
612
613 }