script: generic id support for "this" (instead of "self") and "parent"

This commit is contained in:
bordun 2020-04-17 14:12:51 +02:00
parent 212144e5fd
commit 9afb2c7b55
2 changed files with 24 additions and 14 deletions

View file

@ -39,7 +39,7 @@ Returns the value of the first matching widget.
----
#### `set`: `function(id, value)`
- `id`: widget `id` as a string. Can be `"self"` to target the host widget directly.
- `id`: widget `id` as a string. Can be `"self"` to target the host widget, or `"parent"` to target the parent widget.
- `value`: new value for the widget.
Sets the value of the first matching widget.
@ -57,8 +57,10 @@ If the event that triggered the script's execution was not initiated by a user i
----
#### `getProp`: `function(id, name)`
- `id`: widget `id` as a string. Can be `"self"` to target the host widget, or `"parent"` to target the parent widget.
- `name`: property name.
Returns the property called `"name"` of the first widget whose id is `"id"`.
Returns the property called `"name"` of the first matching widget.
----
@ -70,8 +72,9 @@ Returns the property called `"name"` of the first widget whose id is `"id"`.
----
#### `stateGet`: `function(id)`
- `id`: widget `id` as a string, or array of `id` strings. Can be `"self"` to target the host widget, or `"parent"` to target the parent widget.
Returns a state object (`id:value`) for the widget that match `id` and all its children.
Returns a state object (`id:value`) for matching widget and all their children.
----

View file

@ -42,6 +42,17 @@ class ScriptVm extends Vm {
}
resolveId(id) {
var widget = this.getWidget()
if (widget.builtIn) widget = widget.parent
if (id === 'this') return [widget]
else if (id === 'parent' && widget.parent !== widgetManager) return [widget.parent]
else return widgetManager.getWidgetById(id)
}
registerGlobals() {
super.registerGlobals()
@ -50,15 +61,10 @@ class ScriptVm extends Vm {
var options = this.getValueOptions()
if (id === options.id) options.sync = false // loop stop
// if (id === options.id) options.sync = false // loop stop
// if (this.getWidget() === options.widget) options.sync = false // loop stop
if (id === 'self') {
var widget = this.getWidget()
if (widget.builtIn) widget = widget.parent
return widget.setValue(value, options)
}
var widgets = widgetManager.getWidgetById(id)
var widgets = this.resolveId(id)
for (var i = widgets.length - 1; i >= 0; i--) {
@ -89,13 +95,14 @@ class ScriptVm extends Vm {
var widget = this.getWidget()
if (widget.builtIn) widget = widget.parent
widget.sendValue(overrides)
}
this.sandbox.contentWindow.get = (id)=>{
var widgets = widgetManager.getWidgetById(id)
var widgets = this.resolveId(id)
for (var i = widgets.length - 1; i >= 0; i--) {
@ -112,7 +119,7 @@ class ScriptVm extends Vm {
this.sandbox.contentWindow.getProp = (id, prop)=>{
var widgets = widgetManager.getWidgetById(id)
var widgets = this.resolveId(id)
for (var i = widgets.length - 1; i >= 0; i--) {
@ -148,7 +155,7 @@ class ScriptVm extends Vm {
filter = Array.isArray(filter) ? filter : [filter]
var containers = this.filter.map(x=>widgetManager.getWidgetById(x)).reduce((a,b)=>a.concat(b), [])
var containers = this.filter.map(x=>this.resolveId(id)).reduce((a,b)=>a.concat(b), [])
if (!containers.length) return
filter = (widget)=>{