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