From: Tom Hughes Date: Sat, 15 Feb 2025 17:07:59 +0000 (+0000) Subject: Merge remote-tracking branch 'upstream/pull/4926' X-Git-Tag: live~142 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/29d322c364dcbe5b1adf386d51132296b5809604?hp=3bdbc35bbd8f44bab813adb5c9afe505868e4a09 Merge remote-tracking branch 'upstream/pull/4926' --- diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index adedce543..dd377a727 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -29,7 +29,7 @@ class Ability if user&.active? can :welcome, :site - can :read, [:deletion, :account_terms, :account_pd_declaration] + can :read, [:deletion, :account_terms, :account_pd_declaration, :account_home] if Settings.status != "database_offline" can [:read, :create, :destroy], :changeset_subscription diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index a5916d3ed..810327e3f 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -21,6 +21,7 @@ //= require index/directions //= require index/changeset //= require index/query +//= require index/home //= require router $(document).ready(function () { @@ -221,16 +222,6 @@ $(document).ready(function () { L.marker([params.mlat, params.mlon]).addTo(map); } - $("#homeanchor").on("click", function (e) { - e.preventDefault(); - - var data = $(this).data(), - center = L.latLng(data.lat, data.lon); - - map.setView(center, data.zoom); - L.marker(center, { icon: OSM.getUserIcon() }).addTo(map); - }); - function remoteEditHandler(bbox, object) { var remoteEditHost = "http://127.0.0.1:8111", osmHost = location.protocol + "//" + location.host, @@ -365,7 +356,8 @@ $(document).ready(function () { "/relation/:id(/history)": OSM.Browse(map, "relation"), "/relation/:id/history/:version": OSM.OldBrowse(), "/changeset/:id": OSM.Changeset(map), - "/query": OSM.Query(map) + "/query": OSM.Query(map), + "/account/home": OSM.Home(map) }); if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") { diff --git a/app/assets/javascripts/index/home.js b/app/assets/javascripts/index/home.js new file mode 100644 index 000000000..7e297b724 --- /dev/null +++ b/app/assets/javascripts/index/home.js @@ -0,0 +1,38 @@ +OSM.Home = function (map) { + let marker; + + function clearMarker() { + if (marker) map.removeLayer(marker); + marker = null; + } + + const page = {}; + + page.pushstate = page.popstate = page.load = function () { + map.setSidebarOverlaid(true); + clearMarker(); + + if (OSM.home) { + OSM.router.withoutMoveListener(function () { + map.setView(OSM.home, 15, { reset: true }); + }); + marker = L.marker(OSM.home, { + icon: OSM.getUserIcon(), + title: I18n.t("javascripts.home.marker_title") + }).addTo(map); + } else { + $("#browse_status").html( + $("
").text( + I18n.t("javascripts.home.not_set") + ) + ); + } + }; + + page.unload = function () { + clearMarker(); + $("#browse_status").empty(); + }; + + return page; +}; diff --git a/app/controllers/accounts/homes_controller.rb b/app/controllers/accounts/homes_controller.rb new file mode 100644 index 000000000..e31cce746 --- /dev/null +++ b/app/controllers/accounts/homes_controller.rb @@ -0,0 +1,13 @@ +module Accounts + class HomesController < ApplicationController + layout :map_layout + + before_action :authorize_web + before_action :set_locale + before_action :require_oauth + + authorize_resource :class => :account_home + + def show; end + end +end diff --git a/app/views/accounts/homes/show.html.erb b/app/views/accounts/homes/show.html.erb new file mode 100644 index 000000000..ea6ee7088 --- /dev/null +++ b/app/views/accounts/homes/show.html.erb @@ -0,0 +1 @@ +<% content_for(:content_class) { "overlay-sidebar" } %> diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index aa5249381..1f5856908 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -88,7 +88,9 @@ <%= link_to t("users.show.my settings"), edit_account_path, :class => "dropdown-item" %> <%= link_to t("users.show.my_preferences"), preferences_path, :class => "dropdown-item" %> - <%= yield :greeting %> + <% if current_user.home_location? %> + <%= link_to t("layouts.home"), account_home_path, :class => "dropdown-item" %> + <% end %> <%= link_to t("layouts.logout"), logout_path(:referer => request.fullpath), :method => "post", :class => "geolink dropdown-item" %>
diff --git a/app/views/layouts/map.html.erb b/app/views/layouts/map.html.erb index e17ea4ed8..72f6076b4 100644 --- a/app/views/layouts/map.html.erb +++ b/app/views/layouts/map.html.erb @@ -4,18 +4,6 @@ <% content_for(:body_class) { "map-layout" } %> -<% if current_user&.home_location? %> - <% content_for :greeting do %> - <%= link_to t("layouts.home"), - "#", - :id => "homeanchor", - :class => "set_position dropdown-item", - :data => { :lat => current_user.home_lat, - :lon => current_user.home_lon, - :zoom => 15 } %> - <% end %> -<% end %> - <% content_for :header do %> <%= render :partial => "layouts/search", :locals => { :autofocus => false } %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b52bd137f..1f9792d39 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3317,6 +3317,9 @@ en: show_address: Show address query_features: Query features centre_map: Centre map here + home: + marker_title: My home location + not_set: Home location is not set for your account redactions: edit: heading: "Edit Redaction" diff --git a/config/routes.rb b/config/routes.rb index 7e9cd8dfd..b3fc5af15 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -300,6 +300,7 @@ OpenStreetMap::Application.routes.draw do resource :terms, :only => [:show, :update] resource :pd_declaration, :only => [:show, :create] resource :deletion, :only => :show + resource :home, :only => :show end end diff --git a/test/system/account_home_test.rb b/test/system/account_home_test.rb new file mode 100644 index 000000000..813c45ec8 --- /dev/null +++ b/test/system/account_home_test.rb @@ -0,0 +1,57 @@ +require "application_system_test_case" + +class AccountHomeTest < ApplicationSystemTestCase + test "Go to Home Location works on map layout pages" do + user = create(:user, :display_name => "test user", :home_lat => 60, :home_lon => 30) + sign_in_as(user) + + visit root_path + assert_no_selector "img.leaflet-marker-icon" + + click_on "test user" + click_on "Go to Home Location" + all "img.leaflet-marker-icon", :count => 1 do |marker| + assert_equal "My home location", marker["title"] + end + + click_on "OpenStreetMap logo" + assert_no_selector "img.leaflet-marker-icon" + end + + test "Go to Home Location works on non-map layout pages" do + user = create(:user, :display_name => "test user", :home_lat => 60, :home_lon => 30) + sign_in_as(user) + + visit about_path + assert_no_selector "img.leaflet-marker-icon" + + click_on "test user" + click_on "Go to Home Location" + all "img.leaflet-marker-icon", :count => 1 do |marker| + assert_equal "My home location", marker["title"] + end + + click_on "OpenStreetMap logo" + assert_no_selector "img.leaflet-marker-icon" + end + + test "Go to Home Location is not available for users without home location" do + user = create(:user, :display_name => "test user") + sign_in_as(user) + + visit root_path + assert_no_selector "img.leaflet-marker-icon" + + click_on "test user" + assert_no_link "Go to Home Location" + end + + test "account home page shows a warning when visited by users without home location" do + user = create(:user, :display_name => "test user") + sign_in_as(user) + + visit account_home_path + assert_no_selector "img.leaflet-marker-icon" + assert_text "Home location is not set" + end +end