-
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: countless pagination (#1998)
* feature: countless pagination * i18n-tasks normalize * refactor * spec * fix paginator render condition * refactor * tweak * fix bug * uncomment spec * pagination_type refactor * extract apply_pagination from index * normal => default * fr translations by @jetienne
- Loading branch information
Showing
15 changed files
with
217 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,36 @@ | ||
<% | ||
per_page_options = [*Avo.configuration.per_page_steps, Avo.configuration.per_page.to_i, index_params[:per_page].to_i] | ||
|
||
if parent_model.present? | ||
per_page_options.prepend Avo.configuration.via_per_page | ||
end | ||
|
||
per_page_options = per_page_options.sort.uniq | ||
%> | ||
<% unless @pagy.pages <= 1 && discreet_pagination %> | ||
<div class="flex flex-col sm:flex-row items-center justify-between aborder-t aborder-slate-200 rounded-xl space-y-2 sm:space-y-0"> | ||
<div class="flex-2 sm:flex sm:items-center sm:justify-between"> | ||
<div> | ||
<div class="text-sm leading-5 text-slate-600 flex items-center"> | ||
<div data-controller="per-page"> | ||
<div class="flex items-center"> | ||
<%= select_tag 'per_page', | ||
options_for_select(per_page_options, index_params[:per_page]), | ||
class: 'appearance-none inline-flex bg-white-100 disabled:bg-white-400 disabled:cursor-not-allowed focus:bg-white text-slate-700 disabled:text-slate-600 rounded-md py-1 px-2 leading-tight border border-slate-300 outline-none focus:border-slate-400 outline w-16 mr-1 text-sm', | ||
data: { | ||
'turbo-frame': turbo_frame, | ||
'per-page-target': 'selector', | ||
action: 'change->per-page#reload' | ||
} | ||
%> <%= t('avo.per_page').downcase %> | ||
</div> | ||
<% per_page_options.each do |option| %> | ||
<%= link_to "Change to #{option} items per page", change_items_per_page_url(option), class: 'hidden', 'data-per-page-option': option, 'data-turbo-frame': turbo_frame %> | ||
<% end %> | ||
<div class="flex flex-col sm:flex-row items-center justify-between aborder-t aborder-slate-200 rounded-xl space-y-2 sm:space-y-0"> | ||
<div class="flex-2 sm:flex sm:items-center sm:justify-between"> | ||
<div> | ||
<div class="text-sm leading-5 text-slate-600 flex items-center"> | ||
<div data-controller="per-page"> | ||
<div class="flex items-center"> | ||
<%= select_tag 'per_page', | ||
options_for_select(per_page_options, index_params[:per_page]), | ||
class: 'appearance-none inline-flex bg-white-100 disabled:bg-white-400 disabled:cursor-not-allowed focus:bg-white text-slate-700 disabled:text-slate-600 rounded-md py-1 px-2 leading-tight border border-slate-300 outline-none focus:border-slate-400 outline w-16 mr-1 text-sm', | ||
data: { | ||
'turbo-frame': turbo_frame, | ||
'per-page-target': 'selector', | ||
action: 'change->per-page#reload' | ||
} | ||
%> <%= t('avo.per_page').downcase %> | ||
</div> | ||
<% per_page_options.each do |option| %> | ||
<%= link_to "Change to #{option} items per page", change_items_per_page_url(option), class: 'hidden', 'data-per-page-option': option, 'data-turbo-frame': turbo_frame %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="flex"> | ||
<div class="flex-2 sm:flex sm:items-center sm:justify-between space-y-2 sm:space-y-0 text-center sm:text-left"> | ||
<% if @pagy.count > 0 %> | ||
<div class="text-sm text-slate-600 mr-4"><%== helpers.pagy_info @pagy %></div> | ||
<% end %> | ||
</div> | ||
<div class="flex"> | ||
<div class="flex-2 sm:flex sm:items-center sm:justify-between space-y-2 sm:space-y-0 text-center sm:text-left"> | ||
<% if @resource.pagination_type.default? %> | ||
<div class="text-sm text-slate-600 mr-4"><%== helpers.pagy_info @pagy %></div> | ||
<% end %> | ||
<% if @pagy.pages > 1 %> | ||
<%# @todo: add first & last page. make the first and last buttons rounded %> | ||
<%== helpers.pagy_nav @pagy %> | ||
<% end %> | ||
</div> | ||
<% if pagy.pages > 1 %> | ||
<%# @todo: add first & last page. make the first and last buttons rounded %> | ||
<%== helpers.pagy_nav @pagy %> | ||
<% end %> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Avo | ||
module Concerns | ||
module Pagination | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Pagy::Backend | ||
|
||
class_attribute :pagination, default: {} | ||
|
||
PAGINATION_METHOD = { | ||
default: :pagy, | ||
countless: :pagy_countless, | ||
} unless defined? PAGINATION_METHOD | ||
|
||
PAGINATION_DEFAULTS = { | ||
type: :default, | ||
size: [1, 2, 2, 1], | ||
} unless defined? PAGINATION_DEFAULTS | ||
end | ||
|
||
def pagination_type | ||
@pagination_type ||= ActiveSupport::StringInquirer.new(pagination_hash[:type].to_s) | ||
end | ||
|
||
def apply_pagination(index_params:, query:) | ||
extra_pagy_params = {} | ||
|
||
# Reset open filters when a user navigates to a new page | ||
extra_pagy_params[:keep_filters_panel_open] = if params[:keep_filters_panel_open] == "1" | ||
"0" | ||
end | ||
|
||
send PAGINATION_METHOD[pagination_type.to_sym], | ||
query, | ||
items: index_params[:per_page], | ||
link_extra: "data-turbo-frame=\"#{params[:turbo_frame]}\"", | ||
params: extra_pagy_params, | ||
size: pagination_hash[:size] | ||
end | ||
|
||
private | ||
|
||
def pagination_hash | ||
@pagination ||= PAGINATION_DEFAULTS.merge Avo::ExecutionContext.new( | ||
target: pagination, | ||
resource: self, | ||
view: @view | ||
).handle | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.