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