From d83a9a941c2d20b58e50548cc5fbec7538d755c4 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 27 Jun 2024 03:01:39 +0300 Subject: [PATCH 1/4] Add empty account home page --- app/abilities/ability.rb | 2 +- app/assets/javascripts/index.js | 4 +++- app/assets/javascripts/index/home.js | 9 +++++++++ app/controllers/accounts/homes_controller.rb | 13 +++++++++++++ app/views/accounts/homes/show.html.erb | 1 + config/routes.rb | 1 + 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/index/home.js create mode 100644 app/controllers/accounts/homes_controller.rb create mode 100644 app/views/accounts/homes/show.html.erb diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index 9516a30126..30346d39d0 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -30,7 +30,7 @@ def initialize(user) if user&.active? can :welcome, :site - can :read, :deletion + can :read, [:deletion, :account_home] if Settings.status != "database_offline" can [:subscribe, :unsubscribe], Changeset diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 56495b31a3..65c620882a 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 //= require qs/dist/qs @@ -368,7 +369,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 0000000000..da0538d8aa --- /dev/null +++ b/app/assets/javascripts/index/home.js @@ -0,0 +1,9 @@ +OSM.Home = function (map) { + var page = {}; + + page.pushstate = page.popstate = page.load = function () { + map.setSidebarOverlaid(true); + }; + + return page; +}; diff --git a/app/controllers/accounts/homes_controller.rb b/app/controllers/accounts/homes_controller.rb new file mode 100644 index 0000000000..e31cce7461 --- /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 0000000000..ea6ee70884 --- /dev/null +++ b/app/views/accounts/homes/show.html.erb @@ -0,0 +1 @@ +<% content_for(:content_class) { "overlay-sidebar" } %> diff --git a/config/routes.rb b/config/routes.rb index 9cb3a63edc..f826bd112e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -280,6 +280,7 @@ scope :module => :accounts do resource :terms, :only => [:show, :update] resource :deletion, :only => :show + resource :home, :only => :show end end From 27e7be4ea5032618e50d37376f7d4ecb6d2c061d Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 27 Jun 2024 03:26:35 +0300 Subject: [PATCH 2/4] Show/hide marker in account home js controller --- app/assets/javascripts/index/home.js | 20 ++++++++++++++++++++ config/locales/en.yml | 2 ++ 2 files changed, 22 insertions(+) diff --git a/app/assets/javascripts/index/home.js b/app/assets/javascripts/index/home.js index da0538d8aa..0b5c09bc01 100644 --- a/app/assets/javascripts/index/home.js +++ b/app/assets/javascripts/index/home.js @@ -1,8 +1,28 @@ OSM.Home = function (map) { + var marker; + + function clearMarker() { + if (marker) map.removeLayer(marker); + marker = null; + } + var page = {}; page.pushstate = page.popstate = page.load = function () { map.setSidebarOverlaid(true); + clearMarker(); + + 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); + }; + + page.unload = function () { + clearMarker(); }; return page; diff --git a/config/locales/en.yml b/config/locales/en.yml index 32b0bc6bcc..f6f5563148 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3283,6 +3283,8 @@ en: show_address: Show address query_features: Query features centre_map: Centre map here + home: + marker_title: My home location redactions: edit: heading: "Edit Redaction" From 20ee51b894e603d5d98a2dbc5b48a180e45ea5e1 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 27 Jun 2024 03:37:13 +0300 Subject: [PATCH 3/4] Add "Go to Home Location" to every page, link it to account home page --- app/assets/javascripts/index.js | 10 ------- app/views/layouts/_header.html.erb | 4 ++- app/views/layouts/map.html.erb | 12 -------- test/system/account_home_test.rb | 48 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 test/system/account_home_test.rb diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 65c620882a..efdef7891d 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -227,16 +227,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, diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 45e23fc2b9..592787316d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -104,7 +104,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 e17ea4ed82..72f6076b45 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/test/system/account_home_test.rb b/test/system/account_home_test.rb new file mode 100644 index 0000000000..0d0e10615a --- /dev/null +++ b/test/system/account_home_test.rb @@ -0,0 +1,48 @@ +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 +end From db48062ce112386ed7a6bfcedede2e397e78db7b Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 27 Jun 2024 04:02:38 +0300 Subject: [PATCH 4/4] Show alert on account home page if home location is not set --- app/assets/javascripts/index/home.js | 23 ++++++++++++++++------- config/locales/en.yml | 1 + test/system/account_home_test.rb | 9 +++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/index/home.js b/app/assets/javascripts/index/home.js index 0b5c09bc01..daed04e982 100644 --- a/app/assets/javascripts/index/home.js +++ b/app/assets/javascripts/index/home.js @@ -12,17 +12,26 @@ OSM.Home = function (map) { map.setSidebarOverlaid(true); clearMarker(); - 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); + 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/config/locales/en.yml b/config/locales/en.yml index f6f5563148..7ee8393867 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3285,6 +3285,7 @@ en: 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/test/system/account_home_test.rb b/test/system/account_home_test.rb index 0d0e10615a..813c45ec8d 100644 --- a/test/system/account_home_test.rb +++ b/test/system/account_home_test.rb @@ -45,4 +45,13 @@ class AccountHomeTest < ApplicationSystemTestCase 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