build/modules/block-library/form
view.js
1.4 KB
4 months ago
view.js.map
2.6 KB
4 months ago
view.min.asset.php
83 B
8 months ago
view.min.js
852 B
10 months ago
view.min.js.map
2.7 KB
4 months ago
view.js in Gutenberg 23.2.0, at build/modules/block-library/form/view.js
| 1 | // packages/block-library/build-module/form/view.mjs |
| 2 | var formSettings; |
| 3 | try { |
| 4 | formSettings = JSON.parse( |
| 5 | document.getElementById( |
| 6 | "wp-script-module-data-@wordpress/block-library/form/view" |
| 7 | )?.textContent |
| 8 | ); |
| 9 | } catch { |
| 10 | } |
| 11 | document.querySelectorAll("form.wp-block-form").forEach(function(form) { |
| 12 | if (!formSettings || !form.action || !form.action.startsWith("mailto:")) { |
| 13 | return; |
| 14 | } |
| 15 | const redirectNotification = (status) => { |
| 16 | const urlParams = new URLSearchParams(window.location.search); |
| 17 | urlParams.append("wp-form-result", status); |
| 18 | window.location.search = urlParams.toString(); |
| 19 | }; |
| 20 | form.addEventListener("submit", async function(event) { |
| 21 | event.preventDefault(); |
| 22 | const formData = Object.fromEntries(new FormData(form).entries()); |
| 23 | formData.formAction = form.action; |
| 24 | formData._ajax_nonce = formSettings.nonce; |
| 25 | formData.action = formSettings.action; |
| 26 | formData._wp_http_referer = window.location.href; |
| 27 | formData.formAction = form.action; |
| 28 | try { |
| 29 | const response = await fetch(formSettings.ajaxUrl, { |
| 30 | method: "POST", |
| 31 | headers: { |
| 32 | "Content-Type": "application/x-www-form-urlencoded" |
| 33 | }, |
| 34 | body: new URLSearchParams(formData).toString() |
| 35 | }); |
| 36 | if (response.ok) { |
| 37 | redirectNotification("success"); |
| 38 | } else { |
| 39 | redirectNotification("error"); |
| 40 | } |
| 41 | } catch { |
| 42 | redirectNotification("error"); |
| 43 | } |
| 44 | }); |
| 45 | }); |
| 46 | //# sourceMappingURL=view.js.map |
| 47 |