# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/items # frozen_string_literal: true require 'pagy/extras/frontend_helpers' class Pagy # :nodoc: DEFAULT[:items_param] = :items DEFAULT[:max_items] = 100 DEFAULT[:items_extra] = true # extra enabled by default # Allow the client to request a custom number of items per page with an optional selector UI module ItemsExtra # Additions for the Backend module module BackendAddOn private # Set the items variable considering the params and other pagy variables def pagy_set_items_from_params(vars) return if vars[:items] # :items explicitly set return unless vars.key?(:items_extra) ? vars[:items_extra] : DEFAULT[:items_extra] # :items_extra is false return unless (items_count = pagy_get_items_size(vars)) # no items from request params vars[:items] = [items_count.to_i, vars.key?(:max_items) ? vars[:max_items] : DEFAULT[:max_items]].compact.min end # Get the items count from the params # Overridable by the jsonapi extra def pagy_get_items_size(vars) params[vars[:items_param] || DEFAULT[:items_param]] end end Backend.prepend ItemsExtra::BackendAddOn # Additions for the Frontend module module FrontendAddOn ITEMS_PLACEHOLDER = '__pagy_items__' # Return the items selector HTML. For example "Show [20] items per page" def pagy_items_selector_js(pagy, pagy_id: nil, item_name: nil, item_i18n_key: nil, link_extra: '') return '' unless pagy.vars[:items_extra] p_id = %( id="#{pagy_id}") if pagy_id p_vars = pagy.vars p_items = p_vars[:items] p_vars[:items] = ITEMS_PLACEHOLDER link = pagy_marked_link(pagy_link_proc(pagy, link_extra:)) p_vars[:items] = p_items # restore the items html = +%() input = %() html << pagy_t('pagy.items_selector_js', item_name: item_name || pagy_t(item_i18n_key || p_vars[:item_i18n_key], count: p_items), items_input: input, count: p_items) html << %() end end Frontend.prepend ItemsExtra::FrontendAddOn end end