Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/4149'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Aug 12, 2023
2 parents d62e9a0 + 65a4a95 commit 491a612
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 24 deletions.
115 changes: 102 additions & 13 deletions app/assets/javascripts/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//= require leaflet.locatecontrol/src/L.Control.Locate

$(document).ready(function () {
var defaultHomeZoom = 12;
var map, marker, deleted_lat, deleted_lon;

if ($("#map").length) {
var map = L.map("map", {
map = L.map("map", {
attributionControl: false,
zoomControl: false
}).addLayer(new L.OSM.Mapnik());
Expand Down Expand Up @@ -35,32 +38,83 @@ $(document).ready(function () {
.addClass("control-button");

if (OSM.home) {
map.setView([OSM.home.lat, OSM.home.lon], 12);
map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
} else {
map.setView([0, 0], 0);
}

if ($("#map").hasClass("set_location")) {
var marker = L.marker([0, 0], { icon: OSM.getUserIcon() });
marker = L.marker([0, 0], {
icon: OSM.getUserIcon(),
keyboard: false,
interactive: false
});

if (OSM.home) {
marker.setLatLng([OSM.home.lat, OSM.home.lon]);
marker.addTo(map);
}

map.on("click", function (e) {
if ($("#updatehome").is(":checked")) {
var zoom = map.getZoom(),
precision = OSM.zoomPrecision(zoom),
location = e.latlng.wrap();
if (!$("#updatehome").is(":checked")) return;

var zoom = map.getZoom(),
precision = OSM.zoomPrecision(zoom),
location = e.latlng.wrap();

$("#home_lat").val(location.lat.toFixed(precision));
$("#home_lon").val(location.lng.toFixed(precision));

deleted_lat = null;
deleted_lon = null;
respondToHomeUpdate();
}).on("moveend", function () {
var lat = $("#home_lat").val().trim(),
lon = $("#home_lon").val().trim(),
location;

try {
if (lat && lon) {
location = L.latLng(lat, lon);
}
} catch (error) {
// keep location undefined
}

$("#home_message").hide();
$("#home_lat").val(location.lat.toFixed(precision));
$("#home_lon").val(location.lng.toFixed(precision));
$("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
});

marker.setLatLng(e.latlng);
marker.addTo(map);
}
$("#home_lat, #home_lon").on("input", function () {
deleted_lat = null;
deleted_lon = null;
respondToHomeUpdate();
});

$("#home_show").click(function () {
var lat = $("#home_lat").val(),
lon = $("#home_lon").val();

map.setView([lat, lon], defaultHomeZoom);
});

$("#home_delete").click(function () {
var lat = $("#home_lat").val(),
lon = $("#home_lon").val();

$("#home_lat, #home_lon").val("");
deleted_lat = lat;
deleted_lon = lon;
respondToHomeUpdate();
$("#home_undelete").trigger("focus");
});

$("#home_undelete").click(function () {
$("#home_lat").val(deleted_lat);
$("#home_lon").val(deleted_lon);
deleted_lat = null;
deleted_lon = null;
respondToHomeUpdate();
$("#home_delete").trigger("focus");
});
} else {
$("[data-user]").each(function () {
Expand All @@ -73,6 +127,41 @@ $(document).ready(function () {
}
}

function respondToHomeUpdate() {
var lat = $("#home_lat").val().trim(),
lon = $("#home_lon").val().trim(),
location;

try {
if (lat && lon) {
location = L.latLng(lat, lon);
}
$("#home_lat, #home_lon").removeClass("is-invalid");
} catch (error) {
if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
}

$("#home_message").toggleClass("invisible", Boolean(location));
$("#home_show").prop("hidden", !location);
$("#home_delete").prop("hidden", !location);
$("#home_undelete").prop("hidden", !(!location && deleted_lat && deleted_lon));
if (location) {
marker.setLatLng([lat, lon]);
marker.addTo(map);
map.panTo([lat, lon]);
} else {
marker.removeFrom(map);
}
}

function isCloseEnoughToMapCenter(location) {
var inputPt = map.latLngToContainerPoint(location),
centerPt = map.latLngToContainerPoint(map.getCenter());

return centerPt.distanceTo(inputPt) < 10;
}

function updateAuthUID() {
var provider = $("select#user_auth_provider").val();

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def set_map_location
@lon = @diary_entry.longitude
@lat = @diary_entry.latitude
@zoom = 12
elsif current_user.home_lat.nil? || current_user.home_lon.nil?
elsif !current_user.has_home?
@lon = params[:lon] || -0.1
@lat = params[:lat] || 51.5
@zoom = params[:zoom] || 4
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def application_data
if current_user
data[:user] = current_user.id.to_json

data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } unless current_user.home_lon.nil? || current_user.home_lat.nil?
data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.has_home?
end

data[:location] = session[:location] if session[:location]
Expand Down
8 changes: 6 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ def preferred_languages
@preferred_languages ||= Locale.list(languages)
end

def has_home?
home_lat && home_lon
end

def nearby(radius = Settings.nearby_radius, num = Settings.nearby_users)
if home_lon && home_lat
if has_home?
gc = OSM::GreatCircle.new(home_lat, home_lon)
sql_for_area = QuadTile.sql_for_area(gc.bounds(radius), "home_")
sql_for_distance = gc.sql_for_distance("home_lat", "home_lon")
Expand Down Expand Up @@ -401,6 +405,6 @@ def encrypt_password
end

def update_tile
self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if home_lat && home_lon
self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if has_home?
end
end
2 changes: 1 addition & 1 deletion app/views/api/users/_user.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ json.user do
end

if current_user && current_user == user && can?(:details, User)
if user.home_lat && user.home_lon
if user.has_home?
json.home do
json.lat user.home_lat
json.lon user.home_lon
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/users/_user.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ xml.tag! "user", :id => user.id,
end
end
if current_user && current_user == user && can?(:details, User)
if user.home_lat && user.home_lon
if user.has_home?
xml.tag! "home", :lat => user.home_lat,
:lon => user.home_lon,
:zoom => user.home_zoom
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboards/_contact.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="col">
<p class='text-muted mb-0'>
<%= link_to contact.display_name, user_path(contact) %>
<% if @user.home_lon and @user.home_lat and contact.home_lon and contact.home_lat %>
<% if @user.has_home? and contact.has_home? %>
<% distance = @user.distance(contact) %>
<% if distance < 1 %>
(<%= t ".m away", :count => (distance * 1000).round %>)
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<% if current_user and @user.id == current_user.id %>
<div class="col-md order-md-last">
<% if @user.home_lat.nil? or @user.home_lon.nil? %>
<% if !@user.has_home? %>
<div id="map" class="content_map border border-grey">
<p class="m-3"><%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), edit_profile_path)) %></p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/map.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<% content_for(:body_class) { "map-layout" } %>
<% if current_user and !current_user.home_lon.nil? and !current_user.home_lat.nil? %>
<% if current_user&.has_home? %>
<% content_for :greeting do %>
<%= link_to t("layouts.home"),
"#",
Expand Down
9 changes: 7 additions & 2 deletions app/views/profiles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@

<fieldset>
<legend><%= t ".home location" -%></legend>
<p id="home_message" class="text-muted"<% if current_user.home_lat and current_user.home_lon %> hidden<% end %>><%= t ".no home location" %></p>
<p id="home_message" class="text-muted m-0<% if current_user.has_home? %> invisible<% end %>"><%= t ".no home location" %></p>
<div class="row">
<%= f.text_field :home_lat, :wrapper_class => "col-sm-4", :id => "home_lat" %>
<%= f.text_field :home_lon, :wrapper_class => "col-sm-4", :id => "home_lon" %>
<div class="col-sm-4 pt-2 align-self-end">
<button type="button" id="home_show" class="btn btn-outline-primary"<% unless current_user.has_home? %> hidden<% end %> disabled><%= t ".show" %></button>
<button type="button" id="home_delete" class="btn btn-outline-primary"<% unless current_user.has_home? %> hidden<% end %>><%= t ".delete" %></button>
<button type="button" id="home_undelete" class="btn btn-outline-primary" hidden><%= t ".undelete" %></button>
</div>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.home_lat and current_user.home_lon %> checked="checked" <% end %> id="updatehome" />
<input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.has_home? %> checked <% end %> id="updatehome" />
<label class="form-check-label" for="updatehome"><%= t ".update home location on click" %></label>
</div>
<%= tag.div "", :id => "map", :class => "content_map set_location border border-grey rounded" %>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,9 @@ en:
home location: "Home Location"
no home location: "You have not entered your home location."
update home location on click: "Update home location when I click on the map?"
show: "Show"
delete: "Delete"
undelete: "Undo delete"
update:
success: Profile updated.
failure: Couldn't update profile.
Expand Down

0 comments on commit 491a612

Please sign in to comment.