Skip to content

Commit

Permalink
Inline map and show results count
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Feb 26, 2024
1 parent 2d38a98 commit 1c4e4f6
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 58 deletions.
10 changes: 7 additions & 3 deletions app/helpers/decidim/ideas/ideas_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def ideas_data_for_map(geocoded_ideas_data)
end
end

def ideas_map(geocoded_ideas)
def ideas_map(geocoded_ideas, **options)
map_options = { type: "ideas", markers: geocoded_ideas }
map_center = component_settings.default_map_center_coordinates
map_options[:center_coordinates] = map_center.split(",").map(&:to_f) if map_center

dynamic_map_for(map_options) do
dynamic_map_for(map_options.merge(options)) do
# These snippets need to be added AFTER the other map scripts have
# been added which is why they cannot be within the block. Otherwise
# e.g. the markercluser would not be available when the plans map is
Expand All @@ -41,7 +41,11 @@ def ideas_map(geocoded_ideas)
snippets.add(:foot, snippets.for(:plans_map_scripts))
end

yield
if block_given?
yield
else
""
end
end
end

Expand Down
41 changes: 34 additions & 7 deletions app/models/decidim/ideas/idea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,26 @@ def self.geocoded_data_for(component)
)
end

def self.area_scope_coordinates_for(component)
component.settings.area_scope_coordinates.to_h do |scope_id, coords|
latlng = coords.split(",")
next if latlng.length < 2

[
scope_id.to_s.to_i,
[latlng[0].to_f, latlng[1].to_f]
]
end.compact
end

def self.with_geocoded_area_scopes_for(component)
# Fetch all the configured area scope coodinates and create a select
# statement for each scope with its latitude and longitude in order to
# combine this information with the base query.
scope_selects = component.settings.area_scope_coordinates.map do |scope_id, coords|
latlng = coords.split(",")
next if latlng.length < 2
scope_selects = area_scope_coordinates_for(component).map do |scope_id, coords|
"SELECT #{scope_id} AS scope_id, #{coords[0]} AS latitude, #{coords[1]} AS longitude"
end

lat = latlng[0].to_f
lng = latlng[1].to_f
"SELECT #{scope_id} AS scope_id, #{lat} AS latitude, #{lng} AS longitude"
end.compact
# The empty select ensures there is always at least one row for the
# scope locations so that it will not break even when it is not
# configured correctly.
Expand Down Expand Up @@ -331,6 +339,25 @@ def within_edit_time_limit?
Time.current < limit
end

# Replicates the same data for a single idea as returned for a collection
# through the `#geocoded_data_for` method.
def geocoded_data
locale = I18n.locale.to_s
default_locale = I18n.default_locale.to_s

scope_coordinates = self.class.area_scope_coordinates_for(component)
idea_scope_coordinates = scope_coordinates[area_scope.id] || []

[
id,
title,
body,
address || area_scope.title[locale] || area_scope.title[default_locale],
latitude || idea_scope_coordinates[0],
longitude || idea_scope_coordinates[1]
]
end

# Create i18n ransackers for :title and :body.
# Create the :search_text ransacker alias for searching from both of these.
ransacker_text_multi :search_text, [:title, :body]
Expand Down
2 changes: 1 addition & 1 deletion app/views/decidim/ideas/ideas/_count.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p class="show-for-sr"><%= t(".ideas_count", count: @ideas.total_count) %></p>
<p class="lead"><%= t(".ideas_count", count: @ideas.total_count) %></p>
6 changes: 3 additions & 3 deletions app/views/decidim/ideas/ideas/_idea_actions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
</div>
<% end %>

<% if component_settings.geocoding_enabled? && idea.latitude.present? && idea.longitude.present? %>
<% if component_settings.geocoding_enabled? && idea.address.present? && idea.latitude.present? && idea.longitude.present? %>
<div class="actions-panel bg-secondary">
<%= link_to idea_map_link(idea), class: "action-link", target: "_blank", data: { external_link_spacer: "" } do %>
<a href="#idea_map" class="action-link">
<span class="link-text"><%= t ".show_on_map" %></span>
<%= icon "map-marker", role: "img", "aria-hidden": true %>
<% end %>
</a>
</div>
<% end %>

Expand Down
2 changes: 2 additions & 0 deletions app/views/decidim/ideas/ideas/_idea_full.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
</div>
<% end %>

<%= render partial: "idea_map", locals: { idea: idea } %>

<div class="resource__details margin-top-3">
<%= attachments_for idea %>

Expand Down
7 changes: 7 additions & 0 deletions app/views/decidim/ideas/ideas/_idea_map.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if component_settings.geocoding_enabled? && idea.address.present? && idea.latitude.present? && idea.longitude.present? %>
<div id="idea_map" class="margin-top-3">
<h2><%= t(".title") %></h2>

<%= ideas_map ideas_data_for_map([idea.geocoded_data]), type: "resource-markers" %>
</div>
<% end %>
21 changes: 21 additions & 0 deletions app/views/decidim/ideas/ideas/_map_template.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template id="marker-popup">
<div class="map-info__content">
<h3>${title}</h3>
<div id="bodyContent">
<p>{{html body}}</p>
<div class="map__date-adress">
<div class="address card__extra">
<div class="address__icon">{{html icon}}</div>
<div class="address__details">
<span>${address}</span><br>
</div>
</div>
</div>
<div class="map-info__button">
<a href="${link}" class="button button--sc">
<%= t(".view_idea") %>
</a>
</div>
</div>
</div>
</template>
49 changes: 17 additions & 32 deletions app/views/decidim/ideas/ideas/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,24 @@

<div class="tabs-content" data-tabs-content="listing-tabs">
<div class="tabs-panel is-active" id="ideas-listing">
<div class="collection-controls">
<div class="control">
<%= order_selector available_orders, i18n_scope: "decidim.ideas.ideas.orders" %>
<div class="collection-info row">
<div class="column">
<div id="ideas-count" aria-live="polite">
<%= render partial: "count" %>
</div>
</div>
<% if false %>
<div class="control">
<%= render partial: "decidim/shared/results_per_page" %>
<div class="column">
<div class="collection-controls">
<div class="control">
<%= order_selector available_orders, i18n_scope: "decidim.ideas.ideas.orders" %>
</div>
<% if false %>
<div class="control">
<%= render partial: "decidim/shared/results_per_page" %>
</div>
<% end %>
</div>
<% end %>
</div>

<div id="ideas-count">
<%= render partial: "count" %>
</div>
</div>

<div id="ideas">
Expand All @@ -84,27 +89,7 @@
<% if component_settings.geocoding_enabled? %>
<div class="tabs-panel" id="ideas-map">
<%= ideas_map ideas_data_for_map(@geocoded_ideas) do %>
<template id="marker-popup">
<div class="map-info__content">
<h3>${title}</h3>
<div id="bodyContent">
<p>{{html body}}</p>
<div class="map__date-adress">
<div class="address card__extra">
<div class="address__icon">{{html icon}}</div>
<div class="address__details">
<span>${address}</span><br>
</div>
</div>
</div>
<div class="map-info__button">
<a href="${link}" class="button button--sc">
<%= t(".view_idea") %>
</a>
</div>
</div>
</div>
</template>
<%= render partial: "map_template" %>
<% end %>
</div>
<% end %>
Expand Down
11 changes: 7 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ en:
title: Complete your idea
count:
ideas_count:
one: "%{count} idea"
other: "%{count} ideas"
one: "Found %{count} idea"
other: "Found %{count} ideas"
edit:
back: Back to previous page
send: Save
Expand Down Expand Up @@ -507,7 +507,6 @@ en:
show_list: Show results as list
show_map: Show results on map
subtitle: Search and discover ideas!
view_idea: View idea
map:
dynamic:
skip_button: Skip map
Expand Down Expand Up @@ -556,13 +555,17 @@ en:
edit_idea: Edit idea
show_versions: Show version history
subtitle: Idea
idea_map:
title: Idea on map
ideas:
empty: There are no ideas yet
empty_filters: There isn't any idea with this criteria
map_template:
view_idea: View idea
show:
answer: Answer
back_to: Back to
back_to_list: See other ideas
back_to_list: See all ideas
changes_at_title: Amendment to "%{title}"
feedback_title: Thank you for submitting an idea!
link_to_promoted_emendation_help_text: This idea is a promoted emendation
Expand Down
11 changes: 7 additions & 4 deletions config/locales/fi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ fi:
title: Viimeistele ideasi
count:
ideas_count:
one: "%{count} idea"
other: "%{count} ideaa"
one: "Löytyi %{count} idea"
other: "Löytyi %{count} ideaa"
edit:
back: Palaa edelliselle sivulle
send: Tallenna
Expand Down Expand Up @@ -507,7 +507,6 @@ fi:
show_list: Näytä tulokset listana
show_map: Näytä tulokset kartalla
subtitle: Etsi ja tutki ideoita!
view_idea: Näytä idea
map:
dynamic:
skip_button: Ohita kartta
Expand Down Expand Up @@ -556,13 +555,17 @@ fi:
edit_idea: Muokkaa ideaa
show_versions: Näytä versiohistoria
subtitle: Idea
idea_map:
title: Idea kartalla
ideas:
empty: Ei vielä ideoita
empty_filters: Ideoita ei löytynyt valituilla hakuehdoilla
map_template:
view_idea: Näytä idea
show:
answer: Vastaa
back_to: Takaisin
back_to_list: Näytä muut ideat
back_to_list: Näytä kaikki ideat
changes_at_title: Muutokset kohteessa "%{title}"
feedback_title: Kiitos idean jättämisestä!
link_to_promoted_emendation_help_text: Tämä idea on peräisin toisen ehdotuksen muutoksesta
Expand Down
11 changes: 7 additions & 4 deletions config/locales/sv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ sv:
title: Komplettera din idé
count:
ideas_count:
one: "%{count} idé"
other: "%{count} idéer"
one: "Hittade %{count} idé"
other: "Hittade %{count} idéer"
edit:
back: Tillbaka till föregående sida
send: Spara
Expand Down Expand Up @@ -507,7 +507,6 @@ sv:
show_list: Visa resultat som lista
show_map: Visa resultat på kartan
subtitle: Sök och utforska idéer!
view_idea: Visa idé
map:
dynamic:
skip_button: Hoppa över karta
Expand Down Expand Up @@ -556,13 +555,17 @@ sv:
edit_idea: Redigera idé
show_versions: Visa versionshistorik
subtitle: Idén
idea_map:
title: Idé på kartan
ideas:
empty: Det finns inga idéer ännu
empty_filters: Det finns ingen idé med dessa kriterier
map_template:
view_idea: Visa idé
show:
answer: Svara
back_to: Tillbaka till
back_to_list: Visa andra idéer
back_to_list: Visa alla idéer
changes_at_title: Ändring till %{title}
feedback_title: Tack för att du skickade en idé!
link_to_promoted_emendation_help_text: Denna idé är ett annonserat ändringsförslag
Expand Down

0 comments on commit 1c4e4f6

Please sign in to comment.