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