Skip to content

Commit

Permalink
Load remaining pins (not on current page) asynchronously (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 9, 2016
1 parent f9f2677 commit 4a55c10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ static SearchResponse executeQuery(int from, int size, QueryBuilder query) {
.setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(query)//
.setFrom(from)//
.setSize(size);
searchRequest = withAggregations(from, size, searchRequest, "type.raw",
searchRequest = withAggregations(searchRequest, "type.raw",
localizedLabel("classification.label.raw"),
localizedLabel("fundertype.label.raw"),
localizedLabel("stocksize.label.raw"));
Expand All @@ -326,7 +326,7 @@ static SearchResponse executeQuery(int from, int size, QueryBuilder query) {
return searchRequest.execute().actionGet();
}

private static SearchRequestBuilder withAggregations(int from, int size,
private static SearchRequestBuilder withAggregations(
final SearchRequestBuilder searchRequest, String... fields) {
Arrays.asList(fields).forEach(field -> {
searchRequest
Expand All @@ -338,7 +338,7 @@ private static SearchRequestBuilder withAggregations(int from, int size,
.setSize(Integer.MAX_VALUE);
String position = session("position");
if (position != null) {
topHitsBuilder.setFrom(from).setSize(size)
topHitsBuilder.setSize(Integer.MAX_VALUE)
.addSort(new GeoDistanceSortBuilder("location.geo")
.points(new GeoPoint(position)));
}
Expand Down
46 changes: 32 additions & 14 deletions app/views/facet_map.scala.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

@import play.api.libs.json._
@import com.typesafe.config._
@import play.mvc.Controller.session

@addMarkers(pins: Seq[JsValue]) = {
@for(pin <- pins;
Array(isil, latLon, name, classification, _*) = pin.as[JsArray].value.head.as[String].split(";;;");
if isil != "null" && latLon != "null" && name != "null" && classification != "null") {
var iconLabel = '@Application.CONFIG.getObject("organisation.icons").getOrDefault(classification, ConfigValueFactory.fromAnyRef("library")).unwrapped()';
addMarker('/organisations/@isil', '@latLon', '@name', iconLabel);
}
}

var layer = L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});
Expand Down Expand Up @@ -71,21 +82,28 @@ window.onload = addMarkerLayer;
function addMarkerLayer(){
var addedLocations = [];
var latLngObjects = [];
@for(pin <- (Json.parse(queryMetadata) \\ "pin");
Array(isil, latLon, name, classification, _*) = pin.as[JsArray].value.head.as[String].split(";;;");
if isil != "null" && latLon != "null" && name != "null" && classification != "null") {
var iconLabel = '@Application.CONFIG.getObject("organisation.icons").getOrDefault(classification, ConfigValueFactory.fromAnyRef("library")).unwrapped()';
addMarker('/organisations/@isil', '@latLon', '@name', iconLabel);
}
map.addLayer(markers);

$("#location-facet-loading").hide();
if(addedLocations.length > 0){
$("#location-facet").show();
map.invalidateSize();
map.fitBounds(latLngObjects, {reset: true});
@defining(Json.parse(queryMetadata) \\ "pin") { allPins =>
@defining(allPins.slice(from, from + size + 1)) { pinsForPage =>
@if(session("position") != null) {
@addMarkers(pinsForPage)
} else {
@addMarkers(allPins)
$("#location-facet-loading").hide();
}
map.addLayer(markers);
if(addedLocations.length > 0){
$("#location-facet").show();
map.invalidateSize();
map.fitBounds(latLngObjects, {reset: true});
}
@if(session("position") != null) {
setTimeout(function() {
@addMarkers(allPins diff pinsForPage)
$("#location-facet-loading").hide();
}, 10000);
}
}
}

function addMarker(link, latLon, name, iconLabel){
if(addedLocations.indexOf(latLon) == -1) {
var lat = latLon.split(",")[0];
Expand Down

0 comments on commit 4a55c10

Please sign in to comment.