UNPKG

scrivito-pisasales-questionnaire-builder

Version:
237 lines (236 loc) 10.6 kB
import { jsxs as R, jsx as r } from "react/jsx-runtime"; import { useState as z, useEffect as W, useMemo as Y, useRef as J } from "react"; import { load as K, ContentTag as Z } from "scrivito"; import { u as ee, a as te, b as se, M as ne, H as ae } from "./useValidationField-BrRv6UCn.js"; import { u as q, Q as O } from "./FormContext-CyyAThXO.js"; import { l as oe, j as L, E as ie, M as le, T as P, H as re, a as ce, m as de, Q as ue, A as me, V as pe, n as ye, F as ge, o as he, p as be, q as Te, r as fe, s as Ee } from "./scrivitoConfig-WST4rsmc.js"; import { c as Q, m as Ne, n as Ie, i as X, f as xe, d as ve, a as Re, g as we, b as Se, u as G } from "./QuestionnaireWidgetAttributesContext-BG4HHMga.js"; import { g as _e, a as Le } from "./DocumentDataClass-D1mg6YjJ.js"; import { g as Fe } from "./idGenerator-CnyWSta9.js"; async function Be(e) { return { dataBase64: await $e(e), filename: e instanceof File ? e.name : "unknown-name" }; } async function $e(e) { return new Promise((p, c) => { const u = new FileReader(); u.onabort = (d) => c(d), u.onerror = (d) => c(d), u.onload = () => { const d = u.result; if (typeof d != "string") { c(new Error(`FileReader result is not a string: ${d}`)); return; } const g = `data:${e.type};base64,`; if (!d.startsWith(g)) { c( new Error( `FileReader result does not start with expected prefix: ${d}` ) ); return; } p(d.substring(g.length)); }, u.readAsDataURL(e); }); } const Ae = (e, { multiple: p, maxSizeMB: c, accept: u }, d) => { const g = _e(), [h, b] = z([]), [w, F] = z([]), [B, $] = z([]), { idns: A, handleChange: C } = ee(e, []), N = q(); W(() => { const t = A || []; if (!t.length) { b([]); return; } let n = !1; const l = new Set(t), a = new Map(h.filter((i) => i.status === "ready" && l.has(i.id)).map((i) => [i.id, i])), m = t.map((i) => a.get(i) || { id: i, title: "loading...", type: "", status: "loading" }); return b(m), (async () => { for (const i of t) { if (n || a.has(i)) continue; const o = await K(() => g.get(i)); if (n) return; if (o) { const f = Q([o])[0]; b((T) => T.map((E) => E.id === i ? f : E)); } else b((f) => f.map((T) => T.id === i ? { ...T, status: "error", title: "Something went wrong while loading the file" } : T)), console.warn("Something went wrong while trying to get document with GID: ", i); } })(), () => { n = !0; }; }, [A]); const S = Y(() => new Set(h.map((t) => (t.title || "").toLowerCase())), [h]), j = (t, n) => { b((l) => l.map((a) => a.id === t ? n : a)); }, I = (t) => Array.from(new Map(t.map((n) => [n.id, n])).values()), U = (t) => { const n = Ie(u), l = t.filter((s) => X(s, n)), a = t.filter((s) => !X(s, n)).map((s) => s.name), m = c * 1024 * 1024, { accepted: y, rejected: i } = xe(l, m); let o = ve(S, y); p || (o = o.slice(0, 1)); const f = o.map((s) => s.name.toLowerCase()), T = p ? Array.from(/* @__PURE__ */ new Set([...S || [], ...f])) : f.slice(0, 1), E = Re(o); return { accepted: o, temps: E, nextNames: T, rejectedByType: a, rejectedBySize: i }; }, x = (t, n) => { const l = (t || []).filter((o) => o.status === "ready"), a = n.filter((o) => o.status === "ready"); let m; p ? m = I([...l, ...a]) : m = a.length ? [a[a.length - 1]] : l.length ? [l[l.length - 1]] : []; const y = m.map((o) => o.title), i = m.map((o) => o.id); return { values: y, identifiers: i }; }, v = async (t) => { const n = Array.from(t.target.files || []), { accepted: l, temps: a, nextNames: m, rejectedByType: y, rejectedBySize: i } = U(n); $(y), F(i), b(Ne([...p ? h : [], ...a])), l.length > 0 && (N == null || N.beginUpload(l.length)); try { const f = await D(l, a), { values: T, identifiers: E } = x(h, f); C(T, E); } finally { l.length > 0 && (N == null || N.finishUpload(l.length)); } d && d(m), t.target.value = ""; }, M = (t) => { const n = t ? h.filter((o) => o.id !== t) : h, l = n.map((o) => o.title); b(n), F([]), $([]); const a = n.filter((o) => o.status === "ready"), m = p ? a : a.length ? [a[a.length - 1]] : [], y = m.map((o) => o.title), i = m.map((o) => o.id); C(y, i), d && d(l); }, k = async (t) => { try { const n = await Be(t), l = await g.create({ ...n }); return Q([l])[0]; } catch { return { id: `tmp_${Fe()}`, title: t.name, type: t.type || we(t.name), status: "error" }; } }, D = async (t, n) => { const l = []; for (let a = 0; a < t.length; a++) { const m = t[a], y = n[a].id, i = await k(m); l.push(i), j(y, i); } return l; }; return { files: h, names: S, sizeRejected: w, typeRejected: B, onInputChange: v, removeFile: M }; }, V = ({ file: e, onRemove: p }) => { const c = (e == null ? void 0 : e.title) || "", u = (e == null ? void 0 : e.status) || "", d = c ? (e == null ? void 0 : e.status) === "loading" ? "spinner-border spinner-border-sm" : (e == null ? void 0 : e.status) === "error" ? "bi-x-lg text-danger" : Se(c) : "", g = e != null && e.token ? oe() + e.token : null; return /* @__PURE__ */ R("div", { className: "file-row", children: [ !L(c) && /* @__PURE__ */ r("i", { className: `bi ${d}` }), g && u === "ready" ? /* @__PURE__ */ r( "a", { href: g, download: e == null ? void 0 : e.title, target: "_blank", rel: "noopener noreferrer", className: `file-name ${u}`, title: c, children: c } ) : /* @__PURE__ */ r("span", { className: `file-name ${u}`, title: c, "aria-disabled": "true", children: c }), !L(c) && u !== "loading" && /* @__PURE__ */ r( "button", { type: "button", className: "btn btn-link text-danger p-0 remove", onClick: p, title: "Remove file", children: /* @__PURE__ */ r("i", { className: "bi bi-trash" }) } ) ] }); }, Ce = ({ selectButtonText: e, files: p, disabled: c, onPick: u, onRemove: d }) => { const { buttonsSize: g, buttonsStyle: h } = G(); return /* @__PURE__ */ R("div", { children: [ /* @__PURE__ */ r("div", { className: "multi-upload", children: /* @__PURE__ */ R("button", { disabled: c, type: "button", className: `upload-box btn btn-primary ${g} ${h}`, onClick: u, "aria-label": "Add files", children: [ /* @__PURE__ */ r("i", { className: "bi bi-upload" }), /* @__PURE__ */ r("span", { children: e }) ] }) }), /* @__PURE__ */ r("div", { className: "file-list", children: (p || []).map((b, w) => /* @__PURE__ */ r(V, { file: b, onRemove: () => d(b.id) }, `${b.id}-${w}`)) }) ] }); }, Me = ({ selectButtonText: e, disabled: p, file: c, onPick: u, onRemove: d }) => { const { buttonsSize: g, buttonsStyle: h } = G(); return /* @__PURE__ */ R("div", { className: "single-upload", children: [ /* @__PURE__ */ R("button", { disabled: p, type: "button", className: `btn btn-primary pick-btn upload-button ${g} ${h}`, onClick: u, children: [ /* @__PURE__ */ r("i", { className: "bi bi-upload" }), " ", /* @__PURE__ */ r("span", { children: e }) ] }), /* @__PURE__ */ r(V, { file: c, onRemove: () => d(c.id) }) ] }); }, qe = ({ widget: e }) => { var E; const p = `questionnaire_file_widget_${e.id()}`, c = e.get(ie), u = e.get(le), d = e.get(P), g = e.get(re), h = e.get(ce) || de, b = e.get(ue) || "", w = e.get(me) || "left", F = e.get(pe) || "Please select a file", B = e.get(ye) || "Choose file", $ = e.get(ge) || "Selected file exceeds 8 MB and was skipped.", A = e.get(he) || "The following files exceed 8 MB and were skipped:", C = e.get(be) || "Selected file type is not allowed.", N = e.get(Te) || "The following files are not allowed and were skipped:", S = e.get(fe) || "", j = S.split(",").map((s) => s.trim()).filter(Boolean).map((s) => s.includes("/") || s.startsWith(".") ? s : `.${s}`).join(","), I = te(c, u); se(e); const U = (s) => { const _ = s.some((H) => !L(H)); I == null || I.setIsLocallyValid(!u || _); }, x = h === Ee, { files: v, sizeRejected: M, typeRejected: k, onInputChange: D, removeFile: t } = Ae( b, { multiple: x, maxSizeMB: 8, accept: S }, U ), n = !L(b), l = Le(), a = [ "offline", "invalidToken", "publicSiteNoContext", "unconfiguredUrl", "invalid" ].includes(l), m = J(null), y = x ? v.some((s) => s.status === "loading") : ((E = v[0]) == null ? void 0 : E.status) === "loading", i = () => { var _; !n || y || a || (_ = m.current) == null || _.click(); }, o = async (s) => { D(s); }; if (!q() || !I) return /* @__PURE__ */ r(O, { status: "noFormContext" }); const T = !I.isLocallyValid; return /* @__PURE__ */ R("div", { ref: I.ref, className: `mb-3 file-widget-container ${w}`, children: [ !L(d) && /* @__PURE__ */ R("label", { htmlFor: p, className: "file-label", children: [ /* @__PURE__ */ r(Z, { attribute: P, content: e, tag: "span" }), u && /* @__PURE__ */ r(ne, {}), g && /* @__PURE__ */ r(ae, { widget: e }) ] }), v.filter((s) => s.status === "ready" && !!(s != null && s.title)).map((s, _) => /* @__PURE__ */ r( "input", { type: "hidden", className: "show-in-review show-as-list", name: c, value: s.title }, `rv-${_}` )), /* @__PURE__ */ r( "input", { disabled: !n || y || a, ref: m, className: "d-none", type: "file", multiple: x, accept: j, onChange: o } ), x ? /* @__PURE__ */ r( Ce, { disabled: !n || y, selectButtonText: B, files: v, onPick: i, onRemove: (s) => t(s) } ) : /* @__PURE__ */ r( Me, { disabled: !n || y, selectButtonText: B, file: v[0], onPick: i, onRemove: (s) => t(s) } ), !n && /* @__PURE__ */ r(O, { status: "fileUploadsDisabled" }), u && T && /* @__PURE__ */ r("div", { className: `invalid-feedback ${w}`, children: F }), M.length > 0 && /* @__PURE__ */ r("div", { className: "text-danger invalid-feedback small mt-1", children: x ? `${A} ${M.join(", ")}.` : $ }), k.length > 0 && /* @__PURE__ */ r("div", { className: "text-danger invalid-feedback small mt-1", children: x ? `${N} ${k.join(", ")}.` : C }) ] }); }; export { qe as default }; //# sourceMappingURL=FileQuestionWidgetContent-CgrLnHIZ.js.map