336 lines
8.4 KiB
JavaScript
336 lines
8.4 KiB
JavaScript
const H = {
|
||
full: {
|
||
showPageSummary: !0,
|
||
showTotalCount: !0,
|
||
showInput: !0,
|
||
inputWidth: "100px",
|
||
justifyContent: "space-around",
|
||
pageButtonPx: void 0,
|
||
getPageNumbers: ({ currentPage: t, totalPages: e }) => [
|
||
1,
|
||
t,
|
||
t + 1,
|
||
e
|
||
]
|
||
},
|
||
compact: {
|
||
showPageSummary: !1,
|
||
showTotalCount: !0,
|
||
showInput: !0,
|
||
inputWidth: "56px",
|
||
justifyContent: "space-around",
|
||
pageButtonPx: 2,
|
||
getPageNumbers: ({ currentPage: t, totalPages: e }) => [
|
||
1,
|
||
t,
|
||
e
|
||
]
|
||
},
|
||
simple: {
|
||
showPageSummary: !1,
|
||
showTotalCount: !1,
|
||
showInput: !1,
|
||
inputWidth: "48px",
|
||
justifyContent: "center",
|
||
pageButtonPx: void 0,
|
||
getPageNumbers: () => []
|
||
}
|
||
}, V = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"], ne = /[۰-۹]/g, oe = /[٠-٩]/g;
|
||
function k() {
|
||
const t = window.__UIKIT_REMOTE_DEPS__, e = [];
|
||
if (t?.React || e.push("React"), t?.createRoot || e.push("createRoot"), t?.ChakraProvider || e.push("ChakraProvider"), t?.Button || e.push("Button"), t?.HStack || e.push("HStack"), t?.IconButton || e.push("IconButton"), t?.Input || e.push("Input"), t?.Text || e.push("Text"), e.length)
|
||
throw new Error(
|
||
`[uikit-pagination] Missing remote dependencies: ${e.join(", ")}. Set window.__UIKIT_REMOTE_DEPS__ before loading the remote component.`
|
||
);
|
||
return t;
|
||
}
|
||
function a(t) {
|
||
return t == null ? "" : String(t).replace(/\d/g, (e) => V[Number(e)]);
|
||
}
|
||
function G(t) {
|
||
return t == null ? "" : String(t).replace(
|
||
ne,
|
||
(e) => String(V.indexOf(e))
|
||
).replace(oe, (e) => String(e.charCodeAt(0) - 1632));
|
||
}
|
||
function re(t) {
|
||
return H[t] ? t : "full";
|
||
}
|
||
function W(t, e) {
|
||
const m = Number(t), g = Number(e);
|
||
return !g || g <= 0 || !m || Number.isNaN(m) ? 1 : Math.min(Math.max(m, 1), g);
|
||
}
|
||
function ie(t) {
|
||
const { React: e } = k();
|
||
return e.createElement(
|
||
"svg",
|
||
{
|
||
width: "20",
|
||
height: "20",
|
||
viewBox: "0 0 24 24",
|
||
fill: "none",
|
||
xmlns: "http://www.w3.org/2000/svg",
|
||
"aria-hidden": "true",
|
||
...t
|
||
},
|
||
e.createElement("path", {
|
||
d: "M15 18L9 12L15 6",
|
||
stroke: "currentColor",
|
||
strokeWidth: "2",
|
||
strokeLinecap: "round",
|
||
strokeLinejoin: "round"
|
||
})
|
||
);
|
||
}
|
||
function se(t) {
|
||
const { React: e } = k();
|
||
return e.createElement(
|
||
"svg",
|
||
{
|
||
width: "20",
|
||
height: "20",
|
||
viewBox: "0 0 24 24",
|
||
fill: "none",
|
||
xmlns: "http://www.w3.org/2000/svg",
|
||
"aria-hidden": "true",
|
||
...t
|
||
},
|
||
e.createElement("path", {
|
||
d: "M9 18L15 12L9 6",
|
||
stroke: "currentColor",
|
||
strokeWidth: "2",
|
||
strokeLinecap: "round",
|
||
strokeLinejoin: "round"
|
||
})
|
||
);
|
||
}
|
||
function ue({
|
||
currentPage: t = 1,
|
||
totalCount: e = 0,
|
||
totalPages: m,
|
||
pageSize: g = 10,
|
||
onPageChange: h,
|
||
level: v = "full",
|
||
...B
|
||
}) {
|
||
const { React: n, Button: K, HStack: S, IconButton: j, Input: O, Text: P } = k(), { useCallback: I, useEffect: N, useMemo: U, useRef: X, useState: R } = n, F = re(v), f = H[F], r = F === "simple", y = Number(g), $ = Number.isFinite(y) && y > 0 ? y : 10, L = Number(e), D = Number.isFinite(L) && L > 0 ? L : 0, A = Number(m), u = Number.isFinite(A) && A > 0 ? Math.ceil(A) : Math.ceil(D / $), i = W(t, u), [d, c] = R(
|
||
r ? "" : a(i)
|
||
), [E, b] = R(!1), [p, l] = R(null), C = X(null), w = p ?? i, q = D > 0, J = U(() => r || u <= 0 ? [] : Array.from(
|
||
new Set(
|
||
f.getPageNumbers({
|
||
currentPage: i,
|
||
totalPages: u
|
||
})
|
||
)
|
||
).filter((o) => o >= 1 && o <= u).sort((o, s) => o - s), [f, r, i, u]), _ = I(() => {
|
||
c(r ? "" : a(i)), b(!1), l(null);
|
||
}, [r, i]), M = I(
|
||
(o) => {
|
||
const s = W(o, u);
|
||
b(!1), c(r ? "" : a(i)), l(null), s !== i && h?.(s);
|
||
},
|
||
[r, h, i, u]
|
||
), x = I(() => {
|
||
if (!d) {
|
||
_(), C.current?.blur();
|
||
return;
|
||
}
|
||
const o = parseInt(G(d), 10);
|
||
if (Number.isNaN(o)) {
|
||
_(), C.current?.blur();
|
||
return;
|
||
}
|
||
const s = W(o, u);
|
||
if (r) {
|
||
b(!1), c(""), s !== i ? (l(s), h?.(s)) : l(null), C.current?.blur();
|
||
return;
|
||
}
|
||
s !== i ? (l(s), c(a(s)), h?.(s)) : (c(a(i)), b(!1), l(null)), C.current?.blur();
|
||
}, [
|
||
d,
|
||
r,
|
||
h,
|
||
_,
|
||
i,
|
||
u
|
||
]), Q = I(() => {
|
||
l(null), c(""), b(!0);
|
||
}, []), Y = I((o) => {
|
||
const s = o.target.value, te = G(s).replace(/\D/g, "");
|
||
c(a(te));
|
||
}, []);
|
||
if (N(() => {
|
||
if (r) {
|
||
p !== null && i === p && l(null);
|
||
return;
|
||
}
|
||
if (p !== null) {
|
||
i === p && (l(null), b(!1), c(a(i)));
|
||
return;
|
||
}
|
||
E || c(a(i));
|
||
}, [E, r, p, i]), N(() => {
|
||
r && E && C.current?.focus();
|
||
}, [E, r]), N(() => {
|
||
if (!E || !d || !r && p !== null) return;
|
||
const o = setTimeout(() => {
|
||
x();
|
||
}, 1e3);
|
||
return () => clearTimeout(o);
|
||
}, [x, d, E, r, p]), u <= 0) return null;
|
||
const z = () => n.createElement(O, {
|
||
ref: C,
|
||
type: "text",
|
||
inputMode: "numeric",
|
||
placeholder: "صفحه",
|
||
w: f.inputWidth,
|
||
value: d,
|
||
onFocus: () => {
|
||
r || (b(!0), l(null), c(""));
|
||
},
|
||
onChange: Y,
|
||
onBlur: () => {
|
||
if (d) {
|
||
r && x();
|
||
return;
|
||
}
|
||
_();
|
||
},
|
||
onKeyDown: (o) => {
|
||
o.key === "Enter" && x(), o.key === "Escape" && _();
|
||
},
|
||
textAlign: "center",
|
||
color: "#1D2939",
|
||
px: 1,
|
||
minW: 0,
|
||
dir: "ltr"
|
||
}), T = (o, s = !1) => n.createElement(
|
||
K,
|
||
{
|
||
key: o,
|
||
size: "md",
|
||
px: f.pageButtonPx,
|
||
bg: s ? "#4B49AC" : "#e6e6f1",
|
||
color: s ? "#e6e6f1" : "#4B49AC",
|
||
_hover: { bg: s ? "#4B49AC" : "#cbcbe8" },
|
||
onClick: r && s ? Q : () => M(o),
|
||
variant: "soft"
|
||
},
|
||
a(o)
|
||
), Z = r && u > 1 && w !== 1, ee = r && u > 1 && w !== u;
|
||
return n.createElement(
|
||
S,
|
||
{
|
||
justifyContent: f.justifyContent,
|
||
backgroundColor: "white",
|
||
py: "10px",
|
||
px: "16px",
|
||
rounded: "lg",
|
||
border: "1px",
|
||
borderColor: "blue.200",
|
||
w: "100%",
|
||
...B
|
||
},
|
||
f.showPageSummary && n.createElement(
|
||
S,
|
||
{ spacing: 2, whiteSpace: "nowrap" },
|
||
n.createElement(P, { fontWeight: "bold" }, "صفحه"),
|
||
n.createElement(P, null, a(i)),
|
||
n.createElement(P, { fontWeight: "bold" }, "از"),
|
||
n.createElement(P, null, a(u))
|
||
),
|
||
n.createElement(
|
||
S,
|
||
{ spacing: 2, alignItems: "center", justifyContent: "center" },
|
||
n.createElement(j, {
|
||
size: "md",
|
||
bg: "#e6e6f1",
|
||
color: "#4B49AC",
|
||
_hover: { bg: "#cbcbe8" },
|
||
onClick: () => M(w - 1),
|
||
isDisabled: w === 1,
|
||
icon: n.createElement(ie),
|
||
"aria-label": "صفحه قبلی"
|
||
}),
|
||
r ? [
|
||
Z ? T(1) : null,
|
||
E ? n.createElement(
|
||
n.Fragment,
|
||
{ key: "input" },
|
||
z()
|
||
) : T(w, !0),
|
||
ee ? T(u) : null
|
||
] : [
|
||
...J.map(
|
||
(o) => T(o, i === o)
|
||
),
|
||
f.showInput ? n.createElement(
|
||
n.Fragment,
|
||
{ key: "input" },
|
||
z()
|
||
) : null
|
||
],
|
||
n.createElement(j, {
|
||
size: "md",
|
||
bg: "#e6e6f1",
|
||
color: "#4B49AC",
|
||
_hover: { bg: "#cbcbe8" },
|
||
onClick: () => M(w + 1),
|
||
isDisabled: w === u,
|
||
icon: n.createElement(se),
|
||
"aria-label": "صفحه بعدی"
|
||
})
|
||
),
|
||
f.showTotalCount && q && n.createElement(
|
||
S,
|
||
{ spacing: 2, whiteSpace: "nowrap" },
|
||
n.createElement(
|
||
P,
|
||
{ w: "auto", fontWeight: "bold" },
|
||
"تعداد کل:"
|
||
),
|
||
n.createElement(P, null, a(D))
|
||
)
|
||
);
|
||
}
|
||
class ae extends HTMLElement {
|
||
constructor() {
|
||
super(), this._props = {}, this._root = null;
|
||
}
|
||
connectedCallback() {
|
||
const { createRoot: e } = k();
|
||
this._root || (this._root = e(this)), this.renderComponent();
|
||
}
|
||
disconnectedCallback() {
|
||
this._root && (this._root.unmount(), this._root = null);
|
||
}
|
||
set componentProps(e) {
|
||
this._props = e ?? {}, this.isConnected && this.renderComponent();
|
||
}
|
||
get componentProps() {
|
||
return this._props;
|
||
}
|
||
renderComponent() {
|
||
if (!this._root) return;
|
||
const {
|
||
React: e,
|
||
ChakraProvider: m,
|
||
theme: g,
|
||
chakraProviderProps: h
|
||
} = k(), { theme: v, chakraProviderProps: B, ...n } = this._props || {};
|
||
this._root.render(
|
||
e.createElement(
|
||
m,
|
||
{
|
||
theme: v || g,
|
||
resetCSS: !1,
|
||
...h || {},
|
||
...B || {}
|
||
},
|
||
e.createElement(ue, n)
|
||
)
|
||
);
|
||
}
|
||
}
|
||
customElements.get("uikit-pagination") || customElements.define("uikit-pagination", ae);
|