pagy 4.3.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,8 @@ class Pagy
16
16
 
17
17
  # Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
18
18
  def pagy_countless_get_vars(_collection, vars)
19
- vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
19
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
20
+ vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
20
21
  vars
21
22
  end
22
23
 
@@ -51,6 +51,7 @@ class Pagy
51
51
  # Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
52
52
  # the _collection argument is not available when the method is called
53
53
  def pagy_elasticsearch_rails_get_vars(_collection, vars)
54
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
54
55
  vars[:items] ||= VARS[:items]
55
56
  vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
56
57
  vars
@@ -7,10 +7,11 @@ class Pagy
7
7
  module Frontend
8
8
 
9
9
  # Pagination for Foundation: it returns the html with the series of links to the pages
10
- def pagy_foundation_nav(pagy)
11
- link = pagy_link_proc(pagy)
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)
12
13
 
13
- html = +%(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">)
14
+ html = +%(<nav#{p_id} class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">)
14
15
  html << pagy_foundation_prev_html(pagy, link)
15
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
17
  html << case item
@@ -24,26 +25,30 @@ class Pagy
24
25
  end
25
26
 
26
27
  # Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript
27
- def pagy_foundation_nav_js(pagy, id=pagy_id)
28
- link = pagy_link_proc(pagy)
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)
29
32
  tags = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html pagy, link}),
30
33
  'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>),
31
34
  'active' => %(<li class="current">#{pagy.page}</li>),
32
35
  'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
33
36
  'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }
34
37
 
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)
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))
37
40
  end
38
41
 
39
42
  # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40
- def pagy_foundation_combo_nav_js(pagy, id=pagy_id)
41
- link = pagy_link_proc(pagy)
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)
42
47
  p_page = pagy.page
43
48
  p_pages = pagy.pages
44
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;">)
45
50
 
46
- %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{
51
+ %(<nav#{p_id} class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{
47
52
  if (p_prev = pagy.prev)
48
53
  link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"'
49
54
  else
@@ -56,7 +61,7 @@ class Pagy
56
61
  %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
57
62
  end
58
63
  }</div></nav>#{
59
- pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
64
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
60
65
  })
61
66
  end
62
67
 
@@ -24,11 +24,11 @@ class Pagy
24
24
  countless = defined?(Pagy::Countless) && pagy.is_a?(Pagy::Countless)
25
25
  rels = { 'first' => 1, 'prev' => pagy.prev, 'next' => pagy.next }
26
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|
27
+ url_str = pagy_url_for(pagy, PAGE_PLACEHOLDER, absolute: true)
28
+ hash = { 'Link' => rels.filter_map do |rel, num|
29
29
  next unless num
30
30
  [ rel, url_str.sub(PAGE_PLACEHOLDER, num.to_s) ]
31
- end.compact.to_h }
31
+ end.to_h }
32
32
  headers = pagy.vars[:headers]
33
33
  hash[headers[:page]] = pagy.page.to_s if headers[:page]
34
34
  hash[headers[:items]] = pagy.vars[:items].to_s if headers[:items]
@@ -9,59 +9,43 @@ class Pagy
9
9
  VARS[:items_param] = :items
10
10
  VARS[:max_items] = 100
11
11
 
12
+ VARS[:enable_items_extra] = true
13
+
12
14
  ITEMS_PLACEHOLDER = '__pagy_items__'
13
15
 
14
- module UseItemsExtra
15
- private
16
+ module UseItemsExtra; end
16
17
 
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)
18
+ module Backend
19
+ private
23
20
 
24
- define_method(meth) do |collection, vars|
25
- vars[:items] ||= if (items = params[vars[:items_param] || VARS[:items_param]]) # :items from :items_param
26
- [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
27
- end
28
- super collection, vars
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
29
26
  end
30
- end
31
27
 
32
28
  end
33
- Backend.prepend UseItemsExtra
34
-
35
29
 
36
30
  module Frontend
37
31
 
38
- module UseItemsExtra
39
-
40
- def pagy_url_for(page, pagy, url=nil)
41
- p_vars = pagy.vars
42
- params = request.GET.merge(p_vars[:params])
43
- params[p_vars[:page_param].to_s] = page
44
- params[p_vars[:items_param].to_s] = p_vars[:items]
45
- "#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
46
- end
47
-
48
- end
49
- prepend UseItemsExtra
50
-
51
32
  # Return the items selector HTML. For example "Show [20] items per page"
52
- 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
53
37
  p_vars = pagy.vars
54
38
  p_items = p_vars[:items]
55
39
  p_vars[:items] = ITEMS_PLACEHOLDER
56
- link = pagy_marked_link(pagy_link_proc(pagy))
40
+ link = pagy_marked_link(pagy_link_proc(pagy, link_extra: link_extra))
57
41
  p_vars[:items] = p_items # restore the items
58
42
 
59
- html = %(<span id="#{id}">)
43
+ html = %(<span#{p_id} class="pagy-items-selector-js">)
60
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;">)
61
- html << pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items),
45
+ html << pagy_t('pagy.items_selector_js', item_name: item_name || pagy_t(i18n_key || p_vars[:i18n_key], count: p_items),
62
46
  items_input: input,
63
47
  count: p_items)
64
- html << %(</span>#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)})
48
+ html << %(</span>#{pagy_json_tag pagy, :items_selector, pagy.from, link})
65
49
  end
66
50
 
67
51
  end
@@ -7,10 +7,11 @@ class Pagy
7
7
  module Frontend
8
8
 
9
9
  # Pagination for materialize: it returns the html with the series of links to the pages
10
- def pagy_materialize_nav(pagy)
11
- link = pagy_link_proc(pagy)
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)
12
13
 
13
- html = +%(<div class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">)
14
+ html = +%(<div#{p_id} class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">)
14
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
17
  html << case item
@@ -24,34 +25,39 @@ class Pagy
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 = pagy_link_proc(pagy)
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
+
29
33
  tags = { 'before' => %(<ul class="pagination">#{pagy_materialize_prev_html pagy, link}),
30
34
  'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
31
35
  'active' => %(<li class="active">#{mark}</li>),
32
36
  'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>),
33
37
  'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) }
34
38
 
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
+ 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))
37
41
  end
38
42
 
39
43
  # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40
- def pagy_materialize_combo_nav_js(pagy, id=pagy_id)
41
- link = pagy_link_proc(pagy)
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)
42
48
  p_page = pagy.page
43
49
  p_pages = pagy.pages
44
50
  style = ' style="vertical-align: middle;"'
45
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;">)
46
52
 
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;">#{
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;">#{
48
54
  pagy_materialize_prev_html pagy, link, style
49
55
  }<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{
50
56
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
51
57
  }</div>#{
52
58
  pagy_materialize_next_html pagy, link, style
53
59
  }</ul></div>#{
54
- pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
60
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
55
61
  })
56
62
  end
57
63
 
@@ -16,13 +16,14 @@ class Pagy
16
16
 
17
17
  include Helpers
18
18
 
19
- def pagy_metadata(pagy, url=nil)
19
+ def pagy_metadata(pagy, deprecated_url=nil, absolute: nil)
20
+ absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
20
21
  names = pagy.vars[:metadata]
21
22
  unknown = names - METADATA
22
23
  raise VariableError.new(pagy), "unknown metadata #{unknown.inspect}" \
23
24
  unless unknown.empty?
24
25
 
25
- scaffold_url = pagy_url_for(PAGE_PLACEHOLDER, pagy, url)
26
+ scaffold_url = pagy_url_for(pagy, PAGE_PLACEHOLDER, absolute: absolute)
26
27
  {}.tap do |metadata|
27
28
  names.each do |key|
28
29
  metadata[key] = case key
@@ -7,33 +7,37 @@ class Pagy
7
7
  module Frontend
8
8
 
9
9
  # Javascript pagination: it returns a nav and a JSON tag used by the Pagy.nav javascript
10
- def pagy_nav_js(pagy, id=pagy_id)
11
- link = pagy_link_proc(pagy)
10
+ def pagy_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
11
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
12
+ p_id = %( id="#{pagy_id}") if pagy_id
13
+ link = pagy_link_proc(pagy, link_extra: link_extra)
12
14
  tags = { 'before' => pagy_nav_prev_html(pagy, link),
13
15
  'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER)}</span> ),
14
16
  'active' => %(<span class="page active">#{pagy.page}</span> ),
15
17
  'gap' => %(<span class="page gap">#{pagy_t 'pagy.nav.gap'}</span> ),
16
18
  'after' => pagy_nav_next_html(pagy, link) }
17
19
 
18
- html = %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>)
19
- html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
20
+ html = %(<nav#{p_id} class="pagy-njs pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>)
21
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
20
22
  end
21
23
 
22
24
  # Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
23
- def pagy_combo_nav_js(pagy, id=pagy_id)
24
- link = pagy_link_proc(pagy)
25
+ def pagy_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
26
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
27
+ p_id = %( id="#{pagy_id}") if pagy_id
28
+ link = pagy_link_proc(pagy, link_extra: link_extra)
25
29
  p_page = pagy.page
26
30
  p_pages = pagy.pages
27
31
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
28
32
 
29
- %(<nav id="#{id}" class="pagy-combo-nav-js pagination" role="navigation" aria-label="pager">#{
33
+ %(<nav#{p_id} class="pagy-combo-nav-js pagination" role="navigation" aria-label="pager">#{
30
34
  pagy_nav_prev_html pagy, link
31
35
  }<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{
32
36
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
33
37
  }</span> #{
34
38
  pagy_nav_next_html pagy, link
35
39
  }</nav>#{
36
- pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
40
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
37
41
  })
38
42
  end
39
43
 
@@ -49,6 +49,7 @@ class Pagy
49
49
  # Sub-method called only by #pagy_searchkick: here for easy customization of variables by overriding
50
50
  # the _collection argument is not available when the method is called
51
51
  def pagy_searchkick_get_vars(_collection, vars)
52
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
52
53
  vars[:items] ||= VARS[:items]
53
54
  vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
54
55
  vars
@@ -7,16 +7,17 @@ class Pagy
7
7
  module Frontend
8
8
 
9
9
  # Pagination for semantic: it returns the html with the series of links to the pages
10
- def pagy_semantic_nav(pagy)
11
- link = pagy_link_proc(pagy, 'class="item"')
10
+ def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '')
11
+ p_id = %( id="#{pagy_id}") if pagy_id
12
+ link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}"))
12
13
 
13
- html = +%(<div class="pagy-semantic-nav ui pagination menu" aria-label="pager">)
14
+ html = +%(<div#{p_id} class="pagy-semantic-nav ui pagination menu" aria-label="pager">)
14
15
  html << pagy_semantic_prev_html(pagy, link)
15
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
17
  html << case item
17
18
  when Integer then link.call item # page link
18
19
  when String then %(<a class="item active">#{item}</a>) # current page
19
- when :gap then %(<div class="disabled item">...</div>) # page gap
20
+ when :gap then %(<div class="disabled item">#{pagy_t 'pagy.nav.gap'}</div>) # page gap
20
21
  end
21
22
  end
22
23
  html << pagy_semantic_next_html(pagy, link)
@@ -24,33 +25,37 @@ class Pagy
24
25
  end
25
26
 
26
27
  # Javascript pagination for semantic: it returns a nav and a JSON tag used by the Pagy.nav javascript
27
- def pagy_semantic_nav_js(pagy, id=pagy_id)
28
- link = pagy_link_proc(pagy, 'class="item"')
28
+ def pagy_semantic_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: %(class="item" #{link_extra}))
29
32
  tags = { 'before' => pagy_semantic_prev_html(pagy, link),
30
33
  'link' => link.call(PAGE_PLACEHOLDER),
31
34
  'active' => %(<a class="item active">#{pagy.page}</a>),
32
35
  'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
33
36
  'after' => pagy_semantic_next_html(pagy, link) }
34
37
 
35
- html = %(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>)
36
- html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
38
+ html = %(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>)
39
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
37
40
  end
38
41
 
39
42
  # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40
- def pagy_semantic_combo_nav_js(pagy, id=pagy_id)
41
- link = pagy_link_proc(pagy, 'class="item"')
43
+ def pagy_semantic_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: %(class="item" #{link_extra}))
42
47
  p_page = pagy.page
43
48
  p_pages = pagy.pages
44
49
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin: 0 0.3rem">)
45
50
 
46
- %(<div id="#{id}" class="pagy-semantic-combo-nav-js ui compact menu" role="navigation" aria-label="pager">#{
51
+ %(<div#{p_id} class="pagy-semantic-combo-nav-js ui compact menu" role="navigation" aria-label="pager">#{
47
52
  pagy_semantic_prev_html pagy, link
48
53
  }<div class="pagy-combo-input item">#{
49
54
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
50
55
  }</div> #{
51
56
  pagy_semantic_next_html pagy, link
52
57
  }</div>#{
53
- pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
58
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
54
59
  })
55
60
  end
56
61
 
@@ -16,8 +16,8 @@ class Pagy
16
16
  # "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
17
17
  # "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
18
18
  # Notice: if :steps is false it will use the single {0 => @vars[:size]} size
19
- def sequels
20
- steps = @vars[:steps] || {0 => @vars[:size]}
19
+ def sequels(steps=nil)
20
+ steps ||= @vars[:steps] || {0 => @vars[:size]}
21
21
  raise VariableError.new(self), "expected :steps to define the 0 width; got #{steps.inspect}" \
22
22
  unless steps.key?(0)
23
23
  {}.tap do |sequels|
@@ -30,23 +30,18 @@ class Pagy
30
30
  if defined?(Oj)
31
31
  # it returns a script tag with the JSON-serialized args generated with the faster oj gem
32
32
  def pagy_json_tag(pagy, *args)
33
- args << pagy.vars[:page_param] if pagy.vars[:trim]
33
+ args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
34
34
  %(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
35
35
  end
36
36
  else
37
37
  require 'json'
38
38
  # it returns a script tag with the JSON-serialized args generated with the slower to_json
39
39
  def pagy_json_tag(pagy, *args)
40
- args << pagy.vars[:page_param] if pagy.vars[:trim]
40
+ args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
41
41
  %(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
42
42
  end
43
43
  end
44
44
 
45
- # it returns the SHA1 (fastest on modern ruby) string used as default `id` attribute by all the `*_js` tags
46
- def pagy_id
47
- "pagy-#{Digest::SHA1.hexdigest(caller(2..2)[0].split(':in')[0])}"
48
- end
49
-
50
45
  # it returns the marked link to used by pagy.js
51
46
  def pagy_marked_link(link)
52
47
  link.call PAGE_PLACEHOLDER, '', 'style="display: none;"'
@@ -0,0 +1,71 @@
1
+ # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/standalone
2
+ # frozen_string_literal: true
3
+
4
+ require 'uri'
5
+ class Pagy
6
+
7
+ # extracted from Rack::Utils and reformatted for rubocop
8
+ module QueryUtils
9
+ module_function
10
+ def escape(str)
11
+ URI.encode_www_form_component(str)
12
+ end
13
+ def build_nested_query(value, prefix = nil)
14
+ case value
15
+ when Array
16
+ value.map { |v| build_nested_query(v, "#{prefix}[]") }.join('&')
17
+ when Hash
18
+ value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) }.delete_if(&:empty?).join('&')
19
+ when nil
20
+ prefix
21
+ else
22
+ raise ArgumentError, 'value must be a Hash' if prefix.nil?
23
+ "#{prefix}=#{escape(value)}"
24
+ end
25
+ end
26
+ end
27
+
28
+ module UseStandaloneExtra
29
+ # without any :url var it works exactly as the regular #pagy_url_for;
30
+ # with a defined :url variable it does not use rack/request
31
+ def pagy_url_for(pagy, page, deprecated_url=nil, absolute: nil)
32
+ absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
33
+ pagy, page = Pagy.deprecated_order(pagy, page) if page.is_a?(Pagy)
34
+ p_vars = pagy.vars
35
+ if p_vars[:url]
36
+ url_string = p_vars[:url]
37
+ params = {}
38
+ else
39
+ url_string = "#{request.base_url if absolute}#{request.path}"
40
+ params = request.GET
41
+ end
42
+ params = params.merge(p_vars[:params])
43
+ params[p_vars[:page_param].to_s] = page
44
+ params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(UseItemsExtra)
45
+ query_string = "?#{QueryUtils.build_nested_query(pagy_get_params(params))}" unless params.empty?
46
+ "#{url_string}#{query_string}#{p_vars[:fragment]}"
47
+ end
48
+ end
49
+ Helpers.prepend UseStandaloneExtra
50
+
51
+ # defines a dummy #params method if not already defined in the including module
52
+ module Backend
53
+ def self.included(controller)
54
+ controller.define_method(:params){{}} unless controller.method_defined?(:params)
55
+ end
56
+ end
57
+
58
+ # include Pagy::Console in irb/rails console for a ready to use pagy environment
59
+ module Console
60
+ def self.included(main)
61
+ main.include(Backend)
62
+ main.include(Frontend)
63
+ VARS[:url] = 'http://www.example.com/subdir'
64
+ end
65
+
66
+ def pagy_extras(*extras)
67
+ extras.each {|extra| require "pagy/extras/#{extra}"}
68
+ end
69
+ end
70
+
71
+ end