# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/uikit # frozen_string_literal: true require 'pagy/extras/js_tools' class Pagy # :nodoc: # Frontend modules are specially optimized for performance. # The resulting code may not look very elegant, but produces the best benchmarks module UikitExtra # Pagination for uikit: it returns the html with the series of links to the pages def pagy_uikit_nav(pagy, id: nil, aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy) html = %(#{ uikit_prev_html(pagy, a)}) pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer %(
  • #{a.(item)}
  • ) when String %(
  • #{ pagy.label_for(item)}
  • ) when :gap %(
  • #{pagy_t 'pagy.gap'}
  • ) else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" end end html << %(#{uikit_next_html(pagy, a)}) end # Javascript pagination for uikit: it returns a nav with a data-pagy attribute used by the pagy.js file def pagy_uikit_nav_js(pagy, id: nil, aria_label: nil, **vars) sequels = pagy.sequels(**vars) id = %( id="#{id}") if id a = pagy_anchor(pagy) tokens = { 'before' => uikit_prev_html(pagy, a), 'a' => %(
  • #{a.(PAGE_TOKEN, LABEL_TOKEN)}
  • ), 'current' => %(
  • #{ LABEL_TOKEN}
  • ), 'gap' => %(
  • #{pagy_t 'pagy.gap'}
  • ), 'after' => uikit_next_html(pagy, a) } %() end # Javascript combo pagination for uikit: it returns a nav with a data-pagy attribute used by the pagy.js file def pagy_uikit_combo_nav_js(pagy, id: nil, aria_label: nil) id = %( id="#{id}") if id a = pagy_anchor(pagy) pages = pagy.pages page_input = %() %(#{ uikit_prev_html(pagy, a) }
  • #{ uikit_next_html(pagy, a) }) end private def uikit_prev_html(pagy, a) span = %() if (p_prev = pagy.prev) %(
  • #{a.(p_prev, span, aria_label: pagy_t('pagy.aria_label.prev'))}
  • ) else %(
  • #{span}
  • ) end end def uikit_next_html(pagy, a) span = %() if (p_next = pagy.next) %(
  • #{a.(p_next, span, aria_label: pagy_t('pagy.aria_label.prev'))}
  • ) else %(
  • #{span}
  • ) end end end Frontend.include UikitExtra end