Skip to content

Commit

Permalink
Reduced the public interface of pagy.js and relative files to just ve…
Browse files Browse the repository at this point in the history
…rsion and init()
  • Loading branch information
ddnexus committed Jan 20, 2022
1 parent 575e2e2 commit fbb0f97
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 167 deletions.
95 changes: 47 additions & 48 deletions lib/javascripts/pagy-dev.js

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions lib/javascripts/pagy-module.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
declare type NavArgs = readonly [Tags, Sequels, null | LabelSequels, string?];
declare type ComboArgs = readonly [string, string?];
declare type SelectorArgs = readonly [number, string, string?];
interface Tags {
readonly before: string;
readonly link: string;
readonly active: string;
readonly gap: string;
readonly after: string;
}
interface Sequels {
readonly [width: string]: (string | number | "gap")[];
}
interface LabelSequels {
readonly [width: string]: string[];
}
interface NavElement extends Element {
pagyRender(): void;
}
declare const Pagy: {
version: string;
init(arg?: Element | never): void;
initNav(el: NavElement, [tags, sequels, labelSequels, trimParam]: NavArgs): void;
rjsObserver: ResizeObserver;
initCombo(el: Element, [link, trimParam]: ComboArgs): void;
initSelector(el: Element, [from, link, trimParam]: SelectorArgs): void;
initInput(el: Element, getVars: (v: string) => [string, string], trimParam?: string): void;
trim: (link: string, param: string) => string;
};
export default Pagy;
93 changes: 46 additions & 47 deletions lib/javascripts/pagy-module.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
const Pagy = {
version: "5.8.1",
// Scan for elements with a "data-pagy" attribute and call their init functions with the decoded args
init(arg) {
const target = arg instanceof Element ? arg : document;
const elements = target.querySelectorAll("[data-pagy]");
for (const el of elements) {
try {
const uint8array = Uint8Array.from(atob(el.getAttribute("data-pagy")), c => c.charCodeAt(0));
const [keyword, ...args] = JSON.parse(new TextDecoder().decode(uint8array)); // base64-utf8 JSON -> Array
if (keyword === "nav") {
Pagy.initNav(el, args);
}
else if (keyword === "combo") {
Pagy.initCombo(el, args);
}
else if (keyword === "selector") {
Pagy.initSelector(el, args);
}
else {
console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'", el, keyword);
}
}
catch (err) {
console.warn("Skipped Pagy.init() for: %o\n%s", el, err);
}
}
},
const Pagy = (() => {
// The observer instance for responsive navs
const rjsObserver = new ResizeObserver(entries => entries.forEach(e => e.target.querySelectorAll(".pagy-rjs").forEach(el => el.pagyRender())));
// Init the *_nav_js helpers
initNav(el, [tags, sequels, labelSequels, trimParam]) {
const initNav = (el, [tags, sequels, labelSequels, trimParam]) => {
const container = el.parentElement ?? el;
const widths = Object.keys(sequels).map(w => parseInt(w)).sort((a, b) => b - a);
let lastWidth = -1;
Expand All @@ -44,7 +19,7 @@ const Pagy = {
const item = series[i];
const label = labels[i];
if (typeof trimParam === "string" && item === 1) {
html += Pagy.trim(fillIn(tags.link, item.toString(), label), trimParam);
html += trim(fillIn(tags.link, item.toString(), label), trimParam);
}
else if (typeof item === "number") {
html += fillIn(tags.link, item.toString(), label);
Expand All @@ -62,27 +37,21 @@ const Pagy = {
lastWidth = width;
})();
if (el.classList.contains("pagy-rjs")) {
Pagy.rjsObserver.observe(container);
rjsObserver.observe(container);
}
},
// The observer instance for responsive navs
rjsObserver: new ResizeObserver(entries => {
entries.forEach(e => e.target.querySelectorAll(".pagy-rjs").forEach(el => el.pagyRender()));
}),
};
// Init the *_combo_nav_js helpers
initCombo(el, [link, trimParam]) {
Pagy.initInput(el, inputValue => [inputValue, link.replace(/__pagy_page__/, inputValue)], trimParam);
},
const initCombo = (el, [link, trimParam]) => initInput(el, inputValue => [inputValue, link.replace(/__pagy_page__/, inputValue)], trimParam);
// Init the items_selector_js helper
initSelector(el, [from, link, trimParam]) {
Pagy.initInput(el, inputValue => {
const initSelector = (el, [from, link, trimParam]) => {
initInput(el, inputValue => {
const page = Math.max(Math.ceil(from / parseInt(inputValue)), 1).toString();
const html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, inputValue);
return [page, html];
}, trimParam);
},
};
// Init the input element
initInput(el, getVars, trimParam) {
const initInput = (el, getVars, trimParam) => {
const input = el.querySelector("input");
const initial = input.value;
const action = function () {
Expand All @@ -97,7 +66,7 @@ const Pagy = {
}
let [page, html] = getVars(input.value); // eslint-disable-line prefer-const
if (typeof trimParam === "string" && page === "1") {
html = Pagy.trim(html, trimParam);
html = trim(html, trimParam);
}
el.insertAdjacentHTML("afterbegin", html);
el.querySelector("a").click();
Expand All @@ -107,8 +76,38 @@ const Pagy = {
input.addEventListener("keypress", e => { if (e.key === "Enter") {
action();
} }); // trigger action
},
};
// Trim the ${page-param}=1 params in links
trim: (link, param) => link.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "")
};
const trim = (link, param) => link.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "");
// Public interface
return {
version: "5.8.1",
// Scan for elements with a "data-pagy" attribute and call their init functions with the decoded args
init(arg) {
const target = arg instanceof Element ? arg : document;
const elements = target.querySelectorAll("[data-pagy]");
for (const el of elements) {
try {
const uint8array = Uint8Array.from(atob(el.getAttribute("data-pagy")), c => c.charCodeAt(0));
const [keyword, ...args] = JSON.parse((new TextDecoder()).decode(uint8array)); // base64-utf8 -> JSON -> Array
if (keyword === "nav") {
initNav(el, args);
}
else if (keyword === "combo") {
initCombo(el, args);
}
else if (keyword === "selector") {
initSelector(el, args);
}
else {
console.warn("Skipped Pagy.init() for: %o\nUnknown keyword '%s'", el, keyword);
}
}
catch (err) {
console.warn("Skipped Pagy.init() for: %o\n%s", el, err);
}
}
}
};
})();
export default Pagy;
2 changes: 1 addition & 1 deletion lib/javascripts/pagy.js

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

1 change: 0 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Pagy from "./pagy-module";
// Toplevel var (needed for generating the production file)
window.Pagy = Pagy;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIm1haW4udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxJQUFJLE1BQU0sZUFBZSxDQUFDO0FBTWpDLDJEQUEyRDtBQUMzRCxNQUFNLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBQYWd5IGZyb20gXCIuL3BhZ3ktbW9kdWxlXCI7XG5kZWNsYXJlIGdsb2JhbCB7XG4gICAgaW50ZXJmYWNlIFdpbmRvdyB7XG4gICAgICAgIFBhZ3k6IHR5cGVvZiBQYWd5O1xuICAgIH1cbn1cbi8vIFRvcGxldmVsIHZhciAobmVlZGVkIGZvciBnZW5lcmF0aW5nIHRoZSBwcm9kdWN0aW9uIGZpbGUpXG53aW5kb3cuUGFneSA9IFBhZ3k7XG4iXX0=
Loading

0 comments on commit fbb0f97

Please sign in to comment.