From 93be649ce2fe0dfdcb244108cec602f5907bfde1 Mon Sep 17 00:00:00 2001 From: sparks-alec Date: Wed, 28 Dec 2022 16:32:53 -0500 Subject: [PATCH] be smarter about changing the type of a device --- src/device.js | 9 +++++++++ src/view.js | 15 ++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/device.js b/src/device.js index caffbbc..db4e67c 100644 --- a/src/device.js +++ b/src/device.js @@ -232,9 +232,18 @@ module.exports.deleteActive = function deleteActive() { module.exports.changeActiveType = function changeActiveType(newType) { const device = VIEW.getActiveDevice(); device.type = newType; + device.fields = {}; + device.plugin = PLUGINS.all[newType]; + + if (PLUGINS.all[device.type].config.fields) { + PLUGINS.all[newType].config.fields.forEach((field) => { + device.fields[field.key] = field.value; + }); + } initDeviceConnection(device.id); VIEW.draw(device); + VIEW.updateFields(); // SAVESLOTS.saveAll(); }; diff --git a/src/view.js b/src/view.js index 8d5f499..59a680e 100644 --- a/src/view.js +++ b/src/view.js @@ -211,9 +211,17 @@ function switchDevice(id) { document.getElementById('device-settings-port').disabled = true; } + updateFields(); + + ipcRenderer.send('enableDeviceDropdown', ''); + ipcRenderer.send('setDevicePin', !DEVICE.all[id].pinIndex === false); +} +module.exports.switchDevice = switchDevice; + +function updateFields() { document.getElementById('device-settings-fields').innerHTML = ''; - if (activeDevice.plugin.config.fields) { + if (activeDevice && activeDevice.plugin.config.fields) { const fields = activeDevice.plugin.config.fields; fields.forEach((field) => { @@ -236,11 +244,8 @@ function switchDevice(id) { } }); } - - ipcRenderer.send('enableDeviceDropdown', ''); - ipcRenderer.send('setDevicePin', !DEVICE.all[id].pinIndex === false); } -module.exports.switchDevice = switchDevice; +module.exports.updateFields = updateFields; module.exports.getActiveDevice = function getActiveDevice() { return activeDevice;