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

Create /map/ route for wiki pages #7061

Merged
merged 25 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d1a6590
add route for map/wiki/:id and display wiki location
nstjean Dec 30, 2019
8ceccca
add error messages for no location, no wiki
nstjean Dec 30, 2019
c276774
fix codeclimate error
nstjean Dec 30, 2019
e6ef6be
display url hash on page load
nstjean Dec 31, 2019
578597e
Add buttons on wiki pages to link to map or prompt location entry
nstjean Dec 31, 2019
8c0cd7b
add redirect from /maps/ to /map/
nstjean Dec 31, 2019
97bfba0
change url hash format to #zoom/lat/lon
nstjean Dec 31, 2019
a3798d9
fix codeclimate error
nstjean Dec 31, 2019
156f243
change button on wiki and page to new hash format
nstjean Dec 31, 2019
4007f36
change PublicPagesTest test from /maps to /map
nstjean Dec 31, 2019
f41b7e4
clean up code
nstjean Jan 1, 2020
6d8f8f5
remove center tag and unnecessary javascript
nstjean Jan 3, 2020
03e79ff
add map tests to system tests
nstjean Jan 3, 2020
bbf6d9c
include 0 in testing numbers
nstjean Jan 3, 2020
4b9f645
increase capybara default max wait time
nstjean Jan 6, 2020
17801ed
change test tag contributors to a fixed tag name to stop error
nstjean Jan 6, 2020
13a3100
remove two tests to check if it passes on server
nstjean Jan 7, 2020
10a4ceb
add map controller tests
nstjean Jan 7, 2020
034044a
attempt to fix travis error
nstjean Jan 7, 2020
ed4b780
comment out all system map tests
nstjean Jan 7, 2020
c2829a6
format assertion in show map by hash location in a different way
nstjean Jan 7, 2020
2a1c3c9
hide show map by hash location, show correct url for wiki map
nstjean Jan 7, 2020
9fbe561
Increase wait time for map system tests
nstjean Jan 10, 2020
498d826
update yarn.lock
nstjean Jan 10, 2020
e7a54a3
set max wait time to 90
nstjean Jan 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@
//= require keybindings.js
//= require jquery-validation/dist/jquery.validate.js
//= require validation.js
//= require submit_form_ajax.js
//= require submit_form_ajax.js
//= require urlMapHash.js
53 changes: 53 additions & 0 deletions app/assets/javascripts/urlMapHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function urlMapHash() {
// This is based off of jywarren's urlhash, made specific to our map hash needs

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey this is cool, can you tell what specific changes you did to match plots2? Is the URL different now? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the format to be #zoom/lat/lon. I really like having a hash function available to get and set the values!

Copy link
Contributor

Choose a reason for hiding this comment

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

Quick question, have the layer names been removed from the hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently yes they aren't in the hash. It had been set up to use the route for the layer names, which had to be changed. Do you need them added back in? It's easy enough to add them at the end of the hash, if that works for @jywarren ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, that's fine for now. I was trying to figure out an issue when I noticed we don't have them anymore. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

We had them to track active layers. In LEL it is used in the embed to preserve the state of the layers. I don't remember if this was the case in plots2? @sagarpreet-chadha, could you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey,
So i made embed feature to take array of layers as input, and show them on map by default (@nstjean ...we will use this in /people map in plots2). This feature is unrelated to url hash AFAIK.

If we will require layer names in future, it should be pretty easy, right? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. Got it. To correct what I said earlier, it wasn't in the hash but before it. Thanks, @sagarpreet-chadha!

const paramArray = ["zoom", "lat", "lon"];

function getUrlHashParameter(param) {

var params = getUrlHashParameters();
return params[param];

}

function getUrlHashParameters() {

var sPageURL = window.location.hash;
if (sPageURL) sPageURL = sPageURL.split('#')[1];
var items = sPageURL.split('/');
var object = {};
items.forEach(function(item, i) {
if ((item !== '') && (paramArray[i])) object[paramArray[i]] = item;
});
return object;
}

// accepts an object like { paramName: value, paramName1: value }
// and transforms to: url.com#zoom/lat/lon
function setUrlHashParameters(params) {

var values = [];
paramArray.forEach(function(key, i) {
values.push(params[key]);
});
var hash = values.join('/');
window.location.hash = hash;

}

function setUrlHashParameter(param, value) {

var params = getUrlHashParameters();
params[param] = value;
setUrlHashParameters(params);

}

return {
getUrlHashParameter: getUrlHashParameter,
getUrlHashParameters: getUrlHashParameters,
setUrlHashParameter: setUrlHashParameter,
setUrlHashParameters: setUrlHashParameters
}

}
16 changes: 16 additions & 0 deletions app/assets/stylesheets/location.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@
}
#map_content .form-group .btn {
margin-top: .4rem;
}

a:not([href]):not([tabindex]).btn-location,
.btn-location {
position: inline;
color:#666;
}
a:not([href]):not([tabindex]).btn-outline-secondary.btn-location .fa,
a.btn-outline-secondary.btn-location .fa {
color: #c40;
}
a:not([href]):not([tabindex]).btn-outline-secondary.btn-location:hover,
a:not([href]):not([tabindex]).btn-outline-secondary.btn-location:hover i,
.btn-outline-secondary.btn-location:hover,
.btn-outline-secondary.btn-location:hover i {
color: white;
}
27 changes: 23 additions & 4 deletions app/controllers/map_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,31 @@ def index
end

def map
@layersname = params[:layersname]
@lat = 0
@lon = 0
@zoom = 3

return if current_user&.has_power_tag("lat").blank? || current_user&.has_power_tag("lon").blank?
if current_user&.has_power_tag("lat") && current_user&.has_power_tag("lon")
@lat = current_user.get_value_of_power_tag("lat").to_f
@lon = current_user.get_value_of_power_tag("lon").to_f
end
@zoom = current_user.get_value_of_power_tag("zoom").to_f if current_user&.has_power_tag("zoom")
end

def wiki
@node = Node.find_wiki(params[:id])

if @node.blank? || @node.has_power_tag("lat").blank? || @node.has_power_tag("lon").blank?
flash[:warning] = @node.blank? ? "Wiki page not found." : "No location found for wiki page."
redirect_to controller: 'map', action: 'map'
return
end

@lat = @node.power_tag("lat").to_f
@lon = @node.power_tag("lon").to_f
@zoom = @node.has_power_tag("zoom") ? @node.power_tag("zoom").to_f : 6

@user_lat = current_user.get_value_of_power_tag("lat").to_f
@user_lon = current_user.get_value_of_power_tag("lon").to_f
render :map
end

def show
Expand Down
56 changes: 32 additions & 24 deletions app/views/map/map.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%= javascript_include_tag('/lib/urlhash/urlHash.js') %>
<%= render :partial => "map/mapDependencies" %>

<style>
Expand All @@ -21,53 +20,63 @@
height: 80vh;
background:rgba(255,255,255,0.5);
}
.alert {
z-index: 600;
}
</style>

<div id="map"></div>
<div id="map"></div>

<script>

var urlHash = urlHash();
var urlHash = urlMapHash();

// if there are location params in URL let them override, otherwise save lat/lon/zoom from controller
if (!urlHash.getUrlHashParameter('lat') || !urlHash.getUrlHashParameter('lon')) {
<% if @lat && @lon %>
urlHash.setUrlHashParameter('lat', <%= @lat %> + "");
urlHash.setUrlHashParameter('lon', <%= @lon %> + "");
<% if @zoom %>
urlHash.setUrlHashParameter('zoom', <%= @zoom %> + "");
<% end %>
<% end %>
}

const params = urlHash.getUrlHashParameters();

var lat = parseFloat(params.lat);
var lon = parseFloat(params.lon);
var zoom = parseFloat(params.zoom);

const bounds = new L.LatLngBounds(
new L.LatLng(84.67351257, -172.96875),
new L.LatLng(-54.36775852, 178.59375)
);

var user_lat = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes awesome i wanted this to be removed from here and not the URLHash logic! Thanks!

var user_lon = null;
var profile_zoom = null;
<% if @user_lat && @user_lon %>
user_lat = "<%= @user_lat %>";
user_lon = "<%= @user_lon %>";
profile_zoom = 7;
<% end %>

var lat = params.lat || user_lat || 23;
var lon = params.lon || user_lon || 77;
var zoom = parseFloat(params.zoom) || profile_zoom || 3;
// These must be parsed after evaluating above or else 0 will not be displayed
lat = parseFloat(lat);
lon = parseFloat(lon);

var map = L.map("map", {
maxBounds: bounds,
maxBoundsViscosity: 0.75
}).setView([lat, lon], zoom);

updateLocationHash();
updateZoomHash();

map.options.minZoom = 3;

map.on('moveend', function() {
map.on('moveend', updateLocationHash);

map.on('zoomend', updateZoomHash);

function updateLocationHash() {
const center = map.getCenter();
urlHash.setUrlHashParameter('lat', center.lat + "");
urlHash.setUrlHashParameter('lon', center.lng + "");
})
}

map.on('zoomend', function() {
function updateZoomHash() {
const zoom = map.getZoom();
urlHash.setUrlHashParameter('zoom', zoom + "");
})
}

<% if @layersname.nil? %>
var layersname = ['odorreport', 'asian', 'clouds', 'eonetFiresLayer', 'Unearthing'];
Expand All @@ -90,5 +99,4 @@
embed: true,
}).addTo(map);


</script>
37 changes: 18 additions & 19 deletions app/views/sidebar/_related.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="col-lg-3 d-print-none">
<div class="sidebar-panel">
<div id="sidebar">
<div id="sidebar" class="mb-3">

<% cache('related_sidebar-feature', skip_digest: true) do %>
<%= feature('sidebar') %>
Expand Down Expand Up @@ -41,17 +41,22 @@
<% if @node && @node.has_power_tag('response') %>
<%= render partial: 'sidebar/notes', locals: { notes: @node.responses, title: I18n.t('sidebar._related.responses_to_note'), node: @node } %>
<% end %>
<% if @node && (@node.has_tag("place") || @node.has_power_tag("place")) && @node.lat && @node.lon %>
<%= render_top_map(@node.lat, @node.lon, @node.zoom) %>
<% elsif @node && @node.lat && @node.lon %>
<%= render_map(@node.lat, @node.lon, @node.zoom) %>
<% elsif [email protected] && [email protected] %>
<div id="map_template" style="position: relative; display: inline-block;">
<img src="https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/0/0/0.png" style="height:69px; width: 263px; position: relative; margin-right: -10px;">
<button class="btn btn-outline-secondary btn-lg" id="add_location" style="position: absolute; position: absolute;top: 19% ; left: 24% ;" type="button" name="button"><strong>Add location</strong></button>
</div>
<center><span>Learn about location <a href="https://publiclab.org/location-privacy">privacy</a></span></center>
<% end %>
<% if @node && @node.lat && @node.lon %>
<% if @node.has_tag("place") %>
<%= render_top_map(@node.lat, @node.lon, @node.zoom) %>
<% else %>
<%= render_map(@node.lat, @node.lon, @node.zoom) %>
<% end %>
<%# Link to map %>
<% zoom = @node.zoom ? @node.zoom : "" %>
<% url = "/map#" + zoom.to_s + "/" + @node.lat.to_s + "/" + @node.lon.to_s %>
<% if @node.type == "page" %>
<% url = "/map/" + @node.slug_from_path %>
<% end %>
<a href="<%= url %>" class="btn btn-outline-secondary btn-location mt-2"><i class="fa fa-map" aria-hidden="true"></i> View on a map</a>
<% elsif [email protected] && [email protected] %>
<a class="blurred-location-input btn btn-outline-secondary btn-location my-2"><i class="fa fa-map-marker" aria-hidden="true"></i> Add a location</a>
<% end %>

<br style="clear:both;" />

Expand All @@ -72,10 +77,4 @@
<% end %>
</div>
</div>
<%= javascript_include_tag 'sidebar' %>

<script type="text/javascript">
$('#add_location').on('click', function() {
$('.blurred-location-input').click();
})
</script>
<%= javascript_include_tag 'sidebar' %>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@


get 'map' => 'map#map'
get 'map/:layersname' => 'map#map'
get 'maps' => 'map#index'
get 'map/:id' => 'map#wiki'
get 'maps' => redirect('/map/')
get 'users/map' => 'users#map'
get 'maps/:id' => 'map#tag'
get 'map/edit/:id' => 'map#edit'
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/node_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,21 @@ project2:
tid: 28
uid: 1
nid: 7

test4_lat:
tid: 29
uid: 1
nid: 7
date: <%= DateTime.now.to_i %>

test4_lon:
tid: 30
uid: 1
nid: 7
date: <%= DateTime.now.to_i %>

test4_zoom:
tid: 31
uid: 1
nid: 7
date: <%= DateTime.now.to_i %>
12 changes: 12 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ place:
project:
tid: 28
name: project

test4_lat:
tid: 29
name: lat:41.87

test4_lon:
tid: 30
name: lon:-87.64

test4_zoom:
tid: 31
name: zoom:13
27 changes: 26 additions & 1 deletion test/fixtures/user_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,29 @@ notification_tag1:
notification_tag2:
id: 24
uid: 14
value: notifications:all
value: notifications:all

zoom:
id: 25
uid: 1
value: zoom:10

blurred:
id: 26
uid: 1
value: blurred:true

lat2:
id: 27
uid: 2
value: lat:59

lon2:
id: 28
uid: 2
value: lon:15

zoom2:
id: 29
uid: 2
value: zoom:10
Loading