Skip to content

Commit

Permalink
remove viewpoint in favor of initialview
Browse files Browse the repository at this point in the history
  • Loading branch information
mejackreed committed Apr 28, 2015
1 parent 8ae1f62 commit dcd1f1c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 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
49 changes: 44 additions & 5 deletions app/assets/javascripts/blacklight-maps/blacklight-maps-browse.js
Expand Up @@ -26,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 @@ -95,11 +97,8 @@
// Add markers to map
map.addLayer(markers);

// Fit bounds of map based off of layers
map.fitBounds(markers.getBounds(), {
padding: [10, 10],
maxZoom: options.maxzoom
});
// Fit bounds of map
setMapBounds(map);

// create overlay for search control hover
var searchHoverLayer = L.rectangle([[0,0], [0,0]], {
Expand Down Expand Up @@ -145,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
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

0 comments on commit dcd1f1c

Please sign in to comment.