Skip to content

Commit

Permalink
Maps cleanup to fix sidebar and inline map problems (publiclab#7404)
Browse files Browse the repository at this point in the history
* consolidate map code into setupLEL, remove setupInlineLEL

* clean up for codeclimate

* fix travis error
  • Loading branch information
nstjean authored and NitinBhasneia committed Feb 5, 2020
1 parent b4d5bc6 commit 2003d84
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 67 deletions.
54 changes: 25 additions & 29 deletions app/assets/javascripts/leaflet_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,56 +89,52 @@
});
}



function setupInlineLEL(map , layers, mainLayer, markers_hash) {

layers = layers.split(',');

L.tileLayer('https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png').addTo(map) ;
function setupLEL(map, markers_hash = null, params = {}) {
var options = {};
options.layers = params.layers || [];
options.setHash = params.setHash || false;
options.mainContent = params.mainContent || "";
options.displayLayers = params.displayLayers || false;

if (typeof options.layers === "string") {
options.layers = options.layers.split(',');
}

var oms = omsUtil(map, {
keepSpiderfied: true,
circleSpiralSwitchover: 0
});

L.LayerGroup.EnvironmentalLayers({
include: layers,
}).addTo(map);
var optionsLEL = {
addLayersToMap: options.displayLayers,
};
if (options.layers.length > 0) {
optionsLEL.include = options.layers;
}
L.LayerGroup.EnvironmentalLayers(optionsLEL).addTo(map);

displayMapContent(map, markers_hash, options.mainContent);
}

if(typeof mainLayer !== "undefined" && mainLayer !== ""){
if(mainLayer === "people"){
function displayMapContent(map, markers_hash, mainContent) {
if(typeof mainContent !== "undefined" && mainContent !== ""){
if(mainContent === "people"){
peopleMap();
map.on('zoomend', peopleMap);
map.on('moveend', peopleMap);
}
else {
mainLayer = (mainLayer === "content") ? null : mainLayer;
mainContent = (mainContent === "content") ? null : mainContent;
contentMap();
map.on('zoomend', contentMap);
map.on('moveend', contentMap);
}
}

function contentMap() {
contentLayerParser(map, markers_hash, mainLayer);
contentLayerParser(map, markers_hash, mainContent);
}
function peopleMap() {
peopleLayerParser(map, markers_hash);
}
}

function setupLEL(map , sethash){
L.tileLayer('https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map) ;

var oms = omsUtil(map, {
keepSpiderfied: true,
circleSpiralSwitchover: 0
});

L.LayerGroup.EnvironmentalLayers({
hash: !!sethash,
}).addTo(map);
}
10 changes: 8 additions & 2 deletions app/views/map/_inlineLeaflet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@

$("#iframeID<%= unique_id %>").click(function() {
prompt('Use this HTML code to embed this map on another site.', '<iframe style="border:none;" width="100%" height="900px" src="' + "//publiclab.org/map/" + primary_layers<%= unique_id %> + "#lat=<%= lat %>&lon=<%= lon %>&zoom=6" + '"></iframe>')
});
});

setupInlineLEL(map<%= unique_id %> , primary_layers<%= unique_id %>, main_layer<%= unique_id %>, markers_hash<%= unique_id %>) ;
var options<%= unique_id %> = {
mainContent: main_layer<%= unique_id %>,
layers: primary_layers<%= unique_id %>,
setHash: false,
displayLayers: true
}
setupLEL(map<%= unique_id %>, markers_hash<%= unique_id %>, options<%= unique_id %>);

</script>
2 changes: 1 addition & 1 deletion app/views/map/_leaflet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
var markers_hash<%= unique_id %> = new Map() ;
setupLEL(map<%= unique_id %> , 0) ;
setupLEL(map<%= unique_id %>, markers_hash<%= unique_id %>);
map<%= unique_id %>.on('zoomend' , function () {
var NWlat = map<%= unique_id %>.getBounds().getNorthWest().lat ;
Expand Down
3 changes: 3 additions & 0 deletions app/views/map/_mapDependencies.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
<%= javascript_include_tag('/lib/leaflet-environmental-layers/lib/glify.js') %>
<%= javascript_include_tag('/lib/leaflet-environmental-layers/lib/leaflet-fullUrlHash.js') %>
<%= javascript_include_tag('/lib/heatmap.js/build/heatmap.min.js') %>
<%= javascript_include_tag('/lib/leaflet-heatmap/leaflet-heatmap.js') %>
12 changes: 9 additions & 3 deletions app/views/map/_peopleLeaflet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@
});
}) ;

var setHash = 1;
<% if defined? url_hash != nil and url_hash == 0 %>
setupLEL(map<%= unique_id %> , 0) ;
<% else %>
setupLEL(map<%= unique_id %> , 1) ;
setHash = 0;
<% end %>

var options<%= unique_id %> = {
mainContent: "people",
setHash: setHash
}
setupLEL(map<%= unique_id %>, markers_hash<%= unique_id %>, options<%= unique_id %>);


</script>
15 changes: 8 additions & 7 deletions app/views/map/_plainInlineLeaflet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@

}, 3500);

setupLEL(map<%= unique_id %> , 0) ;
setupLEL(map<%= unique_id %>, markers_hash<%= unique_id %>);

var map_tagname<%= unique_id %> = null;
<% if (defined? tagname != nil and tagname.nil? == false) %>
map_tagname<%= unique_id %> = "<%= tagname %>" ;
<% end %>

map<%= unique_id %>.on('zoomend' , function () {
contentLayerParser(map<%= unique_id %>, markers_hash<%= unique_id %>, map_tagname<%= unique_id %>);
}) ;
// map<%= unique_id %>.on('zoomend' , function () {
// contentLayerParser(map<%= unique_id %>, markers_hash<%= unique_id %>, map_tagname<%= unique_id %>);
// }) ;

map<%= unique_id %>.on('moveend' , function () {
contentLayerParser(map<%= unique_id %>, markers_hash<%= unique_id %>, map_tagname<%= unique_id %>);
}) ;
// map<%= unique_id %>.on('moveend' , function () {
// contentLayerParser(map<%= unique_id %>, markers_hash<%= unique_id %>, map_tagname<%= unique_id %>);
// }) ;

</script>
8 changes: 7 additions & 1 deletion app/views/map/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@
map.setView(user_latlng , 3) ;
<% end %>

setupLEL(map , 1) ;
var markers_hash = new Map();
var options = {
mainContent: "people",
setHash: setHash
}
setupLEL(map, markers_hash, options);

setupFullScreen(map , map.getCenter().lat , map.getCenter().lng) ;
</script>

Expand Down
48 changes: 24 additions & 24 deletions app/views/map/map.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,29 @@
urlHash.setUrlHashParameter('zoom', zoom + "");
}

<% if @layersname.nil? %>
var layersname = ['odorreport', 'asian', 'clouds', 'eonetFiresLayer', 'Unearthing'];
<% else %>
var layersname = "<%= @layersname %>";
layersname = layersname.split(',')
<% end %>

var baseLayer = L.tileLayer(
"https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png",
{
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}
).addTo(map);


L.LayerGroup.EnvironmentalLayers({
// baseLayers: {
// 'Gray-scale': baselayer
// },
// include: layersname,
hash: false,
embed: true,
hostname: 'publiclab.org',
}).addTo(map);
// var baseLayer = L.tileLayer(
// "https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png",
// {
// attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
// }
// ).addTo(map);

var markers_hash = new Map();
var options = {
mainContent: "content",
setHash: false
}
setupLEL(map, markers_hash, options);


// L.LayerGroup.EnvironmentalLayers({
// // baseLayers: {
// // 'Gray-scale': baselayer
// // },
// // include: layersname,
// hash: false,
// embed: true,
// hostname: 'publiclab.org',
// }).addTo(map);

</script>

0 comments on commit 2003d84

Please sign in to comment.