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:....
113 public JsRender? file = null;
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.iconResourceName = "";
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++; // will recurse up.
132 if (this.file != null) {
133 this.file.updateUndo();
138 } // changes to this trigger updates on the tree..
140 public string as_source = "";
141 public int as_source_version = -1;
142 public int as_source_start_line = -1;
146 //public signal void version_changed();
150 this.items = new Gee.ArrayList<Node>();
151 //this._props = new Gee.HashMap<string,NodeProp>();
152 //this._listeners = new Gee.HashMap<string,NodeProp>(); // Nodeprop can include line numbers..
153 this.propstore = new GLib.ListStore(typeof(NodeProp)); // Nodeprop can include line numbers..
155 this.xvala_xcls = "";
158 this.line_start = -1;
160 this.lines = new Gee.ArrayList<int>();
161 this.line_map = new Gee.HashMap<int,string>();
162 this.node_lines = new Gee.ArrayList<int>();
163 this.node_lines_map = new Gee.HashMap<int,Node>();
164 this.childstore = new GLib.ListStore( typeof(Node));
165 this.oid = uid_count++;
169 public bool has_parent(Node n)
171 if (this.parent == null) {
174 if (this.parent.oid == n.oid) {
179 return this.parent.has_parent(n);
183 public Gee.ArrayList<Node> readItems()
185 return this.items; // note should not modify add/remove from this directly..
188 public void setNodeLine(int line, Node node) {
189 //print("Add node @ %d\n", line);
190 if (this.node_lines_map.has_key(line)) {
193 this.node_lines.add(line);
194 this.node_lines_map.set(line, node);
198 public void setLine(int line, string type, string prop)
200 //GLib.debug("set prop %s (%s) to line %d", prop, type, line);
201 if (this.line_map.has_key(line)) {
202 if (this.line_map.get(line) != "e:" ) {
206 this.lines.add(line);
208 this.line_map.set(line, type + ":" + prop);
209 if (type == "e" || type == "p" ) {
211 if (prop == "" || !this.props.has_key(prop)) {
212 ///GLib.debug("cant find prop '%s'", prop);
216 var prope = this.props.get(prop);
217 if (prope != null && type =="p") {
218 prope.start_line = line;
220 if (prope != null && type =="e") {
221 prope.end_line = line;
225 if (type == "l" || type =="x") {
226 if (prop == "" || !this.listeners.has_key(prop)) {
227 //GLib.debug("cant find listener '%s'", prop);
231 var prope = this.listeners.get(prop);
232 if (prope != null && type =="l") {
233 prope.start_line = line;
235 if (prope != null && type =="x") {
236 prope.end_line = line;
245 //GLib.debug("setLine %d, %s", line, type + ":" + prop);
247 public void sortLines() {
248 //print("sortLines\n");
249 this.lines.sort((a,b) => {
250 return (int)a-(int)b;
252 this.node_lines.sort((a,b) => {
253 return (int)a-(int)b;
256 public Node? lineToNode(int line)
258 //print("Searching for line %d\n",line);
260 //foreach(int el in this.node_lines) {
261 //print("all lines %d\n", el);
265 foreach(int el in this.node_lines) {
266 //print("?match %d\n", el);
279 var ret = this.node_lines_map.get(l);
280 if (line > ret.line_end) {
283 //print("RETURNING NODE ON LINE %d", l);
290 var ret = this.node_lines_map.get(l);
291 if (line > ret.line_end) {
294 //print("RETURNING NODE ON LINE %d", l);
303 public NodeProp? lineToProp(int line)
306 for(var i= 0; i < this.propstore.get_n_items();i++) {
307 var p = (NodeProp) this.propstore.get_item(i);
308 //GLib.debug("prop %s lines %d -> %d", p.name, p.start_line, p.end_line);
309 if (p.start_line > line) {
312 if (line > p.end_line) {
322 public bool getPropertyRange(string prop, out int start, out int end)
326 foreach(int el in this.lines) {
328 if (this.line_map.get(el) == prop) {
342 public void dumpProps(string indent = "")
344 print("%s:\n" , this.fqn());
345 foreach(int el in this.lines) {
346 print("%d: %s%s\n", el, indent, this.line_map.get(el));
348 foreach(Node n in this.items) {
349 n.dumpProps(indent + " ");
357 if (this.props.get("id") == null) {
358 return "uid-%d".printf(this.oid);
360 return this.props.get("id").val;
364 public bool hasChildren()
366 return this.items.size > 0;
368 public bool hasXnsType()
370 if (this.props.get("xns") != null && this.props.get("xtype") != null) {
377 public string FQN { // for sorting
378 owned get { return this.fqn(); }
381 public string NS { // for sorting
382 owned get { return this.props.has_key("xns") ? this.props.get("xns").val : ""; }
387 if (!this.hasXnsType ()) {
390 return this.props.get("xns").val + "." + this.props.get("xtype").val;
393 public void setFqn(string name)
395 var ar = name.split(".");
396 var l = name.length - (ar[ar.length-1].length +1);
400 if (this.props.has_key("xtype")) {
401 this.props.get("xtype").val = ar[ar.length-1];
403 this.add_prop(new NodeProp.prop("xtype", "", ar[ar.length-1]));
405 if (this.props.has_key("xns")) {
406 this.props.get("xns").val = name.substring(0, l);
408 this.add_prop(new NodeProp.raw("xns", "", name.substring(0, l)));
412 //print("setFQN %s to %s\n", name , this.fqn());
416 // wrapper around get props that returns empty string if not found.
417 //overrides Glib.object.get (hence new)
418 public new string get(string key)
421 var v = this.props.get(key);
422 return v == null ? "" : v.val;
425 public NodeProp? get_prop(string key)
428 return this.props.get(key);
436 public bool has(string key)
438 return this.props.has_key(key);
445 if (this.parent == null) {
446 GLib.debug("remove - parent is null?");
449 var nlist = new Gee.ArrayList<Node>();
450 for (var i =0;i < this.parent.items.size; i++) {
451 if (this.parent.items.get(i) == this) {
454 nlist.add(this.parent.items.get(i));
457 if ( this.parent.childstore.find(this, out pos)) {
458 this.parent.childstore.remove(pos);
460 this.parent.updated_count++;
461 this.parent.items = nlist;
466 /* creates javascript based on the rules */
467 public Node? findProp(string n) {
468 for(var i=0;i< this.items.size;i++) {
469 var p = this.items.get(i).get("* prop");
474 return this.items.get(i);
484 static Json.Generator gen = null;
486 public string quoteString(string str)
488 if (Node.gen == null) {
489 Node.gen = new Json.Generator();
491 var n = new Json.Node(Json.NodeType.VALUE);
494 Node.gen.set_root (n);
495 return Node.gen.to_data (null);
498 public void loadFromJsonString(string str, int ver)
500 var pa = new Json.Parser();
502 pa.load_from_data(str);
503 } catch (GLib.Error e) {
504 GLib.debug("Error loading string?");
507 var new_node = pa.get_root();
508 var obj = new_node.get_object ();
510 this.loadFromJson(obj, ver);
515 public void loadFromJson(Json.Object obj, int version) {
517 obj.foreach_member((o , key, value) => {
519 if (key == "items") {
520 var ar = value.get_array();
521 ar.foreach_element( (are, ix, el) => {
522 var node = new Node();
524 node.loadFromJson(el.get_object(), version);
525 this.items.add(node);
526 this.childstore.append(node);
530 if (key == "listeners") {
531 var li = value.get_object();
532 li.foreach_member((lio , li_key, li_value) => {
533 this.add_prop(new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
534 //this.listeners.set(li_key, new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
541 var sval = this.jsonNodeAsString(value);
544 rkey = this.upgradeKey(key, sval);
546 var n = new NodeProp.from_json(rkey, sval);
560 // converts the array into a string with line breaks.
561 public string jsonNodeAsString(Json.Node node)
564 if (node.get_node_type() == Json.NodeType.ARRAY) {
565 var buffer = new GLib.StringBuilder();
566 var ar = node.get_array();
567 for (var i = 0; i < ar.get_length(); i++) {
569 buffer.append_c('\n');
571 buffer.append(ar.get_string_element(i));
575 // hopeflyu only type value..
576 var sv = Value (typeof (string));
577 var v = node.get_value();
583 // really old files...
585 public string upgradeKey(string key, string val)
588 if (key.length < 1) {
596 return "* " + key.substring(1);
601 if (key[0] == '.') { // v2 does not start with '.' ?
602 var bits = key.substring(1).split(":");
603 if (bits[0] == "signal") {
604 return "@" + string.joinv(" ", bits).substring(bits[0].length);
606 return "# " + string.joinv(" ", bits);
608 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
611 var bits = key.substring(1).split(":");
612 // two types '$' or '|' << for methods..
614 if (Regex.match_simple ("^function\\s*(", val.strip())) {
615 return "| " + key.substring(1);
619 if (Regex.match_simple ("^\\(", val.strip())) {
621 return "| " + string.joinv(" ", bits);
624 // guessing it's a property..
625 return "$ " + string.joinv(" ", bits);
636 public Node deepClone()
639 n.loadFromJson(this.toJsonObject(), 2);
643 public string toJsonString()
645 if (Node.gen == null) {
646 Node.gen = new Json.Generator();
650 var n = new Json.Node(Json.NodeType.OBJECT);
651 n.set_object(this.toJsonObject () );
652 Node.gen.set_root (n);
653 return Node.gen.to_data (null);
656 public void jsonObjectAddStringValue(Json.Object obj, string key, string v)
658 if (v.index_of_char('\n',0) < 0) {
659 obj.set_string_member(key,v);
662 var aro = new Json.Array();
663 var ar = v.split("\n");
664 for(var i =0;i < ar.length;i++) {
665 aro.add_string_element(ar[i]);
667 obj.set_array_member(key,aro);
670 public Json.Object toJsonObject()
672 var ret = new Json.Object();
675 if (this.listeners.size > 0) {
676 var li = new Json.Object();
677 ret.set_object_member("listeners", li);
678 var liter = this.listeners.map_iterator();
679 while (liter.next()) {
680 this.jsonObjectAddStringValue(li, liter.get_value().to_json_key(), liter.get_value().val);
684 if (this.props.size > 0 ) {
685 var iter = this.props.map_iterator();
686 while (iter.next()) {
687 this.jsonObjectsetMember(ret, iter.get_value().to_json_key(), iter.get_value().val);
690 if (this.items.size > 0) {
691 var ar = new Json.Array();
692 ret.set_array_member("items", ar);
695 for(var i =0;i < this.items.size;i++) {
696 ar.add_object_element(this.items.get(i).toJsonObject());
704 public void jsonObjectsetMember(Json.Object o, string key, string val) {
705 if (Lang.isBoolean(val)) {
706 o.set_boolean_member(key, val.down() == "false" ? false : true);
711 if (Lang.isNumber(val)) {
712 if (val.contains(".")) {
713 //print( "ADD " + key + "=" + val + " as a double?\n");
714 o.set_double_member(key, double.parse (val));
718 //print( "ADD " + key + "=" + val + " as a int?\n") ;
719 o.set_int_member(key,long.parse(val));
722 ///print( "ADD " + key + "=" + val + " as a string?\n");
723 this.jsonObjectAddStringValue(o,key,val);
724 //o.set_string_member(key,val);
729 public string nodeTipProp {
734 return this.nodeTip();
737 // fixme this needs to better handle 'user defined types etc..
738 public string nodeTip()
740 var ret = this.nodeTitle(true);
749 var keys = new Gee.ArrayList<string>();
750 foreach(var k in this.props.keys) {
754 return Posix.strcmp(a, b);
759 foreach(var pk in keys) {
761 var prop = this.props.get(pk);
762 var i = prop.name.strip();
765 val = val == null ? "" : val;
769 case RAW: // should they be the same?
771 props += "\n\t" + (prop.rtype != null && prop.rtype.length > 0 ? GLib.Markup.escape_text(prop.rtype) : "") +
772 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
773 (val.length > 0 ? GLib.Markup.escape_text(val.split("\n")[0]) : "");
779 funcs += "\n\t" + (prop.rtype != null && prop.rtype.length > 0 ? GLib.Markup.escape_text(prop.rtype) : "") +
780 " <b>" + GLib.Markup.escape_text(i) +"</b> : " +
781 (val.length > 0 ? 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 (val.length > 0 ? GLib.Markup.escape_text(val.split("\n")[0]) : "");
791 case SPECIAL : // * prop| args | ctor | init
793 GLib.Markup.escape_text(i) +"</b> : " +
794 (val.length > 0 ? 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 iconResourceName {
937 var clsname = this.fqn();
939 var clsb = clsname.split(".");
940 var sub = clsb.length > 1 ? clsb[1].down() : "";
941 var fn = "/glade-icons/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)
963 this.items.insert(pos, child);
964 this.childstore.insert(pos, child);
967 public void appendChild(Node child)
970 this.items.add( child);
971 this.childstore.append(child);
979 previous we had listeners / and props
981 we really need to store this as flat array - keep it simple!?
993 public void loadProps(GLib.ListStore model, Project.Project project)
996 // fixme sorting?? - no need to loop twice .. just use sorting.!
997 var oldstore = this.propstore;
998 this.propstore = model;
999 for(var i = 0; i < oldstore.n_items; i++ ) {
1000 var it = (NodeProp) oldstore.get_item(i);
1001 it.update_is_valid_ptype(project);
1008 // used to replace propstore, so it does not get wiped by editing a node
1009 public void dupeProps()
1011 GLib.debug("dupeProps START");
1012 var oldstore = this.propstore;
1013 this.propstore = new GLib.ListStore(typeof(NodeProp));;
1014 for(var i = 0; i < oldstore.n_items; i++ ) {
1015 var it = (NodeProp) oldstore.get_item(i);
1016 this.propstore.append(it);
1018 GLib.debug("dupeProps END");
1022 public void remove_prop(NodeProp prop)
1025 if (!this.propstore.find(prop, out pos)) {
1028 this.propstore.remove(pos);
1029 this.updated_count++;
1033 public bool has_prop_key(NodeProp prop)
1035 for(var i = 0; i < this.propstore.n_items; i++ ) {
1036 var it = (NodeProp) this.propstore.get_item(i);
1037 if (it.ptype == prop.ptype && it.to_index_key() == prop.to_index_key()) {
1049 public void add_prop(NodeProp prop)
1051 if (this.has_prop_key(prop) && !prop.to_index_key().has_suffix("[]")) {
1052 GLib.warning("duplicate key' %s'- can not add - call has_prop_key first", prop.to_index_key());
1056 this.propstore.append(prop);
1059 this.updated_count++;
1064 int props_updated_count = -1;
1065 Gee.HashMap<string,NodeProp> props_cache;
1067 public Gee.HashMap<string,NodeProp> props {
1069 if (this.updated_count == this.props_updated_count) {
1070 return this.props_cache;
1072 this.props_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1074 for(var i = 0; i < this.propstore.n_items; i++ ) {
1075 var it = (NodeProp) this.propstore.get_item(i);
1076 if (it.ptype != NodePropType.LISTENER) {
1077 // GLib.debug("props add key %s", it.to_index_key());
1078 this.props_cache.set( it.to_index_key() , it);
1081 this.props_updated_count = this.updated_count;
1082 return this.props_cache;
1085 GLib.error("do not set listerners direclty");
1089 int listeners_updated_count = -1;
1090 Gee.HashMap<string,NodeProp> listeners_cache;
1092 //private Gee.HashMap<string,NodeProp> _listeners; // the listeners..
1093 public Gee.HashMap<string,NodeProp> listeners {
1095 if (this.updated_count == this.listeners_updated_count) {
1096 return this.listeners_cache;
1099 this.listeners_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1101 for(var i = 0; i < this.propstore.n_items; i++ ) {
1102 var it = (NodeProp) this.propstore.get_item(i);
1103 if (it.ptype == NodePropType.LISTENER) {
1104 this.listeners_cache.set( it.to_index_key() , it);
1107 this.listeners_updated_count = this.updated_count;
1108 return this.listeners_cache;;
1111 GLib.error("do not set listerners direclty");
1114 private void sortProps ()
1117 this.propstore.sort( (a, b) => {
1119 return Posix.strcmp( ((NodeProp)a).to_sort_key(), ((NodeProp)b).to_sort_key());