diff --git a/application/controllers/Lookup.php b/application/controllers/Lookup.php index 4b7228922..73a72871a 100644 --- a/application/controllers/Lookup.php +++ b/application/controllers/Lookup.php @@ -55,7 +55,7 @@ public function search() { public function scp() { if($_POST['callsign']) { - $uppercase_callsign = strtoupper($_POST['callsign']); + $uppercase_callsign = str_replace('Ø', '0', strtoupper($_POST['callsign'])); } // SCP results from logbook @@ -106,7 +106,7 @@ public function scp() { foreach ($arCalls as $strCall) { - echo " " . $strCall . " "; + echo $strCall . " "; } } diff --git a/assets/js/sections/contesting.js b/assets/js/sections/contesting.js index 1ce2b5eb1..cad4f73e5 100644 --- a/assets/js/sections/contesting.js +++ b/assets/js/sections/contesting.js @@ -207,31 +207,23 @@ $('#start_date').change(function () { }); // On Key up check and suggest callsigns -$("#callsign").keyup(function () { - var call = $(this).val(); - if (call.length >= 3) { - - $.ajax({ - url: 'lookup/scp', - method: 'POST', - data: { - callsign: $(this).val().toUpperCase() - }, - success: function (result) { - $('.callsign-suggestions').text(result); - highlight(call.toUpperCase()); - } - }); - // moved to blur - // checkIfWorkedBefore(); - var qTable = $('.qsotable').DataTable(); - qTable.search(call).draw(); - } - else if (call.length <= 2) { - $('.callsign-suggestions').text(""); - } +$("#callsign").keyup(scp_keyup({ + selector: $("#callsign"), + showSuggestions: function (call, text) { + $('.callsign-suggestions').text(text); + highlight(call); + } +})); + +$("#callsign").keyup(function() { + const call = $(this).val().toUpperCase(); + if (call.length >= 3) { + var qTable = $('.qsotable').DataTable(); + qTable.search(call).draw(); + } }); + function checkIfWorkedBefore() { var call = $("#callsign").val(); if (call.length >= 3) { @@ -666,3 +658,62 @@ function getUTCDateStamp(el) { var utc = localTime + (now.getTimezoneOffset() * 60000); $(el).attr('value', ("0" + now.getUTCDate()).slice(-2) + '-' + ("0" + (now.getUTCMonth() + 1)).slice(-2) + '-' + now.getUTCFullYear()); } + +function scp_keyup(options) { + // options must have two keys: + // * selector - element, with .val() which gives the entered callsign + // * showSuggestions - function(call, text), where the text is + // the list of callsign-suggestions + const scp = { + request: "", + data: [] + }; + const callFromInput = (el) => el.val().toUpperCase().replace('0','Ø'); + const checkCacheValid = (call) => (scp.request != "" && call.includes(scp.request)); + const filterCallsignList = function (call) { + return scp.data?.filter((el) => (el.includes(call) == true)).join(' ') || ''; + }; + const updateSuggestions = function (call) { + const suggestions = filterCallsignList(call); + options.showSuggestions(call, suggestions); + } + + const keyup = function(){ + const call = callFromInput(options.selector); + + if (call.length < 3) { + options.showSuggestions("", ""); + return; + } + + if ( checkCacheValid(call) ) { + updateSuggestions(call); + return; + } + + // Cache invalid, so update it and reset suggestions + options.showSuggestions(""); + + scp.request = call; + scp.data = []; + $.ajax({ + url: 'lookup/scp', + method: 'POST', + data: { + callsign: call + }, + success: function (result) { + const call_now = callFromInput(options.selector); + if (checkCacheValid(call_now)) { + scp.data = result.split(" "); + + updateSuggestions(call_now); + } + } + }); + + }; + + return keyup; +} + diff --git a/assets/js/sections/qso.js b/assets/js/sections/qso.js index 2f7c936bb..8e02678ff 100644 --- a/assets/js/sections/qso.js +++ b/assets/js/sections/qso.js @@ -1017,22 +1017,13 @@ $("#callsign").on("keypress", function(e) { }); // On Key up check and suggest callsigns -$("#callsign").keyup(function() { - if ($(this).val().length >= 3) { - $('.callsign-suggest').show(); - $callsign = $(this).val().replace('Ø', '0'); - $.ajax({ - url: 'lookup/scp', - method: 'POST', - data: { - callsign: $callsign.toUpperCase() - }, - success: function(result) { - $('.callsign-suggestions').text(result); - } - }); - } - }); +$("#callsign").keyup( scp_keyup({ + selector: $(this), + showSuggestions: function (call, text) { + $('.callsign-suggestions').text(text); + $('.callsign-suggest').show(); + } +})); //Reset QSO form Fields function function resetDefaultQSOFields() { @@ -1094,3 +1085,62 @@ function testTimeOffConsistency() { } return true; } + +function scp_keyup(options) { + // options must have two keys: + // * selector - element, with .val() which gives the entered callsign + // * showSuggestions - function(call, text), where the text is + // the list of callsign-suggestions + const scp = { + request: "", + data: [] + }; + const callFromInput = (el) => el.val().toUpperCase().replace('0','Ø'); + const checkCacheValid = (call) => (scp.request != "" && call.includes(scp.request)); + const filterCallsignList = function (call) { + return scp.data?.filter((el) => (el.includes(call) == true)).join(' ') || ''; + }; + const updateSuggestions = function (call) { + const suggestions = filterCallsignList(call); + options.showSuggestions(call, suggestions); + } + + const keyup = function(){ + const call = callFromInput(options.selector); + + if (call.length < 3) { + options.showSuggestions("", ""); + return; + } + + if ( checkCacheValid(call) ) { + updateSuggestions(call); + return; + } + + // Cache invalid, so update it and reset suggestions + options.showSuggestions(""); + + scp.request = call; + scp.data = []; + $.ajax({ + url: 'lookup/scp', + method: 'POST', + data: { + callsign: call + }, + success: function (result) { + const call_now = callFromInput(options.selector); + if (checkCacheValid(call_now)) { + scp.data = result.split(" "); + + updateSuggestions(call_now); + } + } + }); + + }; + + return keyup; +} +