# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/bulma # frozen_string_literal: true require_relative '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 BulmaExtra # Pagination for bulma: it returns the html with the series of links to the pages def pagy_bulma_nav(pagy, id: nil, classes: 'pagy-bulma nav pagination is-centered', aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) html = %() html << bulma_prev_next_html(pagy, a) html << %() end # Javascript pagination for bulma: it returns a nav with a data-pagy attribute used by the Pagy.nav javascript def pagy_bulma_nav_js(pagy, id: nil, classes: 'pagy-bulma nav-js pagination is-centered', aria_label: nil, **vars) sequels = pagy.sequels(**vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) tokens = { 'before' => %(#{bulma_prev_next_html(pagy, a)}' } %() end # Javascript combo pagination for bulma: it returns a nav with a data-pagy attribute used by the pagy.js file def pagy_bulma_combo_nav_js(pagy, id: nil, classes: 'pagy-bulma combo-nav-js pagination is-centered', aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) pages = pagy.pages page_input = %(#{JSTools::A_TAG}) %(#{ bulma_prev_next_html(pagy, a) }
) end private def bulma_prev_next_html(pagy, a) html = if (p_prev = pagy.prev) a.(p_prev, pagy_t('pagy.prev'), classes: 'pagination-previous', aria_label: pagy_t('pagy.aria_label.prev')) else %(#{pagy_t 'pagy.prev'}) end html << if (p_next = pagy.next) a.(p_next, pagy_t('pagy.next'), classes: 'pagination-next', aria_label: pagy_t('pagy.aria_label.next')) else %(#{pagy_t('pagy.next')}) end end end Frontend.prepend BulmaExtra end