pagy 4.1.0 → 4.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
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
4
  class Pagy
@@ -28,7 +27,8 @@ class Pagy
28
27
  end
29
28
 
30
29
  # Add specialized backend methods to paginate ElasticsearchRails searches
31
- module Backend ; private
30
+ module Backend
31
+ private
32
32
 
33
33
  # Return Pagy object and items
34
34
  def pagy_elasticsearch_rails(pagy_search_args, vars={})
@@ -39,17 +39,19 @@ class Pagy
39
39
  response = model.search(query_or_payload, **options)
40
40
  total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
41
41
  vars[:count] = total.is_a?(Hash) ? total['value'] : total
42
+
42
43
  pagy = Pagy.new(vars)
43
44
  # with :last_page overflow we need to re-run the method in order to get the hits
44
- if defined?(Pagy::Overflow) && pagy.overflow? && pagy.vars[:overflow] == :last_page
45
- return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page))
46
- end
47
- 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) ]
48
49
  end
49
50
 
50
51
  # Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
51
52
  # the _collection argument is not available when the method is called
52
53
  def pagy_elasticsearch_rails_get_vars(_collection, vars)
54
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
53
55
  vars[:items] ||= VARS[:items]
54
56
  vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
55
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,81 @@ 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 = +(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" role="navigation" 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(pagy, :nav, id, tags, pagy.sequels)})
36
+ 'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }
37
+
38
+ html = %(<nav#{p_id} class="pagy-njs pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>)
39
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
40
40
  end
41
41
 
42
42
  # 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(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
43
+ def pagy_foundation_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
+ 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
+
51
+ %(<nav#{p_id} class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{
52
+ if (p_prev = pagy.prev)
53
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"'
54
+ else
55
+ %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t 'pagy.nav.prev'}</a>)
56
+ end
57
+ }<span class="input-group-label">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</span>#{
58
+ if (p_next = pagy.next)
59
+ link.call p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"'
60
+ else
61
+ %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
62
+ end
63
+ }</div></nav>#{
64
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
65
+ })
54
66
  end
55
67
 
68
+ private
69
+
70
+ def pagy_foundation_prev_html(pagy, link)
71
+ if (p_prev = pagy.prev)
72
+ %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
73
+ else
74
+ %(<li class="prev disabled">#{pagy_t 'pagy.nav.prev' }</li>)
75
+ end
76
+ end
77
+
78
+ def pagy_foundation_next_html(pagy, link)
79
+ if (p_next = pagy.next)
80
+ %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
81
+ else
82
+ %(<li class="next disabled">#{pagy_t 'pagy.nav.next'}</li>)
83
+ end
84
+ end
85
+
56
86
  end
57
87
  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.filter_map do |rel, num|
29
+ next unless num
30
+ [ rel, url_str.sub(PAGE_PLACEHOLDER, num.to_s) ]
31
+ end.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,12 +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
- module I18n
16
+ module UseI18nGem
14
17
  def pagy_t(key, **opts) = ::I18n.t(key, **opts)
15
18
  end
16
- prepend I18n
19
+ prepend UseI18nGem
17
20
 
18
21
  end
19
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'
@@ -10,47 +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
- module Items ; private
16
+ module UseItemsExtra; end
17
+
18
+ module Backend
19
+ private
16
20
 
17
- [:pagy_get_vars, :pagy_countless_get_vars, :pagy_elasticsearch_rails_get_vars, :pagy_searchkick_get_vars].each do |meth|
18
- if Pagy::Backend.private_method_defined?(meth, true)
19
- define_method(meth) do |collection, vars|
20
- vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
21
- [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
22
- super(collection, vars)
23
- 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
24
26
  end
25
- end
26
27
 
27
28
  end
28
- Backend.prepend Items
29
-
30
29
 
31
30
  module Frontend
32
31
 
33
- module Items
34
- def pagy_url_for(page, pagy, url=false)
35
- p_vars = pagy.vars; params = request.GET.merge(p_vars[:params]); params[p_vars[:page_param].to_s] = page
36
- params[p_vars[:items_param].to_s] = p_vars[:items]
37
- "#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
38
- end
39
- end
40
- prepend Items
41
-
42
32
  # Return the items selector HTML. For example "Show [20] items per page"
43
- 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
44
37
  p_vars = pagy.vars
45
38
  p_items = p_vars[:items]
46
39
  p_vars[:items] = ITEMS_PLACEHOLDER
47
- link = pagy_marked_link(pagy_link_proc(pagy))
40
+ link = pagy_marked_link(pagy_link_proc(pagy, link_extra: link_extra))
48
41
  p_vars[:items] = p_items # restore the items
49
42
 
50
- html = %(<span id="#{id}">)
43
+ html = %(<span#{p_id} class="pagy-items-selector-js">)
51
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;">)
52
- html << %(#{pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items), items_input: input, count: p_items)})
53
- html << %(</span>#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)})
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>#{pagy_json_tag pagy, :items_selector, pagy.from, link})
54
49
  end
55
50
 
56
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,77 @@ 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 = (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(pagy, :nav, id, tags, pagy.sequels)})
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
+ html = %(<div#{p_id} class="pagy-njs pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>)
40
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
39
41
  end
40
42
 
41
43
  # 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(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
44
+ def pagy_materialize_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
45
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
46
+ p_id = %( id="#{pagy_id}") if pagy_id
47
+ link = pagy_link_proc(pagy, link_extra: link_extra)
48
+ p_page = pagy.page
49
+ p_pages = pagy.pages
50
+ style = ' style="vertical-align: middle;"'
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
+
53
+ %(<div#{p_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;">#{
54
+ pagy_materialize_prev_html pagy, link, style
55
+ }<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{
56
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
57
+ }</div>#{
58
+ pagy_materialize_next_html pagy, link, style
59
+ }</ul></div>#{
60
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
61
+ })
56
62
  end
57
63
 
64
+ private
65
+
66
+ def pagy_materialize_prev_html(pagy, link, style='')
67
+ if (p_prev = pagy.prev)
68
+ %(<li class="waves-effect prev"#{style}>#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
69
+ else
70
+ %(<li class="prev disabled"#{style}><a href="#"><i class="material-icons">chevron_left</i></a></li>)
71
+ end
72
+ end
73
+
74
+ def pagy_materialize_next_html(pagy, link, style='')
75
+ if (p_next = pagy.next)
76
+ %(<li class="waves-effect next"#{style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
77
+ else
78
+ %(<li class="next disabled"#{style}><a href="#"><i class="material-icons">chevron_right</i></a></li>)
79
+ end
80
+ end
81
+
58
82
  end
59
83
  end
@@ -1,36 +1,42 @@
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}")
20
- 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
19
+ def pagy_metadata(pagy, deprecated_url=nil, absolute: nil)
20
+ absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
21
+ names = pagy.vars[:metadata]
22
+ unknown = names - METADATA
23
+ raise VariableError.new(pagy), "unknown metadata #{unknown.inspect}" \
24
+ unless unknown.empty?
25
+
26
+ scaffold_url = pagy_url_for(pagy, PAGE_PLACEHOLDER, absolute: absolute)
27
+ {}.tap do |metadata|
28
+ names.each do |key|
29
+ metadata[key] = case key
30
+ when :scaffold_url then scaffold_url
31
+ when :first_url then scaffold_url.sub(PAGE_PLACEHOLDER, 1.to_s)
32
+ when :prev_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.prev.to_s)
33
+ when :page_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.page.to_s)
34
+ when :next_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.next.to_s)
35
+ when :last_url then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.last.to_s)
36
+ else pagy.send(key)
37
+ end
38
+ end
32
39
  end
33
- metadata
34
40
  end
35
41
 
36
42
  end