script: fixes and timeouts/intervals implementation

This commit is contained in:
bordun 2020-03-20 17:43:53 +01:00
parent 7bf8193b61
commit eacd44cc8c
4 changed files with 88 additions and 23 deletions

View file

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

View file

@ -178,7 +178,7 @@ class Widget extends EventEmitter {
this.visible = true
this.parsers = {}
this.parsersLocalScope = {}
this.parsersLocalScope = options.locals || {}
this.createPropsCache()

View file

@ -12,7 +12,7 @@ class ScriptVm extends Vm {
super()
this.valueOptions = []
this.sendWidget = []
this.widget = []
}
@ -29,16 +29,16 @@ class ScriptVm extends Vm {
}
setSendWidget(widget) {
setWidget(widget) {
if (!widget) this.sendWidget.pop()
else this.sendWidget.push(widget)
if (!widget) this.widget.pop()
else this.widget.push(widget)
}
getSendWidget() {
getWidget() {
return this.sendWidget[this.sendWidget.length - 1]
return this.widget[this.widget.length - 1]
}
@ -76,7 +76,7 @@ class ScriptVm extends Vm {
if (target) overrides.target = Array.isArray(target) ? target : [target]
this.getSendWidget().sendValue(overrides)
this.getWidget().sendValue(overrides)
}
@ -162,8 +162,53 @@ class ScriptVm extends Vm {
}
this.sandbox.contentWindow.setTimeout = (id, callback, timeout)=>{
for (var imports of ['set', 'get', 'getProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage']) {
var widget = this.getWidget()
if (widget.timeouts[id] !== undefined) {
clearTimeout(widget.timeouts[id])
delete widget.timeouts[id]
}
widget.timeouts[id] = setTimeout(()=>{
callback()
}, timeout)
}
this.sandbox.contentWindow.clearTimeout = (id)=>{
var widget = this.getWidget()
clearTimeout(widget.timeouts[id])
delete widget.timeouts[id]
}
this.sandbox.contentWindow.setInterval = (id, callback, timeout)=>{
var widget = this.getWidget()
if (widget.intervals[id] !== undefined) clearTimeout(widget.intervals[id])
delete widget.intervals[id]
widget.intervals[id] = setInterval(()=>{
callback()
}, timeout)
}
this.sandbox.contentWindow.clearInterval = (id)=>{
var widget = this.getWidget()
clearInterval(widget.intervals[id])
delete widget.intervals[id]
}
for (var imports of ['set', 'get', 'getProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage',
'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout']) {
this.sanitize(this.sandbox.contentWindow[imports])
}

View file

@ -61,6 +61,8 @@ class Script extends Widget {
this.scriptLock = false
this.timeouts = {}
this.intervals = {}
if (this.getProp('event') === 'value') {
@ -75,19 +77,26 @@ class Script extends Widget {
keyboardJS.withContext('global', ()=>{
keyboardJS.bind(this.getProp('keyBinding'), this.getProp('keyType') !== 'keyup' ? this.keyCb : undefined, this.getProp('keyType') !== 'keydown' ? this.keyCb : undefined)
keyboardJS.bind(this.getProp('keyBinding'), this.keyCb, this.keyCb)
})
this.script = scriptVm.compile(this.getProp('script'), {
type: '',
key: '',
code: 0,
ctrl: false,
shift: false,
alt: false,
meta: false,
})
try {
this.script = scriptVm.compile(this.getProp('script'), {
type: '',
key: '',
code: 0,
ctrl: false,
shift: false,
alt: false,
meta: false,
})
} catch(err) {
var stackline = err.stack ? (err.stack.match(/>:([0-9]+):[0-9]+/) || '') : '',
line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : ''
console.log((this.getProp('id') || this.props.id) + '.script error:\n' + err + line)
this.script = ()=>{}
}
}
@ -97,7 +106,7 @@ class Script extends Widget {
if (this.scriptLock) return
scriptVm.setSendWidget(this)
scriptVm.setWidget(this)
this.scriptLock = true
try {
@ -107,7 +116,7 @@ class Script extends Widget {
}
this.scriptLock = false
scriptVm.setSendWidget()
scriptVm.setWidget()
}
@ -131,9 +140,11 @@ class Script extends Widget {
keyboardCallback(e) {
if (e.target && (e.target.tagName === 'INPUT' || e.target.tabName === 'TEXTAREA' || e.target.tabName === 'SELECT')) return
if (e.target && (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT')) return
if (e && e.type === 'keydown' && !this.getProp('keyRepeat')) e.preventRepeat()
if (this.getProp('keyType') !== 'both' && e.type !== this.getProp('keyType')) return
if (e.type === 'keydown' && !this.getProp('keyRepeat')) e.preventRepeat()
e.preventDefault()
@ -163,6 +174,14 @@ class Script extends Widget {
}
for (let k in this.timeouts) {
clearTimeout(this.timeouts[k])
}
for (let k in this.intervals) {
clearInterval(this.intervals[k])
}
super.onRemove()
}