Fix #7250 - better handling of adding properties
[roobuilder] / src / Builder4 / PopoverProperty.bjs
1 {
2  "build_module" : "builder",
3  "items" : [
4   {
5    "# JsRender.NodeProp? prop" : "",
6    "$ xns" : "Gtk",
7    "@ void success" : "(Project.Project pr, JsRender.JsRender file)",
8    "Gtk.PositionType position" : "Gtk.PositionType.LEFT",
9    "JsRender.Node node" : "",
10    "Xcls_MainWindow mainwindow" : "null",
11    "bool done" : false,
12    "bool is_new" : false,
13    "bool modal" : true,
14    "id" : "PopoverProperty",
15    "items" : [
16     {
17      "$ pack" : "add",
18      "$ xns" : "Gtk",
19      "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
20      "bool homogeneous" : false,
21      "items" : [
22       {
23        "$ xns" : "Gtk",
24        "* pack" : "pack_start,false,true,0",
25        "id" : "header",
26        "string title" : "Modify / Create Property",
27        "xtype" : "HeaderBar"
28       },
29       {
30        "$ xns" : "Gtk",
31        "* pack" : "add",
32        "Gtk.Align halign" : "Gtk.Align.START",
33        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
34        "int margin_top" : 12,
35        "label" : "Special Flags",
36        "x_options" : 4,
37        "xtype" : "Label"
38       },
39       {
40        "$ xns" : "Gtk",
41        "* init" : "this.el.add_attribute(_this.dbcellrenderer.el , \"markup\", 1 );",
42        "* pack" : "add",
43        "id" : "kflag",
44        "items" : [
45         {
46          "$ xns" : "Gtk",
47          "* pack" : "pack_start,true",
48          "id" : "dbcellrenderer",
49          "xtype" : "CellRendererText"
50         },
51         {
52          "$ columns" : "typeof(JsRender.NodePropType),typeof(string)",
53          "$ xns" : "Gtk",
54          "* pack" : "set_model",
55          "id" : "dbmodel",
56          "n_columns" : 2,
57          "xtype" : "ListStore",
58          "| void loadData" : [
59           "  (JsRender.NodeProp prop) {",
60           "    this.el.clear();                                    ",
61           "    Gtk.TreeIter iter;",
62           "    var el = this.el;",
63           "    ",
64           "    ",
65           "    // vala signal.. '@'",
66           "    // raw value '$'",
67           "    // user defined property '#'",
68           "    // user defined method '|'",
69           "    // special property '*' => prop  |args|ctor|init",
70           "    ",
71           "    ",
72           "    ",
73           "   /// el.append(out iter);",
74           "    ",
75           "     ",
76           "   // el.set_value(iter, 0, \"\");",
77           "   // el.set_value(iter, 1, \"aaa  - Just add Element - aaa\");",
78           "",
79           "    ",
80           "\tif (prop.ptype == JsRender.NodePropType.LISTENER) { ",
81           "\t\tel.append(out iter);",
82           "\t\tel.set(iter, 0, JsRender.NodePropType.LISTENER, 1,   \"Event Handler / Listener\", -1);",
83           "\t}\t ",
84           "\telse if (_this.mainwindow.windowstate.file.xtype == \"Gtk\") {",
85           "\t\t el.append(out iter);",
86           "\t    el.set(iter, 0, JsRender.NodePropType.PROP, 1,   \"Normal Property\", -1);",
87           "\t",
88           "\t\t",
89           "\t\tel.append(out iter);",
90           "\t\tel.set(iter, 0, JsRender.NodePropType.RAW, 1,   \"Raw Property (not escaped)\", -1);",
91           "\t\t ",
92           "\t\t",
93           "\t\tel.append(out iter);",
94           "\t\tel.set(iter, 0, JsRender.NodePropType.USER, 1,   \"User defined property\", -1);",
95           "\t\t ",
96           "\t\tel.append(out iter);",
97           "\t\tel.set(iter, 0, JsRender.NodePropType.METHOD, 1,   \"User defined method\", -1);",
98           "\t\t ",
99           "\t\tel.append(out iter);",
100           "\t\tel.set(iter, 0, JsRender.NodePropType.SPECIAL, 1,   \"Special property (eg. prop | args | ctor | init )\", -1);",
101           "\t\t ",
102           "\t\t",
103           "\t\tel.append(out iter);",
104           "\t    el.set(iter, 0, JsRender.NodePropType.SIGNAL, 1,   \"Vala Signal\", -1);",
105           "\t\t ",
106           "\t\t",
107           "\t} else { ",
108           "\t\t// javascript",
109           "\t    el.append(out iter);",
110           "\t    el.set(iter, 0, JsRender.NodePropType.PROP, 1,   \"Normal Property\", -1);",
111           "\t",
112           "\t\tel.append(out iter);",
113           "\t\tel.set(iter, 0, JsRender.NodePropType.RAW, 1,   \"Raw Property (not escaped)\", -1);",
114           "\t\t ",
115           "\t\tel.append(out iter);",
116           "\t\tel.set(iter, 0, JsRender.NodePropType.METHOD, 1,   \"User defined method\", -1);",
117           "\t ",
118           "\t\tel.append(out iter);",
119           "\t\tel.set(iter, 0,  JsRender.NodePropType.SPECIAL, 1,   \"(*) Special property (eg. prop )\", -1);",
120           "\t\t ",
121           "\t",
122           "\t}",
123           "\t// set selected, based on arg",
124           "\tel.foreach((tm, tp, titer) => {",
125           "\t\tGLib.Value val;",
126           "\t\tel.get_value(titer, 0, out val);",
127           "\t\t ",
128           "\t\t//print(\"check %s against %s\\n\", (string)val, _this.prop.ptype);",
129           "\t\tif (((JsRender.NodePropType)val) == prop.ptype) {",
130           "\t\t\t_this.kflag.el.set_active_iter(titer);",
131           "\t\t\treturn true;",
132           "\t\t}",
133           "\t\treturn false;",
134           "\t});",
135           "\t",
136           "",
137           "                                     ",
138           "}",
139           ""
140          ]
141         }
142        ],
143        "xtype" : "ComboBox"
144       },
145       {
146        "$ visible" : true,
147        "$ xns" : "Gtk",
148        "* pack" : "add",
149        "Gtk.Align halign" : "Gtk.Align.START",
150        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
151        "int margin_top" : 12,
152        "label" : "Type or Return Type",
153        "x_options" : 4,
154        "xtype" : "Label"
155       },
156       {
157        "$ visible" : true,
158        "$ xns" : "Gtk",
159        "* pack" : "add",
160        "id" : "ktype",
161        "xtype" : "Entry"
162       },
163       {
164        "$ visible" : true,
165        "$ xns" : "Gtk",
166        "* pack" : "add",
167        "Gtk.Align halign" : "Gtk.Align.START",
168        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
169        "int margin_top" : 12,
170        "label" : "Name",
171        "tooltip_text" : "center, north, south, east, west",
172        "x_options" : 4,
173        "xtype" : "Label"
174       },
175       {
176        "$ visible" : true,
177        "$ xns" : "Gtk",
178        "* pack" : "add",
179        "id" : "kname",
180        "listeners" : {
181         "focus_out_event" : [
182          "()=>{",
183          "\t_this.error.setError(\"\");",
184          "\tvar val = this.el.get_text().strip(); ",
185          "\tif (val.length < 1) {",
186          "\t\t_this.error.setError(\"Name can not be empty\");",
187          "\t}",
188          "\treturn true;",
189          "}",
190          "",
191          ""
192         ],
193         "key_release_event" : [
194          "()=>{",
195          "\t_this.error.setError(\"\");",
196          "\tvar val = this.el.get_text().strip(); ",
197          "\tif (val.length < 1) {",
198          "\t\t_this.error.setError(\"Name can not be empty\");",
199          "\t}",
200          "\treturn true;",
201          "}",
202          "",
203          ""
204         ]
205        },
206        "xtype" : "Entry"
207       },
208       {
209        "$ visible" : true,
210        "$ xns" : "Gtk",
211        "* pack" : "add",
212        "Gtk.Align halign" : "Gtk.Align.START",
213        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
214        "bool use_markup" : true,
215        "id" : "error",
216        "int margin_top" : 0,
217        "label" : "<span color=\"red\">Error Message</span>",
218        "tooltip_text" : "center, north, south, east, west",
219        "x_options" : 4,
220        "xtype" : "Label",
221        "| void setError" : [
222         "(string err)   {",
223         "\tif (err == \"\") {",
224         "\t\tthis.el.hide();",
225         "\t} else {",
226         "\t\tthis.el.show();",
227         "\t\t",
228         "\t\tthis.el.label = \"<span color=\\\"red\\\">\" + err + \"</span>\";",
229         "\t}",
230         "}",
231         ""
232        ]
233       },
234       {
235        "$ xns" : "Gtk",
236        "* pack" : "add",
237        "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
238        "id" : "buttonbar",
239        "int margin_top" : 20,
240        "items" : [
241         {
242          "$ xns" : "Gtk",
243          "* pack" : "add",
244          "bool always_show_image" : true,
245          "bool hexpand" : true,
246          "items" : [
247           {
248            "$ xns" : "Gtk",
249            "* pack" : "composite_name",
250            "* prop" : "image",
251            "string icon_name" : "window-close",
252            "xtype" : "Image"
253           }
254          ],
255          "listeners" : {
256           "pressed" : [
257            "() => { ",
258            "",
259            "\t_this.prop = null;",
260            "\t_this.is_new = false;",
261            "\t_this.kname.el.set_text(\"Cancel\");",
262            "\t_this.el.hide();",
263            "",
264            "}"
265           ]
266          },
267          "string label" : "Cancel",
268          "xtype" : "Button"
269         },
270         {
271          "$ xns" : "Gtk",
272          "* pack" : "add",
273          "bool always_show_image" : true,
274          "bool hexpand" : true,
275          "items" : [
276           {
277            "$ xns" : "Gtk",
278            "* prop" : "image",
279            "string icon_name" : "list-add",
280            "xtype" : "Image"
281           }
282          ],
283          "listeners" : {
284           "pressed" : [
285            "() => {",
286            "\t// check if text is not empty..",
287            "\tif ( _this.kname.el.get_text().strip().length < 1) {",
288            "\t\t// error should already be showing?",
289            "\t\treturn;",
290            "\t}",
291            "\t_this.updateProp();",
292            "\t",
293            "\t// since we can't add listeners?!?!?",
294            "\t// only check props.",
295            "\t// check if property already exists in node.\t",
296            "\tvar prop = _this.prop;",
297            "\tif (_this.node.props.has_key(prop.to_index_key())) {",
298            "\t\t_this.error.setError(\"Property already exists\");",
299            "\t\treturn;\t",
300            "\t}",
301            "\t",
302            "\t",
303            "\t ",
304            "\t_this.is_new = false;\t",
305            "\t  ",
306            "\t// hide self",
307            "\t_this.prop = null; // skip checks..",
308            "\t_this.el.hide();",
309            "",
310            "// add it, ",
311            "\t// trigger editing of property.",
312            "\t// allow hide to work?",
313            "\twhile (Gtk.events_pending()) {",
314            "\t\tGtk.main_iteration();",
315            "\t}",
316            "\t",
317            "\t_this.mainwindow.windowstate.left_props.addProp(prop);\t\t",
318            "\t",
319            "",
320            "}"
321           ]
322          },
323          "string label" : "Add Property",
324          "xtype" : "Button"
325         }
326        ],
327        "xtype" : "Box"
328       }
329      ],
330      "xtype" : "Box"
331     }
332    ],
333    "listeners" : {
334     "closed" : [
335      "() => {",
336      " \tGLib.debug(\"popover closed\");",
337      "\tif (_this.is_new) {",
338      "\t\t// dont allow hiding if we are creating a new one.",
339      "\t\t// on.hide will reshow it.",
340      "\t\treturn;",
341      "",
342      "\t}",
343      "\tif (_this.prop == null) {",
344      "\t\t// hide and dont update.",
345      "\t\treturn;",
346      "\t}",
347      "\tif (this.kname.el.get_text().strip().length < 1) {",
348      "\t\treturn;",
349      "\t}",
350      "",
351      "\tthis.updateProp();",
352      "",
353      "\t_this.mainwindow.windowstate.left_props.reload();",
354      "",
355      "",
356      "  ",
357      "} "
358     ],
359     "hide" : [
360      "() => {",
361      "  \tGLib.debug(\"popover hidden\");",
362      "\tif (_this.is_new || this.kname.el.get_text().strip().length < 1) {",
363      "\t\t// dont allow hiding if we are creating a new one.",
364      "\t\tGLib.debug(\"prevent hiding as its new or text is empty\"); ",
365      "\t\tthis.el.show_all();",
366      "\t\treturn;",
367      "",
368      "\t}",
369      "\t",
370      "}"
371     ]
372    },
373    "string key_type" : "",
374    "string old_keyname" : "",
375    "uint border_width" : 0,
376    "xtype" : "Popover",
377    "| void show" : [
378     "(",
379     "\tGtk.Widget btn, ",
380     "\tJsRender.Node node, ",
381     "\tJsRender.NodeProp prop, ",
382     "\tint y,",
383     "\tbool is_new = false",
384     "\t ) ",
385     "{",
386     "\t",
387     "   ",
388     "\tthis.is_new = is_new; ",
389     "\tvar pref = is_new ? \"Add \" : \"Modify \";",
390     "\tif (prop.ptype == JsRender.NodePropType.LISTENER) {",
391     "\t\tthis.header.el.title = pref + \"Event Listener\"; // cant really happen yet?",
392     "\t} else {",
393     "\t\tthis.header.el.title = pref + \"Property\";",
394     "\t}",
395     "\tthis.prop = prop;",
396     "\tthis.node = node;",
397     "\t",
398     "\t_this.kname.el.set_text(prop.name);",
399     "\t_this.ktype.el.set_text(prop.rtype);",
400     "\t",
401     "\t_this.dbmodel.loadData(prop );",
402     "\t// does node have this property...",
403     "",
404     "",
405     "\t_this.node = node;",
406     "\t//console.log('show all');",
407     "\tthis.el.set_modal(true);",
408     "\tthis.el.set_relative_to(btn);",
409     "\tif (y > -1) {",
410     "\t\tvar  r = Gdk.Rectangle() {",
411     "\t\t\tx = 0, // align left...",
412     "\t\t\ty = y,",
413     "\t\t\twidth = 1,",
414     "\t\t\theight = 1",
415     "\t\t};",
416     "\t\tthis.el.set_pointing_to( r);",
417     "\t}",
418     "\t",
419     "\t",
420     "",
421     "\t//this.el.set_position(Gtk.PositionType.TOP);",
422     "",
423     "\t// window + header?",
424     "\t print(\"SHOWALL - POPIP\\n\");",
425     "\tthis.el.show_all();",
426     "\tthis.kname.el.grab_focus();",
427     "\tthis.buttonbar.el.hide();",
428     "\tif (this.is_new) {",
429     "\t\tthis.buttonbar.el.show();",
430     "\t}",
431     "\t this.error.setError(\"\");",
432     "",
433     "\t//this.success = c.success;",
434     " ",
435     "}"
436    ],
437    "| void updateProp" : [
438     "() {",
439     "\tvar newtext = \"\";",
440     "\tGtk.TreeIter citer;",
441     "\tGLib.Value gval;",
442     "\tthis.kflag.el.get_active_iter(out citer);",
443     "\tthis.dbmodel.el.get_value(citer, 0, out  gval);",
444     "",
445     "",
446     "\t_this.prop.name = this.kname.el.get_text().strip(); ",
447     "\t_this.prop.rtype = this.ktype.el.get_text().strip(); ",
448     "\t_this.prop.ptype =  (JsRender.NodePropType) gval;",
449     "",
450     "}",
451     ""
452    ]
453   }
454  ],
455  "modOrder" : "",
456  "name" : "PopoverProperty",
457  "parent" : "",
458  "path" : "/home/alan/gitlive/roobuilder/src/Builder4/PopoverProperty.bjs",
459  "permname" : "",
460  "title" : ""
461 }