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