pagy 0.9.2 → 0.10.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: d25549587b5ffb4311920d5a1f52f1e181e2c0b062c27936fe85cf453c4d3361
4
- data.tar.gz: 912cc77296b4e6ba9db0feef70c92580ceaaa8bc2b4794d7a5d3e2bd7700c0d4
3
+ metadata.gz: f42ebff94cf9faa8dadd414d3fd056bcf57da0a2e4930f1a9850d64eca08adf4
4
+ data.tar.gz: 60133804f52c4c687647d2c8b48f2b7344a558fa163063857f24bc0387d41834
5
5
  SHA512:
6
- metadata.gz: c76ee653cc0693cb646b9ed4bbc552e2c91541c3860120322b1105e7197b311b2d3c052f760bda7af2483c5f7f964aeca0b0b84512969b3fb6dab94b54ecdd24
7
- data.tar.gz: 0126c853a4edd59214f791c1ada7c26661b90bc098fd6c252f89b70354b8db59a09162494ccb9df6243dceb88e721d5c4ab02a3110607ee613e1a27dfe619fe4
6
+ metadata.gz: b6dbcbc1041dceb8ff23832f687083e701d3eb187fabddd1051bf3672c1e93efbe4b680c534dd531d506c173f7766cba96473ac8bc5464da8db25206a9c9ee9b
7
+ data.tar.gz: 256e4e8eb88d53c71fe035804e6ad99af606f93fc673d7173150e4ac13e396c084eda16686d52c144ea3d5af7cca29797ddca5d0be21aac87406cc7654757171
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'pathname'
5
5
 
6
- class Pagy ; VERSION = '0.9.2'
6
+ class Pagy ; VERSION = '0.10.0'
7
7
 
8
8
  class OutOfRangeError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end
9
9
 
@@ -11,18 +11,14 @@ class Pagy
11
11
  tags, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
12
12
 
13
13
  tags << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact pagination" role="navigation" aria-label="pager">)
14
-
15
14
  tags << link.call(MARKER, '', %(style="display: none;" ))
16
15
  tags << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
17
16
  : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
18
-
19
17
  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;">)
20
18
  tags << %(<span class="pagy-compact-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</span> )
21
-
22
19
  tags << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
23
20
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
24
-
25
- tags << %(</nav><script>PagyCompact('#{id}', '#{MARKER}', '#{p_page}');</script>)
21
+ tags << %(</nav><script type="application/json" class="pagy-compact">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
26
22
  end
27
23
 
28
24
  # Compact pagination for bootstrap: it returns the html with the series of links to the pages
@@ -31,20 +27,15 @@ class Pagy
31
27
  tags, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
32
28
 
33
29
  tags << %(<nav id="pagy-nav-#{id}" class="pagy-nav-bootstrap-compact pagination" role="navigation" aria-label="pager">)
34
-
35
30
  tags << link.call(MARKER, '', %(style="display: none;" ))
36
-
37
31
  tags << %(<div class="btn-group" role="group">)
38
32
  tags << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
39
33
  : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
40
-
41
34
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
42
35
  tags << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</div>)
43
-
44
36
  tags << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
45
37
  : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
46
-
47
- tags << %(</div></nav><script>PagyCompact('#{id}', '#{MARKER}', '#{p_page}');</script>)
38
+ tags << %(</div></nav><script type="application/json" class="pagy-compact">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
48
39
  end
49
40
 
50
41
  end
@@ -33,12 +33,10 @@ class Pagy
33
33
  pagy = pagy.clone; p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"
34
34
 
35
35
  tags = +%(<span id="pagy-items-#{id}">)
36
-
37
36
  tags << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
38
37
  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;">)
39
38
  tags << %(#{pagy_t('pagy.items.show')} #{input} #{pagy_t('pagy.items.items')})
40
-
41
- tags << %(</span><script>PagyItems('#{id}', '#{MARKER}', #{pagy.from});</script>)
39
+ tags << %(</span><script type="application/json" class="pagy-items">["#{id}", "#{MARKER}", #{pagy.from}]</script>)
42
40
  end
43
41
 
44
42
  end
@@ -0,0 +1,85 @@
1
+ // See the Pagy documentation: https://ddnexus.github.io/pagy/extras
2
+
3
+ function Pagy(){}
4
+
5
+ Pagy.compact = function(id, marker, page){
6
+ var pagyNav = document.getElementById('pagy-nav-'+id),
7
+ input = pagyNav.getElementsByTagName('input')[0],
8
+ link = pagyNav.getElementsByTagName('a')[0];
9
+
10
+ var go = function(){
11
+ if (page !== input.value) {
12
+ var href = link.getAttribute('href').replace(marker, input.value);
13
+ link.setAttribute('href', href);
14
+ link.click();
15
+ }
16
+ };
17
+
18
+ // select the content on click: easier for typing a number
19
+ input.addEventListener('click', function(){ this.select() });
20
+ // go when the input looses focus
21
+ input.addEventListener('focusout', go);
22
+ // … and when pressing enter inside the input
23
+ input.addEventListener('keyup', function(e){ if (e.which === 13) go() }.bind(this));
24
+
25
+ };
26
+
27
+
28
+ Pagy.items = function(id, marker, from){
29
+ var pagyNav = document.getElementById('pagy-items-'+id),
30
+ input = pagyNav.getElementsByTagName('input')[0],
31
+ current = input.value,
32
+ link = pagyNav.getElementsByTagName('a')[0];
33
+
34
+ var go = function(){
35
+ var items = input.value;
36
+ if (current !== items) {
37
+ var page = Math.max(Math.ceil(from / items),1);
38
+ var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
39
+ link.setAttribute('href', href);
40
+ link.click();
41
+ }
42
+ };
43
+
44
+ // select the content on click: easier for typing a number
45
+ input.addEventListener('click', function(){ this.select() });
46
+ // go when the input looses focus
47
+ input.addEventListener('focusout', go);
48
+ // … and when pressing enter inside the input
49
+ input.addEventListener('keyup', function(e){ if (e.which === 13) go() }.bind(this));
50
+
51
+ };
52
+
53
+ Pagy.responsive = function(id, items, widths, series){
54
+ var pagyNav = document.getElementById('pagy-nav-'+id),
55
+ pagyBox = pagyNav.firstChild || pagyNav,
56
+ pagyParent = pagyNav.parentElement,
57
+ lastWidth = undefined;
58
+
59
+ var render = function(){
60
+ var parentWidth = parseInt(pagyParent.clientWidth),
61
+ width = widths.find(function(w){return parentWidth > w});
62
+ if (width !== lastWidth) {
63
+ while (pagyBox.firstChild) { pagyBox.removeChild(pagyBox.firstChild) }
64
+ var tags = items['prev'];
65
+ series[width].forEach(function(item){tags += items[item]});
66
+ tags += items['next'];
67
+ pagyBox.insertAdjacentHTML('beforeend', tags);
68
+ lastWidth = width;
69
+ }
70
+ };
71
+
72
+ if (window.attachEvent) { window.attachEvent('onresize', render) }
73
+ else if (window.addEventListener) { window.addEventListener('resize', render, true) }
74
+
75
+ render();
76
+ };
77
+
78
+
79
+ Pagy.init = function(){
80
+ ['compact', 'items', 'responsive'].forEach(function(name){
81
+ Array.from(document.getElementsByClassName("pagy-"+name)).forEach(function(json) {
82
+ Pagy[name].apply(null, JSON.parse(json.innerHTML))
83
+ })
84
+ })
85
+ };
@@ -34,38 +34,38 @@ class Pagy
34
34
  # Generic responsive pagination: it returns the html with the series of links to the pages
35
35
  # we build the tags as a json object string and render them with the PagyResponsive javascript
36
36
  def pagy_nav_responsive(pagy, id=caller(1,1)[0].hash)
37
- tags, link, p_prev, p_next, responsive = +'{', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
37
+ tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
38
38
 
39
- tags << (p_prev ? %('prev':'<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> ',)
40
- : %('prev':'<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ',))
39
+ tags[:prev] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
40
+ : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
41
41
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
42
- tags << if item.is_a?(Integer); %('#{item}':'<span class="page">#{link.call item}</span> ',) # page link
43
- elsif item.is_a?(String) ; %('#{item}':'<span class="page active">#{item}</span> ',) # current page
44
- elsif item == :gap ; %('#{item}':'<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ',) # page gap
45
- end
42
+ tags[item.to_s] = if item.is_a?(Integer); %(<span class="page">#{link.call item}</span> ) # page link
43
+ elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> ) # current page
44
+ elsif item == :gap ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
45
+ end
46
46
  end
47
- tags << (p_next ? %('next':'<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>'})
48
- : %('next':'<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>'}))
49
- script = %(<script>PagyResponsive('#{id}', #{tags}, #{responsive[:widths].to_json}, #{responsive[:series].to_json});</script>)
47
+ tags[:next] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
48
+ : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
49
+ script = %(<script type="application/json" class="pagy-responsive">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
50
50
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive pagination" role="navigation" aria-label="pager"></nav>#{script})
51
51
  end
52
52
 
53
53
  # Responsive pagination for bootstrap: it returns the html with the series of links to the pages
54
54
  # we build the tags as a json object string and render them with the PagyResponsive javascript
55
55
  def pagy_nav_bootstrap_responsive(pagy, id=caller(1,1)[0].hash)
56
- tags, link, p_prev, p_next, responsive = +'{', pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive
56
+ tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive
57
57
 
58
- tags << (p_prev ? %('prev':'<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>',)
59
- : %('prev':'<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>',))
58
+ tags['prev'] = (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
59
+ : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
60
60
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
61
- tags << if item.is_a?(Integer); %('#{item}':'<li class="page-item">#{link.call item}</li>',) # page link
62
- elsif item.is_a?(String) ; %('#{item}':'<li class="page-item active">#{link.call item}</li>',) # active page
63
- elsif item == :gap ; %('#{item}':'<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>',) # page gap
64
- end
61
+ tags[item.to_s] = if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
62
+ elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
63
+ elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
64
+ end
65
65
  end
66
- tags << (p_next ? %('next':'<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>'})
67
- : %('next':'<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>'}))
68
- script = %(<script>PagyResponsive('#{id}', #{tags}, #{responsive[:widths].to_json}, #{responsive[:series].to_json});</script>)
66
+ tags['next'] = (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
67
+ : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
68
+ script = %(<script type="application/json" class="pagy-responsive">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
69
69
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-bootstrap-responsive pagination" role="navigation" aria-label="pager"><ul class="pagination"></ul></nav>#{script})
70
70
  end
71
71
 
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: 0.9.2
4
+ version: 0.10.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: 2018-06-20 00:00:00.000000000 Z
11
+ date: 2018-06-21 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,
@@ -30,9 +30,7 @@ files:
30
30
  - lib/pagy/extras/i18n.rb
31
31
  - lib/pagy/extras/initializer_example.rb
32
32
  - lib/pagy/extras/items.rb
33
- - lib/pagy/extras/javascripts/pagy-compact.js
34
- - lib/pagy/extras/javascripts/pagy-items.js
35
- - lib/pagy/extras/javascripts/pagy-responsive.js
33
+ - lib/pagy/extras/javascripts/pagy.js
36
34
  - lib/pagy/extras/responsive.rb
37
35
  - lib/pagy/extras/templates/nav.html.erb
38
36
  - lib/pagy/extras/templates/nav.html.haml
@@ -1,23 +0,0 @@
1
- // See the Pagy documentation: https://ddnexus.github.io/pagy/extras/compact
2
-
3
- function PagyCompact(id, marker, page){
4
- var pagyNav = document.getElementById('pagy-nav-'+id),
5
- input = pagyNav.getElementsByTagName('input')[0],
6
- link = pagyNav.getElementsByTagName('a')[0];
7
-
8
- this.go = function(){
9
- if (page !== input.value) {
10
- var href = link.getAttribute('href').replace(marker, input.value);
11
- link.setAttribute('href', href);
12
- link.click();
13
- }
14
- };
15
-
16
- // select the content on click: easier for typing a number
17
- input.addEventListener('click', function(){ this.select() });
18
- // jump to page number from input when the input looses focus
19
- input.addEventListener('focusout', this.go);
20
- // … and when pressing enter inside the input
21
- input.addEventListener('keyup', function(e){ if (e.which === 13) this.go() }.bind(this));
22
-
23
- }
@@ -1,26 +0,0 @@
1
- // See the Pagy documentation: https://ddnexus.github.io/pagy/extras/items
2
-
3
- function PagyItems(id, marker, from){
4
- var pagyNav = document.getElementById('pagy-items-'+id),
5
- input = pagyNav.getElementsByTagName('input')[0],
6
- current = input.value,
7
- link = pagyNav.getElementsByTagName('a')[0];
8
-
9
- this.go = function(){
10
- var items = input.value;
11
- if (current !== items) {
12
- var page = Math.max(Math.ceil(from / items),1);
13
- var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
14
- link.setAttribute('href', href);
15
- link.click();
16
- }
17
- };
18
-
19
- // select the content on click: easier for typing a number
20
- input.addEventListener('click', function(){ this.select() });
21
- // jump to page number from input when the input looses focus
22
- input.addEventListener('focusout', this.go);
23
- // … and when pressing enter inside the input
24
- input.addEventListener('keyup', function(e){ if (e.which === 13) this.go() }.bind(this));
25
-
26
- }
@@ -1,26 +0,0 @@
1
- // See the Pagy documentation: https://ddnexus.github.io/pagy/extras/resposive
2
-
3
- function PagyResponsive(id, items, widths, series){
4
- var pagyNav = document.getElementById('pagy-nav-'+id),
5
- pagyBox = pagyNav.firstChild || pagyNav,
6
- pagyParent = pagyNav.parentElement,
7
- lastWidth = undefined;
8
-
9
- this.render = function(){
10
- var parentWidth = parseInt(pagyParent.clientWidth),
11
- width = widths.find(function(w){return parentWidth > w});
12
- if (width !== lastWidth) {
13
- while (pagyBox.firstChild) { pagyBox.removeChild(pagyBox.firstChild) }
14
- var tags = items['prev'];
15
- series[width].forEach(function(item){tags += items[item]});
16
- tags += items['next'];
17
- pagyBox.insertAdjacentHTML('beforeend', tags);
18
- lastWidth = width;
19
- }
20
- };
21
-
22
- if (window.attachEvent) { window.attachEvent('onresize', this.render) }
23
- else if (window.addEventListener) { window.addEventListener('resize', this.render, true) }
24
-
25
- this.render();
26
- };