From ee609ad28bdeeec89f27b3c3c7d40f2fdfc040b3 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 26 Sep 2021 14:46:56 -0400 Subject: [PATCH] Further develop behavour when typing in text input for new manufacturer or model. If it already exists, select it. --- web/skins/classic/views/js/monitor.js | 49 ++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/web/skins/classic/views/js/monitor.js b/web/skins/classic/views/js/monitor.js index 44bcddb5b..88fae0a67 100644 --- a/web/skins/classic/views/js/monitor.js +++ b/web/skins/classic/views/js/monitor.js @@ -346,7 +346,7 @@ function getLocation() { } function populate_models(ManufacturerId) { - let dropdown = $j('[name="newMonitor[ModelId]"]'); + const dropdown = $j('[name="newMonitor[ModelId]"]'); if (!dropdown.length) { console.log("No element found for ModelId"); return; @@ -356,18 +356,22 @@ function populate_models(ManufacturerId) { dropdown.append(''); dropdown.prop('selectedIndex', 0); - // Populate dropdown with list of provinces - $j.getJSON(thisUrl+'?request=models&ManufacturerId='+ManufacturerId, function (data) { + if (ManufacturerId) { + // Populate dropdown with list of provinces + $j.getJSON(thisUrl+'?request=models&ManufacturerId='+ManufacturerId, function(data) { if (data.result == 'Ok') { - $j.each(data.models, function (key, entry) { - dropdown.append($j('').attr('value', entry.Id).text(entry.Name)); - }); + $j.each(data.models, function(key, entry) { + dropdown.append($j('').attr('value', entry.Id).text(entry.Name)); + }); dropdown.chosen("destroy"); dropdown.chosen(); } else { alert(data.result); } - }); + }); + } + dropdown.chosen("destroy"); + dropdown.chosen(); } function ManufacturerId_onchange(ManufacturerId_select) { @@ -377,7 +381,7 @@ function ManufacturerId_onchange(ManufacturerId_select) { } else { ManufacturerId_select.form.elements['newMonitor[Manufacturer]'].style['display'] = 'inline'; // Set models dropdown to Unknown, text area visible - let ModelId_dropdown = $j('[name="newMonitor[ModelId]"]'); + const ModelId_dropdown = $j('[name="newMonitor[ModelId]"]'); ModelId_dropdown.empty(); ModelId_dropdown.append(''); ModelId_dropdown.prop('selectedIndex', 0); @@ -385,6 +389,31 @@ function ManufacturerId_onchange(ManufacturerId_select) { } } +function select_by_value_case_insensitive(dropdown, value) { + const test_value = value.toLowerCase(); + for (i=1; i < dropdown.options.length; i++) { + if (dropdown.options[i].text.toLowerCase() == test_value) { + dropdown.selectedIndex = i; + dropdown.options[i].selected = true; + $j(dropdown).chosen("destroy").chosen(); + return; + } + } + if (dropdown.selectedIndex != 0) { + dropdown.selectedIndex = 0; + $j(dropdown).chosen("destroy").chosen(); + } +} + +function Manufacturer_onchange(input) { + if (!input.value) { + return; + } + ManufacturerId_select = input.form.elements['newMonitor[ManufacturerId]']; + select_by_value_case_insensitive(ManufacturerId_select, input.value); + populate_models(ManufacturerId_select.value); +} + function ModelId_onchange(ModelId_select) { if (parseInt(ModelId_select.value)) { $j('[name="newMonitor[Model]"]').hide(); @@ -393,4 +422,8 @@ function ModelId_onchange(ModelId_select) { } } +function Model_onchange(input) { + select_by_value_case_insensitive(input.form.elements['newMonitor[ModelId]'], input.value); +} + window.addEventListener('DOMContentLoaded', initPage);