New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactored js to function as a jquery plugin #10
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
237 changes: 122 additions & 115 deletions
237
app/assets/javascripts/blacklight-maps/blacklight-maps-browse.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,135 @@ | ||
var map, sidebar; | ||
|
||
Blacklight.onLoad(function() { | ||
|
||
// Stop doing stuff if the map div isn't there | ||
if ($("#blacklight-map").length === 0){ | ||
return; | ||
} | ||
|
||
// Get the configuration options from the data-attributes | ||
$.extend(Blacklight.mapOptions, $("#blacklight-map").data()); | ||
|
||
map = L.map('blacklight-map').setView([0,0], 2); | ||
L.tileLayer(Blacklight.mapOptions.tileurl, { | ||
attribution: Blacklight.mapOptions.mapattribution, | ||
maxZoom: Blacklight.mapOptions.maxzoom | ||
}).addTo(map); | ||
|
||
// Sets up leaflet-sidebar | ||
sidebar = L.control.sidebar('blacklight-map-sidebar', { | ||
position: 'right', | ||
autoPan: false | ||
}); | ||
|
||
// Adds leaflet-sidebar control to map (object) | ||
map.addControl(sidebar); | ||
|
||
// Create a marker cluster object and set options | ||
markers = new L.MarkerClusterGroup({ | ||
showCoverageOnHover: false, | ||
spiderfyOnMaxZoom: false, | ||
singleMarkerMode: true, | ||
animateAddingMarkers: true | ||
}); | ||
|
||
geoJsonLayer = L.geoJson(geojson_docs, { | ||
onEachFeature: function(feature, layer){ | ||
layer.defaultOptions.title = feature.properties.placename; | ||
layer.on('click', function(e){ | ||
hideSidebar(); | ||
var placenames = {}; | ||
placenames[feature.properties.placename] = [feature.properties.html]; | ||
offsetMap(e); | ||
$('#blacklight-map-sidebar').html(buildList(placenames)); | ||
sidebar.show(); | ||
;(function( $ ) { | ||
|
||
$.fn.blacklight_leaflet_map = function(geojson_docs, arg_opts) { | ||
var map, sidebar, markers, geoJsonLayer; | ||
|
||
// Configure default options and those passed via the constructor options | ||
var options = $.extend({ | ||
datatype : "placename_coordinates", | ||
tileurl : 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
mapattribution : 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>', | ||
sidebar: 'blacklight-map-sidebar' | ||
}, arg_opts ); | ||
|
||
// Extend options from data-attributes | ||
$.extend(options, this.data()); | ||
|
||
// Display the map | ||
this.each(function() { | ||
options.id = this.id; | ||
|
||
// Setup Leaflet map | ||
map = L.map(this.id).setView([0,0], 2); | ||
L.tileLayer(options.tileurl, { | ||
attribution: options.mapattribution, | ||
maxZoom: options.maxzoom | ||
}).addTo(map); | ||
|
||
// Initialize sidebar | ||
sidebar = L.control.sidebar(options.sidebar, { | ||
position: 'right', | ||
autoPan: false | ||
}); | ||
|
||
// Adds leaflet-sidebar control to map | ||
map.addControl(sidebar); | ||
|
||
// Create a marker cluster object and set options | ||
markers = new L.MarkerClusterGroup({ | ||
showCoverageOnHover: false, | ||
spiderfyOnMaxZoom: false, | ||
singleMarkerMode: true, | ||
animateAddingMarkers: true | ||
}); | ||
} | ||
}); | ||
|
||
// Add GeoJSON layer to marker cluster object | ||
markers.addLayer(geoJsonLayer); | ||
geoJsonLayer = L.geoJson(geojson_docs, { | ||
onEachFeature: function(feature, layer){ | ||
layer.defaultOptions.title = feature.properties.placename; | ||
layer.on('click', function(e){ | ||
var placenames = {}; | ||
placenames[feature.properties.placename] = [feature.properties.html]; | ||
setupSidebarDisplay(e,placenames); | ||
}); | ||
} | ||
}); | ||
|
||
// Add marker cluster object to map | ||
map.addLayer(markers); | ||
// Add GeoJSON layer to marker cluster object | ||
markers.addLayer(geoJsonLayer); | ||
|
||
// Listeners for marker cluster clicks | ||
markers.on('clusterclick', function(e){ | ||
|
||
//hide sidebar if it is visible | ||
hideSidebar(); | ||
// Add marker cluster object to map | ||
map.addLayer(markers); | ||
|
||
//if map is at the lowest zoom level | ||
if (map.getZoom() === Blacklight.mapOptions.maxzoom){ | ||
// Listeners for marker cluster clicks | ||
markers.on('clusterclick', function(e){ | ||
|
||
var placenames = generatePlacenamesObject(e.layer.getAllChildMarkers()); | ||
|
||
//if map is at the lowest zoom level | ||
if (map.getZoom() === options.maxzoom){ | ||
|
||
var placenames = generatePlacenamesObject(e.layer.getAllChildMarkers()); | ||
setupSidebarDisplay(e,placenames); | ||
} | ||
}); | ||
|
||
//Add click listener to map | ||
map.on('click drag', hideSidebar); | ||
|
||
}); | ||
|
||
function setupSidebarDisplay(e, placenames){ | ||
hideSidebar(); | ||
offsetMap(e); | ||
|
||
//Update sidebar div with new html | ||
$('#blacklight-map-sidebar').html(buildList(placenames)); | ||
// Update sidebar div with new html | ||
$('#' + options.sidebar).html(buildList(placenames)); | ||
|
||
//Show the sidebar! | ||
// Show the sidebar | ||
sidebar.show(); | ||
} | ||
}); | ||
|
||
//Add click listener to map | ||
map.on('click drag', hideSidebar); | ||
|
||
}); | ||
|
||
Blacklight.mapOptions = { | ||
tileurl : 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
mapattribution : 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' | ||
}; | ||
|
||
function hideSidebar(){ | ||
if (sidebar.isVisible()){ | ||
sidebar.hide(); | ||
} | ||
} | ||
|
||
function buildList(placenames){ | ||
var html = ""; | ||
$.each(placenames, function(i,val){ | ||
html += "<h2>" + i + "</h2>"; | ||
html += "<ul class='sidebar-list'>"; | ||
$.each(val, function(j, val2){ | ||
html += val2; | ||
}); | ||
html += "</ul>"; | ||
}); | ||
return html; | ||
} | ||
|
||
// Generates placenames object | ||
function generatePlacenamesObject(markers){ | ||
var placenames = {}; | ||
$.each(markers, function(i,val){ | ||
if (!(val.feature.properties.placename in placenames)){ | ||
placenames[val.feature.properties.placename] = []; | ||
|
||
// Hides sidebar if it is visible | ||
function hideSidebar(){ | ||
if (sidebar.isVisible()){ | ||
sidebar.hide(); | ||
} | ||
} | ||
|
||
// Build the list | ||
function buildList(placenames){ | ||
var html = ""; | ||
$.each(placenames, function(i,val){ | ||
html += "<h2>" + i + "</h2>"; | ||
html += "<ul class='sidebar-list'>"; | ||
$.each(val, function(j, val2){ | ||
html += val2; | ||
}); | ||
html += "</ul>"; | ||
}); | ||
return html; | ||
} | ||
placenames[val.feature.properties.placename].push(val.feature.properties.html); | ||
}); | ||
return placenames; | ||
} | ||
|
||
// Move the map so that it centers the clicked cluster TODO account for various size screens | ||
function offsetMap(e){ | ||
mapWidth = $('#blacklight-map').width(); | ||
mapHeight = $('#blacklight-map').height(); | ||
if (!e.latlng.equals(map.getCenter())){ | ||
map.panBy([(e.originalEvent.layerX - (mapWidth/4)), (e.originalEvent.layerY - (mapHeight/2))]); | ||
}else{ | ||
map.panBy([(mapWidth/4), 0]); | ||
} | ||
} | ||
|
||
// Generates placenames object | ||
function generatePlacenamesObject(markers){ | ||
var placenames = {}; | ||
$.each(markers, function(i,val){ | ||
if (!(val.feature.properties.placename in placenames)){ | ||
placenames[val.feature.properties.placename] = []; | ||
} | ||
placenames[val.feature.properties.placename].push(val.feature.properties.html); | ||
}); | ||
return placenames; | ||
} | ||
|
||
// Move the map so that it centers the clicked cluster TODO account for various size screens | ||
function offsetMap(e){ | ||
var mapWidth = $('#' + options.id).width(); | ||
var mapHeight = $('#' + options.id).height(); | ||
if (!e.latlng.equals(map.getCenter())){ | ||
map.panBy([(e.originalEvent.layerX - (mapWidth/4)), (e.originalEvent.layerY - (mapHeight/2))]); | ||
}else{ | ||
map.panBy([(mapWidth/4), 0]); | ||
} | ||
} | ||
|
||
}; | ||
|
||
}( jQuery )); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
# Helper methods used for Blacklight Maps | ||
module BlacklightMapsHelper | ||
|
||
def show_map_div | ||
data_attributes = { | ||
:maxzoom => blacklight_config.view.maps.maxzoom, | ||
:tileurl => blacklight_config.view.maps.tileurl | ||
|
||
} | ||
def show_map_div | ||
data_attributes = { | ||
maxzoom: blacklight_config.view.maps.maxzoom, | ||
tileurl: blacklight_config.view.maps.tileurl | ||
|
||
content_tag(:div, "", id: "blacklight-map", | ||
data: data_attributes | ||
) | ||
end | ||
} | ||
|
||
content_tag(:div, "", id: "blacklight-map", | ||
data: data_attributes | ||
) | ||
end | ||
|
||
def serialize_geojson | ||
geojson_docs = {type: "FeatureCollection", features: []} | ||
@response.docs.each_with_index do |doc, counter| | ||
if doc[blacklight_config.view.maps.placename_coord_field] | ||
doc[blacklight_config.view.maps.placename_coord_field].each do |loc| | ||
values = loc.split('|') | ||
feature = {type: "Feature", geometry: {type: "Point", | ||
coordinates: [values[2].to_f, values[1].to_f]}, | ||
properties: {placename: values[0], | ||
html: render_leaflet_sidebar_partial(doc)}} | ||
geojson_docs[:features].push feature | ||
end | ||
def serialize_geojson | ||
geojson_docs = {type: "FeatureCollection", features: []} | ||
@response.docs.each_with_index do |doc, counter| | ||
if doc[blacklight_config.view.maps.placename_coord_field] | ||
doc[blacklight_config.view.maps.placename_coord_field].each do |loc| | ||
values = loc.split('|') | ||
feature = {type: "Feature", geometry: {type: "Point", | ||
coordinates: [values[2].to_f, values[1].to_f]}, | ||
properties: {placename: values[0], | ||
html: render_leaflet_sidebar_partial(doc)}} | ||
geojson_docs[:features].push feature | ||
end | ||
end | ||
return geojson_docs.to_json | ||
end | ||
return geojson_docs.to_json | ||
end | ||
|
||
def render_leaflet_sidebar_partial(doc) | ||
render partial: 'catalog/index_maps', locals: {document: SolrDocument.new(doc)} | ||
end | ||
def render_leaflet_sidebar_partial(doc) | ||
render partial: 'catalog/index_maps', locals: {document: SolrDocument.new(doc)} | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't this and the
clusterclick
callback suspiciously similar? Think we can DRY it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added issue #13