pagy 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c829d223b7c780665135815222ed37bfe250d61e4e9c8f8083b829940d7bedd7
4
- data.tar.gz: c3764805ed4bdbdd692ed71727703e4b007cdeefb9477dccb465c7d8cb327f1b
3
+ metadata.gz: 1c340603d318a1982f676209661b8357a2c386c93e6144c0f950dbad19645a23
4
+ data.tar.gz: 13bd39e2660aff66151c245801a8502320392cd0e62faf82f6042caf45e59603
5
5
  SHA512:
6
- metadata.gz: 9af20f32dae70bc7baa773124283af036a516e419b8e35631798a1bcc29fe99468724032cbddbe84495a92b486016e99e8478d8e8936b31732897ce9d34397cf
7
- data.tar.gz: 9f3c149eeb720c9afd7d4dbe923197c4764a9e05f70c227aef72282c61146389d75709f74c075ae79aa0c6c59bf7fa88a152aaf8a48f41bf4f004fee1c2a16f8
6
+ metadata.gz: f2562e79aa12b299e2e4594d595255d3de4e401ef406c0b2a6228ab2924226905cd4531fbf9c0f563c3680f8e56ef0d13229b3a25be26e6c590cbc1073f9b944
7
+ data.tar.gz: 302da621bcf61a0a2ae97e4ed96d0e7477abec46d50bf3128ed850ff1edf1b5f88e6bcc3f79161907051f4015cf158e37a6bbcfd001609557decbd7569b979a1
data/lib/config/pagy.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
- # Pagy initializer file (3.1.0)
4
+ # Pagy initializer file (3.2.0)
5
5
  # Customize only what you really need and notice that Pagy works also without any of the following lines.
6
6
  # Should you just cherry pick part of this file, please maintain the require-order of the extras
7
7
 
@@ -11,12 +11,12 @@ Pagy.init = function(arg){
11
11
  }
12
12
  };
13
13
 
14
- Pagy.nav = function(id, tags, sequels){
14
+ Pagy.nav = function(id, tags, sequels, param){
15
15
  var pagyEl = document.getElementById(id),
16
16
  container = pagyEl.parentElement,
17
17
  lastWidth = undefined,
18
18
  timeoutId = 0,
19
- markRE = new RegExp('--pagy.page--', 'g'),
19
+ pageREg = new RegExp(/__pagy_page__/g),
20
20
  widths = [];
21
21
  for (var width in sequels) { widths.push(parseInt(width)) } // fine with sequels structure
22
22
  widths.sort(function(a, b){return b-a});
@@ -28,17 +28,18 @@ Pagy.nav = function(id, tags, sequels){
28
28
  if (container.clientWidth > widths[i]) { width = widths[i]; break }
29
29
  }
30
30
  if (width !== lastWidth) {
31
- while (pagyEl.firstChild) { pagyEl.removeChild(pagyEl.firstChild) }
32
31
  var html = tags.before,
33
32
  series = sequels[width];
34
33
  for (i = 0, len = series.length; i < len; i++) {
35
34
  var item = series[i];
36
- if (typeof(item) === 'number') { html += tags.link.replace(markRE, item) }
37
- else if (item === 'gap') { html += tags.gap }
38
- else if (typeof(item) === 'string') { html += tags.active.replace(markRE, item) }
35
+ if (typeof(param) === 'string' && item === 1) { html += Pagy.trim(tags.link.replace(pageREg, item), param) }
36
+ else if (typeof(item) === 'number') { html += tags.link.replace(pageREg, item) }
37
+ else if (item === 'gap') { html += tags.gap }
38
+ else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
39
39
  }
40
40
  html += tags.after;
41
- pagyEl.insertAdjacentHTML('beforeend', html);
41
+ pagyEl.innerHTML = '';
42
+ pagyEl.insertAdjacentHTML('afterbegin', html);
42
43
  lastWidth = width;
43
44
  }
44
45
  },
@@ -54,35 +55,31 @@ Pagy.nav = function(id, tags, sequels){
54
55
  render();
55
56
  };
56
57
 
57
- Pagy.combo_nav = function(id, page, links){
58
+ Pagy.combo_nav = function(id, page, link, param){
58
59
  var pagyEl = document.getElementById(id),
59
60
  input = pagyEl.getElementsByTagName('input')[0],
60
61
  go = function(){
61
62
  if (page !== input.value) {
62
- if (links.hasOwnProperty('trimmed') && input.value === '1') {
63
- pagyEl.insertAdjacentHTML('afterbegin', links.trimmed);
64
- } else {
65
- pagyEl.insertAdjacentHTML('afterbegin', links.standard.replace('--pagy.page--', input.value));
66
- }
63
+ var html = link.replace(/__pagy_page__/, input.value);
64
+ if (typeof(param) === 'string' && input.value === '1') { html = Pagy.trim(html, param) }
65
+ pagyEl.insertAdjacentHTML('afterbegin', html);
67
66
  pagyEl.getElementsByTagName('a')[0].click();
68
67
  }
69
68
  };
70
69
  Pagy.addInputEventListeners(input, go);
71
70
  };
72
71
 
73
- Pagy.items_selector = function(id, from, links){
72
+ Pagy.items_selector = function(id, from, link, param){
74
73
  var pagyEl = document.getElementById(id),
75
74
  input = pagyEl.getElementsByTagName('input')[0],
76
75
  current = input.value,
77
76
  go = function(){
78
77
  var items = input.value;
79
78
  if (current !== items) {
80
- var page = Math.max(Math.ceil(from / items),1);
81
- if (links.hasOwnProperty('trimmed') && page === 1) {
82
- pagyEl.insertAdjacentHTML('afterbegin', links.trimmed.replace('--pagy.items--', items));
83
- } else {
84
- pagyEl.insertAdjacentHTML('afterbegin', links.standard.replace('--pagy.page--', page).replace('--pagy.items--', items));
85
- }
79
+ var page = Math.max(Math.ceil(from / items),1),
80
+ html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, items);
81
+ if (typeof(param) === 'string' && page === 1){ html = Pagy.trim(html, param) }
82
+ pagyEl.insertAdjacentHTML('afterbegin', html);
86
83
  pagyEl.getElementsByTagName('a')[0].click();
87
84
  }
88
85
  };
@@ -99,3 +96,8 @@ Pagy.addInputEventListeners = function(input, handler){
99
96
  // … and when pressing enter inside the input
100
97
  input.addEventListener('keyup', function(e){ if (e.which === 13) handler() }.bind(this));
101
98
  };
99
+
100
+ Pagy.trim = function(html, param){
101
+ var re = new RegExp('[?&]' + param + '=1(?![&])|(?<=[?&])' + param + '=1&');
102
+ return html.replace(re, '');
103
+ };
data/lib/pagy.rb CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  require 'pathname'
6
6
 
7
- class Pagy ; VERSION = '3.1.0'
7
+ class Pagy ; VERSION = '3.2.0'
8
8
 
9
9
  class OverflowError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end
10
10
 
@@ -34,7 +34,7 @@ class Pagy
34
34
  'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>),
35
35
  'after' => p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li></ul>)
36
36
  : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li></ul>) }
37
- %(<nav id="#{id}" class="pagy-bootstrap-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
37
+ %(<nav id="#{id}" class="pagy-bootstrap-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
38
38
  end
39
39
 
40
40
  # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -48,7 +48,7 @@ class Pagy
48
48
  html << %(<div class="pagy-combo-input btn btn-primary disabled" style="white-space: nowrap;">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>)
49
49
  html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
50
50
  : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
51
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
51
+ html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
52
52
  end
53
53
 
54
54
  end
@@ -38,7 +38,7 @@ class Pagy
38
38
  'active' => %(<li>#{link.call(MARK, MARK, %(class="pagination-link is-current" aria-current="page" aria-label="page #{MARK}"))}</li>),
39
39
  'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
40
40
  'after' => '</ul>' }
41
- %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
41
+ %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
42
42
  end
43
43
 
44
44
  # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -53,7 +53,7 @@ class Pagy
53
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
54
  html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
55
55
  : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
56
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
56
+ html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
57
57
  end
58
58
 
59
59
  end
@@ -36,7 +36,7 @@ class Pagy
36
36
  'after' => ( (p_next ? %(<li class="next">#{link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next"')}</li>)
37
37
  : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>)) \
38
38
  + '</ul>' ) }
39
- %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
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])})
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
@@ -50,7 +50,7 @@ class Pagy
50
50
  html << %(<span class="input-group-label">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</span>)
51
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
52
  : %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
53
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
53
+ html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
54
54
  end
55
55
 
56
56
  end
@@ -49,14 +49,14 @@ class Pagy
49
49
  def pagy_items_selector_js(pagy, id=pagy_id)
50
50
  p_vars = pagy.vars
51
51
  p_items = p_vars[:items]
52
- p_vars[:items] = '--pagy.items--'
53
- links = pagy_links(pagy_link_proc(pagy))
52
+ p_vars[:items] = '__pagy_items__'
53
+ link = pagy_marked_link(pagy_link_proc(pagy))
54
54
  p_vars[:items] = p_items # restore the items
55
55
 
56
56
  html = EMPTY + %(<span id="#{id}">)
57
57
  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;">)
58
58
  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 << %(</span>#{pagy_json_tag(:items_selector, id, pagy.from, links)})
59
+ html << %(</span>#{pagy_json_tag(:items_selector, id, pagy.from, link, defined?(TRIM) && p_vars[:page_param])})
60
60
  end
61
61
 
62
62
  end
@@ -35,7 +35,7 @@ class Pagy
35
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
36
  : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>)) \
37
37
  + '</ul>' ) }
38
- %(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
38
+ %(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
39
39
  end
40
40
 
41
41
  # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -52,7 +52,7 @@ class Pagy
52
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
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
54
  : %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
55
- html << %(</ul></div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
55
+ html << %(</ul></div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
56
56
  end
57
57
 
58
58
  end
@@ -17,7 +17,7 @@ class Pagy
17
17
  'gap' => %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ),
18
18
  'after' => p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
19
19
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>) }
20
- %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
20
+ %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
21
21
  end
22
22
 
23
23
  # Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -31,7 +31,7 @@ class Pagy
31
31
  html << %(<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</span> )
32
32
  html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
33
33
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
34
- html << %(</nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
34
+ html << %(</nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
35
35
  end
36
36
 
37
37
  end
@@ -34,7 +34,7 @@ class Pagy
34
34
  'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
35
35
  'after' => (p_next ? %(#{link.call(p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"')})
36
36
  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>)) }
37
- %(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels)})
37
+ %(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
38
38
  end
39
39
 
40
40
  # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -48,7 +48,7 @@ class Pagy
48
48
  html << %(<div class="pagy-combo-input item">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div> )
49
49
  html << (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
50
50
  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
51
- html << %(</div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_links(link))})
51
+ html << %(</div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
52
52
  end
53
53
 
54
54
  end
@@ -43,11 +43,9 @@ class Pagy
43
43
  "pagy-#{Digest::SHA1.hexdigest(caller(2..2)[0].split(':in')[0])}"
44
44
  end
45
45
 
46
- # it return the links hash to used by pagy.js
47
- def pagy_links(link)
48
- links = { 'standard' => link.call(MARK, '', 'style="display: none;"') }
49
- links['trimmed'] = link.call(1, '', 'style="display: none;"') if defined?(TRIM)
50
- links
46
+ # it returns the marked link to used by pagy.js
47
+ def pagy_marked_link(link)
48
+ link.call(MARK, '', 'style="display: none;"')
51
49
  end
52
50
 
53
51
  end
@@ -6,29 +6,18 @@ class Pagy
6
6
 
7
7
  module Frontend
8
8
 
9
- # boolean used by the compact navs
10
- TRIM = true
9
+ TRIM = true # boolean used by *_js helpers
11
10
 
12
11
  alias_method :pagy_link_proc_without_trim, :pagy_link_proc
13
12
  def pagy_link_proc_with_trim(pagy, link_extra='')
14
- p_prev, p_next, p_vars = pagy.prev, pagy.next, pagy.vars
15
- marked_url = pagy_url_for(MARK, pagy)
16
- page1_url = pagy_trim_url(marked_url, "#{p_vars[:page_param]}=#{MARK}")
17
- page1_link = %(<a href="#{page1_url}" #{p_vars[:link_extra]} #{link_extra})
18
- a, b = %(<a href="#{marked_url}" #{p_vars[:link_extra]} #{link_extra}).split(MARK, 2)
19
- lambda{|n, text=n, extra=''| start = n.to_i == 1 ? page1_link : "#{a}#{n}#{b}"
20
- "#{start}#{ if n == p_prev ; ' rel="prev"'
21
- elsif n == p_next ; ' rel="next"'
22
- else '' end } #{extra}>#{text}</a>"}
13
+ link_proc = pagy_link_proc_without_trim(pagy, link_extra)
14
+ page_param = pagy.vars[:page_param]
15
+ lambda do |n, text=n, extra=''|
16
+ link = link_proc.call(n, text, extra)
17
+ n == 1 ? link.sub(/[?&]#{page_param}=1(?![&])|\b(?<=[?&])#{page_param}=1&/, '') : link
18
+ end
23
19
  end
24
20
  alias_method :pagy_link_proc, :pagy_link_proc_with_trim
25
21
 
26
- private
27
-
28
- # separate method easier to test
29
- def pagy_trim_url(url, param_string)
30
- url.sub(/((?:[?&])#{param_string}\z|\b(?<=[?&])#{param_string}&)/, '')
31
- end
32
-
33
22
  end
34
23
  end
data/lib/pagy/frontend.rb CHANGED
@@ -28,7 +28,7 @@ class Pagy
28
28
  include Helpers
29
29
 
30
30
  EMPTY = '' # EMPTY + 'string' is almost as fast as +'string' but is also 1.9 compatible
31
- MARK = '--pagy.page--' # string used for search and replace, hardcoded also in the pagy.js file
31
+ MARK = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file
32
32
 
33
33
  # Generic pagination: it returns the html with the series of links to the pages
34
34
  def pagy_nav(pagy)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-08 00:00:00.000000000 Z
11
+ date: 2019-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Agnostic pagination in plain ruby: it works with any framework, ORM
14
14
  and DB type, with all kinds of collections, even pre-paginated, scopes, Arrays,