# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/navs # frozen_string_literal: true require 'pagy/extras/frontend_helpers' class Pagy # :nodoc: # Frontend modules are specially optimized for performance. # The resulting code may not look very elegant, but produces the best benchmarks module NavsExtra # Javascript pagination: it returns a nav and a JSON tag used by the pagy.js file def pagy_nav_js(pagy, pagy_id: nil, link_extra: '', **vars) sequels = pagy.sequels(**vars) p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra: link_extra) tags = { 'before' => pagy_nav_prev_html(pagy, link), 'link' => %(#{link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)} ), 'active' => %(#{LABEL_PLACEHOLDER} ), 'gap' => %(#{pagy_t 'pagy.nav.gap'} ), 'after' => pagy_nav_next_html(pagy, link) } %() end # Javascript combo pagination: it returns a nav and a JSON tag used by the pagy.js file def pagy_combo_nav_js(pagy, pagy_id: nil, link_extra: '') p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra: link_extra) p_page = pagy.page p_pages = pagy.pages input = %() %(#{ pagy_nav_prev_html pagy, link }#{ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages } #{ pagy_nav_next_html pagy, link }) end private def pagy_nav_prev_html(pagy, link) if (p_prev = pagy.prev) %(#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'} ) else %(#{pagy_t 'pagy.nav.prev'} ) end end def pagy_nav_next_html(pagy, link) if (p_next = pagy.next) %(#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}) else %(#{pagy_t 'pagy.nav.next'}) end end end Frontend.prepend NavsExtra end