pagy 5.6.3 → 5.6.7

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: f567ea14d1f7bf5697ca416cf0cf1bc200986b804099f31aba5fd1ae6a946fa4
4
- data.tar.gz: 8b09e574678ab6673c4e9d8fdc48eaf611657354d457da4856a70173d7194732
3
+ metadata.gz: 0d1bf851d5e0ceba5250106974dc79d90346224bd25b149e28f78c307980ad90
4
+ data.tar.gz: fd0c607bf907c917b0aa637c08c7f773d0a403b6662738f59e23e0aa197f6f49
5
5
  SHA512:
6
- metadata.gz: 2cf7845ce5b8d668cfa0de512bb6fb7ed676e3586167a9b95fdfd6eebb8b9f7bfd5cb688ab523ef1e7e512ce5f5bb379e305beb4531dd00c609e755620bd0004
7
- data.tar.gz: f79e860f496368e67bf328b3359f5860162801600452482cf4b1c0b06439b20f9fb296d5593159410c1817fff4576c4e03ef526f265dbb5f9aa4d8877e9c558b
6
+ metadata.gz: 6b971d9c936a082e73dab19ceb624be6a041e1e8646a3d88d5742ddd93d2fa701669e7dae8ee13450397e4dcc9a2c921788c081aaa545fddb628e975bb669661
7
+ data.tar.gz: 4e2d4ccd5a85264e2b4b85319ccbac120139ea2f1cbadb6a4bb35740e44498027cc353c1aa9ea03f9a6adc6ece776353f879c85745b0c1941b5005b344592537
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.6.3)
3
+ # Pagy initializer file (5.6.7)
4
4
  # Customize only what you really need and notice that the core Pagy works also without any of the following lines.
5
5
  # Should you just cherry pick part of this file, please maintain the require-order of the extras
6
6
 
@@ -1,135 +1 @@
1
- // See the Pagy documentation: https://ddnexus.github.io/pagy/extras#javascript
2
-
3
- // This code should be OK also with very old browsers
4
-
5
- // Container of the whole pagy stuff
6
- function Pagy(){}
7
-
8
- Pagy.version = '5.6.3'
9
-
10
- // Used by the waitForMe function
11
- Pagy.delay = 100
12
-
13
- // Scan the target for data-pagy-json elements and apply their functions
14
- Pagy.init =
15
- function(arg) {
16
- var target = arg instanceof Event || arg === undefined ? document : arg,
17
- elements = target.querySelectorAll('[data-pagy-json]')
18
- for (var i = 0, len = elements.length; i < len; i++) {
19
- var args = JSON.parse(elements[i].getAttribute('data-pagy-json')),
20
- fname = args.shift()
21
- args.unshift(elements[i])
22
- Pagy[fname].apply(null, args)
23
- }
24
- }
25
-
26
- // Power the pagy*_nav_js helpers
27
- Pagy.nav =
28
- function(pagyEl, tags, sequels, label_sequels, trimParam ) {
29
- label_sequels = (label_sequels === null) ? sequels : label_sequels
30
- var widths = [], lastWidth,
31
- fill = function(string, item, label) { return string.replace(/__pagy_page__/g, item)
32
- .replace(/__pagy_label__/g, label) }
33
- for (var width in sequels) {
34
- if (sequels.hasOwnProperty(width)) { widths.push(parseInt(width, 10)) }
35
- }
36
- widths.sort(function(a, b) { return b - a })
37
-
38
- pagyEl.render =
39
- function() {
40
- var width, i, len
41
- for (i = 0, len = widths.length; i < len; i++) {
42
- if (this.parentElement.clientWidth > widths[i]) { width = widths[i]; break }
43
- }
44
- if (width !== lastWidth) {
45
- var html = tags.before,
46
- series = sequels[width],
47
- labels = label_sequels[width]
48
- for (i = 0, len = series.length; i < len; i++) {
49
- var item = series[i],
50
- label = labels[i]
51
- if (typeof(trimParam) === 'string' && item === 1) { html += Pagy.trim(fill(tags.link, item, label), trimParam) }
52
- else if (typeof(item) === 'number') { html += fill(tags.link, item, label) }
53
- else if (item === 'gap') { html += tags.gap }
54
- else if (typeof(item) === 'string') { html += fill(tags.active, item, label) }
55
- }
56
- html += tags.after
57
- this.insertAdjacentHTML('afterbegin', html)
58
- lastWidth = width
59
- }
60
- }.bind(pagyEl)
61
- pagyEl.render()
62
- }
63
-
64
- // Power the pagy*_combo_nav_js helpers
65
- Pagy.combo_nav =
66
- function(pagyEl, page, link, trimParam) {
67
- var input = pagyEl.getElementsByTagName('input')[0],
68
- toPage =
69
- function() {
70
- if (page !== input.value) {
71
- var html = link.replace(/__pagy_page__/, input.value)
72
- if (typeof (trimParam) === 'string' && input.value === '1') { html = Pagy.trim(html, trimParam) }
73
- pagyEl.insertAdjacentHTML('afterbegin', html)
74
- pagyEl.getElementsByTagName('a')[0].click()
75
- }
76
- }
77
- Pagy.addInputEventListeners(input, toPage)
78
- }
79
-
80
- // Power the pagy_items_selector_js helper
81
- Pagy.items_selector =
82
- function(pagyEl, from, link, param) {
83
- var input = pagyEl.getElementsByTagName('input')[0],
84
- current = input.value,
85
- toPage =
86
- function() {
87
- var items = input.value
88
- if (items === 0 || items === '') { return }
89
- if (current !== items) {
90
- var page = Math.max(Math.ceil(from / items), 1),
91
- html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, items)
92
- if (typeof (param) === 'string' && page === 1) { html = Pagy.trim(html, param) }
93
- pagyEl.insertAdjacentHTML('afterbegin', html)
94
- pagyEl.getElementsByTagName('a')[0].click()
95
- }
96
- }
97
- Pagy.addInputEventListeners(input, toPage)
98
- }
99
-
100
- // Utility for input fields
101
- Pagy.addInputEventListeners =
102
- function(input, toPage) {
103
- // select the content on click: easier for typing a number
104
- input.addEventListener('click', function() { this.select() })
105
- // toPage when the input looses focus
106
- input.addEventListener('focusout', toPage)
107
- // … and when pressing enter inside the input
108
- input.addEventListener('keyup', function(e) { if (e.which === 13) {toPage()} }.bind(this))
109
- }
110
-
111
- // Power the trim extra for js helpers
112
- Pagy.trim =
113
- function(html, param) {
114
- var re = new RegExp('[?&]' + param + '=1\\b(?!&)|\\b' + param + '=1&')
115
- return html.replace(re, '')
116
- }
117
-
118
- // Render all *nav_js helpers
119
- Pagy.renderNavs =
120
- function() {
121
- var navs = document.getElementsByClassName('pagy-njs') // 'pagy-njs' is common to all *nav_js helpers
122
- for (var i = 0, len = navs.length; i < len; i++) { navs[i].render() }
123
- }
124
-
125
- // Throttle to avoid to fire multiple time the renderNavs on resize
126
- Pagy.waitForMe =
127
- function() {
128
- if (typeof(Pagy.tid) === 'number') { clearTimeout(Pagy.tid) }
129
- Pagy.tid = setTimeout(Pagy.renderNavs, Pagy.delay)
130
- }
131
-
132
- // If there is a window object then add the event listener on resize
133
- if (typeof window !== 'undefined') {
134
- window.addEventListener('resize', Pagy.waitForMe, true)
135
- }
1
+ "use strict";function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c["return"]||c["return"]()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}var Pagy={version:"5.6.7",init:function init(a){var b,c=a instanceof HTMLElement?a:document,d=c.querySelectorAll("[data-pagy-json]"),e=_createForOfIteratorHelper(d);try{for(e.s();!(b=e.n()).done;){var f=b.value,g=f.getAttribute("data-pagy-json");if(null!==g){var h=JSON.parse(g),i=h.shift();"nav"===i?Pagy.nav.apply(Pagy,[f].concat(_toConsumableArray(h))):"combo_nav"===i?Pagy.combo_nav.apply(Pagy,[f].concat(_toConsumableArray(h))):"items_selector"===i&&Pagy.items_selector.apply(Pagy,[f].concat(_toConsumableArray(h)))}}}catch(a){e.e(a)}finally{e.f()}},nav:function nav(a,b,c,d,e){var f={};if(null===d)for(var k in c)f[k]=c[k].map(function(a){return a.toString()});else f=d;var g,h=Object.getOwnPropertyNames(c).map(function(a){return parseInt(a)}).sort(function(c,a){return a-c}),j=function(a,b,c){return a.replace(/__pagy_page__/g,b).replace(/__pagy_label__/g,c)};a.pagyRender=function(){var a,d=0,k=_createForOfIteratorHelper(h);try{for(k.s();!(a=k.n()).done;){var q=a.value;if(null!==this.parentElement&&this.parentElement.clientWidth>q){d=q;break}}}catch(a){k.e(a)}finally{k.f()}if(d!==g){var l=b.before,m=c[d.toString()],n=f[d.toString()];for(var r in m){var o=m[r],p=n[r];if("string"==typeof e&&1===o){var i=j(b.link,o.toString(),p);l+=Pagy.trim(i,e)}else l+="number"==typeof o?j(b.link,o.toString(),p):"gap"===o?b.gap:j(b.active,o,p)}l+=b.after,this.innerHTML="",this.insertAdjacentHTML("afterbegin",l),g=d}}.bind(a),a.pagyRender(),"undefined"!=typeof window&&window.addEventListener("resize",Pagy.throttleRenderNavs,!0)},throttleRenderNavsTID:0,throttleRenderNavsDelay:100,throttleRenderNavs:function throttleRenderNavs(){clearTimeout(Pagy.throttleRenderNavsTID),Pagy.throttleRenderNavsTID=setTimeout(Pagy.renderNavs,Pagy.throttleRenderNavsDelay)},renderNavs:function renderNavs(){var a,b=document.getElementsByClassName("pagy-njs"),c=_createForOfIteratorHelper(b);try{for(c.s();!(a=c.n()).done;){var d=a.value;d.pagyRender()}}catch(a){c.e(a)}finally{c.f()}},combo_nav:function combo_nav(a,b,c,d){var e=a.getElementsByTagName("input")[0];Pagy.addInputBehavior(e,function goToPage(){if(b!==e.value){var f=c.replace(/__pagy_page__/,e.value);"string"==typeof d&&"1"===e.value&&(f=Pagy.trim(f,d)),a.insertAdjacentHTML("afterbegin",f),a.getElementsByTagName("a")[0].click()}})},trim:function trim(a,b){var c=new RegExp("[?&]".concat(b,"=1\\b(?!&)|\\b").concat(b,"=1&"));return a.replace(c,"")},items_selector:function items_selector(a,b,c,d){var e=a.getElementsByTagName("input")[0],f=e.value;Pagy.addInputBehavior(e,function goToPage(){var g=Math.max,h=Math.ceil,i=e.value;if("0"!==i&&""!==i&&f!==i){var j=g(h(b/parseInt(i)),1).toString(),k=c.replace(/__pagy_page__/,j).replace(/__pagy_items__/,i);"string"==typeof d&&"1"===j&&(k=Pagy.trim(k,d)),a.insertAdjacentHTML("afterbegin",k),a.getElementsByTagName("a")[0].click()}})},addInputBehavior:function addInputBehavior(a,b){a.addEventListener("click",function(){this.select()}),a.addEventListener("focusout",function(){b()}),a.addEventListener("keypress",function(a){"Enter"===a.key&&b()}.bind(this))}};
data/lib/pagy/backend.rb CHANGED
@@ -8,7 +8,7 @@ class Pagy
8
8
  module Backend
9
9
  private
10
10
 
11
- # Return Pagy object and items
11
+ # Return Pagy object and paginated items/results
12
12
  def pagy(collection, vars = {})
13
13
  pagy = Pagy.new(pagy_get_vars(collection, vars))
14
14
  [pagy, pagy_get_items(collection, pagy)]
data/lib/pagy/calendar.rb CHANGED
@@ -41,7 +41,7 @@ class Pagy # :nodoc:
41
41
 
42
42
  protected
43
43
 
44
- # Base class method for the setup of the unit variables
44
+ # Base class method for the setup of the unit variables (subclasses must implement it and call super)
45
45
  def setup_unit_vars
46
46
  raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
47
47
  raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
@@ -85,6 +85,6 @@ class Pagy # :nodoc:
85
85
  end
86
86
  end
87
87
  end
88
-
88
+ # Require the subclass files in UNITS (no custom unit at this point yet)
89
89
  Calendar::UNITS.each { |unit| require "pagy/calendar/#{unit}" }
90
90
  end
@@ -6,11 +6,13 @@ class Pagy # :nodoc:
6
6
  module ArelExtra
7
7
  private
8
8
 
9
+ # Return Pagy object and paginated collection/results
9
10
  def pagy_arel(collection, vars = {})
10
11
  pagy = Pagy.new(pagy_arel_get_vars(collection, vars))
11
12
  [pagy, pagy_get_items(collection, pagy)]
12
13
  end
13
14
 
15
+ # Sub-method called only by #pagy_arel: here for easy customization of variables by overriding
14
16
  def pagy_arel_get_vars(collection, vars)
15
17
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
16
18
  vars[:count] ||= pagy_arel_count(collection)
@@ -18,6 +20,7 @@ class Pagy # :nodoc:
18
20
  vars
19
21
  end
20
22
 
23
+ # Count using Arel when grouping
21
24
  def pagy_arel_count(collection)
22
25
  if collection.group_values.empty?
23
26
  # COUNT(*)
@@ -6,7 +6,7 @@ class Pagy # :nodoc:
6
6
  module ArrayExtra
7
7
  private
8
8
 
9
- # Return Pagy object and items
9
+ # Return Pagy object and paginated items
10
10
  def pagy_array(array, vars = {})
11
11
  pagy = Pagy.new(pagy_array_get_vars(array, vars))
12
12
  [pagy, array[pagy.offset, pagy.items]]
@@ -4,7 +4,7 @@
4
4
  require 'pagy/calendar'
5
5
 
6
6
  class Pagy # :nodoc:
7
- # Paginate based on calendar periods (year month week day) plus the regular pagination
7
+ # Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
8
8
  module CalendarExtra
9
9
  # Additions for the Backend module
10
10
  module Backend
@@ -26,7 +26,9 @@ class Pagy # :nodoc:
26
26
 
27
27
  # Setup and return the calendar objects and the filtered collection
28
28
  def pagy_setup_calendar(collection, conf)
29
- units = Calendar::UNITS & conf.keys # get the units in time length desc order
29
+ units = Calendar::UNITS & conf.keys # get the units in time length desc order
30
+ raise ArgumentError, 'no calendar unit found in pagy_calendar configuration' if units.empty?
31
+
30
32
  page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
31
33
  units.each do |unit| # set all the :page_param vars for later deletion
32
34
  unit_page_param = :"#{unit}_#{page_param}"
@@ -18,7 +18,7 @@ class Pagy # :nodoc:
18
18
  # "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
19
19
  # "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
20
20
  # Notice: if :steps is false it will use the single {0 => @vars[:size]} size
21
- def sequels(steps: @vars[:steps] || { 0 => @vars[:size] })
21
+ def sequels(steps: @vars[:steps] || { 0 => @vars[:size] }, **_)
22
22
  raise VariableError.new(self, :steps, 'to define the 0 width', steps) unless steps.key?(0)
23
23
 
24
24
  {}.tap do |sequels|
@@ -4,14 +4,14 @@
4
4
  class Pagy # :nodoc:
5
5
  # Use ::I18n gem
6
6
  module I18nExtra
7
- # Frontend overriding
7
+ # Frontend overriding for translation
8
8
  module Frontend
9
9
  def pagy_t(key, **opts)
10
10
  ::I18n.t(key, **opts)
11
11
  end
12
12
  end
13
13
 
14
- # Calendar overriding
14
+ # Calendar overriding for localization (see also the block in the calendar section of the config/pagy.rb initializer)
15
15
  module Calendar
16
16
  def localize(time, opts)
17
17
  ::I18n.l(time, **opts)
data/lib/pagy/frontend.rb CHANGED
@@ -77,7 +77,7 @@ class Pagy
77
77
  # Similar to I18n.t: just ~18x faster using ~10x less memory
78
78
  # (@pagy_locale explicitly initialized in order to avoid warning)
79
79
  def pagy_t(key, opts = {})
80
- Pagy::I18n.t(@pagy_locale ||= nil, key, opts)
80
+ Pagy::I18n.translate(@pagy_locale ||= nil, key, opts)
81
81
  end
82
82
  end
83
83
  end
data/lib/pagy/i18n.rb CHANGED
@@ -153,11 +153,12 @@ class Pagy
153
153
  end
154
154
 
155
155
  # Translate and pluralize the key with the locale DATA
156
- def t(locale, key, opts = {})
156
+ def translate(locale, key, opts = {})
157
157
  data, pluralize = DATA[locale]
158
158
  translation = data[key] || (opts[:count] && data[key += ".#{pluralize.call(opts[:count])}"]) \
159
159
  or return %([translation missing: "#{key}"])
160
160
  translation.gsub(/%{[^}]+?}/) { |match| opts[:"#{match[2..-2]}"] || match }
161
161
  end
162
+ alias t translate
162
163
  end
163
164
  end
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.6.3'
8
+ VERSION = '5.6.7'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
@@ -44,7 +44,7 @@ class Pagy
44
44
  end
45
45
 
46
46
  # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
47
- def series(size: @vars[:size])
47
+ def series(size: @vars[:size], **_)
48
48
  return [] if size.empty?
49
49
  raise VariableError.new(self, :size, 'to contain 4 items >= 0', size) \
50
50
  unless size.is_a?(Array) && size.size == 4 && size.all? { |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
@@ -59,11 +59,11 @@ class Pagy
59
59
  series = []
60
60
  start = 1
61
61
  if (left_gap_end - left_gap_start).positive?
62
- series.push(*start..(left_gap_start - 1), :gap)
62
+ series.push(*start...left_gap_start, :gap)
63
63
  start = left_gap_end + 1
64
64
  end
65
65
  if (right_gap_end - right_gap_start).positive?
66
- series.push(*start..(right_gap_start - 1), :gap)
66
+ series.push(*start...right_gap_start, :gap)
67
67
  start = right_gap_end + 1
68
68
  end
69
69
  series.push(*start..@last)
@@ -76,6 +76,7 @@ class Pagy
76
76
  page.to_s
77
77
  end
78
78
 
79
+ # Allow the customization of the output (overridden by the calendar extra)
79
80
  def label
80
81
  @page.to_s
81
82
  end
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: 5.6.3
4
+ version: 5.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-30 00:00:00.000000000 Z
11
+ date: 2021-12-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email:
@@ -111,6 +111,11 @@ licenses:
111
111
  - MIT
112
112
  metadata:
113
113
  rubygems_mfa_required: 'true'
114
+ homepage_uri: https://github.com/ddneus/pagy
115
+ documentation_uri: https://ddnexus.github.io/pagy
116
+ bug_tracker_uri: https://github.com/ddnexus/pagy/issues
117
+ changelog_uri: https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md
118
+ live_support: https://gitter.im/ruby-pagy/Lobby
114
119
  post_install_message:
115
120
  rdoc_options: []
116
121
  require_paths:
@@ -126,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
131
  - !ruby/object:Gem::Version
127
132
  version: '0'
128
133
  requirements: []
129
- rubygems_version: 3.2.3
134
+ rubygems_version: 3.2.32
130
135
  signing_key:
131
136
  specification_version: 4
132
137
  summary: The kick-ass pagination ruby gem