Skip to content

Commit

Permalink
Remove classic pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Sep 12, 2024
1 parent 1a20044 commit 454dd23
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 655 deletions.
69 changes: 60 additions & 9 deletions app/controllers/changesets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,33 @@ def show
@changeset = Changeset.find(params[:id])
case turbo_frame_request_id
when "changeset_nodes"
@node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "node_page")
render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages }
load_nodes

render :partial => "elements", :locals => { :type => "node", :paginator => @nodes_paginator }
when "changeset_ways"
@way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "way_page")
render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages }
load_ways

render :partial => "elements", :locals => { :type => "way", :paginator => @ways_paginator }
when "changeset_relations"
@relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "relation_page")
render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages }
load_relations

render :partial => "elements", :locals => { :type => "relation", :paginator => @relations_paginator }
else
@comments = if current_user&.moderator?
@changeset.comments.unscope(:where => :visible).includes(:author)
else
@changeset.comments.includes(:author)
end
@node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "node_page")
@way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "way_page")
@relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "relation_page")
load_nodes
load_ways
load_relations

if @changeset.user.active? && @changeset.user.data_public?
changesets = conditions_nonempty(@changeset.user.changesets)
@next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
@prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
end

render :layout => map_layout
end
rescue ActiveRecord::RecordNotFound
Expand Down Expand Up @@ -163,4 +168,50 @@ def conditions_bbox(changesets, bbox)
def conditions_nonempty(changesets)
changesets.where("num_changes > 0")
end

def load_nodes
nodes = OldNode.where(:changeset => @changeset)
@nodes_paginator = ElementsPaginator.new(nodes, 20, params[:node_page])
end

def load_ways
ways = OldWay.where(:changeset => @changeset)
@ways_paginator = ElementsPaginator.new(ways, 20, params[:way_page])
end

def load_relations
relations = OldRelation.where(:changeset => @changeset)
@relations_paginator = ElementsPaginator.new(relations, 20, params[:relation_page])
end

class ElementsPaginator
attr_reader :elements_count, :elements_per_page, :current_page, :current_page_elements

def initialize(elements, elements_per_page, current_page = 1)
@elements_count = elements.count
@elements_per_page = elements_per_page.to_i
@current_page = current_page.to_i.clamp(1, pages_count)
@current_page_elements = elements.offset(@elements_per_page * (@current_page - 1)).limit(@elements_per_page)
end

def pages_count
[1, 1 + ((elements_count - 1) / elements_per_page)].max
end

def pages
1..pages_count
end

def pages_window(window_size, page = current_page)
[page - window_size, pages.first].max..[page + window_size, pages.last].min
end

def lower_element_number(page = current_page)
(elements_per_page * (page - 1)) + 1
end

def upper_element_number(page = current_page)
[elements_per_page * page, elements_count].min
end
end
end
43 changes: 33 additions & 10 deletions app/helpers/browse_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,56 @@ def link_follow(object)
"nofollow" if object.tags.empty?
end

def type_and_paginated_count(type, pages, selected_page = pages.current_page)
if pages.page_count == 1
def type_and_paginated_count(type, paginator, page = paginator.current_page)
if paginator.pages_count <= 1
t ".#{type.pluralize}",
:count => pages.item_count
:count => paginator.elements_count
else
t ".#{type.pluralize}_paginated",
:x => selected_page.first_item,
:y => selected_page.last_item,
:count => pages.item_count
:x => paginator.lower_element_number(page),
:y => paginator.upper_element_number(page),
:count => paginator.elements_count
end
end

def sidebar_classic_pagination(pages, page_param)
def sidebar_classic_pagination(paginator, page_param)
window_size = 2
max_width_for_default_padding = 35

pages = paginator.pages
window = paginator.pages_window(window_size)
page_items = []

if window.first != pages.first
page_items.push [pages.first.to_s, pages.first]
page_items.push ["...", "disabled"] if window.first - pages.first > 1
end

window.each do |page|
if paginator.current_page == page
page_items.push [page.to_s, "active"]
else
page_items.push [page.to_s, page]
end
end

if window.last != pages.last
page_items.push ["...", "disabled"] if pages.last - window.last > 1
page_items.push [pages.last.to_s, pages.last]
end

width = 0
pagination_items(pages, {}).each do |body|
page_items.each do |body|
width += 2 # padding width
width += body.length
end
link_classes = ["page-link", { "px-1" => width > max_width_for_default_padding }]

tag.ul :class => "pagination pagination-sm mb-1 ms-auto" do
pagination_items(pages, {}).each do |body, page_or_class|
page_items.each do |body, page_or_class|
linked = !(page_or_class.is_a? String)
link = if linked
link_to body, url_for(page_param => page_or_class.number), :class => link_classes, **yield(page_or_class)
link_to body, url_for(page_param => page_or_class), :class => link_classes, **yield(page_or_class)
else
tag.span body, :class => link_classes
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/changesets/_elements.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= turbo_frame_tag "changeset_#{type.pluralize}" do %>
<%= render :partial => "paging_nav", :locals => { :type => type, :pages => pages } %>
<%= render :partial => "paging_nav", :locals => { :type => type, :paginator => paginator } %>
<ul class="list-unstyled" data-turbo="false">
<% elements.each do |element| %>
<% paginator.current_page_elements.each do |element| %>
<%= element_list_item type, element do
t "printable_name.current_and_old_links_html",
:current_link => link_to(printable_element_name(element), :controller => type.pluralize, :action => :show, :id => element.id[0]),
Expand Down
8 changes: 4 additions & 4 deletions app/views/changesets/_paging_nav.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="d-flex flex-wrap gap-2">
<h4 class="fs-5 mb-0"><%= type_and_paginated_count(type, pages) %></h4>
<% if pages.page_count > 1 %>
<%= sidebar_classic_pagination(pages, "#{type}_page") do |page|
<h4 class="fs-5 mb-0"><%= type_and_paginated_count(type, paginator) %></h4>
<% if paginator.pages_count > 1 %>
<%= sidebar_classic_pagination(paginator, "#{type}_page") do |page|
{
:title => type_and_paginated_count(type, pages, page),
:title => type_and_paginated_count(type, paginator, page),
:data => { :turbo => "true" }
}
end %>
Expand Down
12 changes: 6 additions & 6 deletions app/views/changesets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@
<% end %>
<% end %>
<% unless @ways.empty? %>
<%= render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages } %>
<% unless @ways_paginator.current_page_elements.empty? %>
<%= render :partial => "elements", :locals => { :type => "way", :paginator => @ways_paginator } %>
<% end %>
<% unless @relations.empty? %>
<%= render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages } %>
<% unless @relations_paginator.current_page_elements.empty? %>
<%= render :partial => "elements", :locals => { :type => "relation", :paginator => @relations_paginator } %>
<% end %>
<% unless @nodes.empty? %>
<%= render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages } %>
<% unless @nodes_paginator.current_page_elements.empty? %>
<%= render :partial => "elements", :locals => { :type => "node", :paginator => @nodes_paginator } %>
<% end %>
</div>

Expand Down
33 changes: 0 additions & 33 deletions config/initializers/classic_pagination.rb

This file was deleted.

Loading

0 comments on commit 454dd23

Please sign in to comment.