Create new devices

This commit is contained in:
Magnus Åhall 2026-02-26 22:59:42 +01:00
parent 06054c8d39
commit 7213ee7a28

View file

@ -100,7 +100,7 @@ export class Application {
// deleteDevice removes it from the list and resets app state if it was connected. // deleteDevice removes it from the list and resets app state if it was connected.
deleteDevice(devName) {// {{{ deleteDevice(devName) {// {{{
this.devices.delete(devName) this.devices.delete(devName)
if (this.currentDevice.name() == devName) { if (this.currentDevice?.name() == devName) {
this.resetDeviceState() this.resetDeviceState()
this.settings.set('last_device', '') this.settings.set('last_device', '')
} }
@ -432,6 +432,7 @@ class DeviceSelectWidget {
this.currentlySelected = null this.currentlySelected = null
_mbus.subscribe('device_deleted', event => this.deleteDevice(event.detail.devName)) _mbus.subscribe('device_deleted', event => this.deleteDevice(event.detail.devName))
_mbus.subscribe('device_updated', event => this.updateDevice(event.detail.device))
}// }}} }// }}}
setDevices(devices) {// {{{ setDevices(devices) {// {{{
this.devices = devices this.devices = devices
@ -460,7 +461,9 @@ class DeviceSelectWidget {
emptyOption.dataset.devicename = "-" emptyOption.dataset.devicename = "-"
this.elDeviceSelect.appendChild(emptyOption) this.elDeviceSelect.appendChild(emptyOption)
this.devices.forEach(dev => { const devNames = Array.from(this.devices.keys()).sort()
devNames.forEach(devName => {
const dev = this.devices.get(devName)
const option = document.createElement('option') const option = document.createElement('option')
option.innerText = dev.name() option.innerText = dev.name()
option.dataset.devicename = dev.name() option.dataset.devicename = dev.name()
@ -483,6 +486,11 @@ class DeviceSelectWidget {
this.devices.delete(devName) this.devices.delete(devName)
this.restockDeviceSelect() this.restockDeviceSelect()
}// }}} }// }}}
updateDevice(dev) {// {{{
console.log(dev)
this.devices.set(dev.name(), dev)
this.restockDeviceSelect()
}// }}}
} }
class Folder { class Folder {
@ -1030,7 +1038,7 @@ class DeviceDialog {
<div class="devices"> <div class="devices">
<div class="header"> <div class="header">
<div>Devices</div> <div>Devices</div>
<img src="/images/${_VERSION}/icon_create.svg"> <img class="create" src="/images/${_VERSION}/icon_create.svg">
<input type="text" class="filter" placeholder="Filter"> <input type="text" class="filter" placeholder="Filter">
</div> </div>
@ -1055,7 +1063,7 @@ class DeviceDialog {
<div class="actions"> <div class="actions">
<button class="delete">Delete</button> <button class="delete">Delete</button>
<button class="update">Update</button> <button class="update">Save</button>
</div> </div>
</div> </div>
` `
@ -1069,6 +1077,7 @@ class DeviceDialog {
this.elUsername = this.dlg.querySelector('.fields .username') this.elUsername = this.dlg.querySelector('.fields .username')
this.elPassword = this.dlg.querySelector('.fields .password') this.elPassword = this.dlg.querySelector('.fields .password')
this.dlg.querySelector('.create').addEventListener('click', () => this.createDevice())
this.dlg.querySelector('.filter').addEventListener('input', () => this.filterDevices()) this.dlg.querySelector('.filter').addEventListener('input', () => this.filterDevices())
this.dlg.querySelector('.update').addEventListener('click', () => this.updateDevice()) this.dlg.querySelector('.update').addEventListener('click', () => this.updateDevice())
this.dlg.querySelector('.delete').addEventListener('click', () => this.deleteDevice()) this.dlg.querySelector('.delete').addEventListener('click', () => this.deleteDevice())
@ -1079,7 +1088,7 @@ class DeviceDialog {
document.body.appendChild(this.dlg) document.body.appendChild(this.dlg)
this.dlg.showModal() this.dlg.showModal()
}// }}} }// }}}
filterDevices() { filterDevices() {// {{{
const filter = this.dlg.querySelector('.filter').value.toLowerCase() const filter = this.dlg.querySelector('.filter').value.toLowerCase()
this.elDeviceList.querySelectorAll('.device').forEach(dev=>{ this.elDeviceList.querySelectorAll('.device').forEach(dev=>{
@ -1088,7 +1097,7 @@ class DeviceDialog {
else else
dev.classList.add('filtered') dev.classList.add('filtered')
}) })
} }// }}}
updateDeviceList() {// {{{ updateDeviceList() {// {{{
this.elDeviceList.replaceChildren() this.elDeviceList.replaceChildren()
@ -1101,6 +1110,7 @@ class DeviceDialog {
devices.forEach(dev => { devices.forEach(dev => {
const devEl = document.createElement('div') const devEl = document.createElement('div')
devEl.classList.add('device') devEl.classList.add('device')
devEl.dataset.name = dev.name()
devEl.innerText = dev.name() devEl.innerText = dev.name()
devEl.addEventListener('click', () => this.editDevice(dev, devEl)) devEl.addEventListener('click', () => this.editDevice(dev, devEl))
this.elDeviceList.appendChild(devEl) this.elDeviceList.appendChild(devEl)
@ -1119,6 +1129,26 @@ class DeviceDialog {
this.elUsername.value = dev.username() this.elUsername.value = dev.username()
this.elPassword.value = dev.password() this.elPassword.value = dev.password()
}// }}} }// }}}
async createDevice() {// {{{
let name = prompt('Name of new device')
if (name === null || name.trim() === '')
return
name = name.trim().toLowerCase()
// Make sure it doesn't already exist.
if (this.devices.has(name)) {
alert('The device already exist.')
return
}
const dev = new Device({Name: name, Port: 443}, true)
this.devices.set(name, dev)
this.updateDeviceList()
const devEl = this.elDeviceList.querySelector(`[data-name="${dev.name()}"]`)
this.editDevice(dev, devEl)
}// }}}
async updateDevice() {// {{{ async updateDevice() {// {{{
const req = { const req = {
CurrentName: this.device.name(), CurrentName: this.device.name(),