From 4a55c101ff356c60a0c887ac8a7464c97f789716 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Fri, 9 Sep 2016 15:25:57 +0200 Subject: [PATCH] Load remaining pins (not on current page) asynchronously (#261) --- app/controllers/Application.java | 6 ++--- app/views/facet_map.scala.js | 46 ++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/controllers/Application.java b/app/controllers/Application.java index afeaabb6..e093ac77 100644 --- a/app/controllers/Application.java +++ b/app/controllers/Application.java @@ -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")); @@ -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 @@ -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))); } diff --git a/app/views/facet_map.scala.js b/app/views/facet_map.scala.js index b38cafad..4d356e77 100644 --- a/app/views/facet_map.scala.js +++ b/app/views/facet_map.scala.js @@ -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: '© OpenStreetMap contributors' }); @@ -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];