scripting: onCreate: expose value and call onCreate after default value is applied

This commit is contained in:
jean-emmanuel 2022-11-29 08:57:38 +01:00
parent 6ef1d412f9
commit 5230eb2d4b
6 changed files with 14 additions and 4 deletions

View file

@ -14,6 +14,9 @@
- custom module
- expose `global` object that persists accross module reloads.
- scripting
- onCreate: expose `value`
## 1.20.0
- bug fixes

View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -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') {

View file

@ -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})
}