src/JsRender/Node.vala
[app.Builder.js] / src / JsRender / Node.vala
1
2 // test..
3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
4
5
6 /*
7  * 
8  * props:
9  * 
10  * key value view of properties.
11  * 
12  * Old standard..
13  * XXXXX : YYYYY  -- standard - should be rendered as XXXX : "YYYY" usually.
14  * |XXXXX : YYYYY  -- standard - should be rendered as XXXX : YYYY usually.
15  * |init  -- the initialization...
16  * *prop : a property which is actually an object definition... 
17  * *args : contructor args
18  * .ctor : Full contruct line...  
19  * 
20  * Newer code
21  * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type  name 
22  * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23  *  "|void:clearFiles": "() .... some code...."  | type name
24  *
25  * 
26  * 
27  * 
28  * 
29  * Standardize this crap...
30  * 
31  * standard properties (use to set)
32  *          If they are long values show the dialog..
33  * 
34  * bool is_xxx  :: can show a pulldown.. (true/false)
35  * string html  
36  * $ string html  = string with value interpolated eg. baseURL + ".." 
37  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
38  * $ untypedvalue = javascript untyped value... 
39  * 
40  * object properties (not part of the GOjbect being wrapped?
41  * # Gee.ArrayList<Xcls_fileitem> fileitems
42  * 
43  * signals
44  * @ void open 
45  * 
46  * methods -- always text editor..
47  * | void clearFiles
48  * | someJSmethod
49  * 
50  * specials
51  * * prop -- string
52  * * args  -- string
53  * * ctor -- string
54  * * init -- big string?
55  * 
56  * event handlers (listeners)
57  *   just shown 
58  * 
59  * -----------------
60  * special ID values
61  *  +XXXX -- indicates it's a instance property / not glob...
62  *  *XXXX -- skip writing glob property (used as classes that can be created...)
63  *  _XXXX -- (string) a translatable string.
64  * 
65  * 
66  *  FORMATING?
67 .method {
68          color : green;
69          font-weight: bold;      
70 }
71 .prop {
72         color : #333;
73 }
74 .prop-code {
75     font-style: italic;
76  }
77 .listener {
78     color: #600;
79     font-weight: bold;   
80 }
81 .special { 
82   color : #00c;    font-weight: bold;    
83
84
85 */
86
87
88
89
90
91
92 public class JsRender.Node : Object {
93         
94
95         public static int uid_count = 0;
96         
97         public Node parent;
98         public Gee.ArrayList<Node> items; // child items..
99         
100         public Gee.HashMap<string,string> props; // the properties..
101         public Gee.HashMap<string,string> listeners; // the listeners..
102         public string  xvala_cls;
103         public string xvala_xcls; // 'Xcls_' + id;
104         public string xvala_id; // item id or ""
105         public int line_start;
106         public int line_end;
107         public Gee.ArrayList<int> lines;
108         public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....
109         public Gee.ArrayList<int> node_lines;
110         public Gee.HashMap<int,Node> nodes_line_map; // store of l:xxx or p:....
111         
112
113         public Node()
114         {
115                 this.items = new Gee.ArrayList<Node>();
116                 this.props = new Gee.HashMap<string,string>();
117                 this.listeners = new Gee.HashMap<string,string>();
118                 this.xvala_cls = "";
119                 this.xvala_xcls = "";
120                 this.xvala_id = "";
121                 this.parent = null;
122                 this.line_start = -1;
123                 this.line_end = -1;             
124                 this.lines = new Gee.ArrayList<int>();
125                 this.line_map = new Gee.HashMap<int,string>();
126                 this.node_lines = new Gee.ArrayList<int>();
127                 this.node_line_map = new Gee.HashMap<int,Node>();
128                 
129         }
130         
131         public void setNodeLine(int line, Node node) {
132                 this.node_lines.add(line);
133                 this.node_lines_map.set(line, node);
134         }
135         
136         public void setLine(int line, string type, string prop) {
137                 this.lines.add(line);
138                 this.line_map.set(line, type +":" + prop);
139         }
140         public void sortLines() {
141                 this.lines.sort((a,b) => {   
142                         return (int)a-(int)b;
143                 });
144                 this.node_lines.sort((a,b) => {   
145                         return (int)a-(int)b;
146                 });
147         }
148         
149         public string uid()
150         {
151                 if (this.props.get("id") == null) {
152                         uid_count++;
153                         return "uid-%d".printf(uid_count);
154                 }
155                 return this.props.get("id");
156         }
157         
158         
159         public bool hasChildren()
160         {
161                 return this.items.size > 0;
162         }
163         public bool hasXnsType()
164         {
165                 if (this.props.get("$ xns") != null && this.props.get("xtype") != null) {
166                         return true;
167                         
168                 }
169                 return false;
170         }
171         public string fqn()
172         {
173                 if (!this.hasXnsType ()) {
174                         return "";
175                 }
176                 return this.props.get("$ xns") + "." + this.props.get("xtype"); 
177
178         }
179         public void setFqn(string name)
180         {
181                 var ar = name.split(".");
182                 this.props.set("xtype", ar[ar.length-1]);
183                 var l = name.length - (ar[ar.length-1].length +1);
184                 this.props.set("$ xns", name.substring(0, l));
185                 print("setFQN %s to %s\n", name , this.fqn());
186                                
187
188         }
189         // wrapper around get props that returns empty string if not found.
190         public string get(string key)
191         {
192                 var k = this.props.get(key);
193                 if (k != null) {
194                         return k;
195                 }
196                 
197                 k = this.props.get("$ " + key);
198                 if (k != null) {
199                         return k;
200                 }
201                 
202                 var iter = this.props.map_iterator();
203                 while (iter.next()) {
204                         var kk = iter.get_key().split(" ");
205                         if (kk[kk.length-1] == key) {
206                                 return iter.get_value();
207                         }
208                 }
209                 
210                 
211                 return "";
212                 
213         }
214         
215         public string get_key(string key)
216         {
217                 var k = this.props.get(key);
218                 if (k != null) {
219                         return key;
220                 }
221                 
222                 k = this.props.get("$ " + key);
223                 if (k != null) {
224                         return "$ " + key;
225                 }
226                 
227                 var iter = this.props.map_iterator();
228                 while (iter.next()) {
229                         var kk = iter.get_key().split(" ");
230                         if (kk[kk.length-1] == key) {
231                                 return iter.get_key();
232                         }
233                 }
234                 
235                 
236                 return "";
237                 
238         }
239         public void normalize_key(string key, out string kname, out string kflag, out string ktype)
240         {
241                 // key formats : XXXX
242                 // XXX - plain
243                 // string XXX - with type
244                 // $ XXX - with flag (no type)
245                 // $ string XXX - with flag
246                 kname = "";
247                 ktype = "-";
248                 kflag = "-";
249                 var kk = key.strip().split(" ");
250                 switch(kk.length) {
251                         case 1: 
252                                 kname = kk[0];
253                                 return;
254                         case 2: 
255                                 kname = kk[1];
256                                 if (kk[0].length > 1) {
257                                         ktype = kk[0];
258                                 } else {
259                                         kflag = kk[0];
260                                 }
261                                 return;
262                         case 3:
263                                 kname = kk[2];
264                                 kflag = kk[0];
265                                 ktype = kk[1];
266                                 return;
267                 }
268                 // everything blank otherwise...
269         }
270         public void set(string key, string value) {
271                 this.props.set(key,value);
272         }
273          public bool has(string key)
274         {
275                 var k = this.props.get(key);
276                 if (k != null) {
277                         return true;
278                 }
279                 var iter = this.props.map_iterator();
280                 while (iter.next()) {
281                         var kk = iter.get_key().strip().split(" ");
282                         if (kk[kk.length-1] == key) {
283                                 return true;
284                         }
285                 }
286                 
287                 return false;
288                 
289         }
290
291         public void  remove()
292         {
293                 if (this.parent == null) {
294                         
295                         
296                         return;
297                 }
298                 var nlist = new Gee.ArrayList<Node>();
299                 for (var i =0;i < this.parent.items.size; i++) {
300                         if (this.parent.items.get(i) == this) {
301                                 continue;
302                         }
303                         nlist.add(this.parent.items.get(i));
304                 }
305                 this.parent.items = nlist;
306                 this.parent = null;
307
308         }
309          
310         /* creates javascript based on the rules */
311         public Node? findProp(string n) {
312                 for(var i=0;i< this.items.size;i++) {
313                         var p = this.items.get(i).get("* prop");
314                         if (this.items.get(i).get("* prop").length < 1) {
315                                 continue;
316                         }
317                         if (p == n) {
318                                 return this.items.get(i);
319                         }
320                 }
321                 return null;
322
323         }
324
325         
326         
327          
328         static Json.Generator gen = null;
329         
330         public string quoteString(string str)
331         {
332                 if (Node.gen == null) {
333                         Node.gen = new Json.Generator();
334                 }
335                  var n = new Json.Node(Json.NodeType.VALUE);
336                 n.set_string(str);
337  
338                 Node.gen.set_root (n);
339                 return  Node.gen.to_data (null);   
340         }
341
342         public void loadFromJson(Json.Object obj, int version) {
343                 obj.foreach_member((o , key, value) => {
344                         //print(key+"\n");
345                         if (key == "items") {
346                                 var ar = value.get_array();
347                                 ar.foreach_element( (are, ix, el) => {
348                                         var node = new Node();
349                                         node.parent = this;
350                                         node.loadFromJson(el.get_object(), version);
351                                         this.items.add(node);
352                                 });
353                                 return;
354                         }
355                         if (key == "listeners") {
356                                 var li = value.get_object();
357                                 li.foreach_member((lio , li_key, li_value) => {
358                                         this.listeners.set(li_key, li_value.get_string());
359
360                                 });
361                                 return;
362                         }
363                         var v = value.get_value();
364                         var sv =  Value (typeof (string));
365                         v.transform(ref sv);
366
367                         var rkey = key;
368                         if (version == 1) {
369                                 rkey = this.upgradeKey(key, (string)sv);
370                         }
371
372                         
373                         this.props.set(rkey,  (string)sv);
374                 });
375                 
376
377
378
379         }
380
381         public string upgradeKey(string key, string val)
382         {
383                 // convert V1 to V2
384                 if (key.length < 1) {
385                         return key;
386                 }
387                 switch(key) {
388                         case "*prop":
389                         case "*args":
390                         case ".ctor":
391                         case "|init":
392                                 return "* " + key.substring(1);
393                                 
394                         case "pack":
395                                 return "* " + key;
396                 }
397                 if (key[0] == '.') { // v2 does not start with '.' ?
398                         var bits = key.substring(1).split(":");
399                         if (bits[0] == "signal") {
400                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
401                         }
402                         return "# " + string.joinv(" ", bits);                  
403                 }
404                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
405                         return key;
406                 }
407                 var bits = key.substring(1).split(":");
408                 // two types '$' or '|' << for methods..
409                 // javascript 
410                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
411                         return "| " + key.substring(1);
412                 }
413                 // vala function..
414                 
415                 if  (Regex.match_simple ("^\\(", val.strip())) {
416                 
417                         return "| " + string.joinv(" ", bits);
418                 }
419                 
420                 // guessing it's a property..
421                 return "$ " + string.joinv(" ", bits);
422                 
423                 
424
425         }
426
427
428
429
430
431         
432         public Node  deepClone()
433         {
434                 var n = new Node();
435                 n.loadFromJson(this.toJsonObject(), 2);
436                 return n;
437
438         }
439         public string toJsonString()
440         {
441                 if (Node.gen == null) {
442                         Node.gen = new Json.Generator();
443                         gen.pretty =  true;
444                         gen.indent = 1;
445                 }
446                 var n = new Json.Node(Json.NodeType.OBJECT);
447                 n.set_object(this.toJsonObject () );
448                 Node.gen.set_root (n);
449                 return  Node.gen.to_data (null);   
450         }
451         
452         public Json.Object toJsonObject()
453         {
454                 var ret = new Json.Object();
455
456                 // listeners...
457                 if (this.listeners.size > 0) {
458                         var li = new Json.Object();
459                         ret.set_object_member("listeners", li);
460                         var liter = this.listeners.map_iterator();
461                         while (liter.next()) {
462                                 li.set_string_member(liter.get_key(), liter.get_value());
463                         }
464                 }
465                 //props
466                 if (this.props.size > 0 ) {
467                         var iter = this.props.map_iterator();
468                         while (iter.next()) {
469                                 this.jsonObjectsetMember(ret, iter.get_key(), iter.get_value());
470                         }
471                 }
472                 if (this.items.size > 0) {
473                         var ar = new Json.Array();
474                         ret.set_array_member("items", ar);
475                 
476                         // children..
477                         for(var i =0;i < this.items.size;i++) {
478                                 ar.add_object_element(this.items.get(i).toJsonObject());
479                         }
480                 }
481                 return ret;
482                 
483  
484         }
485          
486         public void jsonObjectsetMember(Json.Object o, string key, string val) {
487                 if (Lang.isBoolean(val)) {
488                         o.set_boolean_member(key, val.down() == "false" ? false : true);
489                         return;
490                 }
491                 
492                 
493                 if (Lang.isNumber(val)) {
494                         if (val.contains(".")) {
495                                 //print( "ADD " + key + "=" + val + " as a double?\n");
496                                 o.set_double_member(key, double.parse (val));
497                                 return;
498
499                         }
500                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
501                         o.set_int_member(key,long.parse(val));
502                         return;
503                 }
504                 ///print( "ADD " + key + "=" + val + " as a string?\n");
505                 o.set_string_member(key,val);
506                 
507         }
508         public string nodeTip()
509         {
510                 var ret = this.nodeTitle(true);
511                 var funcs = "";
512                 var props = "";
513                 var listen = "";
514                 var iter = this.props.map_iterator();
515                 while (iter.next()) {
516                         var i =  iter.get_key().strip();
517                         var val = iter.get_value().strip();
518                         if (val == null || val.length < 1) {
519                                 continue;
520                         }
521                         if ( i[0] != '|') {
522                                 props += "\n\t<b>" + 
523                                         GLib.Markup.escape_text(i) +"</b> : " + 
524                                         GLib.Markup.escape_text(val.split("\n")[0]);
525                                  
526                                 continue;
527                         }
528                 
529                         //if (i == "* init") { 
530                         //      continue;
531                         //}
532                         
533                         if (Regex.match_simple("^\\s*function", val)) { 
534                                 funcs += "\n\t<b>" + 
535                                         GLib.Markup.escape_text(i.substring(1)).strip() +"</b> : " + 
536                                         GLib.Markup.escape_text(val.split("\n")[0]);
537                                 continue;
538                         }
539                         if (Regex.match_simple("^\\s*\\(", val)) {
540                                 funcs += "\n\t<b>" + GLib.Markup.escape_text(i.substring(1)).strip() +
541                                         "</b> : " + 
542                                         GLib.Markup.escape_text(val.split("\n")[0]);
543                                 continue;
544                         }
545                         
546                 }
547                 iter = this.listeners.map_iterator();
548                 while (iter.next()) {
549                         var i =  iter.get_key().strip();
550                         var val = iter.get_value().strip();
551                         if (val == null || val.length < 1) {
552                                 continue;
553                         }
554                          listen += "\n\t<b>" + 
555                                         GLib.Markup.escape_text(i) +"</b> : " + 
556                                         GLib.Markup.escape_text(val.split("\n")[0]);
557                         
558                 }
559                 
560                 
561                 if (props.length > 0) {
562                         ret+="\n\nProperties:" + props;
563                 } 
564                 if (funcs.length > 0) {
565                         ret+="\n\nMethods:" + funcs;
566                 } 
567                 if (listen.length > 0) {
568                         ret+="\n\nListeners:" + listen;
569                 } 
570                 return ret;
571
572         }
573         public string nodeTitle(bool for_tip = false) {
574                 string[] txt = {};
575
576                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
577                 //if (sr) txt.push('<s>');
578
579                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
580                 
581                 //if (renderfull && c['|xns']) {
582                 var fqn = this.fqn();
583                 var fqn_ar = fqn.split(".");
584                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
585                         
586                 //}
587                 
588                 //if (c.xtype)    { txt.push(c.xtype); }
589                         
590                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
591                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
592                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
593                 
594                 
595                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
596                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
597                 if (this.has("html") && this.get("html").length > 0)     { 
598                         var ht = this.get("html").split("\n");
599                         if (ht.length > 1) {
600                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
601                         } else { 
602                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
603                         }
604                 }
605                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
606                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
607                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
608                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
609                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
610                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
611                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
612                 
613                 // for flat classes...
614                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
615                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
616                 
617                 
618                 //if (sr) txt.push('</s>');
619                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
620         }
621
622 }