Skip to content
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 2 commits into from Mar 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
237 changes: 122 additions & 115 deletions app/assets/javascripts/blacklight-maps/blacklight-maps-browse.js
@@ -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 &copy; <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];
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added issue #13

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 &copy; <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 ));
56 changes: 28 additions & 28 deletions app/helpers/blacklight_maps_helper.rb
@@ -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
4 changes: 2 additions & 2 deletions app/views/catalog/_document_maps.html.erb
Expand Up @@ -2,5 +2,5 @@
<div id="documents" class="map">
<%= show_map_div() %>
<div id="blacklight-map-sidebar"></div>
<%= javascript_tag "var geojson_docs = #{serialize_geojson}" %>
</div>
<%= javascript_tag "$('#blacklight-map').blacklight_leaflet_map(#{serialize_geojson});" %>
</div>