smon/static/js/trigger_edit.mjs
2024-04-30 08:04:16 +02:00

64 lines
1.1 KiB
JavaScript

export class UI {
constructor() {
document.getElementById('button-run').
addEventListener('click', evt=>evt.preventDefault())
document.addEventListener('keydown', evt=>this.keyHandler(evt))
}
setTrigger(t) {
this.trigger = t
}
run() {
this.trigger.run()
}
keyHandler(evt) {
if (!(evt.altKey && evt.shiftKey))
return
evt.preventDefault()
evt.stopPropagation()
switch (evt.key) {
case 'T':
this.run()
break
case 'S':
document.getElementById('form-trigger').submit()
break
}
}
}
export class Trigger {
constructor(id, name) {
this.id = id
this.name = name
}
run() {
const result = document.getElementById('run-result')
const classes = result.classList
const expr = document.getElementById('expr').value
fetch(`/trigger/run/${this.id}`, {
method: 'POST',
cache: 'no-cache',
body: expr,
})
.then(data => data.json())
.then(json => {
if (!json.OK) {
classes.remove('ok')
classes.add('error')
result.innerText = json.Error
return
}
classes.remove('error')
classes.add('ok')
result.innerText = json.Output
})
.catch(err => alert(err))
}
}