diff --git a/webassets/js/code.js b/webassets/js/code.js index 8304e8c..aee3161 100644 --- a/webassets/js/code.js +++ b/webassets/js/code.js @@ -10,7 +10,7 @@ var lastUpdateTime; // Load spots and populate the table. function loadSpots() { - $.getJSON('/api/spots', function(jsonData) { + $.getJSON('/api/spots' + buildQueryString(), function(jsonData) { // Store last updated time lastUpdateTime = moment.utc(); updateRefreshDisplay(); @@ -21,13 +21,38 @@ function loadSpots() { }); } +// Build a query string for the API, based on the filters that the user has selected. +function buildQueryString() { + var str = "?"; + var filterNames = ["dx_continent", "de_continent", "mode_type", "source", "band"]; + str = str + filterNames.map(n => { return getQueryStringFor(n); }).join("&"); + return str; +} + +// For a parameter, such as dx_continent, get the query string for the current filter options. +function getQueryStringFor(parameter) { + return parameter + "=" + encodeURIComponent(getSelectedFilterOptions(parameter)); +} + +// For a parameter, such as dx_continent, get the filter options that are currently selected in the UI. +function getSelectedFilterOptions(parameter) { + return $(".filter-button-" + parameter).filter(function() { + return this.checked; + }).map(function() { + return this.value; + }).get().join(","); +} + // Update the spots table function updateTable() { // Populate table with headers - let headers = Object.keys(spots[0]); let table = $('').append(''); ["UTC", "DX", "Frequency", "Mode", "Comment", "Source", "Ref.", "DE"].forEach(header => table.find('thead tr').append(``)); + if (spots.length == 0) { + table.find('tbody').append(''); + } + spots.forEach(s => { // Create row let $tr = $(''); @@ -188,7 +213,7 @@ function generateFilterCard(displayName, filterQuery, options) { $card_body.append(`
${displayName}
`); $p = $("

"); options.forEach(o => { - $p.append(` `); + $p.append(` `); }); $card_body.append($p); $card.append($card_body); @@ -207,7 +232,7 @@ function generateBandsFilterCard(displayName, filterQuery, band_options) { // CSS doesn't like IDs with decimal points in, so we need to replace that in the same way as when we originally // queried the options endpoint and set our CSS. var cssFormattedBandName = o['name'] ? o['name'].replace('.', 'p') : "unknown"; - $p.append(` `); + $p.append(` `); }); $card_body.append($p); $card.append($card_body); @@ -271,15 +296,9 @@ $(document).ready(function() { }); $("#filters-button").click(function() { $("#filters-area").toggle(); - // If we have just dismissed the filters panel, reload spots - if (!$("#filters-area").is(":visible")) { - loadSpots()(); - } }); $("#close-filters-button").click(function() { $("#filters-button").button("toggle"); $("#filters-area").hide(); - // We have just dismissed the filters panel, reload spots - loadSpots(); }); }); \ No newline at end of file

${header}
No spots match your filters.