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
10 * key value view of properties.
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...
21 * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type name
22 * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23 * "|void:clearFiles": "() .... some code...." | type name
29 * Standardize this crap...
31 * standard properties (use to set)
32 * If they are long values show the dialog..
34 * bool is_xxx :: can show a pulldown.. (true/false)
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...
40 * object properties (not part of the GOjbect being wrapped?
41 * # Gee.ArrayList<Xcls_fileitem> fileitems
46 * methods -- always text editor..
54 * * init -- big string?
56 * event handlers (listeners)
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.
82 color : #00c; font-weight: bold;
92 public class JsRender.Node : GLib.Object {
95 public static int uid_count = 0;
97 public int oid { get; private set; }
99 private Gee.ArrayList<Node> items; // child items..
100 public GLib.ListStore childstore; // must be kept in sync with items
101 public GLib.ListStore? propstore; // must be kept in sync with items
102 public string xvala_cls; // set by node to vala
103 public string xvala_xcls; // 'Xcls_' + id; // set by nodetoVala
104 public string xvala_id; // item id or "" // set by nodetovala
107 public int line_start;
109 public Gee.ArrayList<int> lines;
110 public Gee.HashMap<int,string> line_map; // store of l:xxx or p:.... // fixme - not needed as we can store line numbers in props now.
111 public Gee.ArrayList<int> node_lines;
112 public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
115 public string node_pad = "";
117 private int _updated_count = 0;
118 public int updated_count {
120 return this._updated_count;
123 this.nodeTitleProp = ""; // ?? should trigger set?
124 this.iconFilename = "";
125 this._updated_count = value;
128 //GLib.debug("Update Node %d p%d - rev %d", this.oid, this.parent != null ? this.parent.oid : -1, value);
129 if (this.parent != null) {
130 this.parent.updated_count++;
132 //GLib.debug("UNDO top node is %d", value);
133 this.version_changed();
137 } // changes to this trigger updates on the tree..
139 public string as_source = "";
140 public int as_source_version = -1;
141 public int as_source_start_line = -1;
145 public signal void version_changed();
149 this.items = new Gee.ArrayList<Node>();
150 //this._props = new Gee.HashMap<string,NodeProp>();
151 //this._listeners = new Gee.HashMap<string,NodeProp>(); // Nodeprop can include line numbers..
152 this.propstore = new GLib.ListStore(typeof(NodeProp)); // Nodeprop can include line numbers..
154 this.xvala_xcls = "";
157 this.line_start = -1;
159 this.lines = new Gee.ArrayList<int>();
160 this.line_map = new Gee.HashMap<int,string>();
161 this.node_lines = new Gee.ArrayList<int>();
162 this.node_lines_map = new Gee.HashMap<int,Node>();
163 this.childstore = new GLib.ListStore( typeof(Node));
164 this.oid = uid_count++;
168 public bool has_parent(Node n)
170 if (this.parent == null) {
173 if (this.parent.oid == n.oid) {
178 return this.parent.has_parent(n);
182 public Gee.ArrayList<Node> readItems()
184 return this.items; // note should not modify add/remove from this directly..
187 public void setNodeLine(int line, Node node) {
188 //print("Add node @ %d\n", line);
189 if (this.node_lines_map.has_key(line)) {
192 this.node_lines.add(line);
193 this.node_lines_map.set(line, node);
197 public void setLine(int line, string type, string prop)
199 //GLib.debug("set prop %s (%s) to line %d", prop, type, line);
200 if (this.line_map.has_key(line)) {
201 if (this.line_map.get(line) != "e:" ) {
205 this.lines.add(line);
207 this.line_map.set(line, type + ":" + prop);
208 if (type == "e" || type == "p" ) {
210 if (prop == "" || !this.props.has_key(prop)) {
211 ///GLib.debug("cant find prop '%s'", prop);
215 var prope = this.props.get(prop);
216 if (prope != null && type =="p") {
217 prope.start_line = line;
219 if (prope != null && type =="e") {
220 prope.end_line = line;
224 if (type == "l" || type =="x") {
225 if (prop == "" || !this.listeners.has_key(prop)) {
226 //GLib.debug("cant find listener '%s'", prop);
230 var prope = this.listeners.get(prop);
231 if (prope != null && type =="l") {
232 prope.start_line = line;
234 if (prope != null && type =="x") {
235 prope.end_line = line;
244 //GLib.debug("setLine %d, %s", line, type + ":" + prop);
246 public void sortLines() {
247 //print("sortLines\n");
248 this.lines.sort((a,b) => {
249 return (int)a-(int)b;
251 this.node_lines.sort((a,b) => {
252 return (int)a-(int)b;
255 public Node? lineToNode(int line)
257 //print("Searching for line %d\n",line);
259 //foreach(int el in this.node_lines) {
260 //print("all lines %d\n", el);
264 foreach(int el in this.node_lines) {
265 //print("?match %d\n", el);
278 var ret = this.node_lines_map.get(l);
279 if (line > ret.line_end) {
282 //print("RETURNING NODE ON LINE %d", l);
289 var ret = this.node_lines_map.get(l);
290 if (line > ret.line_end) {
293 //print("RETURNING NODE ON LINE %d", l);
302 public NodeProp? lineToProp(int line)
305 for(var i= 0; i < this.propstore.get_n_items();i++) {
306 var p = (NodeProp) this.propstore.get_item(i);
307 GLib.debug("prop %s lines %d -> %d", p.name, p.start_line, p.end_line);
308 if (p.start_line > line) {
311 if (line > p.end_line) {
321 public bool getPropertyRange(string prop, out int start, out int end)
325 foreach(int el in this.lines) {
327 if (this.line_map.get(el) == prop) {
341 public void dumpProps(string indent = "")
343 print("%s:\n" , this.fqn());
344 foreach(int el in this.lines) {
345 print("%d: %s%s\n", el, indent, this.line_map.get(el));
347 foreach(Node n in this.items) {
348 n.dumpProps(indent + " ");
356 if (this.props.get("id") == null) {
357 return "uid-%d".printf(this.oid);
359 return this.props.get("id").val;
363 public bool hasChildren()
365 return this.items.size > 0;
367 public bool hasXnsType()
369 if (this.props.get("xns") != null && this.props.get("xtype") != null) {
376 public string FQN { // for sorting
377 owned get { return this.fqn(); }
380 public string NS { // for sorting
381 owned get { return this.props.has_key("xns") ? this.props.get("xns").val : ""; }
386 if (!this.hasXnsType ()) {
389 return this.props.get("xns").val + "." + this.props.get("xtype").val;
392 public void setFqn(string name)
394 var ar = name.split(".");
395 var l = name.length - (ar[ar.length-1].length +1);
399 if (this.props.has_key("xtype")) {
400 this.props.get("xtype").val = ar[ar.length-1];
402 this.add_prop(new NodeProp.prop("xtype", "", ar[ar.length-1]));
404 if (this.props.has_key("xns")) {
405 this.props.get("xns").val = name.substring(0, l);
407 this.add_prop(new NodeProp.raw("xns", "", name.substring(0, l)));
411 //print("setFQN %s to %s\n", name , this.fqn());
415 // wrapper around get props that returns empty string if not found.
416 //overrides Glib.object.get (hence new)
417 public new string get(string key)
420 var v = this.props.get(key);
421 return v == null ? "" : v.val;
424 public NodeProp? get_prop(string key)
427 return this.props.get(key);
435 public bool has(string key)
437 return this.props.has_key(key);
444 if (this.parent == null) {
445 GLib.debug("remove - parent is null?");
448 var nlist = new Gee.ArrayList<Node>();
449 for (var i =0;i < this.parent.items.size; i++) {
450 if (this.parent.items.get(i) == this) {
453 nlist.add(this.parent.items.get(i));
456 if ( this.parent.childstore.find(this, out pos)) {
457 this.parent.childstore.remove(pos);
459 this.parent.updated_count++;
460 this.parent.items = nlist;
465 /* creates javascript based on the rules */
466 public Node? findProp(string n) {
467 for(var i=0;i< this.items.size;i++) {
468 var p = this.items.get(i).get("* prop");
473 return this.items.get(i);
483 static Json.Generator gen = null;
485 public string quoteString(string str)
487 if (Node.gen == null) {
488 Node.gen = new Json.Generator();
490 var n = new Json.Node(Json.NodeType.VALUE);
493 Node.gen.set_root (n);
494 return Node.gen.to_data (null);
497 public void loadFromJsonString(string str, int ver)
499 var pa = new Json.Parser();
501 pa.load_from_data(str);
502 } catch (GLib.Error e) {
503 GLib.debug("Error loading string?");
506 var new_node = pa.get_root();
507 var obj = new_node.get_object ();
509 this.loadFromJson(obj, ver);
514 public void loadFromJson(Json.Object obj, int version) {
516 obj.foreach_member((o , key, value) => {
518 if (key == "items") {
519 var ar = value.get_array();
520 ar.foreach_element( (are, ix, el) => {
521 var node = new Node();
523 node.loadFromJson(el.get_object(), version);
524 this.items.add(node);
525 this.childstore.append(node);
529 if (key == "listeners") {
530 var li = value.get_object();
531 li.foreach_member((lio , li_key, li_value) => {
532 this.add_prop(new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
533 //this.listeners.set(li_key, new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
540 var sval = this.jsonNodeAsString(value);
543 rkey = this.upgradeKey(key, sval);
545 var n = new NodeProp.from_json(rkey, sval);
559 // converts the array into a string with line breaks.
560 public string jsonNodeAsString(Json.Node node)
563 if (node.get_node_type() == Json.NodeType.ARRAY) {
564 var buffer = new GLib.StringBuilder();
565 var ar = node.get_array();
566 for (var i = 0; i < ar.get_length(); i++) {
568 buffer.append_c('\n');
570 buffer.append(ar.get_string_element(i));
574 // hopeflyu only type value..
575 var sv = Value (typeof (string));
576 var v = node.get_value();
582 // really old files...
584 public string upgradeKey(string key, string val)
587 if (key.length < 1) {
595 return "* " + key.substring(1);
600 if (key[0] == '.') { // v2 does not start with '.' ?
601 var bits = key.substring(1).split(":");
602 if (bits[0] == "signal") {
603 return "@" + string.joinv(" ", bits).substring(bits[0].length);
605 return "# " + string.joinv(" ", bits);
607 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
610 var bits = key.substring(1).split(":");
611 // two types '$' or '|' << for methods..
613 if (Regex.match_simple ("^function\\s*(", val.strip())) {
614 return "| " + key.substring(1);
618 if (Regex.match_simple ("^\\(", val.strip())) {
620 return "| " + string.joinv(" ", bits);
623 // guessing it's a property..
624 return "$ " + string.joinv(" ", bits);
635 public Node deepClone()
638 n.loadFromJson(this.toJsonObject(), 2);
642 public string toJsonString()
644 if (Node.gen == null) {
645 Node.gen = new Json.Generator();
649 var n = new Json.Node(Json.NodeType.OBJECT);
650 n.set_object(this.toJsonObject () );
651 Node.gen.set_root (n);
652 return Node.gen.to_data (null);
655 public void jsonObjectAddStringValue(Json.Object obj, string key, string v)
657 if (v.index_of_char('\n',0) < 0) {
658 obj.set_string_member(key,v);
661 var aro = new Json.Array();
662 var ar = v.split("\n");
663 for(var i =0;i < ar.length;i++) {
664 aro.add_string_element(ar[i]);
666 obj.set_array_member(key,aro);
669 public Json.Object toJsonObject()
671 var ret = new Json.Object();
674 if (this.listeners.size > 0) {
675 var li = new Json.Object();
676 ret.set_object_member("listeners", li);
677 var liter = this.listeners.map_iterator();
678 while (liter.next()) {
679 this.jsonObjectAddStringValue(li, liter.get_value().to_json_key(), liter.get_value().val);
683 if (this.props.size > 0 ) {
684 var iter = this.props.map_iterator();
685 while (iter.next()) {
686 this.jsonObjectsetMember(ret, iter.get_value().to_json_key(), iter.get_value().val);
689 if (this.items.size > 0) {
690 var ar = new Json.Array();
691 ret.set_array_member("items", ar);
694 for(var i =0;i < this.items.size;i++) {
695 ar.add_object_element(this.items.get(i).toJsonObject());
703 public void jsonObjectsetMember(Json.Object o, string key, string val) {
704 if (Lang.isBoolean(val)) {
705 o.set_boolean_member(key, val.down() == "false" ? false : true);
710 if (Lang.isNumber(val)) {
711 if (val.contains(".")) {
712 //print( "ADD " + key + "=" + val + " as a double?\n");
713 o.set_double_member(key, double.parse (val));
717 //print( "ADD " + key + "=" + val + " as a int?\n") ;
718 o.set_int_member(key,long.parse(val));
721 ///print( "ADD " + key + "=" + val + " as a string?\n");
722 this.jsonObjectAddStringValue(o,key,val);
723 //o.set_string_member(key,val);
728 public string nodeTipProp {
733 return this.nodeTip();
736 // fixme this needs to better handle 'user defined types etc..
737 public string nodeTip()
739 var ret = this.nodeTitle(true);
748 var keys = new Gee.ArrayList<string>();
749 foreach(var k in this.props.keys) {
753 return Posix.strcmp(a, b);
758 foreach(var pk in keys) {
760 var prop = this.props.get(pk);
761 var i = prop.name.strip();
764 val = val == null ? "" : val;
768 case RAW: // should they be the same?
770 props += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
771 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
772 GLib.Markup.escape_text(val.split("\n")[0]);
779 funcs += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
780 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
781 GLib.Markup.escape_text(val.split("\n")[0]);
785 case USER : // user defined.
786 uprops += "\n\t<b>" +
787 GLib.Markup.escape_text(i) +"</b> : " +
788 GLib.Markup.escape_text(val.split("\n")[0]);
791 case SPECIAL : // * prop| args | ctor | init
793 GLib.Markup.escape_text(i) +"</b> : " +
794 GLib.Markup.escape_text(val.split("\n")[0]);
797 case LISTENER : return ""; // always raw...
807 keys = new Gee.ArrayList<string>();
808 foreach(var k in this.listeners.keys) {
812 return Posix.strcmp(a, b);
816 foreach(var pk in keys) {
818 var prop = this.listeners.get(pk);
819 var i = prop.name.strip();
821 var val = prop.val.strip();
822 if (val == null || val.length < 1) {
825 listen += "\n\t<b>" +
826 GLib.Markup.escape_text(i) +"</b> : " +
827 GLib.Markup.escape_text(val.split("\n")[0]);
832 if (props.length > 0) {
833 ret+="\n\nProperties:" + props;
835 if (uprops.length > 0) {
836 ret+="\n\nUser defined Properties:" + uprops;
840 if (funcs.length > 0) {
841 ret+="\n\nMethods:" + funcs;
843 if (listen.length > 0) {
844 ret+="\n\nListeners:" + listen;
846 if (spec.length > 0) {
847 ret+="\n\nSpecial:" + spec;
854 public string nodeTitleProp {
859 return this.nodeTitle();
868 public string nodeTitle(bool for_tip = false)
872 //var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
873 //if (sr) txt.push('<s>');
875 if (this.has("* prop")) { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
877 //if (renderfull && c['|xns']) {
878 var fqn = this.fqn();
879 var fqn_ar = fqn.split(".");
880 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
882 if (fqn == "Roo.bootstrap.Element" && this.has("tag")) {
884 txt += GLib.Markup.escape_text(this.get("tag").up());
887 //if (c.xtype) { txt.push(c.xtype); }
889 if (this.has("id")) { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
890 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
891 if (this.has("boxLabel")) { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
894 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
895 if (this.has("title")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
896 if (this.has("html") && this.get("html").length > 0) {
897 var ht = this.get("html").split("\n");
899 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
901 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
904 if (this.has("label")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
905 if (this.has("header")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
906 if (this.has("legend")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
907 if (this.has("text")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
908 if (this.has("name")) { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
909 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
910 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
911 // class is quite important on bootstrap..
912 if (this.has("cls")){ txt += ("<b>[cls=" + GLib.Markup.escape_text(this.get("cls")) + "]</b>"); }
915 if (fqn == "Roo.bootstrap.Link") {
916 txt += ("<b>href=" + (this.has("name") ? GLib.Markup.escape_text(this.get("name")) : "?" ) + "</b>");
917 if (this.has("fa")){ txt += ("<b>[fa=" + GLib.Markup.escape_text(this.get("fa")) + "]</b>"); }
922 // for flat classes...
923 //if (typeof(c["*class"]"))!= "undefined") { txt += ("<b>" + c["*class"]+ "</b>"); }
924 //if (typeof(c["*extends"]"))!= "undefined") { txt += (": <i>" + c["*extends"]+ "</i>"); }
927 //if (sr) txt.push('</s>');
928 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
930 // used by trees to display icons?
931 // needs more thought?!?
932 public string iconFilename {
937 var clsname = this.fqn();
939 var clsb = clsname.split(".");
940 var sub = clsb.length > 1 ? clsb[1].down() : "";
941 var fn = "/usr/share/glade/pixmaps/hicolor/16x16/actions/widget-gtk-" + sub + ".png";
942 //if (FileUtils.test (fn, FileTest.IS_REGULAR)) {
945 //return "/dev/null"; //???
951 public void insertAfter(Node child, Node after)
953 this.insertChild(this.items.index_of(after) + 1, child);
955 public void insertBefore(Node child, Node before)
957 this.insertChild(this.items.index_of(before), child);
960 public void insertChild(int pos, Node child)
962 this.items.insert(pos, child);
963 this.childstore.insert(pos, child);
966 public void appendChild(Node child)
968 this.items.add( child);
969 this.childstore.append(child);
977 previous we had listeners / and props
979 we really need to store this as flat array - keep it simple!?
991 public void loadProps(GLib.ListStore model, Project.Project project)
994 // fixme sorting?? - no need to loop twice .. just use sorting.!
995 var oldstore = this.propstore;
996 this.propstore = model;
997 for(var i = 0; i < oldstore.n_items; i++ ) {
998 var it = (NodeProp) oldstore.get_item(i);
999 it.update_is_valid_ptype(project);
1006 // used to replace propstore, so it does not get wiped by editing a node
1007 public void dupeProps()
1009 GLib.debug("dupeProps START");
1010 var oldstore = this.propstore;
1011 this.propstore = new GLib.ListStore(typeof(NodeProp));;
1012 for(var i = 0; i < oldstore.n_items; i++ ) {
1013 var it = (NodeProp) oldstore.get_item(i);
1014 this.propstore.append(it);
1016 GLib.debug("dupeProps END");
1020 public void remove_prop(NodeProp prop)
1023 if (!this.propstore.find(prop, out pos)) {
1026 this.propstore.remove(pos);
1027 this.updated_count++;
1031 public bool has_prop_key(NodeProp prop)
1033 for(var i = 0; i < this.propstore.n_items; i++ ) {
1034 var it = (NodeProp) this.propstore.get_item(i);
1035 if (it.ptype == prop.ptype && it.to_index_key() == prop.to_index_key()) {
1047 public void add_prop(NodeProp prop)
1049 if (this.has_prop_key(prop) && !prop.to_index_key().has_suffix("[]")) {
1050 GLib.warning("duplicate key' %s'- can not add - call has_prop_key first", prop.to_index_key());
1054 this.propstore.append(prop);
1057 this.updated_count++;
1062 int props_updated_count = -1;
1063 Gee.HashMap<string,NodeProp> props_cache;
1065 public Gee.HashMap<string,NodeProp> props {
1067 if (this.updated_count == this.props_updated_count) {
1068 return this.props_cache;
1070 this.props_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1072 for(var i = 0; i < this.propstore.n_items; i++ ) {
1073 var it = (NodeProp) this.propstore.get_item(i);
1074 if (it.ptype != NodePropType.LISTENER) {
1075 // GLib.debug("props add key %s", it.to_index_key());
1076 this.props_cache.set( it.to_index_key() , it);
1079 this.props_updated_count = this.updated_count;
1080 return this.props_cache;
1083 GLib.error("do not set listerners direclty");
1087 int listeners_updated_count = -1;
1088 Gee.HashMap<string,NodeProp> listeners_cache;
1090 //private Gee.HashMap<string,NodeProp> _listeners; // the listeners..
1091 public Gee.HashMap<string,NodeProp> listeners {
1093 if (this.updated_count == this.listeners_updated_count) {
1094 return this.listeners_cache;
1097 this.listeners_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1099 for(var i = 0; i < this.propstore.n_items; i++ ) {
1100 var it = (NodeProp) this.propstore.get_item(i);
1101 if (it.ptype == NodePropType.LISTENER) {
1102 this.listeners_cache.set( it.to_index_key() , it);
1105 this.listeners_updated_count = this.updated_count;
1106 return this.listeners_cache;;
1109 GLib.error("do not set listerners direclty");
1112 private void sortProps ()
1115 this.propstore.sort( (a, b) => {
1117 return Posix.strcmp( ((NodeProp)a).to_sort_key(), ((NodeProp)b).to_sort_key());