Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce calling scp, cache result #3025

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions application/controllers/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +106,7 @@ public function scp() {

foreach ($arCalls as $strCall)
{
echo " " . $strCall . " ";
echo $strCall . " ";
}

}
Expand Down
97 changes: 74 additions & 23 deletions assets/js/sections/contesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

82 changes: 66 additions & 16 deletions assets/js/sections/qso.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}