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