From 0e5edb76de2a6e5c39a9cfdf3453541911a956a7 Mon Sep 17 00:00:00 2001 From: bordun Date: Fri, 22 May 2020 15:33:36 +0200 Subject: [PATCH] script: add updateProp() to force prop check on a widget --- resources/docs/docs/widgets/scripting.md | 8 ++++++++ src/client/widgets/scripts/script-vm.js | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 53899106..d4f82c6b 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -64,6 +64,14 @@ Returns the property called `"name"` of the first matching widget. ---- +#### `updateProp`: `function(id, name)` +- `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. +- `name`: property name. + +Forcers a widget to check if one of its properties has changed and update itself if needed. + +---- + #### `httpGet`: `function(url, callback)` - `url`: url of the resource to fetch (local url only) diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index 575783b0..b2f0e9e4 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -132,6 +132,21 @@ class ScriptVm extends Vm { } + this.sandbox.contentWindow.updateProp = (id, prop)=>{ + + var widgets = this.resolveId(id), + widget = this.getWidget() + + if (widget.builtIn) widget = widget.parent + + for (var i = widgets.length - 1; i >= 0; i--) { + + widgets[i].updateProps(Array.isArray(prop) ? prop : [prop], widget) + + } + + } + this.sandbox.contentWindow.httpGet = (url, callback)=>{ var parser = urlParser(url), @@ -263,7 +278,7 @@ class ScriptVm extends Vm { } - for (var imports of ['set', 'get', 'getProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', + for (var imports of ['set', 'get', 'getProp', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout']) { this.sanitize(this.sandbox.contentWindow[imports]) }