pagy 8.4.4 → 8.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/apps/calendar.ru +681 -2136
  3. data/apps/demo.ru +1 -1
  4. data/apps/rails.ru +3 -1
  5. data/apps/repro.ru +1 -1
  6. data/apps/tmp/calendar.sqlite3 +0 -0
  7. data/apps/tmp/calendar.sqlite3-shm +0 -0
  8. data/apps/tmp/calendar.sqlite3-wal +0 -0
  9. data/apps/tmp/local_secret.txt +1 -0
  10. data/apps/tmp/pagy-rails.sqlite3 +0 -0
  11. data/bin/pagy +9 -9
  12. data/config/pagy.rb +1 -1
  13. data/javascripts/pagy-module.js +1 -1
  14. data/javascripts/pagy.js +2 -2
  15. data/javascripts/pagy.min.js +2 -2
  16. data/javascripts/pagy.min.js.map +2 -2
  17. data/javascripts/pagy.mjs +1 -1
  18. data/lib/optimist.rb +1 -1
  19. data/lib/pagy/calendar/day.rb +2 -2
  20. data/lib/pagy/calendar/month.rb +2 -2
  21. data/lib/pagy/calendar/quarter.rb +2 -2
  22. data/lib/pagy/calendar/unit.rb +106 -0
  23. data/lib/pagy/calendar/week.rb +2 -2
  24. data/lib/pagy/calendar/year.rb +2 -2
  25. data/lib/pagy/calendar.rb +54 -97
  26. data/lib/pagy/extras/calendar.rb +34 -4
  27. data/lib/pagy/extras/i18n.rb +1 -1
  28. data/lib/pagy/extras/js_tools.rb +1 -1
  29. data/lib/pagy/extras/metadata.rb +5 -1
  30. data/lib/pagy/extras/overflow.rb +3 -2
  31. data/lib/pagy/frontend.rb +8 -5
  32. data/lib/pagy.rb +2 -3
  33. data/locales/ar.yml +1 -1
  34. data/locales/be.yml +1 -1
  35. data/locales/bg.yml +1 -1
  36. data/locales/bs.yml +1 -1
  37. data/locales/ca.yml +4 -6
  38. data/locales/ckb.yml +1 -1
  39. data/locales/cs.yml +1 -1
  40. data/locales/da.yml +1 -1
  41. data/locales/de.yml +1 -1
  42. data/locales/en.yml +1 -1
  43. data/locales/es.yml +1 -1
  44. data/locales/fr.yml +1 -1
  45. data/locales/hr.yml +1 -1
  46. data/locales/id.yml +1 -1
  47. data/locales/it.yml +1 -1
  48. data/locales/ja.yml +1 -1
  49. data/locales/km.yml +1 -1
  50. data/locales/ko.yml +1 -1
  51. data/locales/nb.yml +1 -1
  52. data/locales/nl.yml +1 -1
  53. data/locales/nn.yml +1 -1
  54. data/locales/pl.yml +1 -1
  55. data/locales/pt-BR.yml +1 -1
  56. data/locales/pt.yml +1 -1
  57. data/locales/ru.yml +1 -1
  58. data/locales/sr.yml +1 -1
  59. data/locales/sv-SE.yml +1 -1
  60. data/locales/sv.yml +1 -1
  61. data/locales/sw.yml +1 -1
  62. data/locales/ta.yml +1 -1
  63. data/locales/tr.yml +1 -1
  64. data/locales/uk.yml +1 -1
  65. data/locales/vi.yml +1 -1
  66. data/locales/zh-CN.yml +1 -1
  67. data/locales/zh-HK.yml +1 -1
  68. data/locales/zh-TW.yml +1 -1
  69. metadata +8 -3
  70. data/lib/pagy/calendar/helper.rb +0 -65
data/apps/demo.ru CHANGED
@@ -18,7 +18,7 @@
18
18
  # DOC
19
19
  # https://ddnexus.github.io/pagy/playground/#3-demo-app
20
20
 
21
- VERSION = '8.4.4'
21
+ VERSION = '8.5.0'
22
22
 
23
23
  require 'bundler/inline'
24
24
  require 'bundler'
data/apps/rails.ru CHANGED
@@ -15,7 +15,7 @@
15
15
  # DOC
16
16
  # https://ddnexus.github.io/pagy/playground/#2-rails-app
17
17
 
18
- VERSION = '8.4.4'
18
+ VERSION = '8.5.0'
19
19
 
20
20
  # Gemfile
21
21
  require 'bundler/inline'
@@ -26,6 +26,8 @@ gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
26
26
  gem 'oj'
27
27
  gem 'puma'
28
28
  gem 'rails'
29
+ # activerecord/sqlite3_adapter.rb probably useless) constraint !!!
30
+ # https://github.com/rails/rails/blame/v7.1.3.4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L14
29
31
  gem 'sqlite3', '~> 1.4.0'
30
32
  end
31
33
 
data/apps/repro.ru CHANGED
@@ -15,7 +15,7 @@
15
15
  # DOC
16
16
  # https://ddnexus.github.io/pagy/playground/#1-repro-app
17
17
 
18
- VERSION = '8.4.4'
18
+ VERSION = '8.5.0'
19
19
 
20
20
  require 'bundler/inline'
21
21
  require 'bundler'
Binary file
Binary file
File without changes
@@ -0,0 +1 @@
1
+ 4c768dd9af0cc4594a919e6d97ad57eae7eb49ff053ff829c361a3d8aa7364023f94aa9f9613d122a3c42803217a44c4ddf9846d6a300e7a78fcaa344decd317
Binary file
data/bin/pagy CHANGED
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- VERSION = '8.4.4'
4
+ VERSION = '8.5.0'
5
5
  APPS = %w[repro rails demo calendar].freeze
6
6
  LINUX = RbConfig::CONFIG['host_os'].include?('linux')
7
+ HOST = '0.0.0.0'
8
+ PORT = '8000'
7
9
 
8
10
  require_relative '../lib/optimist'
9
11
  opts = Optimist.options do
@@ -20,19 +22,19 @@ opts = Optimist.options do
20
22
  pagy clone APP Clone APP to the current dir
21
23
  pagy APPFILE [options] Develop APPFILE from local path
22
24
  EXAMPLES
23
- pagy demo Showcase demo at http://0.0.0.0:8000
25
+ pagy demo Showcase demo at http://#{HOST}:#{PORT}
24
26
  pagy clone repro Clone repro to ./repro.ru
25
- pagy ~/my-repro.ru Develop ~/my-repro.ru at http://0.0.0.0:8000
27
+ pagy ~/my-repro.ru Develop ~/my-repro.ru at#{HOST}:#{PORT}
26
28
  HEAD
27
29
  text 'Rackup options'
28
30
  opt :env, 'Environment', default: 'development'
29
- opt :host, 'Host', default: '0.0.0.0', short: :o
30
- opt :port, 'Port', default: 8000
31
+ opt :host, 'Host', default: HOST, short: :o
32
+ opt :port, 'Port', default: PORT
31
33
  opt :install, 'Install bundle for users', default: true
32
34
  if LINUX
33
35
  text 'Rerun options'
34
- opt :rerun, 'Enable rerun for development', default: true
35
- opt :clear, 'Clear screen before each rerun'
36
+ opt :rerun, 'Enable rerun for development', default: true
37
+ opt :clear, 'Clear screen before each rerun'
36
38
  end
37
39
  text 'Other options'
38
40
  opt :quiet, 'Quiet mode for development'
@@ -41,8 +43,6 @@ end
41
43
  Optimist.educate if ARGV.empty?
42
44
 
43
45
  run_from_repo = File.exist?(File.expand_path('../pagy.gemspec', __dir__))
44
- # Never install if run from pagy repo (for pagy devs)
45
- opts[:install] = false if run_from_repo
46
46
 
47
47
  # Handles gems
48
48
  require 'bundler/inline'
data/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (8.4.4)
3
+ # Pagy initializer file (8.5.0)
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
 
@@ -73,7 +73,7 @@ const Pagy = (() => {
73
73
  };
74
74
  const trim = (a, param) => a.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "");
75
75
  return {
76
- version: "8.4.4",
76
+ version: "8.5.0",
77
77
  init(arg) {
78
78
  const target = arg instanceof Element ? arg : document;
79
79
  const elements = target.querySelectorAll("[data-pagy]");
data/javascripts/pagy.js CHANGED
@@ -1,4 +1,4 @@
1
- window.Pagy=(()=>{const j=new ResizeObserver((B)=>B.forEach((D)=>D.target.querySelectorAll(".pagy-rjs").forEach((E)=>E.pagyRender()))),x=(B,[D,E,z,G])=>{const F=B.parentElement??B,K=Object.keys(E).map((H)=>parseInt(H)).sort((H,M)=>M-H);let L=-1;const T=(H,M,R)=>H.replace(/__pagy_page__/g,M).replace(/__pagy_label__/g,R);if((B.pagyRender=function(){const H=K.find((Q)=>Q<F.clientWidth)||0;if(H===L)return;let M=D.before;const R=E[H.toString()],X=z?.[H.toString()]??R.map((Q)=>Q.toString());R.forEach((Q,J)=>{const $=X[J];let U;if(typeof Q==="number")U=T(D.a,Q.toString(),$);else if(Q==="gap")U=D.gap;else U=T(D.current,Q,$);M+=typeof G==="string"&&Q==1?Z(U,G):U}),M+=D.after,B.innerHTML="",B.insertAdjacentHTML("afterbegin",M),L=H})(),B.classList.contains("pagy-rjs"))j.observe(F)},A=(B,[D,E])=>Y(B,(z)=>[z,D.replace(/__pagy_page__/,z)],E),C=(B,[D,E,z])=>{Y(B,(G)=>{const F=Math.max(Math.ceil(D/parseInt(G)),1).toString(),K=E.replace(/__pagy_page__/,F).replace(/__pagy_items__/,G);return[F,K]},z)},Y=(B,D,E)=>{const z=B.querySelector("input"),G=B.querySelector("a"),F=z.value,K=function(){if(z.value===F)return;const[L,T,H]=[z.min,z.value,z.max].map((X)=>parseInt(X)||0);if(T<L||T>H){z.value=F,z.select();return}let[M,R]=D(z.value);if(typeof E==="string"&&M==="1")R=Z(R,E);G.href=R,G.click()};["change","focus"].forEach((L)=>z.addEventListener(L,()=>z.select())),z.addEventListener("focusout",K),z.addEventListener("keypress",(L)=>{if(L.key==="Enter")K()})},Z=(B,D)=>B.replace(new RegExp(`[?&]${D}=1\\b(?!&)|\\b${D}=1&`),"");return{version:"8.4.4",init(B){const E=(B instanceof Element?B:document).querySelectorAll("[data-pagy]");for(let z of E)try{const G=Uint8Array.from(atob(z.getAttribute("data-pagy")),(L)=>L.charCodeAt(0)),[F,...K]=JSON.parse((new TextDecoder()).decode(G));if(F==="nav")x(z,K);else if(F==="combo")A(z,K);else if(F==="selector")C(z,K);else console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'",z,F)}catch(G){console.warn("Skipped Pagy.init() for: %o\n%s",z,G)}}}})();
1
+ window.Pagy=(()=>{const j=new ResizeObserver((B)=>B.forEach((D)=>D.target.querySelectorAll(".pagy-rjs").forEach((E)=>E.pagyRender()))),x=(B,[D,E,z,G])=>{const F=B.parentElement??B,K=Object.keys(E).map((H)=>parseInt(H)).sort((H,M)=>M-H);let L=-1;const T=(H,M,R)=>H.replace(/__pagy_page__/g,M).replace(/__pagy_label__/g,R);if((B.pagyRender=function(){const H=K.find((Q)=>Q<F.clientWidth)||0;if(H===L)return;let M=D.before;const R=E[H.toString()],X=z?.[H.toString()]??R.map((Q)=>Q.toString());R.forEach((Q,J)=>{const $=X[J];let U;if(typeof Q==="number")U=T(D.a,Q.toString(),$);else if(Q==="gap")U=D.gap;else U=T(D.current,Q,$);M+=typeof G==="string"&&Q==1?Z(U,G):U}),M+=D.after,B.innerHTML="",B.insertAdjacentHTML("afterbegin",M),L=H})(),B.classList.contains("pagy-rjs"))j.observe(F)},A=(B,[D,E])=>Y(B,(z)=>[z,D.replace(/__pagy_page__/,z)],E),C=(B,[D,E,z])=>{Y(B,(G)=>{const F=Math.max(Math.ceil(D/parseInt(G)),1).toString(),K=E.replace(/__pagy_page__/,F).replace(/__pagy_items__/,G);return[F,K]},z)},Y=(B,D,E)=>{const z=B.querySelector("input"),G=B.querySelector("a"),F=z.value,K=function(){if(z.value===F)return;const[L,T,H]=[z.min,z.value,z.max].map((X)=>parseInt(X)||0);if(T<L||T>H){z.value=F,z.select();return}let[M,R]=D(z.value);if(typeof E==="string"&&M==="1")R=Z(R,E);G.href=R,G.click()};["change","focus"].forEach((L)=>z.addEventListener(L,()=>z.select())),z.addEventListener("focusout",K),z.addEventListener("keypress",(L)=>{if(L.key==="Enter")K()})},Z=(B,D)=>B.replace(new RegExp(`[?&]${D}=1\\b(?!&)|\\b${D}=1&`),"");return{version:"8.5.0",init(B){const E=(B instanceof Element?B:document).querySelectorAll("[data-pagy]");for(let z of E)try{const G=Uint8Array.from(atob(z.getAttribute("data-pagy")),(L)=>L.charCodeAt(0)),[F,...K]=JSON.parse((new TextDecoder()).decode(G));if(F==="nav")x(z,K);else if(F==="combo")A(z,K);else if(F==="selector")C(z,K);else console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'",z,F)}catch(G){console.warn("Skipped Pagy.init() for: %o\n%s",z,G)}}}})();
2
2
 
3
- //# debugId=42F7B0D379AB673764756e2164756e21
3
+ //# debugId=20862703AA72E11D64756e2164756e21
4
4
  //# sourceMappingURL=pagy.min.js.map
@@ -1,4 +1,4 @@
1
- window.Pagy=(()=>{const j=new ResizeObserver((B)=>B.forEach((D)=>D.target.querySelectorAll(".pagy-rjs").forEach((E)=>E.pagyRender()))),x=(B,[D,E,z,G])=>{const F=B.parentElement??B,K=Object.keys(E).map((H)=>parseInt(H)).sort((H,M)=>M-H);let L=-1;const T=(H,M,R)=>H.replace(/__pagy_page__/g,M).replace(/__pagy_label__/g,R);if((B.pagyRender=function(){const H=K.find((Q)=>Q<F.clientWidth)||0;if(H===L)return;let M=D.before;const R=E[H.toString()],X=z?.[H.toString()]??R.map((Q)=>Q.toString());R.forEach((Q,J)=>{const $=X[J];let U;if(typeof Q==="number")U=T(D.a,Q.toString(),$);else if(Q==="gap")U=D.gap;else U=T(D.current,Q,$);M+=typeof G==="string"&&Q==1?Z(U,G):U}),M+=D.after,B.innerHTML="",B.insertAdjacentHTML("afterbegin",M),L=H})(),B.classList.contains("pagy-rjs"))j.observe(F)},A=(B,[D,E])=>Y(B,(z)=>[z,D.replace(/__pagy_page__/,z)],E),C=(B,[D,E,z])=>{Y(B,(G)=>{const F=Math.max(Math.ceil(D/parseInt(G)),1).toString(),K=E.replace(/__pagy_page__/,F).replace(/__pagy_items__/,G);return[F,K]},z)},Y=(B,D,E)=>{const z=B.querySelector("input"),G=B.querySelector("a"),F=z.value,K=function(){if(z.value===F)return;const[L,T,H]=[z.min,z.value,z.max].map((X)=>parseInt(X)||0);if(T<L||T>H){z.value=F,z.select();return}let[M,R]=D(z.value);if(typeof E==="string"&&M==="1")R=Z(R,E);G.href=R,G.click()};["change","focus"].forEach((L)=>z.addEventListener(L,()=>z.select())),z.addEventListener("focusout",K),z.addEventListener("keypress",(L)=>{if(L.key==="Enter")K()})},Z=(B,D)=>B.replace(new RegExp(`[?&]${D}=1\\b(?!&)|\\b${D}=1&`),"");return{version:"8.4.4",init(B){const E=(B instanceof Element?B:document).querySelectorAll("[data-pagy]");for(let z of E)try{const G=Uint8Array.from(atob(z.getAttribute("data-pagy")),(L)=>L.charCodeAt(0)),[F,...K]=JSON.parse((new TextDecoder()).decode(G));if(F==="nav")x(z,K);else if(F==="combo")A(z,K);else if(F==="selector")C(z,K);else console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'",z,F)}catch(G){console.warn("Skipped Pagy.init() for: %o\n%s",z,G)}}}})();
1
+ window.Pagy=(()=>{const j=new ResizeObserver((B)=>B.forEach((D)=>D.target.querySelectorAll(".pagy-rjs").forEach((E)=>E.pagyRender()))),x=(B,[D,E,z,G])=>{const F=B.parentElement??B,K=Object.keys(E).map((H)=>parseInt(H)).sort((H,M)=>M-H);let L=-1;const T=(H,M,R)=>H.replace(/__pagy_page__/g,M).replace(/__pagy_label__/g,R);if((B.pagyRender=function(){const H=K.find((Q)=>Q<F.clientWidth)||0;if(H===L)return;let M=D.before;const R=E[H.toString()],X=z?.[H.toString()]??R.map((Q)=>Q.toString());R.forEach((Q,J)=>{const $=X[J];let U;if(typeof Q==="number")U=T(D.a,Q.toString(),$);else if(Q==="gap")U=D.gap;else U=T(D.current,Q,$);M+=typeof G==="string"&&Q==1?Z(U,G):U}),M+=D.after,B.innerHTML="",B.insertAdjacentHTML("afterbegin",M),L=H})(),B.classList.contains("pagy-rjs"))j.observe(F)},A=(B,[D,E])=>Y(B,(z)=>[z,D.replace(/__pagy_page__/,z)],E),C=(B,[D,E,z])=>{Y(B,(G)=>{const F=Math.max(Math.ceil(D/parseInt(G)),1).toString(),K=E.replace(/__pagy_page__/,F).replace(/__pagy_items__/,G);return[F,K]},z)},Y=(B,D,E)=>{const z=B.querySelector("input"),G=B.querySelector("a"),F=z.value,K=function(){if(z.value===F)return;const[L,T,H]=[z.min,z.value,z.max].map((X)=>parseInt(X)||0);if(T<L||T>H){z.value=F,z.select();return}let[M,R]=D(z.value);if(typeof E==="string"&&M==="1")R=Z(R,E);G.href=R,G.click()};["change","focus"].forEach((L)=>z.addEventListener(L,()=>z.select())),z.addEventListener("focusout",K),z.addEventListener("keypress",(L)=>{if(L.key==="Enter")K()})},Z=(B,D)=>B.replace(new RegExp(`[?&]${D}=1\\b(?!&)|\\b${D}=1&`),"");return{version:"8.5.0",init(B){const E=(B instanceof Element?B:document).querySelectorAll("[data-pagy]");for(let z of E)try{const G=Uint8Array.from(atob(z.getAttribute("data-pagy")),(L)=>L.charCodeAt(0)),[F,...K]=JSON.parse((new TextDecoder()).decode(G));if(F==="nav")x(z,K);else if(F==="combo")A(z,K);else if(F==="selector")C(z,K);else console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'",z,F)}catch(G){console.warn("Skipped Pagy.init() for: %o\n%s",z,G)}}}})();
2
2
 
3
- //# debugId=42F7B0D379AB673764756e2164756e21
3
+ //# debugId=20862703AA72E11D64756e2164756e21
4
4
  //# sourceMappingURL=pagy.min.js.map
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/pagy.ts"],
4
4
  "sourcesContent": [
5
- "type NavArgs = readonly [Tokens, Sequels, null | LabelSequels, string?]\ntype ComboArgs = readonly [string, string?]\ntype SelectorArgs = readonly [number, string, string?]\ntype JsonArgs = ['nav', NavArgs] | ['combo', ComboArgs] | ['selector', SelectorArgs]\n\ninterface Tokens {\n readonly before:string\n readonly a:string\n readonly current:string\n readonly gap:string\n readonly after:string\n}\ninterface Sequels {readonly [width:string]:(string | number)[]}\ninterface LabelSequels {readonly [width:string]:string[]}\ninterface NavElement extends Element {pagyRender():void}\n\nconst Pagy = (() => {\n // The observer instance for responsive navs\n const rjsObserver = new ResizeObserver(\n entries => entries.forEach(e => e.target.querySelectorAll<NavElement>(\".pagy-rjs\")\n .forEach(el => el.pagyRender())));\n // Init the *_nav_js helpers\n const initNav = (el:NavElement, [tokens, sequels, labelSequels, trimParam]:NavArgs) => {\n const container = el.parentElement ?? el;\n const widths = Object.keys(sequels).map(w => parseInt(w)).sort((a, b) => b - a);\n let lastWidth = -1;\n const fillIn = (a:string, page:string, label:string):string =>\n a.replace(/__pagy_page__/g, page).replace(/__pagy_label__/g, label);\n (el.pagyRender = function () {\n const width = widths.find(w => w < container.clientWidth) || 0;\n if (width === lastWidth) { return } // no change: abort\n let html = tokens.before; // already trimmed in html\n const series = sequels[width.toString()];\n const labels = labelSequels?.[width.toString()] ?? series.map(l => l.toString());\n series.forEach((item, i) => {\n const label = labels[i];\n let filled;\n if (typeof item === \"number\") {\n filled = fillIn(tokens.a, item.toString(), label);\n } else if (item === \"gap\") {\n filled = tokens.gap;\n } else { // active page\n filled = fillIn(tokens.current, item, label);\n }\n html += (typeof trimParam === \"string\" && item == 1) ? trim(filled, trimParam) : filled;\n });\n html += tokens.after;\n el.innerHTML = \"\";\n el.insertAdjacentHTML(\"afterbegin\", html);\n lastWidth = width;\n })();\n if (el.classList.contains(\"pagy-rjs\")) { rjsObserver.observe(container) }\n };\n\n // Init the *_combo_nav_js helpers\n const initCombo = (el:Element, [url_token, trimParam]:ComboArgs) =>\n initInput(el, inputValue => [inputValue, url_token.replace(/__pagy_page__/, inputValue)], trimParam);\n\n // Init the items_selector_js helper\n const initSelector = (el:Element, [from, url_token, trimParam]:SelectorArgs) => {\n initInput(el, inputValue => {\n const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString();\n const url = url_token.replace(/__pagy_page__/, page).replace(/__pagy_items__/, inputValue);\n return [page, url];\n }, trimParam);\n };\n\n // Init the input element\n const initInput = (el:Element, getVars:(v:string) => [string, string], trimParam?:string) => {\n const input = el.querySelector(\"input\") as HTMLInputElement;\n const link = el.querySelector(\"a\") as HTMLAnchorElement;\n const initial = input.value;\n const action = function () {\n if (input.value === initial) { return } // not changed\n const [min, val, max] = [input.min, input.value, input.max].map(n => parseInt(n) || 0);\n if (val < min || val > max) { // reset invalid/out-of-range\n input.value = initial;\n input.select();\n return;\n }\n let [page, url] = getVars(input.value); // eslint-disable-line prefer-const\n if (typeof trimParam === \"string\" && page === \"1\") { url = trim(url, trimParam) }\n link.href = url;\n link.click();\n };\n [\"change\", \"focus\"].forEach(e => input.addEventListener(e, () => input.select())); // auto-select\n input.addEventListener(\"focusout\", action); // trigger action\n input.addEventListener(\"keypress\", e => { if (e.key === \"Enter\") { action() } }); // trigger action\n };\n\n // Trim the ${page-param}=1 params in links\n const trim = (a:string, param:string) =>\n a.replace(new RegExp(`[?&]${param}=1\\\\b(?!&)|\\\\b${param}=1&`), \"\");\n\n // Public interface\n return {\n version: \"8.4.4\",\n\n // Scan for elements with a \"data-pagy\" attribute and call their init functions with the decoded args\n init(arg?:Element) {\n const target = arg instanceof Element ? arg : document;\n const elements = target.querySelectorAll(\"[data-pagy]\");\n for (const el of elements) {\n try {\n const uint8array = Uint8Array.from(atob(el.getAttribute(\"data-pagy\") as string), c => c.charCodeAt(0));\n const [keyword, ...args] = JSON.parse((new TextDecoder()).decode(uint8array)) as JsonArgs; // base64-utf8 -> JSON -> Array\n if (keyword === \"nav\") {\n initNav(el as NavElement, args as unknown as NavArgs);\n } else if (keyword === \"combo\") {\n initCombo(el, args as unknown as ComboArgs);\n } else if (keyword === \"selector\") {\n initSelector(el, args as unknown as SelectorArgs);\n } else {\n console.warn(\"Skipped Pagy.init() for: %o\\nUnknown keyword '%s'\", el, keyword);\n }\n } catch (err) { console.warn(\"Skipped Pagy.init() for: %o\\n%s\", el, err) }\n }\n }\n };\n})();\n\nexport default Pagy;\n"
5
+ "type NavArgs = readonly [Tokens, Sequels, null | LabelSequels, string?]\ntype ComboArgs = readonly [string, string?]\ntype SelectorArgs = readonly [number, string, string?]\ntype JsonArgs = ['nav', NavArgs] | ['combo', ComboArgs] | ['selector', SelectorArgs]\n\ninterface Tokens {\n readonly before:string\n readonly a:string\n readonly current:string\n readonly gap:string\n readonly after:string\n}\ninterface Sequels {readonly [width:string]:(string | number)[]}\ninterface LabelSequels {readonly [width:string]:string[]}\ninterface NavElement extends Element {pagyRender():void}\n\nconst Pagy = (() => {\n // The observer instance for responsive navs\n const rjsObserver = new ResizeObserver(\n entries => entries.forEach(e => e.target.querySelectorAll<NavElement>(\".pagy-rjs\")\n .forEach(el => el.pagyRender())));\n // Init the *_nav_js helpers\n const initNav = (el:NavElement, [tokens, sequels, labelSequels, trimParam]:NavArgs) => {\n const container = el.parentElement ?? el;\n const widths = Object.keys(sequels).map(w => parseInt(w)).sort((a, b) => b - a);\n let lastWidth = -1;\n const fillIn = (a:string, page:string, label:string):string =>\n a.replace(/__pagy_page__/g, page).replace(/__pagy_label__/g, label);\n (el.pagyRender = function () {\n const width = widths.find(w => w < container.clientWidth) || 0;\n if (width === lastWidth) { return } // no change: abort\n let html = tokens.before; // already trimmed in html\n const series = sequels[width.toString()];\n const labels = labelSequels?.[width.toString()] ?? series.map(l => l.toString());\n series.forEach((item, i) => {\n const label = labels[i];\n let filled;\n if (typeof item === \"number\") {\n filled = fillIn(tokens.a, item.toString(), label);\n } else if (item === \"gap\") {\n filled = tokens.gap;\n } else { // active page\n filled = fillIn(tokens.current, item, label);\n }\n html += (typeof trimParam === \"string\" && item == 1) ? trim(filled, trimParam) : filled;\n });\n html += tokens.after;\n el.innerHTML = \"\";\n el.insertAdjacentHTML(\"afterbegin\", html);\n lastWidth = width;\n })();\n if (el.classList.contains(\"pagy-rjs\")) { rjsObserver.observe(container) }\n };\n\n // Init the *_combo_nav_js helpers\n const initCombo = (el:Element, [url_token, trimParam]:ComboArgs) =>\n initInput(el, inputValue => [inputValue, url_token.replace(/__pagy_page__/, inputValue)], trimParam);\n\n // Init the items_selector_js helper\n const initSelector = (el:Element, [from, url_token, trimParam]:SelectorArgs) => {\n initInput(el, inputValue => {\n const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString();\n const url = url_token.replace(/__pagy_page__/, page).replace(/__pagy_items__/, inputValue);\n return [page, url];\n }, trimParam);\n };\n\n // Init the input element\n const initInput = (el:Element, getVars:(v:string) => [string, string], trimParam?:string) => {\n const input = el.querySelector(\"input\") as HTMLInputElement;\n const link = el.querySelector(\"a\") as HTMLAnchorElement;\n const initial = input.value;\n const action = function () {\n if (input.value === initial) { return } // not changed\n const [min, val, max] = [input.min, input.value, input.max].map(n => parseInt(n) || 0);\n if (val < min || val > max) { // reset invalid/out-of-range\n input.value = initial;\n input.select();\n return;\n }\n let [page, url] = getVars(input.value); // eslint-disable-line prefer-const\n if (typeof trimParam === \"string\" && page === \"1\") { url = trim(url, trimParam) }\n link.href = url;\n link.click();\n };\n [\"change\", \"focus\"].forEach(e => input.addEventListener(e, () => input.select())); // auto-select\n input.addEventListener(\"focusout\", action); // trigger action\n input.addEventListener(\"keypress\", e => { if (e.key === \"Enter\") { action() } }); // trigger action\n };\n\n // Trim the ${page-param}=1 params in links\n const trim = (a:string, param:string) =>\n a.replace(new RegExp(`[?&]${param}=1\\\\b(?!&)|\\\\b${param}=1&`), \"\");\n\n // Public interface\n return {\n version: \"8.5.0\",\n\n // Scan for elements with a \"data-pagy\" attribute and call their init functions with the decoded args\n init(arg?:Element) {\n const target = arg instanceof Element ? arg : document;\n const elements = target.querySelectorAll(\"[data-pagy]\");\n for (const el of elements) {\n try {\n const uint8array = Uint8Array.from(atob(el.getAttribute(\"data-pagy\") as string), c => c.charCodeAt(0));\n const [keyword, ...args] = JSON.parse((new TextDecoder()).decode(uint8array)) as JsonArgs; // base64-utf8 -> JSON -> Array\n if (keyword === \"nav\") {\n initNav(el as NavElement, args as unknown as NavArgs);\n } else if (keyword === \"combo\") {\n initCombo(el, args as unknown as ComboArgs);\n } else if (keyword === \"selector\") {\n initSelector(el, args as unknown as SelectorArgs);\n } else {\n console.warn(\"Skipped Pagy.init() for: %o\\nUnknown keyword '%s'\", el, keyword);\n }\n } catch (err) { console.warn(\"Skipped Pagy.init() for: %o\\n%s\", el, err) }\n }\n }\n };\n})();\n\nexport default Pagy;\n"
6
6
  ],
7
7
  "mappings": "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",
8
- "debugId": "42F7B0D379AB673764756e2164756e21",
8
+ "debugId": "20862703AA72E11D64756e2164756e21",
9
9
  "names": []
10
10
  }
data/javascripts/pagy.mjs CHANGED
@@ -73,7 +73,7 @@ const Pagy = (() => {
73
73
  };
74
74
  const trim = (a, param) => a.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "");
75
75
  return {
76
- version: "8.4.4",
76
+ version: "8.5.0",
77
77
  init(arg) {
78
78
  const target = arg instanceof Element ? arg : document;
79
79
  const elements = target.querySelectorAll("[data-pagy]");
data/lib/optimist.rb CHANGED
@@ -137,7 +137,7 @@ module Optimist
137
137
  ## There's one ambiguous case to be aware of: when +:multi+: is true and a
138
138
  ## +:default+ is set to an array (of something), it's ambiguous whether this
139
139
  ## is a multi-value argument as well as a multi-occurrence argument.
140
- ## In thise case, Optimist assumes that it's not a multi-value argument.
140
+ ## In this case, Optimist assumes that it's not a multi-value argument.
141
141
  ## If you want a multi-value, multi-occurrence argument with a default
142
142
  ## value, you must specify +:type+ as well.
143
143
 
@@ -3,8 +3,8 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar day subclass
7
- class Day < Calendar
6
+ # Day unit subclass
7
+ class Day < Unit
8
8
  DEFAULT = { size: 31, # rubocop:disable Style/MutableConstant
9
9
  order: :asc,
10
10
  format: '%d' }
@@ -3,8 +3,8 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar month subclass
7
- class Month < Calendar
6
+ # Month unit subclass
7
+ class Month < Unit
8
8
  DEFAULT = { size: 12, # rubocop:disable Style/MutableConstant
9
9
  order: :asc,
10
10
  format: '%b' }
@@ -3,8 +3,8 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar quarter subclass
7
- class Quarter < Calendar
6
+ # Quarter unit subclass
7
+ class Quarter < Unit
8
8
  DEFAULT = { size: 4, # rubocop:disable Style/MutableConstant
9
9
  order: :asc,
10
10
  format: 'Q%q' } # '%q' token
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext/time'
5
+ require 'active_support/core_ext/date_and_time/calculations'
6
+ require 'active_support/core_ext/numeric/time'
7
+ require 'active_support/core_ext/integer/time'
8
+
9
+ class Pagy # :nodoc:
10
+ class Calendar < Hash # :nodoc:
11
+ # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
12
+ class Unit < Pagy
13
+ attr_reader :order, :from, :to
14
+
15
+ # Merge and validate the options, do some simple arithmetic and set a few instance variables
16
+ def initialize(vars) # rubocop:disable Lint/MissingSuper
17
+ raise InternalError, 'Pagy::Calendar::Unit is a base class; use one of its subclasses' \
18
+ if instance_of?(Pagy::Calendar::Unit)
19
+
20
+ vars = self.class::DEFAULT.merge(vars) # subclass specific default
21
+ normalize_vars(vars) # general default
22
+ setup_vars(page: 1)
23
+ setup_unit_vars
24
+ raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
25
+
26
+ @prev = (@page - 1 unless @page == 1)
27
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
28
+ end
29
+
30
+ # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
31
+ def label(opts = {})
32
+ label_for(@page, opts)
33
+ end
34
+
35
+ # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
36
+ def label_for(page, opts = {})
37
+ opts[:format] ||= @vars[:format]
38
+ localize(starting_time_for(page.to_i), opts) # page could be a string
39
+ end
40
+
41
+ protected
42
+
43
+ # The page that includes time
44
+ # In case of out of range time, the :fit_time option avoids the outOfRangeError
45
+ # and returns the closest page to the passed time argument (first or last page)
46
+ def page_at(time, **opts)
47
+ fit_time = time
48
+ fit_final = @final - 1
49
+ unless time.between?(@initial, fit_final)
50
+ raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
51
+
52
+ if time < @final
53
+ fit_time = @initial
54
+ ordinal = 'first'
55
+ else
56
+ fit_time = fit_final
57
+ ordinal = 'last'
58
+ end
59
+ warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
60
+ end
61
+ offset = page_offset_at(fit_time) # offset starts from 0
62
+ @order == :asc ? offset + 1 : @last - offset
63
+ end
64
+
65
+ # Base class method for the setup of the unit variables (subclasses must implement it and call super)
66
+ def setup_unit_vars
67
+ raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
68
+ raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
69
+ unless %i[asc desc].include?(@order = @vars[:order])
70
+
71
+ @starting, @ending = @vars[:period]
72
+ raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
73
+ unless @starting.is_a?(ActiveSupport::TimeWithZone) \
74
+ && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
75
+ end
76
+
77
+ # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
78
+ def localize(time, opts)
79
+ time.strftime(opts[:format])
80
+ end
81
+
82
+ # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
83
+ # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
84
+ def time_offset_for(page)
85
+ @order == :asc ? page - 1 : @last - page
86
+ end
87
+
88
+ # Period of the active page (used internally for nested units)
89
+ def active_period
90
+ [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
91
+ end
92
+
93
+ # :nocov:
94
+ # This method must be implemented by the unit subclass
95
+ def starting_time_for(*)
96
+ raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
97
+ end
98
+
99
+ # This method must be implemented by the unit subclass
100
+ def page_offset_at(*)
101
+ raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
102
+ end
103
+ # :nocov:
104
+ end
105
+ end
106
+ end
@@ -3,8 +3,8 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar week subclass
7
- class Week < Calendar
6
+ # Week unit subclass
7
+ class Week < Unit
8
8
  DEFAULT = { order: :asc, # rubocop:disable Style/MutableConstant
9
9
  format: '%Y-%W' }
10
10
 
@@ -3,8 +3,8 @@
3
3
 
4
4
  class Pagy # :nodoc:
5
5
  class Calendar # :nodoc:
6
- # Calendar year subclass
7
- class Year < Calendar
6
+ # Year unit subclass
7
+ class Year < Unit
8
8
  DEFAULT = { size: 10, # rubocop:disable Style/MutableConstant
9
9
  order: :asc,
10
10
  format: '%Y' }
data/lib/pagy/calendar.rb CHANGED
@@ -1,122 +1,79 @@
1
1
  # See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/calendar
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'active_support'
5
- require 'active_support/core_ext/time'
6
- require 'active_support/core_ext/date_and_time/calculations'
7
- require 'active_support/core_ext/numeric/time'
8
- require 'active_support/core_ext/integer/time'
9
-
10
4
  require_relative '../pagy'
5
+ require_relative 'calendar/unit'
11
6
 
12
7
  class Pagy # :nodoc:
13
- # Base class for time units subclasses (Year, Quarter, Month, Week, Day)
14
- class Calendar < Pagy
8
+ # Calendar class
9
+ class Calendar < Hash
15
10
  # Specific out of range error
16
11
  class OutOfRangeError < VariableError; end
17
12
 
18
13
  # List of units in desc order of duration. It can be used for custom units.
19
14
  UNITS = %i[year quarter month week day] # rubocop:disable Style/MutableConstant
20
15
 
21
- attr_reader :order, :from, :to
22
-
23
- # Merge and validate the options, do some simple arithmetic and set a few instance variables
24
- def initialize(vars) # rubocop:disable Lint/MissingSuper
25
- raise InternalError, 'Pagy::Calendar is a base class; use one of its subclasses' if instance_of?(Pagy::Calendar)
26
-
27
- vars = self.class::DEFAULT.merge(vars) # subclass specific default
28
- normalize_vars(vars) # general default
29
- setup_vars(page: 1)
30
- setup_unit_vars
31
- raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
32
-
33
- @prev = (@page - 1 unless @page == 1)
34
- @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
35
- end
36
-
37
- # The label for the current page (it can pass along the I18n gem opts when it's used with the i18n extra)
38
- def label(opts = {})
39
- label_for(@page, opts)
40
- end
41
-
42
- # The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
43
- def label_for(page, opts = {})
44
- opts[:format] ||= @vars[:format]
45
- localize(starting_time_for(page.to_i), opts) # page could be a string
46
- end
47
-
48
- protected
16
+ class << self
17
+ private
49
18
 
50
- # The page that includes time
51
- # In case of out of range time, the :fit_time option avoids the outOfRangeError
52
- # and returns the closest page to the passed time argument (first or last page)
53
- def page_at(time, **opts)
54
- fit_time = time
55
- fit_final = @final - 1
56
- unless time.between?(@initial, fit_final)
57
- raise OutOfRangeError.new(self, :time, "between #{@initial} and #{fit_final}", time) unless opts[:fit_time]
19
+ # Create a unit subclass instance by using the unit name (internal use)
20
+ def create(unit, vars)
21
+ raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
58
22
 
59
- if time < @final
60
- fit_time = @initial
61
- ordinal = 'first'
62
- else
63
- fit_time = fit_final
64
- ordinal = 'last'
65
- end
66
- Warning.warn "Pagy::Calendar#page_at: Rescued #{time} out of range by returning the #{ordinal} page."
23
+ name = unit.to_s
24
+ name[0] = name[0].capitalize
25
+ Object.const_get("Pagy::Calendar::#{name}").new(vars)
67
26
  end
68
- offset = page_offset_at(fit_time) # offset starts from 0
69
- @order == :asc ? offset + 1 : @last - offset
70
- end
71
27
 
72
- # Base class method for the setup of the unit variables (subclasses must implement it and call super)
73
- def setup_unit_vars
74
- raise VariableError.new(self, :format, 'to be a strftime format', @vars[:format]) unless @vars[:format].is_a?(String)
75
- raise VariableError.new(self, :order, 'to be in [:asc, :desc]', @order) \
76
- unless %i[asc desc].include?(@order = @vars[:order])
77
-
78
- @starting, @ending = @vars[:period]
79
- raise VariableError.new(self, :period, 'to be a an Array of min and max TimeWithZone instances', @vars[:period]) \
80
- unless @starting.is_a?(ActiveSupport::TimeWithZone) \
81
- && @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
82
- end
83
-
84
- # Apply the strftime format to the time (overridden by the i18n extra when localization is required)
85
- def localize(time, opts)
86
- time.strftime(opts[:format])
87
- end
88
-
89
- # Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
90
- # Used in starting_time_for(page) where page starts from 1 (e.g. page to starting_time means subtracting 1)
91
- def time_offset_for(page)
92
- @order == :asc ? page - 1 : @last - page
93
- end
94
-
95
- # Period of the active page (used internally for nested units)
96
- def active_period
97
- [[@starting, @from].max, [@to - 1, @ending].min] # -1 sec: include only last unit day
28
+ # Return calendar, from, to
29
+ def init(conf, period, params)
30
+ new.send(:init, conf, period, params)
31
+ end
98
32
  end
99
33
 
100
- # :nocov:
101
- # This method must be implemented by the unit subclass
102
- def starting_time_for(*)
103
- raise NoMethodError, 'the starting_time_for method must be implemented by the unit subclass'
34
+ # Return the current time of the smallest time unit shown
35
+ def showtime
36
+ self[@units.last].from
104
37
  end
105
38
 
106
- # This method must be implemented by the unit subclass
107
- def page_offset_at(*)
108
- raise NoMethodError, 'the page_offset_at method must be implemented by the unit subclass'
39
+ private
40
+
41
+ # Create the calendar
42
+ def init(conf, period, params)
43
+ @conf = Marshal.load(Marshal.dump(conf)) # store a copy
44
+ @units = Calendar::UNITS & @conf.keys # get the units in time length desc order
45
+ raise ArgumentError, 'no calendar unit found in pagy_calendar @configuration' if @units.empty?
46
+
47
+ @period = period
48
+ @params = params
49
+ @page_param = conf[:pagy][:page_param] || DEFAULT[:page_param]
50
+ @units.each do |unit| # set all the :page_param vars for later deletion
51
+ unit_page_param = :"#{unit}_#{@page_param}"
52
+ conf[unit][:page_param] = unit_page_param
53
+ conf[unit][:page] = @params[unit_page_param]
54
+ end
55
+ calendar = {}
56
+ object = nil
57
+ @units.each_with_index do |unit, index|
58
+ params_to_delete = @units[(index + 1), @units.size].map { |sub| conf[sub][:page_param] } + [@page_param]
59
+ conf[unit][:params] = lambda { |up| up.except(*params_to_delete.map(&:to_s)) } # rubocop:disable Style/Lambda
60
+ conf[unit][:period] = object&.send(:active_period) || @period
61
+ calendar[unit] = object = Calendar.send(:create, unit, conf[unit])
62
+ end
63
+ [replace(calendar), object.from, object.to]
109
64
  end
110
- # :nocov:
111
-
112
- class << self
113
- # Create a subclass instance by unit name (internal use)
114
- def create(unit, vars)
115
- raise InternalError, "unit must be in #{UNITS.inspect}; got #{unit}" unless UNITS.include?(unit)
116
65
 
117
- name = unit.to_s
118
- name[0] = name[0].capitalize
119
- Object.const_get("Pagy::Calendar::#{name}").new(vars)
66
+ # Return the calendar object at time
67
+ def calendar_at(time, **opts)
68
+ conf = Marshal.load(Marshal.dump(@conf))
69
+ page_params = {}
70
+ @units.inject(nil) do |object, unit|
71
+ conf[unit][:period] = object&.send(:active_period) || @period
72
+ conf[unit][:page] = page_params[:"#{unit}_#{@page_param}"] \
73
+ = Calendar.send(:create, unit, conf[unit]).send(:page_at, time, **opts)
74
+ conf[unit][:params] ||= {}
75
+ conf[unit][:params].merge!(page_params)
76
+ Calendar.send(:create, unit, conf[unit])
120
77
  end
121
78
  end
122
79
  end
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative '../calendar'
5
- require_relative '../calendar/helper'
6
5
 
7
6
  class Pagy # :nodoc:
8
7
  # Add pagination filtering by calendar unit (:year, :quarter, :month, :week, :day) to the regular pagination
@@ -20,8 +19,13 @@ class Pagy # :nodoc:
20
19
 
21
20
  conf[:pagy] ||= {}
22
21
  unless conf.key?(:active) && !conf[:active]
23
- calendar, from, to = Calendar::Helper.send(:init, conf, pagy_calendar_period(collection), params)
24
- collection = pagy_calendar_filter(collection, from, to)
22
+ calendar, from, to = Calendar.send(:init, conf, pagy_calendar_period(collection), params)
23
+ if respond_to?(:pagy_calendar_counts)
24
+ calendar.each_key do |unit|
25
+ calendar[unit].vars[:counts] = pagy_calendar_counts(collection, unit, *calendar[unit].vars[:period])
26
+ end
27
+ end
28
+ collection = pagy_calendar_filter(collection, from, to)
25
29
  end
26
30
  pagy, results = send(conf[:pagy][:backend] || :pagy, collection, conf[:pagy]) # use backend: :pagy when omitted
27
31
  [calendar, pagy, results]
@@ -40,6 +44,32 @@ class Pagy # :nodoc:
40
44
  end
41
45
  end
42
46
 
47
+ # Override the pagy_anchor
48
+ module FrontendOverride
49
+ # Consider the vars[:count]
50
+ def pagy_anchor(pagy)
51
+ return super unless (counts = pagy.vars[:counts])
52
+
53
+ a_string = pagy.vars[:anchor_string]
54
+ a_string = %( #{a_string}) if a_string
55
+ left, right = %(<a#{a_string} href="#{pagy_url_for(pagy, PAGE_TOKEN)}").split(PAGE_TOKEN, 2)
56
+ # lambda used by all the helpers
57
+ lambda do |page, text = pagy.label_for(page), classes: nil, aria_label: nil|
58
+ count = counts[page - 1]
59
+ item_name = pagy_t('pagy.item_name', count:)
60
+ if count.zero?
61
+ classes = "#{classes && (classes + ' ')}empty-page"
62
+ title = %( title="#{pagy_t('pagy.info.no_items', item_name:, count:)}")
63
+ else
64
+ title = %( title="#{pagy_t('pagy.info.single_page', item_name:, count:)}")
65
+ end
66
+ classes = %( class="#{classes}") if classes
67
+ aria_label = %( aria-label="#{aria_label}") if aria_label
68
+ %(#{left}#{page}#{right}#{title}#{classes}#{aria_label}>#{text}</a>)
69
+ end
70
+ end
71
+ end
72
+
43
73
  # Additions for the Frontend module
44
74
  module UrlHelperAddOn
45
75
  # Return the url for the calendar page at time
@@ -49,5 +79,5 @@ class Pagy # :nodoc:
49
79
  end
50
80
  end
51
81
  Backend.prepend CalendarExtra::BackendAddOn, CalendarExtra::UrlHelperAddOn
52
- Frontend.prepend CalendarExtra::UrlHelperAddOn
82
+ Frontend.prepend CalendarExtra::UrlHelperAddOn, CalendarExtra::FrontendOverride
53
83
  end
@@ -19,7 +19,7 @@ class Pagy # :nodoc:
19
19
  end
20
20
  end
21
21
  end
22
- Calendar.prepend I18nExtra::CalendarOverride if defined?(Calendar)
22
+ Calendar::Unit.prepend I18nExtra::CalendarOverride if defined?(Calendar::Unit)
23
23
 
24
24
  # Add the pagy locales to the I18n.load_path
25
25
  ::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
@@ -42,7 +42,7 @@ class Pagy # :nodoc:
42
42
  end
43
43
  end
44
44
  end
45
- Calendar.prepend CalendarOverride if defined?(Calendar)
45
+ Calendar::Unit.prepend CalendarOverride if defined?(Calendar::Unit)
46
46
 
47
47
  # Additions for the Frontend
48
48
  module FrontendAddOn