Skip to content

Commit

Permalink
#545 refactor of leaflet mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Collins committed Jan 31, 2024
1 parent 7f4e680 commit 9a251b9
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 58 deletions.
23 changes: 21 additions & 2 deletions grails-app/assets/javascripts/exploreYourArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ $(document).ready(function() {
});
}

// jQuery.i18n.properties is required now, wait a bit
setTimeout(function () {
init()
}, 50)
}); // end onLoad event

function init() {
// check for i18n
var i = 0;
$.each(jQuery.i18n.map, function() { i++ });
if (i < 100) { // wait for at least 100 elements in this map
// wait longer for i18n
setTimeout(function () {
init()
}, 50)
return
}

leafletI18n();

// initialise Google Geocoder
geocoder = new google.maps.Geocoder();

Expand Down Expand Up @@ -247,8 +267,7 @@ $(document).ready(function() {
geocodeAddress();
}
});

}); // end onLoad event
}

//var proj900913 = new OpenLayers.Projection("EPSG:900913");
//var proj4326 = new OpenLayers.Projection("EPSG:4326");
Expand Down
94 changes: 93 additions & 1 deletion grails-app/assets/javascripts/jquery_i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,96 @@
//= require jquery_migration
//= require jquery.i18n.properties


function leafletI18n() {
L.drawLocal = {
draw: {
toolbar: {
actions: {
title: jQuery.i18n.prop('advancedsearch.js.map.canceldrawing'),
text: jQuery.i18n.prop('advancedsearch.js.map.cancel')
},
undo: {
title: jQuery.i18n.prop('advancedsearch.js.map.deletepointdrawn'),
text: jQuery.i18n.prop('advancedsearch.js.map.deletepoint')
},
buttons: {
polyline: 'Draw a polyline',
polygon: jQuery.i18n.prop('advancedsearch.js.map.polygon'),
rectangle: jQuery.i18n.prop('advancedsearch.js.map.rectangle'),
circle: jQuery.i18n.prop('advancedsearch.js.map.circle'),
marker: 'Draw a marker'
}
},
handlers: {
circle: {
tooltip: {
start: jQuery.i18n.prop('advancedsearch.js.map.circle.tooltip')
}
},
marker: {
tooltip: {
start: 'Click map to place marker.'
}
},
polygon: {
tooltip: {
start: jQuery.i18n.prop('advancedsearch.js.map.clicktostart'),
cont: jQuery.i18n.prop('advancedsearch.js.map.clicktocontinue'),
end: jQuery.i18n.prop('advancedsearch.js.map.clickfirst')
}
},
polyline: {
error: '<strong>'+jQuery.i18n.prop('advancedsearch.js.map.error1')+'</strong> '+jQuery.i18n.prop('advancedsearch.js.map.error2'),
tooltip: {
start: jQuery.i18n.prop('advancedsearch.js.map.polyline.tooltip.start'),
cont: jQuery.i18n.prop('advancedsearch.js.map.polyline.tooltip.cont'),
end: jQuery.i18n.prop('advancedsearch.js.map.polyline.tooltip.end')
}
},
rectangle: {
tooltip: {
start: jQuery.i18n.prop('advancedsearch.js.map.rectangleguide')
}
},
simpleshape: {
tooltip: {
end: jQuery.i18n.prop('advancedsearch.js.map.finish')
}
}
}
},
edit: {
toolbar: {
actions: {
save: {
title: jQuery.i18n.prop('advancedsearch.js.map.save.title'),
text: jQuery.i18n.prop('advancedsearch.js.map.save.text')
},
cancel: {
title: jQuery.i18n.prop('advancedsearch.js.map.cancel.title'),
text: jQuery.i18n.prop('advancedsearch.js.map.cancel.text')
}
},
buttons: {
edit: jQuery.i18n.prop('advancedsearch.js.map.edit'),
editDisabled: jQuery.i18n.prop('advancedsearch.js.map.noedit'),
remove: jQuery.i18n.prop('advancedsearch.js.map.remove'),
removeDisabled: jQuery.i18n.prop('advancedsearch.js.map.nodelete')
}
},
handlers: {
edit: {
tooltip: {
text: jQuery.i18n.prop('advancedsearch.js.map.edit.tooltip.text'),
subtext: jQuery.i18n.prop('advancedsearch.js.map.edit.tooltip.subtext')
}
},
remove: {
tooltip: {
text: jQuery.i18n.prop('advancedsearch.js.map.remove.tooltip')
}
}
}
}
};
}
36 changes: 30 additions & 6 deletions grails-app/assets/javascripts/map.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ function addClickEventForVector(layer) {
MAP_VAR.map[name].enable();
}

function removeLayer(leaflet_id) {
function removeLayer(leaflet_id, local_id) {
if (MAP_VAR.map._layers[leaflet_id] !== undefined) {
var layer = MAP_VAR.map._layers[leaflet_id];
MAP_VAR.map.removeLayer(layer);
} else if (MAP_VAR.layers[local_id] !== undefined) {
var layer = MAP_VAR.layers[local_id];
MAP_VAR.map.removeLayer(layer);
}
}

Expand Down Expand Up @@ -101,23 +104,44 @@ function generatePopup(layer, latlng) {
"<a id='showOnlyTheseRecords' href='" + BC_CONF.contextPath + "/occurrences/search" +
params + "'>" + jQuery.i18n.prop("search.map.popup.linkText") + "</a><br>" +
"<a id='removeArea' href='javascript:void(0)' " +
"onclick='removeLayer(\"" + layer._leaflet_id + "\");MAP_VAR.map.closePopup()'>" +
"onclick='removeLayer(\"" + layer._leaflet_id + "\",\"" + layer._local_id + "\");MAP_VAR.map.closePopup()'>" +
jQuery.i18n.prop("search.map.popup.removeText") + "</a>")
.openOn(MAP_VAR.map);

getSpeciesCountInArea(params);
getOccurrenceCountInArea(params);
}

var setLaterCounter = {}
function setLater(elementId, innerHtml, looping) {
if (!looping) {
setLaterCounter[elementId] = 0
} else {
if (++setLaterCounter[elementId] > 10) {
// exit loop
return
}
}
var element = document.getElementById(elementId)
if (element === null || element === undefined) {

setTimeout(function() {
setLater(element, innerHtml, true)
}, 300);
} else {
element.innerHTML = innerHtml
}
}

function getSpeciesCountInArea(params) {
speciesCount = -1;
$.getJSON(BC_CONF.biocacheServiceUrl + "/occurrences/facets" + params + "&facets=taxon_name",
function( data ) {
if (data && data.length > 0 && data[0].count !== undefined) {
var speciesCount = data[0].count;
document.getElementById("speciesCountDiv").innerHTML = speciesCount;
setLater('speciesCountDiv', speciesCount)
} else {
document.getElementById("speciesCountDiv").innerHTML = 0;
setLater('speciesCountDiv', 0)
}
});
}
Expand All @@ -128,13 +152,13 @@ function getOccurrenceCountInArea(params) {
function( data ) {
if (data && data.totalRecords !== undefined) {
var occurrenceCount = data.totalRecords;
document.getElementById("occurrenceCountDiv").innerHTML = occurrenceCount;
setLater('occurrenceCountDiv', occurrenceCount)

if (occurrenceCount == "0") {
$("#showOnlyTheseRecords").hide()
}
} else {
document.getElementById("occurrenceCountDiv").innerHTML = 0;
setLater('occurrenceCountDiv', 0)
}
});
}
Expand Down
52 changes: 34 additions & 18 deletions grails-app/assets/javascripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@ $(document).ready(function() {
});
}

// jQuery.i18n.properties was removed, as it already loaded on the seach page via 'biocache-hubs.js' file
// which requires jQuery.i18n.properties to be loaded earlier for leaflet plugin. NdR Nov 2018.
// jQuery.i18n.properties is required now, wait a bit
setTimeout(function () {
init()
}, 50)
}); // end JQuery document ready

function init() {
// check for i18n
var i = 0;
$.each(jQuery.i18n.map, function() { i++ });
if (i < 100) { // wait for at least 100 elements in this map
// wait longer for i18n
setTimeout(function () {
init()
}, 50)
return
}

leafletI18n();

//alert("doc is loaded");
// listeners for sort & paging widgets
var excludeCounts = {};
$.get(BC_CONF.excludeCountUrl).done(function(data) {
Expand Down Expand Up @@ -341,24 +357,24 @@ $(document).ready(function() {
e.preventDefault();
window.location.href = BC_CONF.contextPath + "/occurrences/" + $(this).attr("id");
}).hover(function(){
// mouse in
$(this).css('cursor','pointer');
$(this).css('background-color','#FFF');
}, function() {
// mouse out
$(this).css('cursor','default');
$(this).css('background-color','transparent');
// mouse in
$(this).css('cursor','pointer');
$(this).css('background-color','#FFF');
}, function() {
// mouse out
$(this).css('cursor','default');
$(this).css('background-color','transparent');
});

$('.multipleFacetsLink').click(function() {
var link = this;
var facetName = link.id.
replace("multi-", "").
replace("_guid", "").
replace("_uid", "_name").
replace("data_resource_name", "data_resource_uid").
replace("data_provider_name", "data_provider_uid").
replace("species_list_name", "species_list_uid").
replace("multi-", "").
replace("_guid", "").
replace("_uid", "_name").
replace("data_resource_name", "data_resource_uid").
replace("data_provider_name", "data_provider_uid").
replace("species_list_name", "species_list_uid").
//replace(/(_[id])$/, "$1_RNG").
replace("occurrence_year", "decade");

Expand Down Expand Up @@ -1387,7 +1403,7 @@ $(document).ready(function() {
var flagIssueLink = '<a href="RECORD_URL">record.</a>';
flagIssueLink = flagIssueLink.replace('RECORD_URL', recordUrl);
attribution += '<br>' + recordLink +
'<br><br>If this image is incorrectly<br>identified please flag an<br>issue on the ' + flagIssueLink +'<br>';
'<br><br>If this image is incorrectly<br>identified please flag an<br>issue on the ' + flagIssueLink +'<br>';
setDialogSize();
$('#imageDialog').modal('show');
});
Expand Down Expand Up @@ -1449,7 +1465,7 @@ $(document).ready(function() {
$(el).addClass('fa-caret-down');
}
}
}); // end JQuery document ready
}

/**
* Catch sort drop-down and build GET URL manually
Expand Down
25 changes: 23 additions & 2 deletions grails-app/assets/javascripts/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ $(document).ready(function() {
});
}

// jQuery.i18n.properties is required now, wait a bit
setTimeout(function () {
init()
}, 50)

}); // end JQuery document ready

function init() {
// check for i18n
var i = 0;
$.each(jQuery.i18n.map, function() { i++ });
if (i < 100) { // wait for at least 100 elements in this map
// wait longer for i18n
setTimeout(function () {
init()
}, 50)
return
}

leafletI18n();

$('#showUncheckedTests').on('click', function(e){
$('.uncheckTestResult').toggle();
});
Expand Down Expand Up @@ -270,7 +291,7 @@ $(document).ready(function() {
// bind to form "close" button TODO
$("input#close").on("click", function(e) {
// close the popup
// $.fancybox.close();
// $.fancybox.close();
// reset form back to default state
$('form#issueForm')[0].reset();
$("#submitSuccess").html("");
Expand Down Expand Up @@ -438,7 +459,7 @@ $(document).ready(function() {

});

}); // end JQuery document ready
}

/**
* Delete a user assertion
Expand Down
Loading

0 comments on commit 9a251b9

Please sign in to comment.