# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/limit
# frozen_string_literal: true
require_relative 'js_tools'
class Pagy # :nodoc:
DEFAULT[:limit_param] = :limit
DEFAULT[:limit_max] = 100
DEFAULT[:limit_extra] = true # extra enabled by default
# Allow the client to request a custom limit per page with an optional selector UI
module LimitExtra
# Additions for the Backend module
module BackendAddOn
private
# Set the limit variable considering the params and other pagy variables
def pagy_get_limit(vars)
return unless vars.key?(:limit_extra) ? vars[:limit_extra] : DEFAULT[:limit_extra] # :limit_extra is false
return unless (limit_count = pagy_get_limit_param(vars)) # no limit from request params
vars[:limit] = [limit_count.to_i, vars.key?(:limit_max) ? vars[:limit_max] : DEFAULT[:limit_max]].compact.min
end
# Get the limit count from the params
# Overridable by the jsonapi extra
def pagy_get_limit_param(vars)
params[vars[:limit_param] || DEFAULT[:limit_param]]
end
end
Backend.prepend LimitExtra::BackendAddOn
# Additions for the Frontend module
module FrontendAddOn
LIMIT_TOKEN = '__pagy_limit__'
# Return the limit selector HTML. For example "Show [20] items per page"
def pagy_limit_selector_js(pagy, id: nil, item_name: nil)
return '' unless pagy.vars[:limit_extra]
id = %( id="#{id}") if id
vars = pagy.vars
limit = vars[:limit]
vars[:limit] = LIMIT_TOKEN
url_token = pagy_url_for(pagy, PAGE_TOKEN)
vars[:limit] = limit # restore the limit
limit_input = %(#{JSTools::A_TAG})
%()
end
end
Frontend.prepend LimitExtra::FrontendAddOn
end
end