diff --git a/locales/en/messages.json b/locales/en/messages.json index af923fdb30..a956439b53 100755 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -55,6 +55,9 @@ "portsSelectManual": { "message": "Manual Selection" }, + "portsSelectNone": { + "message": "No connection available" + }, "portsSelectVirtual": { "message": "Virtual Mode (Experimental)", "description": "Configure a Virtual Flight Controller without the need of a physical FC." diff --git a/src/js/port_handler.js b/src/js/port_handler.js index cb5c4159c4..da1cb87678 100644 --- a/src/js/port_handler.js +++ b/src/js/port_handler.js @@ -108,8 +108,8 @@ PortHandler.check_serial_devices = function () { // auto-select port (only during initialization) if (!self.initialPorts) { - self.currentPorts = self.updatePortSelect(self.currentPorts); - self.selectPort(self.currentPorts); + self.updatePortSelect(self.currentPorts); + self.selectActivePort(); self.initialPorts = self.currentPorts; GUI.updateManualPortVisibility(); } else { @@ -123,8 +123,8 @@ PortHandler.check_usb_devices = function (callback) { const self = this; chrome.usb.getDevices(usbDevices, function (result) { - const dfuActive = !!portSelect.value.startsWith('DFU'); + const dfuElement = self.portPickerElement.children("[value='DFU']"); if (result.length) { // Found device in DFU mode, add it to the list if (!dfuElement.length) { @@ -177,59 +177,6 @@ PortHandler.check_usb_devices = function (callback) { }); }; -PortHandler.clearOptions = function () { - const portSelect = document.querySelector('#port'); - let index = portSelect.length; - - while (index--) { - if (portSelect.options[index]) { - portSelect.remove(index); - } - } -}; - -PortHandler.addManualOption = function() { - const self = this; - - if (self.showManualMode) { - const portSelect = document.querySelector('#port'); - const manualOption = document.createElement('option'); - - manualOption.value = 'manual'; - manualOption.text = i18n.getMessage('portsSelectManual'); - - portSelect.add(manualOption); - } -}; - -PortHandler.addVirtualOption = function() { - const self = this; - - if (self.showVirtualMode) { - const portSelect = document.querySelector('#port'); - const virtualOption = document.createElement('option'); - - virtualOption.value = 'virtual'; - virtualOption.text = i18n.getMessage('portsSelectVirtual'); - - portSelect.add(virtualOption); - } -}; - -PortHandler.addNoPortsOption = function() { - const self = this; - - if (!self.showVirtualMode && !self.showManualMode) { - const portSelect = document.querySelector('#port'); - const noPortsOption = document.createElement('option'); - - noPortsOption.value = 'none'; - noPortsOption.text = 'No connections available'; - - portSelect.add(noPortsOption); - } -}; - PortHandler.removePort = function() { const self = this; const removePorts = self.array_difference(self.initialPorts, self.currentPorts); @@ -238,16 +185,9 @@ PortHandler.removePort = function() { console.log(`PortHandler - Removed: ${JSON.stringify(removePorts)}`); self.port_available = false; // disconnect "UI" - routine can't fire during atmega32u4 reboot procedure !!! - if (GUI.connected_to) { - for (let i = 0; i < removePorts.length; i++) { - if (removePorts[i].path === GUI.connected_to) { - const button = document.querySelector('div.connect_controls a.connect'); - button.click(); - - const buttonActive = document.querySelector('div.connect_controls a.connect.active'); - buttonActive.click(); - } - } + if (removePorts.some(port => port.path === GUI.connected_to)) { + $('div.connect_controls a.connect').click(); + $('div.connect_controls a.connect.active').click(); } // trigger callbacks (only after initialization) for (let i = (self.port_removed_callbacks.length - 1); i >= 0; i--) { @@ -265,8 +205,8 @@ PortHandler.removePort = function() { self.port_removed_callbacks.splice(index, 1); } } - for (let i = 0; i < removePorts.length; i++) { - self.initialPorts.splice(self.initialPorts.indexOf(removePorts[i]), 1); + for (const port of removePorts) { + self.initialPorts.splice(self.initialPorts.indexOf(port, 1)); } self.updatePortSelect(self.initialPorts); self.portPickerElement.trigger('change'); @@ -278,13 +218,13 @@ PortHandler.detectPort = function() { const newPorts = self.array_difference(self.currentPorts, self.initialPorts); if (newPorts.length) { - self.currentPorts = self.updatePortSelect(self.currentPorts); + self.updatePortSelect(self.currentPorts); console.log(`PortHandler - Found: ${JSON.stringify(newPorts)}`); if (newPorts.length === 1) { self.portPickerElement.val(newPorts[0].path); } else if (newPorts.length > 1) { - self.selectPort(self.currentPorts); + self.selectActivePort(); } self.port_available = true; @@ -296,12 +236,9 @@ PortHandler.detectPort = function() { self.portPickerElement.trigger('change'); // auto-connect if enabled - if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) { + if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to && GUI.active_tab !== 'firmware_flasher') { // start connect procedure. We need firmware flasher protection over here - if (GUI.active_tab !== 'firmware_flasher') { - const button = document.querySelector('div.connect_controls a.connect'); - button.click(); - } + $('div.connect_controls a.connect').click(); } // trigger callbacks for (let i = (self.port_detected_callbacks.length - 1); i >= 0; i--) { @@ -332,62 +269,64 @@ PortHandler.sortPorts = function(ports) { }); }; -PortHandler.updatePortSelect = function (ports) { - const portSelect = document.querySelector('#port'); +PortHandler.addNoPortSelection = function() { + if (!this.showVirtualMode && !this.showManualMode) { + this.portPickerElement.append($("