diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6d6840..d6af78ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ - custom module - expose `global` object that persists accross module reloads. +- scripting + - onCreate: expose `value` + ## 1.20.0 - bug fixes diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index c36c8224..0745414d 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -68,6 +68,7 @@ This syntax creates a custom variable in the widget that can be read and modifie This allows loading the content of an external file in a property. If used in a `JS{}` of `#{}` block, it will be seen as a string variable containing the raw content of the file. Changes made to the file be applied automatically. File paths are resolved in this order: + - in the session's directory - in theme's directory - in the server's `remote-root` directory if the option is set diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 29e10db0..b2d4f02e 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -21,6 +21,10 @@ This script is called when the widget is created. If the widget has children, it !!! warning "Modifying parent container" Modifying a non-dynamic property on a parent container from this script **will not work**. +The following variables are available in this context: + +- `value`: widget's value + ### `onValue` This script is called when the widget's value updates and when it receives a value. diff --git a/src/client/parser.js b/src/client/parser.js index 9f4abc8b..0f0247db 100644 --- a/src/client/parser.js +++ b/src/client/parser.js @@ -87,8 +87,6 @@ var Parser = class Parser { widgetManager.addWidget(widget) - widget.created(index) - // set widget's initial state var defaultValue = widget.getProp('default'), currentValue = widget.getProp('value') @@ -103,6 +101,8 @@ var Parser = class Parser { } + widget.created(index) + parentNode.appendChild(widget.container) widget.mounted = true diff --git a/src/client/ui/ui-code-editor.js b/src/client/ui/ui-code-editor.js index c2cce2a4..498cce06 100644 --- a/src/client/ui/ui-code-editor.js +++ b/src/client/ui/ui-code-editor.js @@ -167,6 +167,8 @@ class CodeEditor { let globals = {} if (this.name === 'onKeyboard') { globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true} + } else if (this.name === 'onCreate') { + globals = {value: true} } else if (this.name === 'onValue') { globals = {id: true, value: true, touch: true} } else if (this.name === 'onDraw') { diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 9bf02e19..6c51f389 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -232,7 +232,7 @@ class Widget extends EventEmitter { widget: this, property: 'onCreate', code: this.getProp('onCreate'), - context: {} + context: {value: 0} }) } @@ -337,7 +337,7 @@ class Widget extends EventEmitter { index: index }) - if (this.scripts.onCreate) this.scripts.onCreate.run({}, {sync: true, send: true, onCreate: true}) + if (this.scripts.onCreate) this.scripts.onCreate.run({value: this.value}, {sync: true, send: true, onCreate: true}) }