pagy 4.1.0 → 4.2.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,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,52 +8,71 @@ class Pagy
9
8
 
10
9
  # Pagination for Bulma: it returns the html with the series of links to the pages
11
10
  def pagy_bulma_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
11
+ link = pagy_link_proc(pagy)
13
12
 
14
- html = (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
15
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
16
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
17
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
18
- html << '<ul class="pagination-list">'
13
+ html = +%(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">)
14
+ html << pagy_bulma_prev_next_html(pagy, link)
15
+ html << %(<ul class="pagination-list">)
19
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
20
- html << if item.is_a?(Integer); %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
21
- elsif item.is_a?(String) ; %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
22
- elsif item == :gap ; %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>) # page gap
17
+ html << case item
18
+ when Integer then %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
19
+ when String then %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
20
+ when :gap then %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>) # page gap
23
21
  end
24
22
  end
25
- html << '</ul>'
26
- %(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
23
+ html << %(</ul></nav>)
27
24
  end
28
25
 
29
- # Javascript pagination for bulma: it returns a nav and a JSON tag used by the Pagy.nav javascript
30
26
  def pagy_bulma_nav_js(pagy, id=pagy_id)
31
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
32
- tags = { 'before' => ( (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
33
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
34
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
35
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>)) \
36
- + '<ul class="pagination-list">' ),
37
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}"))}</li>),
38
- 'active' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}</li>),
39
- 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
27
+ link = pagy_link_proc(pagy)
28
+ tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
29
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
30
+ 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}")}</li>),
31
+ 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap' }</span></li>),
40
32
  'after' => '</ul>' }
41
- %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
33
+
34
+ html = %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>)
35
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
42
36
  end
43
37
 
44
38
  # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
45
39
  def pagy_bulma_combo_nav_js(pagy, id=pagy_id)
46
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
47
-
48
- html = %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination">) \
49
- + %(<div class="field is-grouped is-grouped-centered" role="group">)
50
- html << (p_prev ? %(<p class="control">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"')}</p>)
51
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.prev')}</a></p>))
52
- input = %(<input class="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; margin:0 0.3rem;">)
53
- html << %(<div class="pagy-combo-input control level is-mobile">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>)
54
- html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
55
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
56
- html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
40
+ link = pagy_link_proc(pagy)
41
+ p_page = pagy.page
42
+ p_pages = pagy.pages
43
+ input = %(<input class="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; margin:0 0.3rem;">)
44
+
45
+ %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group">#{
46
+ if (p_prev = pagy.prev)
47
+ %(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
48
+ else
49
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>)
50
+ end
51
+ }<div class="pagy-combo-input control level is-mobile">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>#{
52
+ if (p_next = pagy.next)
53
+ %(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>)
54
+ else
55
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
56
+ end
57
+ }</div></nav>#{
58
+ pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))
59
+ })
57
60
  end
58
61
 
62
+ private
63
+
64
+ def pagy_bulma_prev_next_html(pagy, link)
65
+ html = if (p_prev = pagy.prev)
66
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"'
67
+ else
68
+ %(<a class="pagination-previous" disabled>#{pagy_t 'pagy.nav.prev'}</a>)
69
+ end
70
+ html << if (p_next = pagy.next)
71
+ link.call p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"'
72
+ else
73
+ %(<a class="pagination-next" disabled>#{pagy_t 'pagy.nav.next' }</a>)
74
+ end
75
+ end
76
+
59
77
  end
60
78
  end
@@ -1,17 +1,17 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/countless
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/countless'
6
5
 
7
6
  class Pagy
8
7
 
9
- module Backend ; private # the whole module is private so no problem with including it in a controller
8
+ module Backend
9
+ private # the whole module is private so no problem with including it in a controller
10
10
 
11
11
  # Return Pagy object and items
12
12
  def pagy_countless(collection, vars={})
13
13
  pagy = Pagy::Countless.new(pagy_countless_get_vars(collection, vars))
14
- return pagy, pagy_countless_get_items(collection, pagy)
14
+ [ pagy, pagy_countless_get_items(collection, pagy) ]
15
15
  end
16
16
 
17
17
  # Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
@@ -26,7 +26,8 @@ class Pagy
26
26
  items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
27
27
  items_size = items.size
28
28
  items.pop if items_size == pagy.items + 1
29
- pagy.finalize(items_size) # finalize may adjust pagy.items, so must be used after checking the size
29
+ # finalize may adjust pagy.items, so must be used after checking the size
30
+ pagy.finalize(items_size)
30
31
  items
31
32
  end
32
33
 
@@ -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,12 +39,13 @@ 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
@@ -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'
@@ -9,49 +8,73 @@ class Pagy
9
8
 
10
9
  # Pagination for Foundation: it returns the html with the series of links to the pages
11
10
  def pagy_foundation_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
11
+ link = pagy_link_proc(pagy)
13
12
 
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>))
13
+ html = +%(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">)
14
+ html << pagy_foundation_prev_html(pagy, link)
16
15
  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
16
+ html << case item
17
+ when Integer then %(<li>#{link.call item}</li>) # page link
18
+ when String then %(<li class="current">#{item}</li>) # active page
19
+ when :gap then %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
20
20
  end
21
21
  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>)
22
+ html << pagy_foundation_next_html(pagy, link)
23
+ html << %(</ul></nav>)
25
24
  end
26
25
 
27
26
  # Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
27
  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
+ link = pagy_link_proc(pagy)
29
+ tags = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html(pagy, link)}),
30
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>),
34
31
  'active' => %(<li class="current">#{pagy.page}</li>),
35
32
  '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)})
33
+ 'after' => %(#{pagy_foundation_next_html(pagy, link)}</ul>) }
34
+
35
+ html = %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>)
36
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
40
37
  end
41
38
 
42
39
  # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
43
40
  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))})
41
+ link = pagy_link_proc(pagy)
42
+ p_page = pagy.page
43
+ p_pages = pagy.pages
44
+ 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;">)
45
+
46
+ %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{
47
+ if (p_prev = pagy.prev)
48
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"'
49
+ else
50
+ %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>)
51
+ end
52
+ }<span class="input-group-label">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</span>#{
53
+ if (p_next = pagy.next)
54
+ link.call p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"'
55
+ else
56
+ %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
57
+ end
58
+ }</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
54
59
  end
55
60
 
61
+ private
62
+
63
+ def pagy_foundation_prev_html(pagy, link)
64
+ if (p_prev = pagy.prev)
65
+ %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
66
+ else
67
+ %(<li class="prev disabled">#{pagy_t 'pagy.nav.prev' }</li>)
68
+ end
69
+ end
70
+
71
+ def pagy_foundation_next_html(pagy, link)
72
+ if (p_next = pagy.next)
73
+ %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
74
+ else
75
+ %(<li class="next disabled">#{pagy_t 'pagy.nav.next'}</li>)
76
+ end
77
+ end
78
+
56
79
  end
57
80
  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(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,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'
@@ -12,32 +11,40 @@ class Pagy
12
11
 
13
12
  ITEMS_PLACEHOLDER = '__pagy_items__'
14
13
 
15
- module Items ; private
14
+ module UseItemsExtra
15
+ private
16
16
 
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
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)
23
+
24
+ define_method(meth) do |collection, vars|
25
+ vars[:items] ||= if (items = params[vars[:items_param] || VARS[:items_param]]) # :items from :items_param
21
26
  [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
27
+ end
28
+ super collection, vars
24
29
  end
25
30
  end
26
31
 
27
32
  end
28
- Backend.prepend Items
33
+ Backend.prepend UseItemsExtra
29
34
 
30
35
 
31
36
  module Frontend
32
37
 
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
38
+ module UseItemsExtra
39
+ def pagy_url_for(page, pagy, url=nil)
40
+ p_vars = pagy.vars
41
+ params = request.GET.merge(p_vars[:params])
42
+ params[p_vars[:page_param].to_s] = page
36
43
  params[p_vars[:items_param].to_s] = p_vars[:items]
37
44
  "#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
38
45
  end
39
46
  end
40
- prepend Items
47
+ prepend UseItemsExtra
41
48
 
42
49
  # Return the items selector HTML. For example "Show [20] items per page"
43
50
  def pagy_items_selector_js(pagy, id=pagy_id)
@@ -47,9 +54,11 @@ class Pagy
47
54
  link = pagy_marked_link(pagy_link_proc(pagy))
48
55
  p_vars[:items] = p_items # restore the items
49
56
 
50
- html = %(<span id="#{id}">)
57
+ html = %(<span id="#{id}">)
51
58
  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)})
59
+ html << pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items),
60
+ items_input: input,
61
+ count: p_items)
53
62
  html << %(</span>#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)})
54
63
  end
55
64
 
@@ -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 = (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
32
  '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)})
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(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
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