Skip to content

Commit

Permalink
Merge dcd1f1c into 83896b0
Browse files Browse the repository at this point in the history
  • Loading branch information
mejackreed committed Apr 28, 2015
2 parents 83896b0 + dcd1f1c commit 4454373
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 81 deletions.
24 changes: 13 additions & 11 deletions README.md
Expand Up @@ -167,17 +167,19 @@ However, the catalog#show maplet widget must be included manually, via one of tw
The ```blacklight_map_tag``` helper takes an options hash as one of its arguments that can be used to provide customization options for the Leaflet map functionality via data attributes. (See ```app/views/catalog/index_map``` for an example.) The available options include:
- ```viewpoint``` = the center point of the map (```[lat,long]```)
- ```searchcontrol``` = whether to display the search control on the map (```boolean```)
- ```catalogpath``` = the search path for the search control (e.g. ```catalog_index_path```)
- ```placenamefield``` = the name of the Solr field containing the location names (e.g. ```"placename_field"```)
- ```searchctrlcue``` = the hover text to display when the mouse hovers over the search control
- ```singlemarkermode``` = whether locations should be clustered (```boolean```)
- ```clustercount``` = whether clusters should display the location count or the number of hits (```"hits" || "locations"```)
- ```maxzoom``` = the maxZoom [property of the map](http://leafletjs.com/reference.html#map-maxzoom)
- ```tileurl``` = a [tileLayer url](http://leafletjs.com/reference.html#tilelayer-l.tilelayer) to change the basemap
- ```mapattribution``` = an [attribution string](http://leafletjs.com/reference.html#tilelayer-attribution) to describe the basemap layer
- ```nodata``` = a message to display in the Leaflet popup when the "popup" member is not present in the properties hash in the GeoJSON Feature for a location.
Option | Type | Default | Description
------ | ---- | ------- | -----------
`initialview` | Array | `null` | the initial extend of the map as a 2d Array (e.g. `[[minLat, minLng], [maxLat, maxLng]]`)
`searchcontrol` | Boolean | `false` | display the search control on the map
`catalogpath` | String | `'catalog'` | the search path for the search control
`placenamefield` | String | `'placename_field'` | the name of the Solr field containing the location names
`searchctrlcue` | String | `'Search for all items within the current map window'` | the hover text to display when the mouse hovers over the search control
`singlemarkermode` | Boolean | `true` | whether locations should be clustered
`clustercount` | String | `'locations'` | whether clusters should display the location count or the number of hits (`'hits'` or `'locations'`)
`maxzoom` | Integer | 18 | the maxZoom [property of the map](http://leafletjs.com/reference.html#map-maxzoom)
`tileurl` | String | `'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'` | a [tileLayer url](http://leafletjs.com/reference.html#tilelayer-l.tilelayer) to change the basemap
`mapattribution` | String | ``Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'` | an [attribution string](http://leafletjs.com/reference.html#tilelayer-attribution) to describe the basemap layer
`nodata` | String | `'Sorry, there is no data for this location.'` | a message to display in the Leaflet popup when the "popup" member is not present in the properties hash in the GeoJSON Feature for a location.
## Contributing
Expand Down
64 changes: 50 additions & 14 deletions app/assets/javascripts/blacklight-maps/blacklight-maps-browse.js
Expand Up @@ -7,8 +7,7 @@
var options = $.extend({
tileurl : 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
mapattribution : 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
viewpoint: [0,0],
initialzoom: 2,
initialZoom: 2,
singlemarkermode: true,
searchcontrol: false,
catalogpath: 'catalog',
Expand All @@ -27,6 +26,8 @@

var sortAndPerPage = $('#sortAndPerPage');

var markers;

// Update page links with number of mapped items, disable sort, per_page, pagination
if (sortAndPerPage.length) { // catalog#index and #map view
var page_links = sortAndPerPage.find('.page_links');
Expand Down Expand Up @@ -64,18 +65,10 @@
options.id = this.id;

// Setup Leaflet map
map = L.map(this.id);

// set the viewpoint and zoom
if (options.viewpoint[0].constructor === Array) {
map.fitBounds(options.viewpoint,
{
padding:[10,10],
maxZoom:options.maxzoom
});
} else {
map.setView(options.viewpoint, options.initialzoom);
}
map = L.map(this.id, {
center: [0, 0],
zoom: options.initialZoom
});

L.tileLayer(options.tileurl, {
attribution: options.mapattribution,
Expand Down Expand Up @@ -104,6 +97,9 @@
// Add markers to map
map.addLayer(markers);

// Fit bounds of map
setMapBounds(map);

// create overlay for search control hover
var searchHoverLayer = L.rectangle([[0,0], [0,0]], {
color: "#0033ff",
Expand Down Expand Up @@ -148,6 +144,46 @@

});

/**
* Sets the view of the map, based off of the map bounds
*/
function setMapBounds() {
map.fitBounds(mapBounds(), {
padding: [10, 10],
maxZoom: options.maxzoom
});
}

/**
* Returns the bounds of the map based off of initialview being set or gets
* the bounds of the markers object
*/
function mapBounds() {
if (options.initialview) {
return options.initialview;
} else {
return markerBounds();
}
}

/**
* Returns the bounds of markers, if there are not any return
*/
function markerBounds() {
if (hasAnyFeatures()) {
return markers.getBounds();
} else {
return [[90, 180], [-90, -180]];
}
}

/**
* Checks to see if there are any features in the markers MarkerClusterGroup
*/
function hasAnyFeatures() {
return !$.isEmptyObject(markers._featureGroup._layers);
}

// remove stale params, add new params, and run a new search
function _search() {
var params = filterParams(['view', 'spatial_search_type', 'coordinates', 'f%5B' + options.placenamefield + '%5D%5B%5D']),
Expand Down
30 changes: 1 addition & 29 deletions app/helpers/blacklight/blacklight_maps_helper_behavior.rb
Expand Up @@ -95,32 +95,4 @@ def serialize_geojson(documents, options={})
options)
export.to_geojson
end

# determine the best viewpoint for the map so all markers are visible
def set_viewpoint(geojson_features)
viewpoint = nil
geojson_docs = JSON.parse(geojson_features)["features"]
if !geojson_docs.blank?
if geojson_docs.length == 1
viewpoint = geojson_docs[0]["bbox"] ? nil : geojson_docs[0]["geometry"]["coordinates"].reverse
end
if geojson_docs.length > 1 || !viewpoint
longs, lats = [[],[]]
geojson_docs.each do |feature|
if feature["bbox"]
feature["bbox"].values_at(0,2).each {|long| longs << long }
feature["bbox"].values_at(1,3).each {|lat| lats << lat }
else
longs << feature["geometry"]["coordinates"][0]
lats << feature["geometry"]["coordinates"][1]
end
end
sorted_longs, sorted_lats = longs.sort, lats.sort
viewpoint = [[sorted_lats.first,sorted_longs.first],[sorted_lats.last,sorted_longs.last]]
end
end
viewpoint = [0,0] if !viewpoint
viewpoint
end

end
end
3 changes: 1 addition & 2 deletions app/views/catalog/_index_map.html.erb
@@ -1,6 +1,5 @@
<%= blacklight_map_tag('blacklight-index-map',
{data:{viewpoint: set_viewpoint(geojson_features),
searchcontrol: true,
{data:{searchcontrol: true,
catalogpath: catalog_index_path,
placenamefield: blacklight_config.view.maps.placename_field,
clustercount:'hits'
Expand Down
3 changes: 1 addition & 2 deletions app/views/catalog/_show_maplet_default.html.erb
Expand Up @@ -3,8 +3,7 @@
<% if @document[blacklight_config.view.maps.geojson_field.to_sym] || @document[blacklight_config.view.maps.coordinates_field.to_sym] %>
<% geojson_features = serialize_geojson(@document) %>
<%= blacklight_map_tag('blacklight-show-map',
{data:{viewpoint: set_viewpoint(geojson_features),
initialzoom:blacklight_config.view.maps.show_initial_zoom,
{data:{initialzoom:blacklight_config.view.maps.show_initial_zoom,
singlemarkermode:false}}) %>
<%= javascript_tag "$('#blacklight-show-map').blacklight_leaflet_map(#{geojson_features});" %>
<% end %>
Expand Down
21 changes: 21 additions & 0 deletions spec/features/initial_view_spec.rb
@@ -0,0 +1,21 @@
require 'spec_helper'

feature 'Initial view parameter', js: true do
before :all do
CatalogController.configure_blacklight do |config|
config.view.maps.facet_mode = 'coordinates'
config.view.maps.coordinates_facet_field = 'coordinates_facet'
config.add_facet_field 'coordinates_facet', :limit => -2, :label => 'Coordinates', :show => false
end
end
scenario 'defaults to zoom area of markers' do
visit catalog_index_path f: { format: ['Book'] }, view: 'maps'
expect(page).to have_css '.leaflet-marker-icon.marker-cluster', count: 8
end
scenario 'when provided sets map to its view' do
map_tag = '<div id="blacklight-index-map" data-initialview="[[37.65, -122.56],[37.89, -122.27]]" data-maxzoom="18" data-tileurl="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" data-mapattribution="Map data &amp;copy; &lt;a href=&quot;http://openstreetmap.org&quot;&gt;OpenStreetMap&lt;/a&gt; contributors, &lt;a href=&quot;http://creativecommons.org/licenses/by-sa/2.0/&quot;&gt;CC-BY-SA&lt;/a&gt;" data-searchcontrol="true" data-catalogpath="/catalog" data-placenamefield="placename_field" data-clustercount="hits" />'.html_safe
expect_any_instance_of(Blacklight::BlacklightMapsHelperBehavior).to receive(:blacklight_map_tag).and_return(map_tag)
visit catalog_index_path f: { format: ['Book'] }, view: 'maps'
expect(page).to_not have_css '.leaflet-marker-icon.marker-cluster'
end
end
5 changes: 3 additions & 2 deletions spec/features/maps_spec.rb
Expand Up @@ -91,8 +91,9 @@
end

describe "marker popups" do

before { find(".marker-cluster:first-child").click }
before do
find('.marker-cluster', text: '1', match: :first).click
end

it "should show a popup with correct content" do
expect(page).to have_selector("div.leaflet-popup-content-wrapper")
Expand Down
21 changes: 0 additions & 21 deletions spec/helpers/blacklight_maps_helper_spec.rb
Expand Up @@ -144,27 +144,6 @@ def create_response

end

describe "set_viewpoint" do

describe "map with a single point" do

it "should return a coordinate array matching the point" do
expect(helper.set_viewpoint(helper.serialize_geojson([@docs.first]))).to eq([29.646923, 91.117212])
end

end

describe "map with multiple points" do

it "should return an array of coordinates with the max lat-long of the range of points" do
expect(helper.set_viewpoint(helper.serialize_geojson(@docs))).to eq([[20.593684, 78.96288], [35.86166, 104.195397]])
end

end

end


def mock_query_response
%({"responseHeader"=>{"status"=>0, "QTime"=>14, "params"=>{"q"=>"tibet", "spellcheck.q"=>"tibet", "qt"=>"search", "wt"=>"ruby", "rows"=>"10"}}, "response"=>{"numFound"=>2, "start"=>0, "maxScore"=>0.016135123, "docs"=>[{"published_display"=>["Dharamsala, H.P."], "author_display"=>"Thub-bstan-yar-ʼphel, Rnam-grwa", "lc_callnum_display"=>["DS785 .T475 2005"], "pub_date"=>["2005"], "format"=>"Book", "material_type_display"=>["a-e, iv, ii, 407 p."], "title_display"=>"Bod gaṅs can gyi rgyal rabs mdor bsdus dris lan brgya pa rab gsal śel gyi me loṅ źes bya ba bźugs so", "id"=>"2008308202", "subject_geo_facet"=>["Tibet"], "language_facet"=>["Tibetan"], "geojson"=>["{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[91.117212, 29.646923]},\\"properties\\":{\\"placename\\":\\"Tibet\\"}}", "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Polygon\\",\\"coordinates\\":[[[78.3955448, 26.8548157], [99.116241, 26.8548157], [99.116241, 36.4833345], [78.3955448, 36.4833345], [78.3955448, 26.8548157]]]},\\"bbox\\":[78.3955448,26.8548157,99.116241,36.4833345]}"], "coordinates"=>["91.117212 29.646923", "78.3955448 26.8548157 99.116241 36.4833345"], "score"=>0.016135123}, {"published_display"=>["Dharamsala, Distt. Kangra, H.P."], "pub_date"=>["2007"], "format"=>"Book", "title_display"=>"Ses yon", "material_type_display"=>["xii, 419 p."], "id"=>"2008308478", "subject_geo_facet"=>["China", "Tibet", "India"], "subject_topic_facet"=>["Education and state", "Tibetans", "Tibetan language", "Teaching"], "language_facet"=>["Tibetan"], "geojson"=>["{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[104.195397,35.86166]},\\"properties\\":{\\"placename\\":\\"China\\"}}", "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[91.117212,29.646923]},\\"properties\\":{\\"placename\\":\\"Tibet\\"}}", "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[78.96288,20.593684]},\\"properties\\":{\\"placename\\":\\"India\\"}}","{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Polygon\\",\\"coordinates\\":[[[68.162386, 6.7535159], [97.395555, 6.7535159], [97.395555, 35.5044752], [68.162386, 35.5044752], [68.162386, 6.7535159]]]},\\"bbox\\":[68.162386,6.7535159,97.395555,35.5044752]}"], "coordinates"=>["68.162386 6.7535159 97.395555 35.5044752", "104.195397 35.86166", "91.117212 29.646923", "78.96288 20.593684"], "score"=>0.0026767207}]}, "facet_counts"=>{"facet_queries"=>{}, "facet_fields"=>{"format"=>["Book", 2], "lc_1letter_facet"=>["D - World History", 1], "lc_alpha_facet"=>["DS", 1], "lc_b4cutter_facet"=>["DS785", 1], "language_facet"=>["Tibetan", 2], "pub_date"=>["2005", 1, "2007", 1], "subject_era_facet"=>[], "subject_geo_facet"=>["China", 1, "India", 1, "Tibet", 1, "Tibet (China)", 1], "coordinates"=>["91.117212 29.646923", 2, "78.3955448 26.8548157 99.116241 36.4833345", 1, "68.162386 6.7535159 97.395555 35.5044752", 1, "104.195397 35.86166", 1, "78.96288 20.593684", 1], "geojson"=>["{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[91.117212, 29.646923]},\\"properties\\":{\\"placename\\":\\"Tibet\\"}}", 2, "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Polygon\\",\\"coordinates\\":[[[78.3955448, 26.8548157], [99.116241, 26.8548157], [99.116241, 36.4833345], [78.3955448, 36.4833345], [78.3955448, 26.8548157]]]},\\"bbox\\":[78.3955448,26.8548157,99.116241,36.4833345]}", 1, "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[104.195397,35.86166]},\\"properties\\":{\\"placename\\":\\"China\\"}}", 1, "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Point\\",\\"coordinates\\":[78.96288,20.593684]},\\"properties\\":{\\"placename\\":\\"India\\"}}", 1, "{\\"type\\":\\"Feature\\",\\"geometry\\":{\\"type\\":\\"Polygon\\",\\"coordinates\\":[[[68.162386, 6.7535159], [97.395555, 6.7535159], [97.395555, 35.5044752], [68.162386, 35.5044752], [68.162386, 6.7535159]]]},\\"bbox\\":[68.162386,6.7535159,97.395555,35.5044752]}", 1], "subject_topic_facet"=>["Education and state", 1, "Teaching", 1, "Tibetan language", 1, "Tibetans", 1]}, "facet_dates"=>{}, "facet_ranges"=>{}}, "spellcheck"=>{"suggestions"=>["tibet", {"numFound"=>1, "startOffset"=>0, "endOffset"=>5, "origFreq"=>2, "suggestion"=>[{"word"=>"tibetan", "freq"=>6}]}, "correctlySpelled", true]}})
end
Expand Down

0 comments on commit 4454373

Please sign in to comment.