filo_caspar/app/main/add/module.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-06-26 18:35:12 +00:00
const m = require('mithril')
const createModule = require('../common/module')
2020-12-08 11:09:46 +00:00
const Module = require('../module')
2018-06-26 18:35:12 +00:00
const components = require('../common/components')
const socket = require('../../shared/socket')
2018-06-26 18:35:12 +00:00
const store = require('../store')
2020-12-08 11:09:46 +00:00
const Add = Module({
2018-06-26 18:35:12 +00:00
init: function() {
2020-12-08 11:09:46 +00:00
this.engines = []
this.graphic = { }
this._socketOn(() => this.socketOpen())
},
socketOpen: function() {
socket.on('engine.all', (res) => {
this.engines = res
m.redraw()
})
socket.on('graphic.created', (res) => {
if (res.name === this.graphic.name) {
m.route.set(`/graphic/${res.id}`)
2018-06-26 18:35:12 +00:00
}
})
2020-12-08 11:09:46 +00:00
socket.emit('engine.all', {})
2018-06-26 18:35:12 +00:00
},
updated: function(name, control) {
this.graphic[name] = control.target.value
},
create: function() {
if (!this.graphic.engine) {
this.graphic.engine = this.engines[0]
}
if (!this.graphic.name) {
this.error = 'Name cannot be empty'
return
}
socket.emit('graphic.create', this.graphic)
},
removing: function() {
store.unlisten('graphic.single')
},
2020-12-08 11:09:46 +00:00
view: function() {
return [
m('h4.header', 'Add graphic'),
components.error(this.error),
m('label', { for: 'create-name' }, 'Name'),
m('input#create-name[type=text]', {
oninput: (control) => this.updated('name', control),
}),
m('label', { for: 'create-engine' }, 'Engine'),
m('select', {
onchange: (control) => this.updated('engine', control),
}, this.engines.map(engine =>
m('option', { key: engine, value: engine }, engine)
)),
m('input[type=submit]', {
value: 'Create',
onclick: () => this.create(),
}),
]
},
2018-06-26 18:35:12 +00:00
})
module.exports = Add