Support user defined properties in JS (BC for old files really?)
[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.RIGHT",
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\t// we appear to still use this?!? (builderCfg?)",
116           "\t\tel.append(out iter);",
117           "\t\tel.set(iter, 0, JsRender.NodePropType.USER, 1,   \"User defined property\", -1);",
118           "\t\t",
119           "\t\t",
120           "\t\tel.append(out iter);",
121           "\t\tel.set(iter, 0, JsRender.NodePropType.METHOD, 1,   \"User defined method\", -1);",
122           "\t ",
123           "\t\tel.append(out iter);",
124           "\t\tel.set(iter, 0,  JsRender.NodePropType.SPECIAL, 1,   \"(*) Special property (eg. prop )\", -1);",
125           "\t\t ",
126           "\t",
127           "\t}",
128           "\t// set selected, based on arg",
129           "\tel.foreach((tm, tp, titer) => {",
130           "\t\tGLib.Value val;",
131           "\t\tel.get_value(titer, 0, out val);",
132           "\t\t ",
133           "\t\t//print(\"check %s against %s\\n\", (string)val, _this.prop.ptype);",
134           "\t\tif (((JsRender.NodePropType)val) == prop.ptype) {",
135           "\t\t\t_this.kflag.el.set_active_iter(titer);",
136           "\t\t\treturn true;",
137           "\t\t}",
138           "\t\treturn false;",
139           "\t});",
140           "\t",
141           "",
142           "                                     ",
143           "}",
144           ""
145          ]
146         }
147        ],
148        "xtype" : "ComboBox"
149       },
150       {
151        "$ visible" : true,
152        "$ xns" : "Gtk",
153        "* pack" : "add",
154        "Gtk.Align halign" : "Gtk.Align.START",
155        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
156        "int margin_top" : 12,
157        "label" : "Type or Return Type",
158        "x_options" : 4,
159        "xtype" : "Label"
160       },
161       {
162        "$ visible" : true,
163        "$ xns" : "Gtk",
164        "* pack" : "add",
165        "id" : "ktype",
166        "xtype" : "Entry"
167       },
168       {
169        "$ visible" : true,
170        "$ xns" : "Gtk",
171        "* pack" : "add",
172        "Gtk.Align halign" : "Gtk.Align.START",
173        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
174        "int margin_top" : 12,
175        "label" : "Name",
176        "tooltip_text" : "center, north, south, east, west",
177        "x_options" : 4,
178        "xtype" : "Label"
179       },
180       {
181        "$ visible" : true,
182        "$ xns" : "Gtk",
183        "* pack" : "add",
184        "id" : "kname",
185        "listeners" : {
186         "focus_out_event" : [
187          "()=>{",
188          "\t_this.error.setError(\"\");",
189          "\tvar val = this.el.get_text().strip(); ",
190          "\tif (val.length < 1) {",
191          "\t\t_this.error.setError(\"Name can not be empty\");",
192          "\t}",
193          "\treturn true;",
194          "}",
195          "",
196          ""
197         ],
198         "key_release_event" : [
199          "()=>{",
200          "\t_this.error.setError(\"\");",
201          "\tvar val = this.el.get_text().strip(); ",
202          "\tif (val.length < 1) {",
203          "\t\t_this.error.setError(\"Name can not be empty\");",
204          "\t}",
205          "\treturn true;",
206          "}",
207          "",
208          ""
209         ]
210        },
211        "xtype" : "Entry"
212       },
213       {
214        "$ visible" : true,
215        "$ xns" : "Gtk",
216        "* pack" : "add",
217        "Gtk.Align halign" : "Gtk.Align.START",
218        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
219        "bool use_markup" : true,
220        "id" : "error",
221        "int margin_top" : 0,
222        "label" : "<span color=\"red\">Error Message</span>",
223        "tooltip_text" : "center, north, south, east, west",
224        "x_options" : 4,
225        "xtype" : "Label",
226        "| void setError" : [
227         "(string err)   {",
228         "\tif (err == \"\") {",
229         "\t\tthis.el.hide();",
230         "\t} else {",
231         "\t\tthis.el.show();",
232         "\t\t",
233         "\t\tthis.el.label = \"<span color=\\\"red\\\">\" + err + \"</span>\";",
234         "\t}",
235         "}",
236         ""
237        ]
238       },
239       {
240        "$ xns" : "Gtk",
241        "* pack" : "add",
242        "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
243        "id" : "buttonbar",
244        "int margin_top" : 20,
245        "items" : [
246         {
247          "$ xns" : "Gtk",
248          "* pack" : "add",
249          "bool always_show_image" : true,
250          "bool hexpand" : true,
251          "items" : [
252           {
253            "$ xns" : "Gtk",
254            "* pack" : "composite_name",
255            "* prop" : "image",
256            "string icon_name" : "window-close",
257            "xtype" : "Image"
258           }
259          ],
260          "listeners" : {
261           "pressed" : [
262            "() => { ",
263            "",
264            "\t_this.prop = null;",
265            "\t_this.is_new = false;",
266            "\t_this.kname.el.set_text(\"Cancel\");",
267            "\t_this.el.hide();",
268            "",
269            "}"
270           ]
271          },
272          "string label" : "Cancel",
273          "xtype" : "Button"
274         },
275         {
276          "$ xns" : "Gtk",
277          "* pack" : "add",
278          "bool always_show_image" : true,
279          "bool hexpand" : true,
280          "items" : [
281           {
282            "$ xns" : "Gtk",
283            "* prop" : "image",
284            "string icon_name" : "list-add",
285            "xtype" : "Image"
286           }
287          ],
288          "listeners" : {
289           "pressed" : [
290            "() => {",
291            "\t// check if text is not empty..",
292            "\tif ( _this.kname.el.get_text().strip().length < 1) {",
293            "\t\t// error should already be showing?",
294            "\t\treturn;",
295            "\t}",
296            "\t_this.updateProp();",
297            "\t",
298            "\t// since we can't add listeners?!?!?",
299            "\t// only check props.",
300            "\t// check if property already exists in node.\t",
301            "\tvar prop = _this.prop;",
302            "\tif (_this.node.props.has_key(prop.to_index_key())) {",
303            "\t\t_this.error.setError(\"Property already exists\");",
304            "\t\treturn;\t",
305            "\t}",
306            "\t",
307            "\t",
308            "\t ",
309            "\t_this.is_new = false;\t",
310            "\t  ",
311            "\t// hide self",
312            "\t_this.prop = null; // skip checks..",
313            "\t_this.el.hide();",
314            "",
315            "// add it, ",
316            "\t// trigger editing of property.",
317            "\t// allow hide to work?",
318            "\twhile (Gtk.events_pending()) {",
319            "\t\tGtk.main_iteration();",
320            "\t}",
321            "\t",
322            "\t_this.mainwindow.windowstate.left_props.addProp(prop);\t\t",
323            "\t",
324            "",
325            "}"
326           ]
327          },
328          "string label" : "Add Property",
329          "xtype" : "Button"
330         }
331        ],
332        "xtype" : "Box"
333       }
334      ],
335      "xtype" : "Box"
336     }
337    ],
338    "listeners" : {
339     "closed" : [
340      "() => {",
341      "",
342      " \tGLib.debug(\"popover closed\");",
343      "\tif (_this.is_new) {",
344      "\t\t// dont allow hiding if we are creating a new one.",
345      "\t\t// on.hide will reshow it.",
346      "\t\treturn;",
347      "\t}",
348      "\tif (_this.prop == null) {",
349      "\t\t// hide and dont update.",
350      "\t\treturn;",
351      "\t}",
352      "\tif (this.kname.el.get_text().strip().length < 1) {",
353      "\t\treturn;",
354      "\t}",
355      "\t",
356      "\tvar oldkey = this.prop.to_index_key();\t",
357      "         \t",
358      "         ",
359      "  \tthis.updateProp();",
360      "         \t",
361      " \tvar newkey = this.prop.to_index_key();\t",
362      " \tif (oldkey != newkey) {",
363      " \t",
364      " \t\tif (_this.prop.ptype == JsRender.NodePropType.LISTENER) {",
365      " \t\t\tthis.node.listeners.unset(oldkey);",
366      " \t\t\tthis.node.listeners.set(newkey, _this.prop);",
367      " \t\t} else {",
368      " \t\t\tthis.node.props.unset(oldkey);",
369      " \t\t\tthis.node.props.set(newkey, _this.prop);",
370      " \t\t}",
371      " \t",
372      " \t}",
373      "\t_this.mainwindow.windowstate.left_props.reload();",
374      "",
375      "",
376      "  ",
377      "} "
378     ],
379     "hide" : [
380      "() => {",
381      "  \tGLib.debug(\"popover hidden\");",
382      "\tif (_this.is_new || this.kname.el.get_text().strip().length < 1) {",
383      "\t\t// dont allow hiding if we are creating a new one.",
384      "\t\tGLib.debug(\"prevent hiding as its new or text is empty\"); ",
385      "\t\tthis.el.show_all();",
386      "\t\treturn;",
387      "",
388      "\t}",
389      "\t",
390      "}"
391     ]
392    },
393    "string key_type" : "",
394    "string old_keyname" : "",
395    "uint border_width" : 0,
396    "xtype" : "Popover",
397    "| void show" : [
398     "(",
399     "\tGtk.Widget btn, ",
400     "\tJsRender.Node node, ",
401     "\tJsRender.NodeProp prop, ",
402     "\tint y,",
403     "\tbool is_new = false",
404     "\t ) ",
405     "{",
406     "\t",
407     "   ",
408     "\tthis.is_new = is_new; ",
409     "\tvar pref = is_new ? \"Add \" : \"Modify \";",
410     "\tif (prop.ptype == JsRender.NodePropType.LISTENER) {",
411     "\t\tthis.header.el.title = pref + \"Event Listener\"; // cant really happen yet?",
412     "\t} else {",
413     "\t\tthis.header.el.title = pref + \"Property\";",
414     "\t}",
415     "\tthis.prop = prop;",
416     "\tthis.node = node;",
417     "\t",
418     "\t_this.kname.el.set_text(prop.name);",
419     "\t_this.ktype.el.set_text(prop.rtype);",
420     "\t",
421     "\t_this.dbmodel.loadData(prop );",
422     "\t// does node have this property...",
423     "",
424     "",
425     "\t_this.node = node;",
426     "\t//console.log('show all');",
427     "\tthis.el.set_modal(true);",
428     "\tthis.el.set_relative_to(btn);",
429     "\tif (y > -1) {",
430     "\t\t",
431     "\t",
432     "\t\tvar  r = Gdk.Rectangle() {",
433     "\t\t\tx = btn.get_allocated_width(), // align left...",
434     "\t\t\ty = y,",
435     "\t\t\twidth = 1,",
436     "\t\t\theight = 1",
437     "\t\t};",
438     "\t\tthis.el.set_pointing_to( r);",
439     "\t}",
440     "\t",
441     "\t",
442     "",
443     "\t//this.el.set_position(Gtk.PositionType.TOP);",
444     "",
445     "\t// window + header?",
446     "\t print(\"SHOWALL - POPIP\\n\");",
447     "\tthis.el.show_all();",
448     "\tthis.kname.el.grab_focus();",
449     "\tthis.buttonbar.el.hide();",
450     "\tif (this.is_new) {",
451     "\t\tthis.buttonbar.el.show();",
452     "\t}",
453     "\t this.error.setError(\"\");",
454     "",
455     "\t//this.success = c.success;",
456     " ",
457     "}"
458    ],
459    "| void updateProp" : [
460     "() {",
461     " ",
462     "\tGtk.TreeIter citer;",
463     "\tGLib.Value gval;",
464     "\tthis.kflag.el.get_active_iter(out citer);",
465     "\tthis.dbmodel.el.get_value(citer, 0, out  gval);",
466     "",
467     "",
468     "\t_this.prop.name = this.kname.el.get_text().strip(); ",
469     "\t_this.prop.rtype = this.ktype.el.get_text().strip(); ",
470     "\t_this.prop.ptype =  (JsRender.NodePropType) gval;",
471     "",
472     "}",
473     ""
474    ]
475   }
476  ],
477  "modOrder" : "",
478  "name" : "PopoverProperty",
479  "parent" : "",
480  "path" : "/home/alan/gitlive/roobuilder/src/Builder4/PopoverProperty.bjs",
481  "permname" : "",
482  "title" : ""
483 }