1 | import {
|
2 | __spreadValues
|
3 | } from "./chunk.K2NRSETB.js";
|
4 |
|
5 |
|
6 | var FormDataEventPolyfill = class extends Event {
|
7 | constructor(formData) {
|
8 | super("formdata");
|
9 | this.formData = formData;
|
10 | }
|
11 | };
|
12 | var FormDataPolyfill = class extends FormData {
|
13 | constructor(form) {
|
14 | var __super = (...args) => {
|
15 | super(...args);
|
16 | };
|
17 | if (form) {
|
18 | __super(form);
|
19 | this.form = form;
|
20 | form.dispatchEvent(new FormDataEventPolyfill(this));
|
21 | } else {
|
22 | __super();
|
23 | }
|
24 | }
|
25 | append(name, value) {
|
26 | if (!this.form) {
|
27 | return super.append(name, value);
|
28 | }
|
29 | let input = this.form.elements[name];
|
30 | if (!input) {
|
31 | input = document.createElement("input");
|
32 | input.type = "hidden";
|
33 | input.name = name;
|
34 | this.form.appendChild(input);
|
35 | }
|
36 | if (this.has(name)) {
|
37 | const entries = this.getAll(name);
|
38 | const index = entries.indexOf(input.value);
|
39 | if (index !== -1) {
|
40 | entries.splice(index, 1);
|
41 | }
|
42 | entries.push(value);
|
43 | this.set(name, entries);
|
44 | } else {
|
45 | super.append(name, value);
|
46 | }
|
47 | input.value = value;
|
48 | }
|
49 | };
|
50 | function supportsFormDataEvent() {
|
51 | const form = document.createElement("form");
|
52 | let isSupported = false;
|
53 | document.body.append(form);
|
54 | form.addEventListener("submit", (event) => {
|
55 | new FormData(event.target);
|
56 | event.preventDefault();
|
57 | });
|
58 | form.addEventListener("formdata", () => isSupported = true);
|
59 | form.dispatchEvent(new Event("submit", { cancelable: true }));
|
60 | form.remove();
|
61 | return isSupported;
|
62 | }
|
63 | function polyfillFormData() {
|
64 | if (!window.FormData || supportsFormDataEvent()) {
|
65 | return;
|
66 | }
|
67 | window.FormData = FormDataPolyfill;
|
68 | window.addEventListener("submit", (event) => {
|
69 | if (!event.defaultPrevented) {
|
70 | new FormData(event.target);
|
71 | }
|
72 | });
|
73 | }
|
74 | if (document.readyState === "complete") {
|
75 | polyfillFormData();
|
76 | } else {
|
77 | window.addEventListener("DOMContentLoaded", () => polyfillFormData());
|
78 | }
|
79 |
|
80 |
|
81 | var reportValidityOverloads = new WeakMap();
|
82 | var FormSubmitController = class {
|
83 | constructor(host, options) {
|
84 | (this.host = host).addController(this);
|
85 | this.options = __spreadValues({
|
86 | form: (input) => input.closest("form"),
|
87 | name: (input) => input.name,
|
88 | value: (input) => input.value,
|
89 | defaultValue: (input) => input.defaultValue,
|
90 | disabled: (input) => input.disabled,
|
91 | reportValidity: (input) => {
|
92 | return typeof input.reportValidity === "function" ? input.reportValidity() : true;
|
93 | },
|
94 | setValue: (input, value) => {
|
95 | input.value = value;
|
96 | }
|
97 | }, options);
|
98 | this.handleFormData = this.handleFormData.bind(this);
|
99 | this.handleFormSubmit = this.handleFormSubmit.bind(this);
|
100 | this.handleFormReset = this.handleFormReset.bind(this);
|
101 | this.reportFormValidity = this.reportFormValidity.bind(this);
|
102 | }
|
103 | hostConnected() {
|
104 | this.form = this.options.form(this.host);
|
105 | if (this.form) {
|
106 | this.form.addEventListener("formdata", this.handleFormData);
|
107 | this.form.addEventListener("submit", this.handleFormSubmit);
|
108 | this.form.addEventListener("reset", this.handleFormReset);
|
109 | if (!reportValidityOverloads.has(this.form)) {
|
110 | reportValidityOverloads.set(this.form, this.form.reportValidity);
|
111 | this.form.reportValidity = () => this.reportFormValidity();
|
112 | }
|
113 | }
|
114 | }
|
115 | hostDisconnected() {
|
116 | if (this.form) {
|
117 | this.form.removeEventListener("formdata", this.handleFormData);
|
118 | this.form.removeEventListener("submit", this.handleFormSubmit);
|
119 | this.form.removeEventListener("reset", this.handleFormReset);
|
120 | if (reportValidityOverloads.has(this.form)) {
|
121 | this.form.reportValidity = reportValidityOverloads.get(this.form);
|
122 | reportValidityOverloads.delete(this.form);
|
123 | }
|
124 | this.form = void 0;
|
125 | }
|
126 | }
|
127 | handleFormData(event) {
|
128 | const disabled = this.options.disabled(this.host);
|
129 | const name = this.options.name(this.host);
|
130 | const value = this.options.value(this.host);
|
131 | if (!disabled && typeof name === "string" && typeof value !== "undefined") {
|
132 | if (Array.isArray(value)) {
|
133 | value.forEach((val) => {
|
134 | event.formData.append(name, val.toString());
|
135 | });
|
136 | } else {
|
137 | event.formData.append(name, value.toString());
|
138 | }
|
139 | }
|
140 | }
|
141 | handleFormSubmit(event) {
|
142 | const disabled = this.options.disabled(this.host);
|
143 | const reportValidity = this.options.reportValidity;
|
144 | if (this.form && !this.form.noValidate && !disabled && !reportValidity(this.host)) {
|
145 | event.preventDefault();
|
146 | event.stopImmediatePropagation();
|
147 | }
|
148 | }
|
149 | handleFormReset() {
|
150 | this.options.setValue(this.host, this.options.defaultValue(this.host));
|
151 | }
|
152 | reportFormValidity() {
|
153 | if (this.form && !this.form.noValidate) {
|
154 | const elements = this.form.querySelectorAll("*");
|
155 | for (const element of elements) {
|
156 | if (typeof element.reportValidity === "function") {
|
157 | if (!element.reportValidity()) {
|
158 | return false;
|
159 | }
|
160 | }
|
161 | }
|
162 | }
|
163 | return true;
|
164 | }
|
165 | doAction(type, invoker) {
|
166 | if (this.form) {
|
167 | const button = document.createElement("button");
|
168 | button.type = type;
|
169 | button.style.position = "absolute";
|
170 | button.style.width = "0";
|
171 | button.style.height = "0";
|
172 | button.style.clipPath = "inset(50%)";
|
173 | button.style.overflow = "hidden";
|
174 | button.style.whiteSpace = "nowrap";
|
175 | if (invoker) {
|
176 | ["formaction", "formmethod", "formnovalidate", "formtarget"].forEach((attr) => {
|
177 | if (invoker.hasAttribute(attr)) {
|
178 | button.setAttribute(attr, invoker.getAttribute(attr));
|
179 | }
|
180 | });
|
181 | }
|
182 | this.form.append(button);
|
183 | button.click();
|
184 | button.remove();
|
185 | }
|
186 | }
|
187 | reset(invoker) {
|
188 | this.doAction("reset", invoker);
|
189 | }
|
190 | submit(invoker) {
|
191 | this.doAction("submit", invoker);
|
192 | }
|
193 | };
|
194 |
|
195 | export {
|
196 | FormSubmitController
|
197 | };
|