Module: Pagy::PagyExtra

Included in:
Frontend
Defined in:
lib/pagy/extras/pagy.rb

Overview

Frontend modules are specially optimized for performance. The resulting code may not look very elegant, but produces the best benchmarks

Instance Method Summary collapse

Instance Method Details

#pagy_combo_nav_js(pagy, id: nil, aria_label: nil) ⇒ Object

Javascript combo pagination: it returns a nav with a data-pagy attribute used by the pagy.js file



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pagy/extras/pagy.rb', line 30

def pagy_combo_nav_js(pagy, id: nil, aria_label: nil)
  id    = %( id="#{id}") if id
  a     = pagy_anchor(pagy)
  pages = pagy.pages

  page_input = %(<input name="page" type="number" min="1" max="#{pages}" value="#{pagy.page}" aria-current="page" ) <<
               %(style="text-align: center; width: #{pages.to_s.length + 1}rem; padding: 0;">#{JSTools::A_TAG})

  %(<nav#{id} class="pagy combo-nav-js" #{
      nav_aria_label(pagy, aria_label:)} #{
      pagy_data(pagy, :combo, pagy_url_for(pagy, PAGE_TOKEN))}>#{
      prev_a(pagy, a)
    }<label>#{
      pagy_t('pagy.combo_nav_js', page_input:, pages:)
    }</label>#{
      next_a(pagy, a)
    }</nav>)
end

#pagy_nav_js(pagy, id: nil, aria_label: nil, **vars) ⇒ Object

pagy_nav is defined in the Frontend itself Javascript pagination: it returns a nav with a data-pagy attribute used by the pagy.js file



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pagy/extras/pagy.rb', line 12

def pagy_nav_js(pagy, id: nil, aria_label: nil, **vars)
  sequels = pagy.sequels(**vars)
  id      = %( id="#{id}") if id
  a       = pagy_anchor(pagy)
  tokens  = { 'before'  => prev_a(pagy, a),
              'a'       => a.(PAGE_TOKEN, LABEL_TOKEN),
              'current' => %(<a class="current" role="link" aria-current="page" aria-disabled="true">#{
                               LABEL_TOKEN}</a>),
              'gap'     => %(<a class="gap" role="link" aria-disabled="true">#{pagy_t('pagy.gap')}</a>),
              'after'   => next_a(pagy, a) }

  %(<nav#{id} class="#{'pagy-rjs ' if sequels.size > 1}pagy nav-js" #{
      nav_aria_label(pagy, aria_label:)} #{
      pagy_data(pagy, :nav, tokens, sequels, pagy.label_sequels(sequels))
    }></nav>)
end

#pagy_next_a(pagy, text: pagy_t('pagy.next'), aria_label: pagy_t('pagy.aria_label.prev')) ⇒ Object

Return the enabled/disabled next page anchor tag



66
67
68
69
# File 'lib/pagy/extras/pagy.rb', line 66

def pagy_next_a(pagy, text: pagy_t('pagy.next'), aria_label: pagy_t('pagy.aria_label.prev'))
  a = pagy_anchor(pagy)
  next_a(pagy, a, text:, aria_label:)
end

Conditionally return the next page link tag



77
78
79
# File 'lib/pagy/extras/pagy.rb', line 77

def pagy_next_link(pagy, absolute: false)
  %(<link href="#{pagy_url_for(pagy, pagy.next, absolute:)}"/>) if pagy.next
end

#pagy_next_url(pagy, absolute: false) ⇒ Object

Return the next page URL string or nil



55
56
57
# File 'lib/pagy/extras/pagy.rb', line 55

def pagy_next_url(pagy, absolute: false)
  pagy_url_for(pagy, pagy.next, absolute:) if pagy.next
end

#pagy_prev_a(pagy, text: pagy_t('pagy.prev'), aria_label: pagy_t('pagy.aria_label.prev')) ⇒ Object

Return the enabled/disabled previous page anchor tag



60
61
62
63
# File 'lib/pagy/extras/pagy.rb', line 60

def pagy_prev_a(pagy, text: pagy_t('pagy.prev'), aria_label: pagy_t('pagy.aria_label.prev'))
  a = pagy_anchor(pagy)
  prev_a(pagy, a, text:, aria_label:)
end

Conditionally return the previous page link tag



72
73
74
# File 'lib/pagy/extras/pagy.rb', line 72

def pagy_prev_link(pagy, absolute: false)
  %(<link href="#{pagy_url_for(pagy, pagy.prev, absolute:)}"/>) if pagy.prev
end

#pagy_prev_url(pagy, absolute: false) ⇒ Object

Return the previous page URL string or nil



50
51
52
# File 'lib/pagy/extras/pagy.rb', line 50

def pagy_prev_url(pagy, absolute: false)
  pagy_url_for(pagy, pagy.prev, absolute:) if pagy.prev
end