Skip to content

Commit

Permalink
#610 Fix for Explore Your Area pagination broken in species column
Browse files Browse the repository at this point in the history
Also fixed species group labels showing square braces around them - i18n tool adds this when there is a missing translation entry. @vjrj - might need to add to LA i18n files.
  • Loading branch information
nickdos committed Aug 14, 2024
1 parent e5cefb2 commit b58c3e1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
99 changes: 56 additions & 43 deletions grails-app/assets/javascripts/exploreYourArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function init() {
// Register events for the species_group column
$('#taxa-level-0').on("mouseover mouseout", "tbody tr", function() {
// mouse hover on groups
if ( event.type == "mouseover" ) {
if ( event.type === "mouseover" ) {
$(this).addClass('hoverRow');
} else {
$(this).removeClass('hoverRow');
Expand Down Expand Up @@ -146,19 +146,19 @@ function init() {
var radiusInMetres = $.url().param('radius') * 1000; // assume radius is in km (SOLR radius param)
var radius = zoomForRadius[radiusInMetres];
var species_group;
var fqs = $.url().param('fq'); // can be array or string
var fqs = $.url().param('fq'); // can be an array or string

if (Array.isArray(fqs)) {
// multiple fq params
fqs.forEach(function(fq) {
var parts = fq.split(":"); // e.g. speciesGroup:Insects
if (parts[0] == "species_group") {
if (parts[0] === "species_group") {
species_group = parts[1];
}
});
} else if (fqs) {
var parts = fqs.split(":");
if (parts[0] == "species_group") {
if (parts[0] === "species_group") {

This comment has been minimized.

Copy link
@qifeng-bai

qifeng-bai Aug 15, 2024

Contributor

Is it worth checking parts[1] exists?

This comment has been minimized.

Copy link
@nickdos

nickdos Aug 15, 2024

Author Contributor

probably should but this code is 10+ years old - I just fixed the IDE warnings which was == -> ===. We'll be replacing this code with the atlas-index rewrite, I think.

species_group = parts[1];
}
}
Expand All @@ -183,7 +183,7 @@ function init() {
$('#viewAllRecords').on("click", function(e) {
e.preventDefault();
var params = "q=*:*&lat="+$('#latitude').val()+"&lon="+$('#longitude').val()+"&radius="+$('#radius').val()+"&fq=spatiallyValid:true";
if (speciesGroup != "ALL_SPECIES") {
if (speciesGroup !== "ALL_SPECIES") {
params += "&fq=species_group:" + speciesGroup;
}
document.location.href = MAP_VAR.contextPath +'/occurrences/search?' + params;
Expand All @@ -193,7 +193,7 @@ function init() {
$('#downloadData').on("click", function(e) {
e.preventDefault();
var params = "?q=*:*&lat="+$('#latitude').val()+"&lon="+$('#longitude').val()+"&radius="+$('#radius').val()+"&fq=spatiallyValid:true";
if (speciesGroup != "ALL_SPECIES") {
if (speciesGroup !== "ALL_SPECIES") {
params += "&fq=species_group:" + speciesGroup;
}
var searchParams = encodeURIComponent(params);
Expand Down Expand Up @@ -255,7 +255,7 @@ function init() {

// Catch enter key press on form
$("#searchForm").bind("keypress", function(e) {
if (e.keyCode == 13) {
if (e.keyCode === 13) {
e.preventDefault();
geocodeAddress();
}
Expand Down Expand Up @@ -345,7 +345,7 @@ function loadLeafletMap() {

// Add a Circle overlay to the map.
radius = parseInt($('select#radius').val()) * 1010;
var circlProps = {
var circleProps = {
//radius: radius, leaflet 1+ uses this syntax
stroke: true,
weight: 2,
Expand All @@ -356,7 +356,7 @@ function loadLeafletMap() {
zIndex: -10
}

circle = L.circle(latLng, radius, circlProps).addTo(MAP_VAR.map);
circle = L.circle(latLng, radius, circleProps).addTo(MAP_VAR.map);

// detect click event and trigger record info popup
circle.on("click", function (event) {
Expand All @@ -371,8 +371,8 @@ function loadLeafletMap() {
var newLatLng = L.latLng(lat, lon);
MAP_VAR.map.panTo({lon:lon,lat:lat})
MAP_VAR.map.removeLayer(circle);
circlProps.radius = radius;
circle = L.circle([lat, lon], radius, circlProps).addTo(MAP_VAR.map);
circleProps.radius = radius;
circle = L.circle([lat, lon], radius, circleProps).addTo(MAP_VAR.map);
updateMarkerAddress('Drag ended');
updateMarkerPosition(newLatLng);
geocodePosition(newLatLng);
Expand All @@ -395,7 +395,7 @@ function loadLeafletMap() {
* Google geocode function
*/
function geocodePosition(pos) {
var gLatLng = new google.maps.LatLng(pos.lat, pos.lng); // convert leaflet Latlng to Google Latlng
var gLatLng = new google.maps.LatLng(pos.lat, pos.lng); // convert leaflet LatLng to Google LatLng

geocoder.geocode({
latLng: gLatLng
Expand Down Expand Up @@ -440,7 +440,7 @@ function updateMarkerPosition(latLng) {
function loadRecordsLayer(retry) {
if (!MAP_VAR.map && !retry) {
// in case a callback calls this function before map has initialised
setTimeout(function() {if (!points || points.length == 0) {loadRecordsLayer(true)}}, 2000);
setTimeout(function() {if (!points || points.length === 0) {loadRecordsLayer(true)}}, 2000);
return;
} else if (!MAP_VAR.map) {
return;
Expand All @@ -457,7 +457,7 @@ function loadRecordsLayer(retry) {
}

// URL for GeoJSON web service
var speciesGroupParam = "species_group:" + (speciesGroup == "ALL_SPECIES" ? "*" : speciesGroup);
var speciesGroupParam = "species_group:" + (speciesGroup === "ALL_SPECIES" ? "*" : speciesGroup);
var alaParams = jQuery.param({
q: (taxon) ? "taxon_name:\"" + taxon + "\"" : "*:*",
lat: $('#latitude').val(),
Expand Down Expand Up @@ -490,7 +490,7 @@ function loadRecordsLayer(retry) {
MAP_VAR.layerControl.addOverlay(alaWmsLayer, 'Records');

alaWmsLayer.on('tileload', function(te){
// populate points array so we can tell if data is loaded - legacy from geoJson version
// populate points array, so we can tell if data is loaded - legacy from geoJson version
points.push(te.url);
});
}
Expand All @@ -501,7 +501,7 @@ function loadRecordsLayer(retry) {
function attemptGeolocation() {
// HTML5 GeoLocation
if (navigator && navigator.geolocation) {
function getMyPostion(position) {
function getMyPosition(position) {
updateMarkerPosition(L.latLng(position.coords.latitude, position.coords.longitude));
initialize();
}
Expand All @@ -518,9 +518,9 @@ function attemptGeolocation() {
initialize();
}

navigator.geolocation.getCurrentPosition(getMyPostion, positionWasDeclined);
navigator.geolocation.getCurrentPosition(getMyPosition, positionWasDeclined);

// Neither functions gets called for some reason, so I've added a delay to initalize map anyway
// Neither functions gets called for some reason, so I've added a delay to initialize map anyway
setTimeout(function() {if (!MAP_VAR.map) positionWasDeclined();}, 9000);
} else if (google.loader && google.loader.ClientLocation) {
// Google AJAX API fallback GeoLocation
Expand Down Expand Up @@ -558,7 +558,7 @@ function geocodeAddress(reverseGeocode) {

if (!latLng && geocoder && address) {
geocoder.geocode( {'address': address, region: MAP_VAR.geocodeRegion}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status === google.maps.GeocoderStatus.OK) {
// geocode was successful
updateMarkerAddress(results[0].formatted_address);
var gLatLng = results[0].geometry.location;
Expand All @@ -580,13 +580,13 @@ function geocodeAddress(reverseGeocode) {
* Geocode location via Google Maps API
*/
function addAddressToPage(response) {
if (!response || response.Status.code != 200) {
if (!response || response.Status.code !== 200) {
alert("Sorry, we were unable to geocode that address");
} else {
var location = response.Placemark[0];
var lat = location.Point.coordinates[1]
var lon = location.Point.coordinates[0];
var locationStr = response.Placemark[0].address;
var locationStr = location.address;
updateMarkerAddress(locationStr);
updateMarkerPosition(L.latLng(lat, lon));
}
Expand All @@ -602,7 +602,6 @@ var lastParameters
* Species group was clicked
*/
function groupClicked(el) {
speciesJson = []

// Change the global var speciesGroup
speciesGroup = $(el).find('a.taxonBrowse').attr('id');
Expand All @@ -612,7 +611,7 @@ function groupClicked(el) {
$(el).addClass("activeRow");
$('#taxa-level-1 tbody tr').addClass("activeRow");
// update records page link text
if (speciesGroup == "ALL_SPECIES") {
if (speciesGroup === "ALL_SPECIES") {
$("#recordsGroupText").text("all");
} else {
$("#recordsGroupText").text("selected");
Expand Down Expand Up @@ -640,6 +639,7 @@ function groupClicked(el) {
return
} else {
lastParameters = allParameters
speciesJson = [] // flow continues, so clear out old data for AJAX call below
}

$('#rightList tbody').empty();
Expand Down Expand Up @@ -667,7 +667,7 @@ function groupClicked(el) {
}

/**
* Process the JSON data from an Species list AJAX request (species in area)
* Process the JSON data from a species list AJAX request (species in area)
*/
function processSpeciesJsonData(data, currentGroup) {
var offset = globalOffset
Expand All @@ -687,25 +687,28 @@ function processSpeciesJsonData(data, currentGroup) {
var count = i;
// add count
var tr = '<tr><td class="speciesIndex">'+(count+1)+'.</td>';
const sciName = data[i].name;
const commonName = data[i].commonName;
const taxonGuid = data[i].guid;
// add common name
if (data[i].commonName) {
tr = tr + '<td class="sciName">' + data[i].commonName +'</td>';
if (commonName) {
tr = tr + '<td class="sciName">' + commonName +'</td>';
} else {
tr = tr + ' <td class="sciName"> [Not supplied] </td>';
}
// add scientific name
tr = tr + '<td class="sciName"><a id="' + data[i].guid + '" class="taxonBrowse2" title="'+linkTitle+'" href="'+ // id=taxon_name
data[i].name+'"><i>'+data[i].name+'</i></a>';
tr = tr + '<td class="sciName"><a id="' + taxonGuid + '" class="taxonBrowse2" title="'+linkTitle+'" href="'+ // id=taxon_name
sciName+'"><i>'+sciName+'</i></a>';

// add links to species page and occurrence search (inside hidden div)
if(MAP_VAR.speciesPageUrl) {
var speciesInfo = '<div class="speciesInfo">';
if (data[i].guid) {
speciesInfo = speciesInfo + '<a class="speciesPageLink" title="' + infoTitle + '" href="' + MAP_VAR.speciesPageUrl + data[i].guid +
if (taxonGuid) {
speciesInfo = speciesInfo + '<a class="speciesPageLink" title="' + infoTitle + '" href="' + MAP_VAR.speciesPageUrl + taxonGuid +
'"><img src="' + MAP_VAR.imagesUrlPrefix + '/page_white_go.png" alt="species page icon" style="margin-bottom:-3px;" class="no-rounding"/>' +
' species profile</a> | ';
}
speciesInfo = speciesInfo + '<a href="' + MAP_VAR.contextPath + '/occurrences/search?q=taxon_name:%22' + data[i].name +
speciesInfo = speciesInfo + '<a href="' + MAP_VAR.contextPath + '/occurrences/search?q=taxon_name:%22' + sciName +
'%22&lat=' + $('input#latitude').val() + '&lon=' + $('input#longitude').val() + '&radius=' + $('select#radius').val() + '" title="' +
recsTitle + '"><img src="' + MAP_VAR.imagesUrlPrefix + '/database_go.png" ' +
'alt="search list icon" style="margin-bottom:-3px;" class="no-rounding"/> list of records</a></div>';
Expand All @@ -728,7 +731,7 @@ function processSpeciesJsonData(data, currentGroup) {

globalOffset += pageSize
} else {
// no spceies were found (either via paging or clicking on taxon group
// no species were found (either via paging or clicking on taxon group
var text = '<tr><td></td><td colspan="2">[no species found]</td></tr>';
contents += text;
}
Expand Down Expand Up @@ -800,7 +803,7 @@ function processSpeciesJsonData(data, currentGroup) {
}

function sortSpeciesJson() {
if (globalSortOrder == "common") {
if (globalSortOrder === "common") {
speciesJson.sort(function (a,b) {
if (a.commonName === b.commonName) return 0

Expand All @@ -809,13 +812,13 @@ function sortSpeciesJson() {
if (b.commonName === '') return -1
return (a.commonName < b.commonName ? -1 : 1)
})
} else if (globalSortOrder == "taxa") {
} else if (globalSortOrder === "taxa") {
speciesJson.sort(function (a,b) {
return (a.name == b.name) ? 0 : (a.name < b.name ? -1 : 1)
return (a.name === b.name) ? 0 : (a.name < b.name ? -1 : 1)
})
} else {
speciesJson.sort(function (a,b) {
return (a.count == b.count) ? 0 : (a.count < b.count ? 1 : -1)
return (a.count === b.count) ? 0 : (a.count < b.count ? 1 : -1)
})
}
}
Expand Down Expand Up @@ -860,16 +863,26 @@ function populateSpeciesGroups(data) {
}

function addGroupRow(group, count, indent) {
var label = group;
if (group == "ALL_SPECIES") label = "all.species";
var rc = (group == speciesGroup) ? " class='activeRow'" : ""; // highlight active group
var label = camelCase(group);
if (group === "ALL_SPECIES") label = "all.species";
var rc = (group === speciesGroup) ? " class='activeRow'" : ""; // highlight active group
var i18nLabel = jQuery.i18n.prop(label);

var h = "<tr"+rc+" title='click to view group on map'><td class='indent"+indent+"'><a href='#' id='"+group+"' class='taxonBrowse' title='click to view group on map'>"+i18nLabel+"</a></td><td>"+count+"</td></tr>";
var h = "<tr"+rc+" title='click to view group on map'><td class='indent"+indent+"'><a href='#' id='"+group+"' " +
"class='taxonBrowse' title='click to view group on map'>"+i18nLabel+"</a></td><td>"+count+"</td></tr>";
$("#taxa-level-0 tbody").append(h);
}
}

// Function to convert into camel Case
function camelCase(str) {
// Using replace method with regEx
const word = str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
return word[0].toUpperCase() + word.substring(1);
}

function bookmarkedSearch(lat, lng, zoom1, group) {
MAP_VAR.radius = radiusForZoom[zoom1]; // set global var
MAP_VAR.zoom = parseInt(zoom1);
Expand All @@ -885,9 +898,9 @@ function loadStateFromHash(encodedHash) {
// update map state from URL hash
// e.g. #-35.2512|149.1630|12|ALL_SPECIES
var hashParts = encodedHash.split("%7C"); // note escaped version of |
if (hashParts.length == 3) {
if (hashParts.length === 3) {
bookmarkedSearch(hashParts[0], hashParts[1], hashParts[2], null);
} else if (hashParts.length == 4) {
} else if (hashParts.length === 4) {
// not sure what is going on with the selected species group encoding, but this works
bookmarkedSearch(hashParts[0], hashParts[1], hashParts[2], decodeURIComponent(decodeURIComponent(hashParts[3])));
} else {
Expand All @@ -900,7 +913,7 @@ function getEncodedHash() {
var hash = window.location.hash.replace( /^#/, '');
var encodedHash;

if (hash.indexOf("%7C") != -1) {
if (hash.indexOf("%7C") !== -1) {
// already escaped
encodedHash = hash;
} else {
Expand Down
2 changes: 2 additions & 0 deletions grails-app/i18n/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@ Plants=Plants
Bryophytes=Bryophytes
Gymnosperms=Gymnosperms
FernsAndAllies=Ferns and Allies
FloweringPlants=Flowering plants
Conifers,Cycads=Conifers, Cycads
Angiosperms=Angiosperms
Monocots=Monocots
Dicots=Dicots
Expand Down

0 comments on commit b58c3e1

Please sign in to comment.