From 9afb2c7b5503136d1222cb1fa050f577dabca44c Mon Sep 17 00:00:00 2001 From: bordun Date: Fri, 17 Apr 2020 14:12:51 +0200 Subject: [PATCH] script: generic id support for "this" (instead of "self") and "parent" --- resources/docs/docs/widgets/scripting.md | 9 +++++--- src/client/widgets/scripts/script-vm.js | 29 +++++++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index a70532cc..dd88fb99 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -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. ---- diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index b39d5e03..357cd4d8 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -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)=>{