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