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