# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/uikit # 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 UikitExtra # Pagination for uikit: it returns the html with the series of links to the pages def pagy_uikit_nav(pagy, pagy_id: nil, link_extra: '', nav_aria_label: nil, nav_i18n_key: nil, **vars) p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra:) html = +%(#{uikit_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 %(
  • #{link.call(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, link) html << %() end # Javascript pagination for uikit: it returns a nav and a JSON tag used by the pagy.js file def pagy_uikit_nav_js(pagy, pagy_id: nil, link_extra: '', nav_aria_label: nil, nav_i18n_key: nil, **vars) sequels = pagy.sequels(**vars) p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra:) tags = { 'before' => uikit_prev_html(pagy, link), 'link' => %(
  • #{link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}
  • ), 'active' => %(
  • #{ LABEL_PLACEHOLDER}
  • ), 'gap' => %(
  • #{pagy_t 'pagy.gap'}
  • ), 'after' => uikit_next_html(pagy, link) } %() end # Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the pagy.js file def pagy_uikit_combo_nav_js(pagy, pagy_id: nil, link_extra: '', nav_aria_label: nil, nav_i18n_key: nil) p_id = %( id="#{pagy_id}") if pagy_id link = pagy_link_proc(pagy, link_extra:) p_page = pagy.page p_pages = pagy.pages input = %() %(#{ uikit_prev_html pagy, link }
  • #{ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages }
  • #{ uikit_next_html pagy, link }) end private def uikit_prev_html(pagy, link) previous_span = %(#{pagy_t 'pagy.prev'}) if (p_prev = pagy.prev) %(
  • #{link.call(p_prev, previous_span, prev_aria_label_attr)}
  • ) else %(
  • #{previous_span}
  • ) end end def uikit_next_html(pagy, link) next_span = %(#{pagy_t 'pagy.next'}) if (p_next = pagy.next) %(
  • #{link.call(p_next, next_span, next_aria_label_attr)}
  • ) else %(
  • #{next_span}
  • ) end end end Frontend.include UikitExtra end