pagy 3.13.0 → 4.10.1

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/lib/config/pagy.rb +36 -25
  4. data/lib/javascripts/pagy.js +104 -93
  5. data/lib/locales/ar.yml +26 -0
  6. data/lib/locales/bg.yml +2 -2
  7. data/lib/locales/bs.yml +2 -2
  8. data/lib/locales/ca.yml +2 -2
  9. data/lib/locales/cs.yml +2 -2
  10. data/lib/locales/da.yml +2 -2
  11. data/lib/locales/de.yml +2 -2
  12. data/lib/locales/en.yml +2 -2
  13. data/lib/locales/es.yml +2 -2
  14. data/lib/locales/fr.yml +2 -2
  15. data/lib/locales/hr.yml +2 -2
  16. data/lib/locales/id.yml +2 -2
  17. data/lib/locales/it.yml +2 -2
  18. data/lib/locales/ja.yml +2 -2
  19. data/lib/locales/km.yml +2 -2
  20. data/lib/locales/ko.yml +2 -2
  21. data/lib/locales/nb.yml +2 -2
  22. data/lib/locales/nl.yml +2 -2
  23. data/lib/locales/pl.yml +2 -2
  24. data/lib/locales/pt-BR.yml +2 -2
  25. data/lib/locales/pt.yml +2 -2
  26. data/lib/locales/ru.yml +2 -2
  27. data/lib/locales/sr.yml +2 -2
  28. data/lib/locales/sv-SE.yml +2 -2
  29. data/lib/locales/sv.yml +2 -2
  30. data/lib/locales/sw.yml +2 -2
  31. data/lib/locales/tr.yml +2 -2
  32. data/lib/locales/uk.yml +24 -0
  33. data/lib/locales/utils/i18n.rb +3 -11
  34. data/lib/locales/utils/loader.rb +7 -10
  35. data/lib/locales/utils/p11n.rb +44 -23
  36. data/lib/locales/zh-CN.yml +2 -2
  37. data/lib/locales/zh-HK.yml +2 -2
  38. data/lib/locales/zh-TW.yml +3 -3
  39. data/lib/pagy.rb +51 -40
  40. data/lib/pagy/backend.rb +5 -3
  41. data/lib/pagy/console.rb +21 -0
  42. data/lib/pagy/countless.rb +13 -10
  43. data/lib/pagy/deprecation.rb +27 -0
  44. data/lib/pagy/exceptions.rb +6 -2
  45. data/lib/pagy/extras/arel.rb +4 -3
  46. data/lib/pagy/extras/array.rb +4 -3
  47. data/lib/pagy/extras/bootstrap.rb +61 -30
  48. data/lib/pagy/extras/bulma.rb +60 -38
  49. data/lib/pagy/extras/countless.rb +7 -8
  50. data/lib/pagy/extras/elasticsearch_rails.rb +28 -17
  51. data/lib/pagy/extras/foundation.rb +61 -32
  52. data/lib/pagy/extras/headers.rb +13 -9
  53. data/lib/pagy/extras/i18n.rb +10 -11
  54. data/lib/pagy/extras/items.rb +21 -34
  55. data/lib/pagy/extras/materialize.rb +58 -35
  56. data/lib/pagy/extras/meilisearch.rb +55 -0
  57. data/lib/pagy/extras/metadata.rb +27 -21
  58. data/lib/pagy/extras/navs.rb +44 -20
  59. data/lib/pagy/extras/overflow.rb +52 -48
  60. data/lib/pagy/extras/searchkick.rb +27 -16
  61. data/lib/pagy/extras/semantic.rb +55 -30
  62. data/lib/pagy/extras/shared.rb +14 -15
  63. data/lib/pagy/extras/standalone.rb +62 -0
  64. data/lib/pagy/extras/support.rb +20 -11
  65. data/lib/pagy/extras/trim.rb +12 -11
  66. data/lib/pagy/extras/uikit.rb +54 -35
  67. data/lib/pagy/frontend.rb +64 -33
  68. metadata +10 -8
  69. data/lib/pagy/extras/pagy_search.rb +0 -18
@@ -1,13 +1,21 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/elasticsearch_rails
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
- require 'pagy/extras/pagy_search'
6
-
7
4
  class Pagy
8
5
 
9
- # used by the items extra
10
- ELASTICSEARCH_RAILS = true
6
+ VARS[:elasticsearch_rails_search_method] ||= :pagy_search
7
+
8
+ module ElasticsearchRails
9
+ # returns an array used to delay the call of #search
10
+ # after the pagination variables are merged to the options
11
+ # it also pushes to the same array an eventually called method
12
+ def pagy_elasticsearch_rails(query_or_payload, **options)
13
+ [self, query_or_payload, options].tap do |args|
14
+ args.define_singleton_method(:method_missing){|*a| args += a}
15
+ end
16
+ end
17
+ alias_method VARS[:elasticsearch_rails_search_method], :pagy_elasticsearch_rails
18
+ end
11
19
 
12
20
  # create a Pagy object from an Elasticsearch::Model::Response::Response object
13
21
  def self.new_from_elasticsearch_rails(response, vars={})
@@ -19,28 +27,31 @@ class Pagy
19
27
  end
20
28
 
21
29
  # Add specialized backend methods to paginate ElasticsearchRails searches
22
- module Backend ; private
30
+ module Backend
31
+ private
23
32
 
24
33
  # Return Pagy object and items
25
34
  def pagy_elasticsearch_rails(pagy_search_args, vars={})
26
- model, search_args, _block, *called = pagy_search_args
27
- vars = pagy_elasticsearch_rails_get_vars(nil, vars)
28
- search_args[-1][:size] = vars[:items]
29
- search_args[-1][:from] = vars[:items] * (vars[:page] - 1)
30
- response = model.search(*search_args)
31
- total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
32
- vars[:count] = total.is_a?(Hash) ? total['value'] : total
35
+ model, query_or_payload, options, *called = pagy_search_args
36
+ vars = pagy_elasticsearch_rails_get_vars(nil, vars)
37
+ options[:size] = vars[:items]
38
+ options[:from] = vars[:items] * (vars[:page] - 1)
39
+ response = model.search(query_or_payload, **options)
40
+ total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
41
+ vars[:count] = total.is_a?(Hash) ? total['value'] : total
42
+
33
43
  pagy = Pagy.new(vars)
34
44
  # with :last_page overflow we need to re-run the method in order to get the hits
35
- if defined?(OVERFLOW) && pagy.overflow? && pagy.vars[:overflow] == :last_page
36
- return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page))
37
- end
38
- return pagy, called.empty? ? response : response.send(*called)
45
+ return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page)) \
46
+ if defined?(Pagy::UseOverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
47
+
48
+ [ pagy, called.empty? ? response : response.send(*called) ]
39
49
  end
40
50
 
41
51
  # Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
42
52
  # the _collection argument is not available when the method is called
43
53
  def pagy_elasticsearch_rails_get_vars(_collection, vars)
54
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
44
55
  vars[:items] ||= VARS[:items]
45
56
  vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
46
57
  vars
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/foundation
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -8,50 +7,80 @@ class Pagy
8
7
  module Frontend
9
8
 
10
9
  # Pagination for Foundation: it returns the html with the series of links to the pages
11
- def pagy_foundation_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
10
+ def pagy_foundation_nav(pagy, pagy_id: nil, link_extra: '')
11
+ p_id = %( id="#{pagy_id}") if pagy_id
12
+ link = pagy_link_proc(pagy, link_extra: link_extra)
13
13
 
14
- html = EMPTY + (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
15
- : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
14
+ html = +%(<nav#{p_id} class="pagy-foundation-nav" aria-label="Pagination"><ul class="pagination">)
15
+ html << pagy_foundation_prev_html(pagy, link)
16
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
- html << if item.is_a?(Integer); %(<li>#{link.call item}</li>) # page link
18
- elsif item.is_a?(String) ; %(<li class="current">#{item}</li>) # active page
19
- elsif item == :gap ; %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
17
+ html << case item
18
+ when Integer then %(<li>#{link.call item}</li>) # page link
19
+ when String then %(<li class="current">#{item}</li>) # active page
20
+ when :gap then %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
20
21
  end
21
22
  end
22
- html << (p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
23
- : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
24
- %(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
23
+ html << pagy_foundation_next_html(pagy, link)
24
+ html << %(</ul></nav>)
25
25
  end
26
26
 
27
27
  # Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
- def pagy_foundation_nav_js(pagy, id=pagy_id)
29
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
30
- tags = { 'before' => ( '<ul class="pagination">' \
31
- + (p_prev ? %(<li class="prev">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"')}</li>)
32
- : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>)) ),
33
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
28
+ def pagy_foundation_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
29
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
30
+ p_id = %( id="#{pagy_id}") if pagy_id
31
+ link = pagy_link_proc(pagy, link_extra: link_extra)
32
+ tags = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html pagy, link}),
33
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>),
34
34
  'active' => %(<li class="current">#{pagy.page}</li>),
35
35
  'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
36
- 'after' => ( (p_next ? %(<li class="next">#{link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next"')}</li>)
37
- : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>)) \
38
- + '</ul>' ) }
39
- %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
36
+ 'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }
37
+
38
+ %(<nav#{p_id} class="pagy-njs pagy-foundation-nav-js" aria-label="Pagination" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
40
39
  end
41
40
 
42
41
  # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
43
- def pagy_foundation_combo_nav_js(pagy, id=pagy_id)
44
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
45
-
46
- html = %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination">) + %(<div class="input-group">)
47
- html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"')
48
- : %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
49
- input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">)
50
- html << %(<span class="input-group-label">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</span>)
51
- html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"')
52
- : %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
53
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
42
+ def pagy_foundation_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
43
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
44
+ p_id = %( id="#{pagy_id}") if pagy_id
45
+ link = pagy_link_proc(pagy, link_extra: link_extra)
46
+ p_page = pagy.page
47
+ p_pages = pagy.pages
48
+ input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">)
49
+
50
+ %(<nav#{p_id} class="pagy-foundation-combo-nav-js" aria-label="Pagination"><div class="input-group" #{
51
+ pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
52
+ }>#{
53
+ if (p_prev = pagy.prev)
54
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0" aria-label="previous" class="prev button primary"'
55
+ else
56
+ %(<a style="margin-bottom: 0" class="prev button primary disabled" href="#">#{pagy_t 'pagy.nav.prev'}</a>)
57
+ end
58
+ }<span class="input-group-label">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</span>#{
59
+ if (p_next = pagy.next)
60
+ link.call p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0" aria-label="next" class="next button primary"'
61
+ else
62
+ %(<a style="margin-bottom: 0" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
63
+ end
64
+ }</div></nav>)
54
65
  end
55
66
 
67
+ private
68
+
69
+ def pagy_foundation_prev_html(pagy, link)
70
+ if (p_prev = pagy.prev)
71
+ %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
72
+ else
73
+ %(<li class="prev disabled">#{pagy_t 'pagy.nav.prev' }</li>)
74
+ end
75
+ end
76
+
77
+ def pagy_foundation_next_html(pagy, link)
78
+ if (p_next = pagy.next)
79
+ %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
80
+ else
81
+ %(<li class="next disabled">#{pagy_t 'pagy.nav.next'}</li>)
82
+ end
83
+ end
84
+
56
85
  end
57
86
  end
@@ -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(pagy, PAGE_PLACEHOLDER, absolute: true)
28
+ hash = { 'Link' => rels.map do |rel, num| # filter_map if ruby >=2.7
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,18 @@ 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)
18
+ ::I18n.t(key, **opts)
19
+ end
21
20
  end
22
- alias_method :pagy_t, :pagy_t_with_i18n
21
+ prepend UseI18nGem
23
22
 
24
23
  end
25
24
  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'
@@ -10,55 +9,43 @@ class Pagy
10
9
  VARS[:items_param] = :items
11
10
  VARS[:max_items] = 100
12
11
 
12
+ VARS[:enable_items_extra] = true
13
+
13
14
  ITEMS_PLACEHOLDER = '__pagy_items__'
14
15
 
15
- # Handle a custom number of :items from params
16
- module Backend ; private
16
+ module UseItemsExtra; end
17
17
 
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
18
+ module Backend
19
+ private
22
20
 
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
36
- end
21
+ def pagy_set_items_from_params(vars)
22
+ return if vars[:items]
23
+ return unless vars.key?(:enable_item_extra) ? vars[:enable_item_extra] : VARS[:enable_items_extra]
24
+ return unless (items = params[vars[:items_param] || VARS[:items_param]]) # :items from :items_param
25
+ vars[:items] = [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
26
+ end
37
27
 
38
28
  end
39
29
 
40
30
  module Frontend
41
31
 
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]}"
47
- end
48
- alias_method :pagy_url_for, :pagy_url_for_with_items
49
-
50
32
  # Return the items selector HTML. For example "Show [20] items per page"
51
- def pagy_items_selector_js(pagy, id=pagy_id)
33
+ def pagy_items_selector_js(pagy, deprecated_id=nil, pagy_id: nil, item_name: nil, i18n_key: nil, link_extra: '')
34
+ return '' unless pagy.vars[:enable_items_extra]
35
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
36
+ p_id = %( id="#{pagy_id}") if pagy_id
52
37
  p_vars = pagy.vars
53
38
  p_items = p_vars[:items]
54
39
  p_vars[:items] = ITEMS_PLACEHOLDER
55
- link = pagy_marked_link(pagy_link_proc(pagy))
40
+ link = pagy_marked_link(pagy_link_proc(pagy, link_extra: link_extra))
56
41
  p_vars[:items] = p_items # restore the items
57
42
 
58
- html = EMPTY + %(<span id="#{id}">)
43
+ html = +%(<span#{p_id} class="pagy-items-selector-js" #{pagy_json_attr pagy, :items_selector, pagy.from, link}>)
59
44
  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])})
45
+ html << pagy_t('pagy.items_selector_js', item_name: item_name || pagy_t(i18n_key || p_vars[:i18n_key], count: p_items),
46
+ items_input: input,
47
+ count: p_items)
48
+ html << %(</span>)
62
49
  end
63
50
 
64
51
  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'
@@ -8,52 +7,76 @@ class Pagy
8
7
  module Frontend
9
8
 
10
9
  # Pagination for materialize: it returns the html with the series of links to the pages
11
- 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>))
10
+ def pagy_materialize_nav(pagy, pagy_id: nil, link_extra: '')
11
+ p_id = %( id="#{pagy_id}") if pagy_id
12
+ link = pagy_link_proc(pagy, link_extra: link_extra)
13
+
14
+ html = +%(<div#{p_id} class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">)
15
+ html << pagy_materialize_prev_html(pagy, link)
15
16
  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
17
+ html << case item
18
+ when Integer then %(<li class="waves-effect">#{link.call item}</li>) # page link
19
+ when String then %(<li class="active">#{link.call item}</li>) # active page
20
+ when :gap then %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>) # page gap
19
21
  end
20
22
  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>)
23
+ html << pagy_materialize_next_html(pagy, link)
24
+ html << %(</ul></div>)
24
25
  end
25
26
 
26
27
  # Javascript pagination for materialize: it returns a nav and a JSON tag used by the Pagy.nav javascript
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
+ def pagy_materialize_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
29
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
30
+ p_id = %( id="#{pagy_id}") if pagy_id
31
+ link = pagy_link_proc(pagy, link_extra: link_extra)
32
+
33
+ tags = { 'before' => %(<ul class="pagination">#{pagy_materialize_prev_html pagy, link}),
32
34
  'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
33
35
  '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])})
36
+ 'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>),
37
+ 'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) }
38
+
39
+ %(<div#{p_id} class="pagy-njs pagy-materialize-nav-js" role="navigation" aria-label="pager" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
39
40
  end
40
41
 
41
42
  # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
42
- 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])})
43
+ def pagy_materialize_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
44
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
45
+ p_id = %( id="#{pagy_id}") if pagy_id
46
+ link = pagy_link_proc(pagy, link_extra: link_extra)
47
+ p_page = pagy.page
48
+ p_pages = pagy.pages
49
+ style = ' style="vertical-align: middle"'
50
+ input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="text-align: center; width: #{p_pages.to_s.length+1}rem;">)
51
+
52
+ %(<ul#{p_id} class="pagy-materialize-combo-nav-js pagination chip" role="navigation" aria-label="pager" style="padding-right: 0" #{
53
+ pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
54
+ }>#{
55
+ pagy_materialize_prev_html pagy, link, style
56
+ }<li class="pagy-combo-input">#{
57
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
58
+ }</li>#{
59
+ pagy_materialize_next_html pagy, link, style
60
+ }</ul>)
56
61
  end
57
62
 
63
+ private
64
+
65
+ def pagy_materialize_prev_html(pagy, link, style='')
66
+ if (p_prev = pagy.prev)
67
+ %(<li class="waves-effect prev"#{style}>#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
68
+ else
69
+ %(<li class="prev disabled"#{style}><a href="#"><i class="material-icons">chevron_left</i></a></li>)
70
+ end
71
+ end
72
+
73
+ def pagy_materialize_next_html(pagy, link, style='')
74
+ if (p_next = pagy.next)
75
+ %(<li class="waves-effect next"#{style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
76
+ else
77
+ %(<li class="next disabled"#{style}><a href="#"><i class="material-icons">chevron_right</i></a></li>)
78
+ end
79
+ end
80
+
58
81
  end
59
82
  end