pagy 3.8.0 → 4.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/lib/config/pagy.rb +36 -25
- data/lib/javascripts/pagy.js +104 -93
- data/lib/locales/ar.yml +26 -0
- data/lib/locales/bg.yml +2 -2
- data/lib/locales/bs.yml +24 -0
- data/lib/locales/ca.yml +2 -2
- data/lib/locales/cs.yml +22 -0
- data/lib/locales/da.yml +2 -2
- data/lib/locales/de.yml +2 -2
- data/lib/locales/en.yml +2 -2
- data/lib/locales/es.yml +2 -2
- data/lib/locales/fr.yml +2 -2
- data/lib/locales/hr.yml +24 -0
- data/lib/locales/id.yml +2 -2
- data/lib/locales/it.yml +2 -2
- data/lib/locales/ja.yml +2 -2
- data/lib/locales/km.yml +2 -2
- data/lib/locales/ko.yml +2 -2
- data/lib/locales/nb.yml +2 -2
- data/lib/locales/nl.yml +2 -2
- data/lib/locales/pl.yml +2 -2
- data/lib/locales/pt-BR.yml +2 -2
- data/lib/locales/pt.yml +22 -0
- data/lib/locales/ru.yml +2 -2
- data/lib/locales/sr.yml +23 -0
- data/lib/locales/sv-SE.yml +2 -2
- data/lib/locales/sv.yml +2 -2
- data/lib/locales/sw.yml +22 -0
- data/lib/locales/tr.yml +2 -2
- data/lib/locales/uk.yml +24 -0
- data/lib/locales/utils/i18n.rb +3 -11
- data/lib/locales/utils/loader.rb +7 -10
- data/lib/locales/utils/p11n.rb +49 -17
- data/lib/locales/zh-CN.yml +2 -2
- data/lib/locales/zh-HK.yml +2 -2
- data/lib/locales/zh-TW.yml +3 -3
- data/lib/pagy.rb +50 -26
- data/lib/pagy/backend.rb +5 -3
- data/lib/pagy/console.rb +21 -0
- data/lib/pagy/countless.rb +13 -10
- data/lib/pagy/deprecation.rb +27 -0
- data/lib/pagy/exceptions.rb +7 -2
- data/lib/pagy/extras/arel.rb +4 -3
- data/lib/pagy/extras/array.rb +4 -3
- data/lib/pagy/extras/bootstrap.rb +61 -30
- data/lib/pagy/extras/bulma.rb +60 -38
- data/lib/pagy/extras/countless.rb +7 -8
- data/lib/pagy/extras/elasticsearch_rails.rb +28 -17
- data/lib/pagy/extras/foundation.rb +61 -32
- data/lib/pagy/extras/headers.rb +13 -9
- data/lib/pagy/extras/i18n.rb +11 -5
- data/lib/pagy/extras/items.rb +21 -34
- data/lib/pagy/extras/materialize.rb +58 -35
- data/lib/pagy/extras/meilisearch.rb +55 -0
- data/lib/pagy/extras/metadata.rb +27 -21
- data/lib/pagy/extras/navs.rb +44 -20
- data/lib/pagy/extras/overflow.rb +52 -48
- data/lib/pagy/extras/searchkick.rb +27 -16
- data/lib/pagy/extras/semantic.rb +55 -30
- data/lib/pagy/extras/shared.rb +14 -15
- data/lib/pagy/extras/standalone.rb +62 -0
- data/lib/pagy/extras/support.rb +26 -9
- data/lib/pagy/extras/trim.rb +12 -11
- data/lib/pagy/extras/uikit.rb +54 -35
- data/lib/pagy/frontend.rb +64 -32
- metadata +16 -7
- data/lib/locales/README.md +0 -35
- data/lib/pagy/extras/pagy_search.rb +0 -18
- data/pagy.gemspec +0 -16
data/lib/pagy/extras/bulma.rb
CHANGED
@@ -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'
|
@@ -8,53 +7,76 @@ class Pagy
|
|
8
7
|
module Frontend
|
9
8
|
|
10
9
|
# Pagination for Bulma: it returns the html with the series of links to the pages
|
11
|
-
def pagy_bulma_nav(pagy)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
html << '<ul class="pagination-list">'
|
10
|
+
def pagy_bulma_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 = +%(<nav#{p_id} class="pagy-bulma-nav pagination is-centered" aria-label="pagination">)
|
15
|
+
html << pagy_bulma_prev_next_html(pagy, link)
|
16
|
+
html << %(<ul class="pagination-list">)
|
19
17
|
pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
|
20
|
-
html <<
|
21
|
-
|
22
|
-
|
18
|
+
html << case item
|
19
|
+
when Integer then %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
|
20
|
+
when String then %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
|
21
|
+
when :gap then %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>) # page gap
|
23
22
|
end
|
24
23
|
end
|
25
|
-
html <<
|
26
|
-
%(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
|
24
|
+
html << %(</ul></nav>)
|
27
25
|
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
+
def pagy_bulma_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
|
28
|
+
pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
|
29
|
+
p_id = %( id="#{pagy_id}") if pagy_id
|
30
|
+
link = pagy_link_proc(pagy, link_extra: link_extra)
|
31
|
+
tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
|
32
|
+
'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
|
33
|
+
'active' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}")}</li>),
|
34
|
+
'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap' }</span></li>),
|
40
35
|
'after' => '</ul>' }
|
41
|
-
|
36
|
+
|
37
|
+
%(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" aria-label="pagination" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
|
42
38
|
end
|
43
39
|
|
44
40
|
# Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
|
45
|
-
def pagy_bulma_combo_nav_js(pagy,
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
41
|
+
def pagy_bulma_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
|
42
|
+
pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
|
43
|
+
p_id = %( id="#{pagy_id}") if pagy_id
|
44
|
+
link = pagy_link_proc(pagy, link_extra: link_extra)
|
45
|
+
p_page = pagy.page
|
46
|
+
p_pages = pagy.pages
|
47
|
+
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;">)
|
48
|
+
|
49
|
+
%(<nav#{p_id} class="pagy-bulma-combo-nav-js" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group" #{
|
50
|
+
pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
|
51
|
+
}>#{
|
52
|
+
if (p_prev = pagy.prev)
|
53
|
+
%(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
|
54
|
+
else
|
55
|
+
%(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>)
|
56
|
+
end
|
57
|
+
}<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>#{
|
58
|
+
if (p_next = pagy.next)
|
59
|
+
%(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>)
|
60
|
+
else
|
61
|
+
%(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
|
62
|
+
end
|
63
|
+
}</div></nav>)
|
57
64
|
end
|
58
65
|
|
66
|
+
private
|
67
|
+
|
68
|
+
def pagy_bulma_prev_next_html(pagy, link)
|
69
|
+
html = +if (p_prev = pagy.prev)
|
70
|
+
link.call p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"'
|
71
|
+
else
|
72
|
+
%(<a class="pagination-previous" disabled>#{pagy_t 'pagy.nav.prev'}</a>)
|
73
|
+
end
|
74
|
+
html << if (p_next = pagy.next)
|
75
|
+
link.call p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"'
|
76
|
+
else
|
77
|
+
%(<a class="pagination-next" disabled>#{pagy_t 'pagy.nav.next' }</a>)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
59
81
|
end
|
60
82
|
end
|
@@ -1,25 +1,23 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
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
|
13
10
|
|
14
11
|
# Return Pagy object and items
|
15
12
|
def pagy_countless(collection, vars={})
|
16
13
|
pagy = Pagy::Countless.new(pagy_countless_get_vars(collection, vars))
|
17
|
-
|
14
|
+
[ pagy, pagy_countless_get_items(collection, pagy) ]
|
18
15
|
end
|
19
16
|
|
20
17
|
# Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
|
21
18
|
def pagy_countless_get_vars(_collection, vars)
|
22
|
-
vars
|
19
|
+
pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
|
20
|
+
vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
|
23
21
|
vars
|
24
22
|
end
|
25
23
|
|
@@ -29,7 +27,8 @@ class Pagy
|
|
29
27
|
items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
|
30
28
|
items_size = items.size
|
31
29
|
items.pop if items_size == pagy.items + 1
|
32
|
-
|
30
|
+
# finalize may adjust pagy.items, so must be used after checking the size
|
31
|
+
pagy.finalize(items_size)
|
33
32
|
items
|
34
33
|
end
|
35
34
|
|
@@ -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
|
-
|
10
|
-
|
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
|
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,
|
27
|
-
vars
|
28
|
-
|
29
|
-
|
30
|
-
response
|
31
|
-
total
|
32
|
-
vars[:count]
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
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 =
|
15
|
-
|
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 <<
|
18
|
-
|
19
|
-
|
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 << (
|
23
|
-
|
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,
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
'link' => %(<li>#{link.call
|
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' =>
|
37
|
-
|
38
|
-
|
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,
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
input
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
data/lib/pagy/extras/headers.rb
CHANGED
@@ -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
|
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
|
-
|
19
|
-
|
20
|
-
|
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
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
data/lib/pagy/extras/i18n.rb
CHANGED
@@ -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,11 +7,18 @@ class Pagy
|
|
8
7
|
|
9
8
|
::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
|
10
9
|
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
+
module UseI18nGem
|
17
|
+
def pagy_t(key, **opts)
|
18
|
+
::I18n.t(key, **opts)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
prepend UseI18nGem
|
16
22
|
|
17
23
|
end
|
18
24
|
end
|
data/lib/pagy/extras/items.rb
CHANGED
@@ -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
|
-
|
16
|
-
module Backend ; private
|
16
|
+
module UseItemsExtra; end
|
17
17
|
|
18
|
-
|
19
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
|
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,
|
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
|
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 <<
|
61
|
-
|
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
|