From 15e2e105289d4a6dfea5fbf152f39c37a799e2b4 Mon Sep 17 00:00:00 2001 From: DJ3CE Date: Mon, 25 Mar 2024 00:40:27 +0100 Subject: [PATCH 1/3] Reduce calling scp, cache result --- application/controllers/Lookup.php | 2 +- assets/js/sections/contesting.js | 45 ++++++++++++++++++++++-------- assets/js/sections/qso.js | 42 ++++++++++++++++++++-------- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/application/controllers/Lookup.php b/application/controllers/Lookup.php index 4b7228922..70858790b 100644 --- a/application/controllers/Lookup.php +++ b/application/controllers/Lookup.php @@ -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..a51498d43 100644 --- a/assets/js/sections/contesting.js +++ b/assets/js/sections/contesting.js @@ -8,6 +8,12 @@ $(document).ready(async function () { setRst($("#mode").val()); }); +var scp_data = { + request: "", + status: -1 , // -1 - never req, 0 - req in progress, 1 - request done + data: [], +}; + // Resets the logging form and deletes session from database function reset_contest_session() { $('#name').val(""); @@ -208,20 +214,35 @@ $('#start_date').change(function () { // On Key up check and suggest callsigns $("#callsign").keyup(function () { - var call = $(this).val(); + var call = $(this).val().toUpperCase(); 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()); - } - }); + if (scp_data.status < 0 || scp_data.request != call.substr(0, scp_data.request.length)) { + scp_data.status = 0; + scp_data.request = call; + scp_data.data = []; + $.ajax({ + url: 'lookup/scp', + method: 'POST', + data: { + callsign: call + }, + success: function (result) { + scp_data.status = 1; + scp_data.data = result.split(" "); + + var call = $("#callsign").val().toUpperCase(); // Might have changed while running the query... + $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); + highlight(call); + } + }); + + } else { + // Filter the already obtained list: + $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); + highlight(call); + + } // moved to blur // checkIfWorkedBefore(); var qTable = $('.qsotable').DataTable(); diff --git a/assets/js/sections/qso.js b/assets/js/sections/qso.js index 2f7c936bb..9fffae0e3 100644 --- a/assets/js/sections/qso.js +++ b/assets/js/sections/qso.js @@ -1,4 +1,9 @@ var lastCallsignUpdated="" +var scp_data = { + request: "", + status: -1 , // -1 - never req, 0 - req in progress, 1 - request done + data: [], +}; $( document ).ready(function() { setTimeout(function() { @@ -1019,18 +1024,31 @@ $("#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 = $(this).val().replace('Ø', '0').toUpperCase(); + + if (scp_data.status < 0 || scp_data.request != $callsign.substr(0, scp_data.request.length)) { + scp_data.status = 0; + scp_data.request = $callsign; + scp_data.data = []; + $.ajax({ + url: 'lookup/scp', + method: 'POST', + data: { + callsign: $callsign + }, + success: function(result) { + scp_data.status = 1; + scp_data.data = result.split(" "); + + var call = $("#callsign").val().replace('Ø', '0').toUpperCase(); + $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); + $('.callsign-suggest').show(); + } + }); + } else { + $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith($callsign)).join(' ')); + $('.callsign-suggest').show(); + } } }); From aaf7f994aacc502529159d8846f204029d6dd4cd Mon Sep 17 00:00:00 2001 From: DJ3CE Date: Fri, 29 Mar 2024 16:13:10 +0100 Subject: [PATCH 2/3] Updates, refinements, etc - fix '0' - improvements in data handling - filter allows prefixes --- assets/js/sections/contesting.js | 30 ++++++++++++++++++------------ assets/js/sections/qso.js | 30 +++++++++++++++++++----------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/assets/js/sections/contesting.js b/assets/js/sections/contesting.js index a51498d43..298263990 100644 --- a/assets/js/sections/contesting.js +++ b/assets/js/sections/contesting.js @@ -10,7 +10,6 @@ $(document).ready(async function () { var scp_data = { request: "", - status: -1 , // -1 - never req, 0 - req in progress, 1 - request done data: [], }; @@ -214,11 +213,10 @@ $('#start_date').change(function () { // On Key up check and suggest callsigns $("#callsign").keyup(function () { - var call = $(this).val().toUpperCase(); + let call = $(this).val().toUpperCase(); if (call.length >= 3) { - if (scp_data.status < 0 || scp_data.request != call.substr(0, scp_data.request.length)) { - scp_data.status = 0; + if ( scp_data.request == "" || ! call.startsWith(scp_data.request) ) { scp_data.request = call; scp_data.data = []; $.ajax({ @@ -228,18 +226,21 @@ $("#callsign").keyup(function () { callsign: call }, success: function (result) { - scp_data.status = 1; - scp_data.data = result.split(" "); - - var call = $("#callsign").val().toUpperCase(); // Might have changed while running the query... - $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); - highlight(call); + var call_now = $("#callsign").val().toUpperCase(); // Might have changed while running the query... + if (call_now.startsWith(call)) { + scp_data.data = result.split(" "); + + call_now = call_now.replace('0','Ø'); + $('.callsign-suggestions').text(filterCallsignList(call_now, scp_data.data)); + highlight(call_now); + } } }); } else { // Filter the already obtained list: - $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); + call = call.replace('0','Ø'); + $('.callsign-suggestions').text(filterCallsignList(call, scp_data.data)); highlight(call); } @@ -248,7 +249,7 @@ $("#callsign").keyup(function () { var qTable = $('.qsotable').DataTable(); qTable.search(call).draw(); } - else if (call.length <= 2) { + else { $('.callsign-suggestions').text(""); } }); @@ -687,3 +688,8 @@ 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 filterCallsignList(call, list) { + let re = "(^|\/)" + call; + return list?.filter((el) => (el.search(re) !== -1)).join(' ') || ''; +} diff --git a/assets/js/sections/qso.js b/assets/js/sections/qso.js index 9fffae0e3..e9d427a9b 100644 --- a/assets/js/sections/qso.js +++ b/assets/js/sections/qso.js @@ -1024,29 +1024,32 @@ $("#callsign").on("keypress", function(e) { // On Key up check and suggest callsigns $("#callsign").keyup(function() { if ($(this).val().length >= 3) { - $callsign = $(this).val().replace('Ø', '0').toUpperCase(); + let call = $(this).val().toUpperCase(); - if (scp_data.status < 0 || scp_data.request != $callsign.substr(0, scp_data.request.length)) { - scp_data.status = 0; - scp_data.request = $callsign; + if ( scp_data.request == "" || ! call.startsWith(scp_data.request) ) { + scp_data.request = call; scp_data.data = []; $.ajax({ url: 'lookup/scp', method: 'POST', data: { - callsign: $callsign + callsign: call }, success: function(result) { - scp_data.status = 1; - scp_data.data = result.split(" "); + var call_now = $("#callsign").val().toUpperCase(); - var call = $("#callsign").val().replace('Ø', '0').toUpperCase(); - $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith(call)).join(' ')); - $('.callsign-suggest').show(); + if (call_now.startsWith(call)) { + scp_data.data = result.split(" "); + + call_now = call_now.replace('0','Ø'); + $('.callsign-suggestions').text(filterCallsignList(call_now, scp_data.data)); + $('.callsign-suggest').show(); + } } }); } else { - $('.callsign-suggestions').text(scp_data.data.filter((el) => el.startsWith($callsign)).join(' ')); + call = call.replace('0','Ø'); + $('.callsign-suggestions').text(filterCallsignList(call, scp_data.data)); $('.callsign-suggest').show(); } } @@ -1112,3 +1115,8 @@ function testTimeOffConsistency() { } return true; } + +function filterCallsignList(call, list) { + let re = "(^|\/)" + call; + return list?.filter((el) => (el.search(re) !== -1)).join(' ') || ''; +} From 181dcbe53c26c00453d007242d9c9d054e12548f Mon Sep 17 00:00:00 2001 From: DJ3CE Date: Thu, 4 Apr 2024 18:38:47 +0200 Subject: [PATCH 3/3] Fixes matching to 'anywhere', isolates and restructures code - server side matches anywhere, thus replace `startsWith` by `includes`, - callsigns *should* be with stroked-0, improving consistency here, - code restructuring (reduce nesting a lot), - isolating code, same codebase for qso.js and contesting.js, could be put in a 'lib'-like file --- application/controllers/Lookup.php | 2 +- assets/js/sections/contesting.js | 120 +++++++++++++++++------------ assets/js/sections/qso.js | 104 +++++++++++++++---------- 3 files changed, 137 insertions(+), 89 deletions(-) diff --git a/application/controllers/Lookup.php b/application/controllers/Lookup.php index 70858790b..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 diff --git a/assets/js/sections/contesting.js b/assets/js/sections/contesting.js index 298263990..cad4f73e5 100644 --- a/assets/js/sections/contesting.js +++ b/assets/js/sections/contesting.js @@ -8,11 +8,6 @@ $(document).ready(async function () { setRst($("#mode").val()); }); -var scp_data = { - request: "", - data: [], -}; - // Resets the logging form and deletes session from database function reset_contest_session() { $('#name').val(""); @@ -212,48 +207,23 @@ $('#start_date').change(function () { }); // On Key up check and suggest callsigns -$("#callsign").keyup(function () { - let call = $(this).val().toUpperCase(); - if (call.length >= 3) { - - if ( scp_data.request == "" || ! call.startsWith(scp_data.request) ) { - scp_data.request = call; - scp_data.data = []; - $.ajax({ - url: 'lookup/scp', - method: 'POST', - data: { - callsign: call - }, - success: function (result) { - var call_now = $("#callsign").val().toUpperCase(); // Might have changed while running the query... - if (call_now.startsWith(call)) { - scp_data.data = result.split(" "); - - call_now = call_now.replace('0','Ø'); - $('.callsign-suggestions').text(filterCallsignList(call_now, scp_data.data)); - highlight(call_now); - } - } - }); - - } else { - // Filter the already obtained list: - call = call.replace('0','Ø'); - $('.callsign-suggestions').text(filterCallsignList(call, scp_data.data)); - highlight(call); - - } - // moved to blur - // checkIfWorkedBefore(); - var qTable = $('.qsotable').DataTable(); - qTable.search(call).draw(); - } - else { - $('.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) { @@ -689,7 +659,61 @@ function getUTCDateStamp(el) { $(el).attr('value', ("0" + now.getUTCDate()).slice(-2) + '-' + ("0" + (now.getUTCMonth() + 1)).slice(-2) + '-' + now.getUTCFullYear()); } -function filterCallsignList(call, list) { - let re = "(^|\/)" + call; - return list?.filter((el) => (el.search(re) !== -1)).join(' ') || ''; +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 e9d427a9b..8e02678ff 100644 --- a/assets/js/sections/qso.js +++ b/assets/js/sections/qso.js @@ -1,9 +1,4 @@ var lastCallsignUpdated="" -var scp_data = { - request: "", - status: -1 , // -1 - never req, 0 - req in progress, 1 - request done - data: [], -}; $( document ).ready(function() { setTimeout(function() { @@ -1022,38 +1017,13 @@ $("#callsign").on("keypress", function(e) { }); // On Key up check and suggest callsigns -$("#callsign").keyup(function() { - if ($(this).val().length >= 3) { - let call = $(this).val().toUpperCase(); - - if ( scp_data.request == "" || ! call.startsWith(scp_data.request) ) { - scp_data.request = call; - scp_data.data = []; - $.ajax({ - url: 'lookup/scp', - method: 'POST', - data: { - callsign: call - }, - success: function(result) { - var call_now = $("#callsign").val().toUpperCase(); - - if (call_now.startsWith(call)) { - scp_data.data = result.split(" "); - - call_now = call_now.replace('0','Ø'); - $('.callsign-suggestions').text(filterCallsignList(call_now, scp_data.data)); - $('.callsign-suggest').show(); - } - } - }); - } else { - call = call.replace('0','Ø'); - $('.callsign-suggestions').text(filterCallsignList(call, scp_data.data)); - $('.callsign-suggest').show(); - } - } - }); +$("#callsign").keyup( scp_keyup({ + selector: $(this), + showSuggestions: function (call, text) { + $('.callsign-suggestions').text(text); + $('.callsign-suggest').show(); + } +})); //Reset QSO form Fields function function resetDefaultQSOFields() { @@ -1116,7 +1086,61 @@ function testTimeOffConsistency() { return true; } -function filterCallsignList(call, list) { - let re = "(^|\/)" + call; - return list?.filter((el) => (el.search(re) !== -1)).join(' ') || ''; +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; } +