pagy 3.11.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/headers
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
5
  # Add specialized backend methods to add pagination response headers
7
- module Backend ; private
6
+ module Backend
7
+ private
8
8
 
9
9
  VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' }
10
10
 
@@ -15,17 +15,21 @@ class Pagy
15
15
  end
16
16
 
17
17
  def pagy_headers(pagy)
18
- hash = pagy_headers_hash(pagy)
19
- hash['Link'] = hash['Link'].map{|rel, link| %(<#{link}>; rel="#{rel}")}.join(', ')
20
- hash
18
+ pagy_headers_hash(pagy).tap do |hash|
19
+ hash['Link'] = hash['Link'].map{|rel, link| %(<#{link}>; rel="#{rel}")}.join(', ')
20
+ end
21
21
  end
22
22
 
23
23
  def pagy_headers_hash(pagy)
24
24
  countless = defined?(Pagy::Countless) && pagy.is_a?(Pagy::Countless)
25
- rels = { 'first' => 1, 'prev' => pagy.prev, 'next' => pagy.next }; rels['last'] = pagy.last unless countless
26
- url_str = pagy_url_for(PAGE_PLACEHOLDER, pagy, :url)
27
- hash = { 'Link' => Hash[rels.map{|rel, n|[rel, url_str.sub(PAGE_PLACEHOLDER, n.to_s)] if n}.compact] }
28
- headers = pagy.vars[:headers]
25
+ rels = { 'first' => 1, 'prev' => pagy.prev, 'next' => pagy.next }
26
+ rels['last'] = pagy.last unless countless
27
+ url_str = pagy_url_for(PAGE_PLACEHOLDER, pagy, :url)
28
+ hash = { 'Link' => rels.map do |rel, num|
29
+ next unless num
30
+ [ rel, url_str.sub(PAGE_PLACEHOLDER, num.to_s) ]
31
+ end.compact.to_h }
32
+ headers = pagy.vars[:headers]
29
33
  hash[headers[:page]] = pagy.page.to_s if headers[:page]
30
34
  hash[headers[:items]] = pagy.vars[:items].to_s if headers[:items]
31
35
  unless countless
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/i18n
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
@@ -8,18 +7,16 @@ class Pagy
8
7
 
9
8
  ::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
10
9
 
11
- Pagy::I18n.clear.instance_eval { undef :load; undef :t } # unload the pagy default constant for efficiency
10
+ # unload the pagy default constant for efficiency
11
+ Pagy::I18n.clear.instance_eval do
12
+ undef :load
13
+ undef :t
14
+ end
12
15
 
13
- alias_method :pagy_without_i18n, :pagy_t
14
- if Gem::Version.new(::I18n::VERSION) < Gem::Version.new('1.6.0')
15
- def pagy_t_with_i18n(*args) ::I18n.t(*args) end
16
- else
17
- # keep 1.9 compatibility by hiding 2.0+ syntax in string
18
- module_eval <<-RUBY
19
- def pagy_t_with_i18n(key, **opts) ::I18n.t(key, **opts) end
20
- RUBY
16
+ module UseI18nGem
17
+ def pagy_t(key, **opts) = ::I18n.t(key, **opts)
21
18
  end
22
- alias_method :pagy_t, :pagy_t_with_i18n
19
+ prepend UseI18nGem
23
20
 
24
21
  end
25
22
  end
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/items
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -12,40 +11,42 @@ class Pagy
12
11
 
13
12
  ITEMS_PLACEHOLDER = '__pagy_items__'
14
13
 
15
- # Handle a custom number of :items from params
16
- module Backend ; private
14
+ module UseItemsExtra
15
+ private
17
16
 
18
- def pagy_with_items(vars)
19
- vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
20
- [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
21
- end
17
+ %i[ pagy_get_vars
18
+ pagy_countless_get_vars
19
+ pagy_elasticsearch_rails_get_vars
20
+ pagy_searchkick_get_vars
21
+ ].each do |meth|
22
+ next unless Backend.private_method_defined?(meth, true)
22
23
 
23
- # add the pagy*_get_vars alias-chained methods for frontend, and defined/required extras
24
- [nil, 'countless', 'elasticsearch_rails', 'searchkick'].each do |name|
25
- prefix, if_start, if_end = "_#{name}", "if defined?(Pagy::#{name.upcase})", "end" if name
26
- module_eval <<-RUBY
27
- #{if_start}
28
- alias_method :pagy#{prefix}_get_vars_without_items, :pagy#{prefix}_get_vars
29
- def pagy#{prefix}_get_vars_with_items(collection, vars)
30
- pagy_with_items(vars)
31
- pagy#{prefix}_get_vars_without_items(collection, vars)
32
- end
33
- alias_method :pagy#{prefix}_get_vars, :pagy#{prefix}_get_vars_with_items
34
- #{if_end}
35
- RUBY
24
+ define_method(meth) do |collection, vars|
25
+ vars[:items] ||= if (items = params[vars[:items_param] || VARS[:items_param]]) # :items from :items_param
26
+ [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
27
+ end
28
+ super collection, vars
29
+ end
36
30
  end
37
31
 
38
32
  end
33
+ Backend.prepend UseItemsExtra
34
+
39
35
 
40
36
  module Frontend
41
37
 
42
- alias_method :pagy_url_for_without_items, :pagy_url_for
43
- def pagy_url_for_with_items(page, pagy, url=false)
44
- p_vars = pagy.vars; params = request.GET.merge(p_vars[:params]); params[p_vars[:page_param].to_s] = page
45
- params[p_vars[:items_param].to_s] = p_vars[:items]
46
- "#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
38
+ module UseItemsExtra
39
+
40
+ def pagy_url_for(page, pagy, url=nil)
41
+ p_vars = pagy.vars
42
+ params = request.GET.merge(p_vars[:params])
43
+ params[p_vars[:page_param].to_s] = page
44
+ params[p_vars[:items_param].to_s] = p_vars[:items]
45
+ "#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
46
+ end
47
+
47
48
  end
48
- alias_method :pagy_url_for, :pagy_url_for_with_items
49
+ prepend UseItemsExtra
49
50
 
50
51
  # Return the items selector HTML. For example "Show [20] items per page"
51
52
  def pagy_items_selector_js(pagy, id=pagy_id)
@@ -55,10 +56,12 @@ class Pagy
55
56
  link = pagy_marked_link(pagy_link_proc(pagy))
56
57
  p_vars[:items] = p_items # restore the items
57
58
 
58
- html = EMPTY + %(<span id="#{id}">)
59
+ html = %(<span id="#{id}">)
59
60
  input = %(<input type="number" min="1" max="#{p_vars[:max_items]}" value="#{p_items}" style="padding: 0; text-align: center; width: #{p_items.to_s.length+1}rem;">)
60
- html << %(#{pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items), items_input: input, count: p_items)})
61
- html << %(</span>#{pagy_json_tag(:items_selector, id, pagy.from, link, defined?(TRIM) && p_vars[:page_param])})
61
+ html << pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items),
62
+ items_input: input,
63
+ count: p_items)
64
+ html << %(</span>#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)})
62
65
  end
63
66
 
64
67
  end
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/materialize
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,51 +8,70 @@ class Pagy
9
8
 
10
9
  # Pagination for materialize: it returns the html with the series of links to the pages
11
10
  def pagy_materialize_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
13
- html = EMPTY + (p_prev ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
14
- : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
11
+ link = pagy_link_proc(pagy)
12
+
13
+ html = +%(<div class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">)
14
+ html << pagy_materialize_prev_html(pagy, link)
15
15
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
- html << if item.is_a?(Integer); %(<li class="waves-effect">#{link.call item}</li>) # page link
17
- elsif item.is_a?(String) ; %(<li class="active">#{link.call item}</li>) # active page
18
- elsif item == :gap ; %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
16
+ html << case item
17
+ when Integer then %(<li class="waves-effect">#{link.call item}</li>) # page link
18
+ when String then %(<li class="active">#{link.call item}</li>) # active page
19
+ when :gap then %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>) # page gap
19
20
  end
20
21
  end
21
- html << (p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
22
- : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
23
- %(<div class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></div>)
22
+ html << pagy_materialize_next_html(pagy, link)
23
+ html << %(</ul></div>)
24
24
  end
25
25
 
26
26
  # Javascript pagination for materialize: it returns a nav and a JSON tag used by the Pagy.nav javascript
27
27
  def pagy_materialize_nav_js(pagy, id=pagy_id)
28
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
29
- tags = { 'before' => ( '<ul class="pagination">' \
30
- + (p_prev ? %(<li class="waves-effect prev">#{link.call(p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"')}</li>)
31
- : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>)) ),
28
+ link = pagy_link_proc(pagy)
29
+ tags = { 'before' => %(<ul class="pagination">#{pagy_materialize_prev_html pagy, link}),
32
30
  'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
33
31
  'active' => %(<li class="active">#{mark}</li>),
34
- 'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>),
35
- 'after' => ( (p_next ? %(<li class="waves-effect next">#{link.call(p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"')}</li>)
36
- : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>)) \
37
- + '</ul>' ) }
38
- %(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
32
+ 'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>),
33
+ 'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) }
34
+
35
+ html = %(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>)
36
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
39
37
  end
40
38
 
41
39
  # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
42
40
  def pagy_materialize_combo_nav_js(pagy, id=pagy_id)
43
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
44
-
45
- html = %(<div id="#{id}" class="pagy-materialize-combo-nav-js pagination" role="navigation" aria-label="pager">) \
46
- + %(<div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;">)
47
- html << '<ul class="pagination" style="margin: 0px;">'
48
- li_style = 'style="vertical-align: middle;"'
49
- html << (p_prev ? %(<li class="waves-effect prev" #{li_style}>#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
50
- : %(<li class="prev disabled" #{li_style}><a href="#"><i class="material-icons">chevron_left</i></a></li>))
51
- input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 2px; border: none; border-radius: 2px; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
52
- html << %(<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>)
53
- html << (p_next ? %(<li class="waves-effect next" #{li_style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
54
- : %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
55
- html << %(</ul></div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
41
+ link = pagy_link_proc(pagy)
42
+ p_page = pagy.page
43
+ p_pages = pagy.pages
44
+ style = ' style="vertical-align: middle;"'
45
+ input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 2px; border: none; border-radius: 2px; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
46
+
47
+ %(<div id="#{id}" class="pagy-materialize-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;"><ul class="pagination" style="margin: 0px;">#{
48
+ pagy_materialize_prev_html pagy, link, style
49
+ }<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{
50
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
51
+ }</div>#{
52
+ pagy_materialize_next_html pagy, link, style
53
+ }</ul></div>#{
54
+ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
55
+ })
56
56
  end
57
57
 
58
+ private
59
+
60
+ def pagy_materialize_prev_html(pagy, link, style='')
61
+ if (p_prev = pagy.prev)
62
+ %(<li class="waves-effect prev"#{style}>#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
63
+ else
64
+ %(<li class="prev disabled"#{style}><a href="#"><i class="material-icons">chevron_left</i></a></li>)
65
+ end
66
+ end
67
+
68
+ def pagy_materialize_next_html(pagy, link, style='')
69
+ if (p_next = pagy.next)
70
+ %(<li class="waves-effect next"#{style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
71
+ else
72
+ %(<li class="next disabled"#{style}><a href="#"><i class="material-icons">chevron_right</i></a></li>)
73
+ end
74
+ end
75
+
58
76
  end
59
77
  end
@@ -1,36 +1,41 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/metadata
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
5
  # Add a specialized backend method for pagination metadata
7
- module Backend ; private
6
+ module Backend
7
+ private
8
8
 
9
- METADATA = [ :scaffold_url, :first_url, :prev_url, :page_url, :next_url, :last_url,
10
- :count, :page, :items, :vars, :pages, :last, :from, :to, :prev, :next, :series ]
11
- METADATA << :sequels if VARS.key?(:steps) # :steps gets defined along with the #sequels method
9
+ METADATA = %i[ scaffold_url first_url prev_url page_url next_url last_url
10
+ count page items vars pages last from to prev next series
11
+ ].tap do |metadata|
12
+ metadata << :sequels if VARS.key?(:steps) # :steps gets defined along with the #sequels method
13
+ end.freeze
12
14
 
13
15
  VARS[:metadata] = METADATA.dup
14
16
 
15
17
  include Helpers
16
18
 
17
- def pagy_metadata(pagy, url=false)
18
- names = pagy.vars[:metadata]
19
- (unknown = names - METADATA).empty? or raise(VariableError.new(pagy), "unknown metadata #{unknown.inspect}")
19
+ def pagy_metadata(pagy, url=nil)
20
+ names = pagy.vars[:metadata]
21
+ unknown = names - METADATA
22
+ raise VariableError.new(pagy), "unknown metadata #{unknown.inspect}" \
23
+ unless unknown.empty?
24
+
20
25
  scaffold_url = pagy_url_for(PAGE_PLACEHOLDER, pagy, url)
21
- metadata = {}
22
- names.each do |key|
23
- metadata[key] = case key
24
- when :scaffold_url ; scaffold_url
25
- when :first_url ; scaffold_url.sub(PAGE_PLACEHOLDER, 1.to_s)
26
- when :prev_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.prev.to_s)
27
- when :page_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.page.to_s)
28
- when :next_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.next.to_s)
29
- when :last_url ; scaffold_url.sub(PAGE_PLACEHOLDER, pagy.last.to_s)
30
- else pagy.send(key)
31
- end
26
+ {}.tap do |metadata|
27
+ names.each do |key|
28
+ metadata[key] = case key
29
+ when :scaffold_url then scaffold_url
30
+ when :first_url then scaffold_url.sub(PAGE_PLACEHOLDER, 1.to_s)
31
+ when :prev_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.prev.to_s)
32
+ when :page_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.page.to_s)
33
+ when :next_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.next.to_s)
34
+ when :last_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.last.to_s)
35
+ else pagy.send(key)
36
+ end
37
+ end
32
38
  end
33
- metadata
34
39
  end
35
40
 
36
41
  end
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/navs
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,30 +8,52 @@ class Pagy
9
8
 
10
9
  # Javascript pagination: it returns a nav and a JSON tag used by the Pagy.nav javascript
11
10
  def pagy_nav_js(pagy, id=pagy_id)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
13
- tags = { 'before' => p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
14
- : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ),
11
+ link = pagy_link_proc(pagy)
12
+ tags = { 'before' => pagy_nav_prev_html(pagy, link),
15
13
  'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER)}</span> ),
16
14
  'active' => %(<span class="page active">#{pagy.page}</span> ),
17
- 'gap' => %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ),
18
- 'after' => p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
19
- : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>) }
20
- %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
15
+ 'gap' => %(<span class="page gap">#{pagy_t 'pagy.nav.gap'}</span> ),
16
+ 'after' => pagy_nav_next_html(pagy, link) }
17
+
18
+ html = %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>)
19
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
21
20
  end
22
21
 
23
22
  # Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
24
23
  def pagy_combo_nav_js(pagy, id=pagy_id)
25
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
26
-
27
- html = EMPTY + %(<nav id="#{id}" class="pagy-combo-nav-js pagination" role="navigation" aria-label="pager">)
28
- html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
29
- : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
30
- input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
31
- html << %(<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</span> )
32
- html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
33
- : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
34
- html << %(</nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
24
+ link = pagy_link_proc(pagy)
25
+ p_page = pagy.page
26
+ p_pages = pagy.pages
27
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
28
+
29
+ %(<nav id="#{id}" class="pagy-combo-nav-js pagination" role="navigation" aria-label="pager">#{
30
+ pagy_nav_prev_html pagy, link
31
+ }<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{
32
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
33
+ }</span> #{
34
+ pagy_nav_next_html pagy, link
35
+ }</nav>#{
36
+ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
37
+ })
35
38
  end
36
39
 
40
+ private
41
+
42
+ def pagy_nav_prev_html(pagy, link)
43
+ if (p_prev = pagy.prev)
44
+ %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
45
+ else
46
+ %(<span class="page prev disabled">#{pagy_t 'pagy.nav.prev'}</span> )
47
+ end
48
+ end
49
+
50
+ def pagy_nav_next_html(pagy, link)
51
+ if (p_next = pagy.next)
52
+ %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
53
+ else
54
+ %(<span class="page next disabled">#{pagy_t 'pagy.nav.next'}</span>)
55
+ end
56
+ end
57
+
37
58
  end
38
59
  end
@@ -1,71 +1,73 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/overflow
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
5
 
7
- OVERFLOW = true
6
+ module UseOverflowExtra
7
+ VARS[:overflow] = :empty_page
8
8
 
9
- VARS[:overflow] = :empty_page
9
+ def overflow? = @overflow
10
10
 
11
- def overflow?; @overflow end
12
-
13
- alias_method :initialize_without_overflow, :initialize
14
- def initialize_with_overflow(vars)
15
- @overflow ||= false # don't override if :last_page re-run the method after an overflow
16
- initialize_without_overflow(vars)
17
- rescue OverflowError
18
- @overflow = true # add the overflow flag
19
- case @vars[:overflow]
20
- when :exception
21
- raise # same as without the extra
22
- when :last_page
23
- initial_page = @vars[:page] # save the very initial page (even after re-run)
24
- initialize(vars.merge!(page: @last)) # re-run with the last page
25
- @vars[:page] = initial_page # restore the inital page
26
- when :empty_page
27
- @offset = @items = @from = @to = 0 # vars relative to the actual page
28
- @prev = @last # prev relative to the actual page
29
- extend(Series) # special series for :empty_page
30
- else
31
- raise VariableError.new(self), "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
11
+ def initialize(vars)
12
+ @overflow ||= false # don't override if :last_page re-run the method after an overflow
13
+ super
14
+ rescue OverflowError
15
+ @overflow = true # add the overflow flag
16
+ case @vars[:overflow]
17
+ when :exception
18
+ raise # same as without the extra
19
+ when :last_page
20
+ initial_page = @vars[:page] # save the very initial page (even after re-run)
21
+ initialize vars.merge!(page: @last) # re-run with the last page
22
+ @vars[:page] = initial_page # restore the inital page
23
+ when :empty_page
24
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
25
+ @prev = @last # prev relative to the actual page
26
+ extend Series # special series for :empty_page
27
+ else
28
+ raise VariableError.new(self), "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
29
+ end
32
30
  end
33
- end
34
- alias_method :initialize, :initialize_with_overflow
35
31
 
36
- module Series
37
- def series(size=@vars[:size])
38
- @page = @last # series for last page
39
- super(size).tap do |s| # call original series
40
- s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
41
- @page = @vars[:page] # restore the actual page
32
+ module Series
33
+ def series(size=@vars[:size])
34
+ @page = @last # series for last page
35
+ super(size).tap do |s| # call original series
36
+ s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
37
+ @page = @vars[:page] # restore the actual page
38
+ end
42
39
  end
43
40
  end
41
+
44
42
  end
43
+ prepend UseOverflowExtra
45
44
 
46
45
 
47
46
  # support for Pagy::Countless
48
47
  if defined?(Pagy::Countless)
49
48
  class Countless
50
49
 
51
- alias_method :finalize_without_overflow, :finalize
52
- def finalize_with_overflow(items)
53
- @overflow = false
54
- finalize_without_overflow(items)
55
- rescue OverflowError
56
- @overflow = true # add the overflow flag
57
- case @vars[:overflow]
58
- when :exception
59
- raise # same as without the extra
60
- when :empty_page
61
- @offset = @items = @from = @to = 0 # vars relative to the actual page
62
- @vars[:size] = [] # no page in the series
63
- self
64
- else
65
- raise VariableError.new(self), "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
50
+ module UseOverflowExtra
51
+
52
+ def finalize(items)
53
+ @overflow = false
54
+ super
55
+ rescue OverflowError
56
+ @overflow = true # add the overflow flag
57
+ case @vars[:overflow]
58
+ when :exception
59
+ raise # same as without the extra
60
+ when :empty_page
61
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
62
+ @vars[:size] = [] # no page in the series
63
+ self
64
+ else
65
+ raise VariableError.new(self), "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
66
+ end
66
67
  end
68
+
67
69
  end
68
- alias_method :finalize, :finalize_with_overflow
70
+ prepend UseOverflowExtra
69
71
 
70
72
  end
71
73
  end