Skip to content

Commit

Permalink
Add final things
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Dec 23, 2024
1 parent 901bd0e commit 6e53dec
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 90 deletions.
4 changes: 2 additions & 2 deletions gem/javascripts/pagy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gem/javascripts/pagy.min.js.map

Large diffs are not rendered by default.

34 changes: 11 additions & 23 deletions gem/javascripts/pagy.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const Pagy = (() => {
const sS = sessionStorage;
const sync = new BroadcastChannel("pagy");
const tabId = Date.now();
const sS = sessionStorage, sync = new BroadcastChannel("pagy"), tabId = Date.now();
sync.addEventListener("message", (e) => {
if (e.data.from) {
const cutoffs = sS.getItem(e.data.key);
Expand Down Expand Up @@ -33,24 +31,20 @@ const Pagy = (() => {
}
const pagyId = document.cookie.split(/;\s+/).find((row) => row.startsWith("pagy="))?.split("=")[1] || Math.floor(Math.random() * 36 ** 3).toString(36);
document.cookie = "pagy=" + pagyId;
let [key, last, latest] = opts.update;
let [key, ...spliceArgs] = opts.update;
if (key && !(key in sS)) {
sync.postMessage({ from: tabId, key });
await new Promise((resolve) => setTimeout(() => resolve(""), 100));
}
key ||= "pagy-" + Date.now().toString(36);
const cs = sS.getItem(key);
const cutoffs = cs ? JSON.parse(cs) : [null];
if (last && latest) {
cutoffs[last] = latest;
const cs = sS.getItem(key), cutoffs = cs ? JSON.parse(cs) : [null];
if (spliceArgs) {
cutoffs.splice(...spliceArgs);
sS.setItem(key, JSON.stringify(cutoffs));
}
(el.completeUrls = () => {
for (const a of el.querySelectorAll("a[href]")) {
const url = a.href;
const re = new RegExp(`(?<=\\?.*)\\b${opts.page_param}=([\\d]+)`);
const page = parseInt(url.match(re)?.[1]);
const value = b64.safeEncode(JSON.stringify([
const url = a.href, re = new RegExp(`(?<=\\?.*)\\b${opts.page_param}=([\\d]+)`), page = parseInt(url.match(re)?.[1]), value = b64.safeEncode(JSON.stringify([
pagyId,
key,
cutoffs.length,
Expand All @@ -62,8 +56,7 @@ const Pagy = (() => {
})();
};
const initNavJs = (el, [tokens, sequels, labelSequels, opts]) => {
const container = el.parentElement ?? el;
const widths = Object.keys(sequels).map((w) => parseInt(w)).sort((a, b) => b - a);
const container = el.parentElement ?? el, widths = Object.keys(sequels).map((w) => parseInt(w)).sort((a, b) => b - a);
let lastWidth = -1;
const fillIn = (a, page, label) => a.replace(/__pagy_page__/g, page).replace(/__pagy_label__/g, label);
(el.pagyRender = () => {
Expand All @@ -72,8 +65,7 @@ const Pagy = (() => {
return;
}
let html = tokens.before;
const series = sequels[width.toString()];
const labels = labelSequels?.[width.toString()] ?? series.map((l) => l.toString());
const series = sequels[width.toString()], labels = labelSequels?.[width.toString()] ?? series.map((l) => l.toString());
series.forEach((item, i) => {
const label = labels[i];
let filled;
Expand Down Expand Up @@ -101,15 +93,12 @@ const Pagy = (() => {
const initComboJs = (el, [url_token, opts]) => initInput(el, (inputValue) => [inputValue, url_token.replace(/__pagy_page__/, inputValue)], opts);
const initSelectorJs = (el, [from, url_token, opts]) => {
initInput(el, (inputValue) => {
const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString();
const url = url_token.replace(/__pagy_page__/, page).replace(/__pagy_limit__/, inputValue);
const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString(), url = url_token.replace(/__pagy_page__/, page).replace(/__pagy_limit__/, inputValue);
return [page, url];
}, opts);
};
const initInput = (el, getVars, opts) => {
const input = el.querySelector("input");
const link = el.querySelector("a");
const initial = input.value;
const input = el.querySelector("input"), link = el.querySelector("a"), initial = input.value;
const action = () => {
if (input.value === initial) {
return;
Expand Down Expand Up @@ -139,8 +128,7 @@ const Pagy = (() => {
return {
version: "9.3.3",
init(arg) {
const target = arg instanceof Element ? arg : document;
const elements = target.querySelectorAll("[data-pagy]");
const target = arg instanceof Element ? arg : document, elements = target.querySelectorAll("[data-pagy]");
for (const el of elements) {
try {
const [keyword, ...args] = JSON.parse(b64.decode(el.getAttribute("data-pagy")));
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def pagy_bootstrap_nav_js(pagy, id: nil, classes: 'pagination', aria_label: nil,
'after' => %(#{bootstrap_next_html pagy, a}</ul>) }

%(<nav#{id} class="#{'pagy-rjs ' if sequels.size > 1}#{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && Pagy.is_a?(KeysetForUI)}pagy-bootstrap nav-js" #{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && pagy.is_a?(KeysetForUI)}pagy-bootstrap nav-js" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :nav_js, tokens, sequels, pagy.label_sequels(sequels))
}></nav>)
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def pagy_bulma_nav_js(pagy, id: nil, classes: 'pagy-bulma nav-js pagination is-c
'after' => '</ul>' }

%(<nav#{id} class="#{'pagy-rjs ' if sequels.size > 1}#{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && Pagy.is_a?(KeysetForUI)}#{classes}" #{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && pagy.is_a?(KeysetForUI)}#{classes}" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :nav_js, tokens, sequels, pagy.label_sequels(sequels))
}></nav>)
Expand Down
9 changes: 3 additions & 6 deletions gem/lib/pagy/extras/keyset_for_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ def get_cutoffs(vars)

cutoffs = JSON.parse(B64.urlsafe_decode(cutoffs))
pagy_id = cutoffs.shift
return cutoffs if request.cookies['pagy'] == pagy_id

# The url has been requested from another browser, which does not have the same sessionStorage,
# hence we need to restart the pagination to page 1
vars[:page] = 1
KeysetForUI::FIRST_PAGE
# No cutoffs if the url has been requested from another browser,
# which does not have the same sessionStorage, hence we need to restart the pagination to page 1
request.cookies['pagy'] == pagy_id ? cutoffs : nil
end
end
Backend.prepend KeysetForUIExtra
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def pagy_nav_js(pagy, id: nil, aria_label: nil, **vars)
'after' => next_a(pagy, a) }

%(<nav#{id} class="#{'pagy-rjs ' if sequels.size > 1}#{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && Pagy.is_a?(KeysetForUI)}pagy nav-js" #{
'pagy-keyset ' if defined?(::Pagy::KeysetForUI) && pagy.is_a?(KeysetForUI)}pagy nav-js" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :nav_js, tokens, sequels, pagy.label_sequels(sequels))
}></nav>)
Expand Down
16 changes: 9 additions & 7 deletions gem/lib/pagy/keyset_for_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Sequel < KeysetForUI

# Avoid args conflicts in composite SQL fragments
CUTOFF_PREFIX = 'cutoff_' # Prefix for cutoff_args
FIRST_PAGE = [nil, 1, nil, nil].freeze

include SharedUIMethods
attr_reader :update
Expand All @@ -32,10 +31,13 @@ def initialize(set, **vars)

# Get the cutoff from the client
def assign_cutoffs
# key, is from the client and sent back as-is in order to id the requests of the same set
key, @last, @prev_cutoff, @cutoff = @vars[:cutoffs] || FIRST_PAGE
@update = [key]
# raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
if @vars[:cutoffs]
key, @last, @prev_cutoff, @cutoff = @vars[:cutoffs]
raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
else
@page = @last = 1
end
@update = [key] # key, is from the client and sent back as-is in order to id the requests of the same set
end

# Assign different args to support the AFTER_CUTOFF SQL if @cutoff
Expand Down Expand Up @@ -134,8 +136,8 @@ def next
@next ||= (@page + 1).tap do
unless @cutoff
@cutoff = derive_cutoff
@update.push(@last, @cutoff) # operation arguments for the client cutoffs
@last += 1 # reflect the added cutoff
@update.push(@page, 1, @cutoff) # splice arguments for the client cutoffs
@last += 1 # reflect the added cutoff
end
end
end
Expand Down
Loading

0 comments on commit 6e53dec

Please sign in to comment.