advanced syntax: VAR{}

This commit is contained in:
jean-emmanuel 2021-05-01 17:56:47 +02:00
parent daa9038dfe
commit 99ae34103c
4 changed files with 83 additions and 1 deletions

View file

@ -107,6 +107,7 @@ function updateWidget(widget, options={}) {
parentNode: widget.parentNode,
reCreateOptions: options.reCreateOptions,
locals: widget.parsersLocalScope,
variables: widget.variables,
hash: widget.hash,
children: children,
index: index

View file

@ -37,7 +37,8 @@ var Parser = class Parser {
children,
index,
hash,
locals
locals,
variables
} = options
var props = data
@ -81,6 +82,7 @@ var Parser = class Parser {
children,
hash,
locals,
variables
})
widgetManager.addWidget(widget)

View file

@ -193,6 +193,7 @@ class Widget extends EventEmitter {
this.parsers = {}
this.parsersLocalScope = options.locals || {}
this.variables = options.variables || {}
this.createPropsCache()
@ -621,6 +622,38 @@ class Widget extends EventEmitter {
})
propValue = balancedReplace('VAR{', '}', propValue, (args)=>{
if (args === '') return 'undefined'
let [name, value] = args.split(',')
.map(x=>x.trim())
.map(x=>x.replace(/VAR_[0-9]+/g, (m)=>{
return typeof variables[m] === 'string' ? variables[m] : JSON.stringify(variables[m])
}))
if (!this.variables[name]) {
this.variables[name] = {value: value, propNames: [propName]}
} else if (!this.variables[name].propNames.includes(propName)) {
this.variables[name].propNames.push(propName)
}
var r = this.variables[name].value
var varname = 'VAR_' + varnumber
varnumber--
variables[varname] = r
jsScope[varname] = r
return varname
})
propValue = balancedReplace('OSC{', '}', propValue, (args)=>{
if (args === '') return 'undefined'

View file

@ -91,6 +91,40 @@ class ScriptVm extends Vm {
}
this.sandbox.contentWindow.setVar = (id, name, value)=>{
var options = deepCopy(this.getValueOptions())
var widgets
if (id.includes('*')) {
var widget = this.getWidget()
if (widget.builtIn) widget = widget.parent
widgets = this.resolveId(
Object.keys(widgetManager.idRoute).filter(key => key.match(new RegExp('^' + id.replace(/\*/g, '.*') + '$')))
).filter(w => w !== widget)
} else {
widgets = this.resolveId(id).slice(0, 1)
}
for (var i = widgets.length - 1; i >= 0; i--) {
if (widgets[i].variables[name]) {
widgets[i].variables[name].value = value
widgets[i].updateProps(widgets[i].variables[name].propNames)
} else {
widgets[i].variables[name] = {value: value, propNames: []}
}
}
}
this.sandbox.contentWindow.send = (target, address, ...args)=>{
var options = this.getValueOptions()
@ -135,6 +169,18 @@ class ScriptVm extends Vm {
}
this.sandbox.contentWindow.getVar = (id, name)=>{
var widgets = this.resolveId(id)
for (var i = widgets.length - 1; i >= 0; i--) {
if (widgets[i].variables[name]) return widgets[i].variables[name].value
}
}
this.sandbox.contentWindow.getProp = (id, prop)=>{
var widgets = this.resolveId(id)