# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/semantic # 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 SemanticExtra # Pagination for semantic: it returns the html with the series of links to the pages def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '', **vars) p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra})) html = +%() html << pagy_semantic_prev_html(pagy, link) pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then link.call item when String then %(#{pagy.label_for(item)}) when :gap then %(
#{pagy_t 'pagy.nav.gap'}
) else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" end end html << pagy_semantic_next_html(pagy, link) html << %() end # Javascript pagination for semantic: it returns a nav and a JSON tag used by the Pagy.nav javascript def pagy_semantic_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: %(class="item" #{link_extra})) tags = { 'before' => pagy_semantic_prev_html(pagy, link), 'link' => link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER), 'active' => %(#{LABEL_PLACEHOLDER}), 'gap' => %(
#{pagy_t('pagy.nav.gap')}
), 'after' => pagy_semantic_next_html(pagy, link) } %() end # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript def pagy_semantic_combo_nav_js(pagy, pagy_id: nil, link_extra: '') p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra})) p_page = pagy.page p_pages = pagy.pages input = %() %(#{ pagy_semantic_prev_html pagy, link }
#{ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages }
#{ pagy_semantic_next_html pagy, link }) end private def pagy_semantic_prev_html(pagy, link) if (p_prev = pagy.prev) link.call p_prev, '', 'aria-label="previous"' else +%(
) end end def pagy_semantic_next_html(pagy, link) if (p_next = pagy.next) link.call p_next, '', 'aria-label="next"' else +%(
) end end end Frontend.prepend SemanticExtra end