refactor: migrate legacy wire namespace to wrn
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Client-side validation. `renderSchemasScript` bakes the discovered schema
|
||||
* descriptors into `window.__wireSchemas`; `VALIDATE_RUNTIME` is a generic,
|
||||
* descriptors into `window.__wrnSchemas`; `VALIDATE_RUNTIME` is a generic,
|
||||
* eval-free validator that reads them and validates every `form[data-schema]`
|
||||
* on submit and blur, writing messages into `[data-error="<field>"]` elements.
|
||||
* The rule logic mirrors `checkField`/`applyRule` in index.ts.
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
import type { SchemaDescriptor } from "./index.ts";
|
||||
|
||||
/** `window.__wireSchemas = { name: descriptor, ... }` for the client validator. */
|
||||
/** `window.__wrnSchemas = { name: descriptor, ... }` for the client validator. */
|
||||
export function renderSchemasScript(descriptors: Record<string, SchemaDescriptor>): string {
|
||||
return `window.__wireSchemas=Object.assign(window.__wireSchemas||{},${JSON.stringify(descriptors)});`;
|
||||
return `window.__wrnSchemas=Object.assign(window.__wrnSchemas||{},${JSON.stringify(descriptors)});`;
|
||||
}
|
||||
|
||||
export const VALIDATE_RUNTIME = String.raw`
|
||||
@@ -72,9 +72,9 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
if (box) box.textContent = err || "";
|
||||
var el = form.elements[name];
|
||||
if (el && el.setAttribute) {
|
||||
if (err) { el.setAttribute("aria-invalid", "true"); if (el.classList) el.classList.add("wire-invalid"); }
|
||||
else { el.removeAttribute("aria-invalid"); if (el.classList) el.classList.remove("wire-invalid"); }
|
||||
var field = el.closest && el.closest(".wire-next--field, .wire-next--choice-field");
|
||||
if (err) { el.setAttribute("aria-invalid", "true"); if (el.classList) el.classList.add("wrn-invalid"); }
|
||||
else { el.removeAttribute("aria-invalid"); if (el.classList) el.classList.remove("wrn-invalid"); }
|
||||
var field = el.closest && el.closest(".wrn-next--field, .wrn-next--choice-field");
|
||||
if (field) field.setAttribute("data-invalid", err ? "true" : "false");
|
||||
}
|
||||
}
|
||||
@@ -124,7 +124,7 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
|
||||
function onSuccess(form, data, responseRedirect) {
|
||||
showFormError(form, "");
|
||||
form.dispatchEvent(new CustomEvent("wire:success", { detail: data, bubbles: true }));
|
||||
form.dispatchEvent(new CustomEvent("wrn:success", { detail: data, bubbles: true }));
|
||||
var redirect = responseRedirect || form.getAttribute("data-redirect") || (data && data.redirect);
|
||||
if (redirect) {
|
||||
// Prefer client-side navigation for same-origin application redirects.
|
||||
@@ -148,7 +148,7 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
}
|
||||
|
||||
function csrfHeader() {
|
||||
var m = document.cookie.match(/(?:^|;\s*)wire-csrf=([^;]+)/);
|
||||
var m = document.cookie.match(/(?:^|;\s*)wrn-csrf=([^;]+)/);
|
||||
return m ? { "x-csrf-token": decodeURIComponent(m[1]) } : {};
|
||||
}
|
||||
|
||||
@@ -184,18 +184,18 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
if (errors) Object.keys(errors).forEach(function (f) { showError(form, f, errors[f]); });
|
||||
var message = r.data && (r.data.message || r.data.error);
|
||||
showFormError(form, message || (errors ? "" : ("Request failed (" + r.res.status + ")")));
|
||||
form.dispatchEvent(new CustomEvent("wire:error", { detail: r.data, bubbles: true }));
|
||||
form.dispatchEvent(new CustomEvent("wrn:error", { detail: r.data, bubbles: true }));
|
||||
})
|
||||
.catch(function (error) {
|
||||
var detail = { network: true, message: error && error.message ? error.message : "Network request failed" };
|
||||
showFormError(form, detail.message);
|
||||
form.dispatchEvent(new CustomEvent("wire:error", { detail: detail, bubbles: true }));
|
||||
form.dispatchEvent(new CustomEvent("wrn:error", { detail: detail, bubbles: true }));
|
||||
})
|
||||
.then(function () { btns.forEach(function (b) { b.disabled = false; }); });
|
||||
}
|
||||
|
||||
function runSubmitGuards(form, event) {
|
||||
var guards = form.__wireSubmitGuards || [];
|
||||
var guards = form.__wrnSubmitGuards || [];
|
||||
for (var index = 0; index < guards.length; index += 1) {
|
||||
if (guards[index](event) === false) return false;
|
||||
}
|
||||
@@ -203,21 +203,21 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
}
|
||||
|
||||
function bind(form) {
|
||||
if (form.__wireValidateBound) return;
|
||||
if (form.__wrnValidateBound) return;
|
||||
var name = form.getAttribute("data-schema");
|
||||
var schema = (window.__wireSchemas || {})[name];
|
||||
var schema = (window.__wrnSchemas || {})[name];
|
||||
|
||||
// Package runtimes may register schemas later in the same page load.
|
||||
// Do not report an error during the first scan.
|
||||
if (!schema) {
|
||||
form.__wireValidatePending = 1;
|
||||
form.__wrnValidatePending = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
form.__wireValidatePending = 0;
|
||||
form.__wireValidateMissingWarned = 0;
|
||||
form.__wireValidateBound = 1;
|
||||
form.__wireValidateMissingWarned = 0;
|
||||
form.__wrnValidatePending = 0;
|
||||
form.__wrnValidateMissingWarned = 0;
|
||||
form.__wrnValidateBound = 1;
|
||||
form.__wrnValidateMissingWarned = 0;
|
||||
// Schema-backed forms use WRNexus messages instead of the browser's
|
||||
// non-themeable native validation bubbles.
|
||||
form.noValidate = true;
|
||||
@@ -242,12 +242,12 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
}
|
||||
|
||||
function registerSchemas(descriptors, root) {
|
||||
window.__wireSchemas =
|
||||
window.__wireSchemas || {};
|
||||
window.__wrnSchemas =
|
||||
window.__wrnSchemas || {};
|
||||
|
||||
Object.keys(descriptors || {}).forEach(
|
||||
function (name) {
|
||||
window.__wireSchemas[name] =
|
||||
window.__wrnSchemas[name] =
|
||||
descriptors[name];
|
||||
}
|
||||
);
|
||||
@@ -259,7 +259,7 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
(root || document).querySelectorAll("form[data-schema]").forEach(bind);
|
||||
}
|
||||
|
||||
window.__wireValidate = {
|
||||
window.__wrnValidate = {
|
||||
init: init,
|
||||
registerSchemas: registerSchemas
|
||||
};
|
||||
@@ -279,7 +279,7 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
return;
|
||||
}
|
||||
|
||||
if (form.__wireValidateBound) {
|
||||
if (form.__wrnValidateBound) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
form.getAttribute("data-schema");
|
||||
|
||||
var schema =
|
||||
(window.__wireSchemas || {})[name];
|
||||
(window.__wrnSchemas || {})[name];
|
||||
|
||||
if (schema) {
|
||||
bind(form);
|
||||
@@ -302,11 +302,11 @@ export const VALIDATE_RUNTIME = String.raw`
|
||||
);
|
||||
|
||||
if (
|
||||
!form.__wireValidateMissingWarned &&
|
||||
!form.__wrnValidateMissingWarned &&
|
||||
window.console &&
|
||||
console.error
|
||||
) {
|
||||
form.__wireValidateMissingWarned = 1;
|
||||
form.__wrnValidateMissingWarned = 1;
|
||||
|
||||
console.error(
|
||||
"[wrnexus:validation] Schema '" +
|
||||
|
||||
Reference in New Issue
Block a user