@inertiajs/core 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +157 -8
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +157 -8
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/prefetched.d.ts +1 -0
- package/types/requestParams.d.ts +1 -0
- package/types/resetFormFields.d.ts +1 -0
- package/types/router.d.ts +4 -4
- package/types/types.d.ts +92 -40
- package/types/url.d.ts +3 -1
package/dist/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
hrefToUrl: () => hrefToUrl,
|
|
37
37
|
mergeDataIntoQueryString: () => mergeDataIntoQueryString,
|
|
38
38
|
objectToFormData: () => objectToFormData,
|
|
39
|
+
resetFormFields: () => resetFormFields,
|
|
39
40
|
revealProgress: () => reveal,
|
|
40
41
|
router: () => router,
|
|
41
42
|
setupProgress: () => setupProgress,
|
|
@@ -388,14 +389,15 @@ var transformUrlAndData = (href, data, method, forceFormData, queryStringArrayFo
|
|
|
388
389
|
return [hrefToUrl(_href), _data];
|
|
389
390
|
};
|
|
390
391
|
function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets") {
|
|
392
|
+
const hasDataForQueryString = method === "get" && !isFormData(data) && Object.keys(data).length > 0;
|
|
391
393
|
const hasHost = /^[a-z][a-z0-9+.-]*:\/\//i.test(href.toString());
|
|
392
|
-
const hasAbsolutePath = hasHost || href.toString().startsWith("/");
|
|
394
|
+
const hasAbsolutePath = hasHost || href.toString().startsWith("/") || href.toString() === "";
|
|
393
395
|
const hasRelativePath = !hasAbsolutePath && !href.toString().startsWith("#") && !href.toString().startsWith("?");
|
|
394
396
|
const hasRelativePathWithDotPrefix = /^[.]{1,2}([/]|$)/.test(href.toString());
|
|
395
|
-
const hasSearch = href.toString().includes("?") ||
|
|
397
|
+
const hasSearch = href.toString().includes("?") || hasDataForQueryString;
|
|
396
398
|
const hasHash = href.toString().includes("#");
|
|
397
399
|
const url = new URL(href.toString(), typeof window === "undefined" ? "http://localhost" : window.location.toString());
|
|
398
|
-
if (
|
|
400
|
+
if (hasDataForQueryString) {
|
|
399
401
|
const parseOptions = { ignoreQueryPrefix: true, parseArrays: false };
|
|
400
402
|
url.search = qs.stringify(
|
|
401
403
|
{ ...qs.parse(url.search, parseOptions), ...data },
|
|
@@ -404,7 +406,6 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
|
|
|
404
406
|
arrayFormat: qsArrayFormat
|
|
405
407
|
}
|
|
406
408
|
);
|
|
407
|
-
data = {};
|
|
408
409
|
}
|
|
409
410
|
return [
|
|
410
411
|
[
|
|
@@ -414,7 +415,7 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
|
|
|
414
415
|
hasSearch ? url.search : "",
|
|
415
416
|
hasHash ? url.hash : ""
|
|
416
417
|
].join(""),
|
|
417
|
-
data
|
|
418
|
+
hasDataForQueryString ? {} : data
|
|
418
419
|
];
|
|
419
420
|
}
|
|
420
421
|
function urlWithoutHash(url) {
|
|
@@ -1082,6 +1083,10 @@ var PrefetchedRequests = class {
|
|
|
1082
1083
|
},
|
|
1083
1084
|
onPrefetchResponse(response) {
|
|
1084
1085
|
resolve(response);
|
|
1086
|
+
},
|
|
1087
|
+
onPrefetchError(error) {
|
|
1088
|
+
prefetchedRequests.removeFromInFlight(params);
|
|
1089
|
+
reject(error);
|
|
1085
1090
|
}
|
|
1086
1091
|
});
|
|
1087
1092
|
}).then((response) => {
|
|
@@ -1095,9 +1100,7 @@ var PrefetchedRequests = class {
|
|
|
1095
1100
|
inFlight: false
|
|
1096
1101
|
});
|
|
1097
1102
|
this.scheduleForRemoval(params, expires);
|
|
1098
|
-
this.
|
|
1099
|
-
return !this.paramsAreEqual(prefetching.params, params);
|
|
1100
|
-
});
|
|
1103
|
+
this.removeFromInFlight(params);
|
|
1101
1104
|
response.handlePrefetch();
|
|
1102
1105
|
return response;
|
|
1103
1106
|
});
|
|
@@ -1122,6 +1125,11 @@ var PrefetchedRequests = class {
|
|
|
1122
1125
|
});
|
|
1123
1126
|
this.clearTimer(params);
|
|
1124
1127
|
}
|
|
1128
|
+
removeFromInFlight(params) {
|
|
1129
|
+
this.inFlightRequests = this.inFlightRequests.filter((prefetching) => {
|
|
1130
|
+
return !this.paramsAreEqual(prefetching.params, params);
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1125
1133
|
extractStaleValues(cacheFor) {
|
|
1126
1134
|
const [stale, expires] = this.cacheForToStaleAndExpires(cacheFor);
|
|
1127
1135
|
return [timeToMs(stale), timeToMs(expires)];
|
|
@@ -1253,6 +1261,8 @@ var RequestParams = class _RequestParams {
|
|
|
1253
1261
|
...params,
|
|
1254
1262
|
...wrappedCallbacks,
|
|
1255
1263
|
onPrefetchResponse: params.onPrefetchResponse || (() => {
|
|
1264
|
+
}),
|
|
1265
|
+
onPrefetchError: params.onPrefetchError || (() => {
|
|
1256
1266
|
})
|
|
1257
1267
|
};
|
|
1258
1268
|
}
|
|
@@ -1302,6 +1312,11 @@ var RequestParams = class _RequestParams {
|
|
|
1302
1312
|
this.params.onPrefetchResponse(response);
|
|
1303
1313
|
}
|
|
1304
1314
|
}
|
|
1315
|
+
onPrefetchError(error) {
|
|
1316
|
+
if (this.params.onPrefetchError) {
|
|
1317
|
+
this.params.onPrefetchError(error);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1305
1320
|
all() {
|
|
1306
1321
|
return this.params;
|
|
1307
1322
|
}
|
|
@@ -1684,6 +1699,9 @@ var Request = class _Request {
|
|
|
1684
1699
|
return;
|
|
1685
1700
|
}
|
|
1686
1701
|
if (fireExceptionEvent(error)) {
|
|
1702
|
+
if (originallyPrefetch) {
|
|
1703
|
+
this.requestParams.onPrefetchError(error);
|
|
1704
|
+
}
|
|
1687
1705
|
return Promise.reject(error);
|
|
1688
1706
|
}
|
|
1689
1707
|
}).finally(() => {
|
|
@@ -2117,6 +2135,137 @@ function formDataToObject(source) {
|
|
|
2117
2135
|
return form;
|
|
2118
2136
|
}
|
|
2119
2137
|
|
|
2138
|
+
// src/resetFormFields.ts
|
|
2139
|
+
function isFormElement(element) {
|
|
2140
|
+
return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
|
|
2141
|
+
}
|
|
2142
|
+
function resetInputElement(input, defaultValues) {
|
|
2143
|
+
const oldValue = input.value;
|
|
2144
|
+
const oldChecked = input.checked;
|
|
2145
|
+
switch (input.type.toLowerCase()) {
|
|
2146
|
+
case "checkbox":
|
|
2147
|
+
input.checked = defaultValues.includes(input.value);
|
|
2148
|
+
break;
|
|
2149
|
+
case "radio":
|
|
2150
|
+
input.checked = defaultValues[0] === input.value;
|
|
2151
|
+
break;
|
|
2152
|
+
case "file":
|
|
2153
|
+
input.value = "";
|
|
2154
|
+
break;
|
|
2155
|
+
case "button":
|
|
2156
|
+
case "submit":
|
|
2157
|
+
case "reset":
|
|
2158
|
+
case "image":
|
|
2159
|
+
break;
|
|
2160
|
+
default:
|
|
2161
|
+
input.value = defaultValues[0] !== null && defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2162
|
+
}
|
|
2163
|
+
return input.value !== oldValue || input.checked !== oldChecked;
|
|
2164
|
+
}
|
|
2165
|
+
function resetSelectElement(select, defaultValues) {
|
|
2166
|
+
const oldValue = select.value;
|
|
2167
|
+
const oldSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2168
|
+
if (select.multiple) {
|
|
2169
|
+
const defaultStrings = defaultValues.map((value) => String(value));
|
|
2170
|
+
Array.from(select.options).forEach((option) => {
|
|
2171
|
+
option.selected = defaultStrings.includes(option.value);
|
|
2172
|
+
});
|
|
2173
|
+
} else {
|
|
2174
|
+
select.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2175
|
+
}
|
|
2176
|
+
const newSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2177
|
+
const hasChanged = select.multiple ? JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort()) : select.value !== oldValue;
|
|
2178
|
+
return hasChanged;
|
|
2179
|
+
}
|
|
2180
|
+
function resetFormElement(element, defaultValues) {
|
|
2181
|
+
if (element.disabled) {
|
|
2182
|
+
if (element instanceof HTMLInputElement) {
|
|
2183
|
+
const oldValue = element.value;
|
|
2184
|
+
const oldChecked = element.checked;
|
|
2185
|
+
switch (element.type.toLowerCase()) {
|
|
2186
|
+
case "checkbox":
|
|
2187
|
+
case "radio":
|
|
2188
|
+
element.checked = element.defaultChecked;
|
|
2189
|
+
return element.checked !== oldChecked;
|
|
2190
|
+
case "file":
|
|
2191
|
+
element.value = "";
|
|
2192
|
+
return oldValue !== "";
|
|
2193
|
+
case "button":
|
|
2194
|
+
case "submit":
|
|
2195
|
+
case "reset":
|
|
2196
|
+
case "image":
|
|
2197
|
+
return false;
|
|
2198
|
+
default:
|
|
2199
|
+
element.value = element.defaultValue;
|
|
2200
|
+
return element.value !== oldValue;
|
|
2201
|
+
}
|
|
2202
|
+
} else if (element instanceof HTMLSelectElement) {
|
|
2203
|
+
const oldSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2204
|
+
Array.from(element.options).forEach((option) => {
|
|
2205
|
+
option.selected = option.defaultSelected;
|
|
2206
|
+
});
|
|
2207
|
+
const newSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2208
|
+
return JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort());
|
|
2209
|
+
} else if (element instanceof HTMLTextAreaElement) {
|
|
2210
|
+
const oldValue = element.value;
|
|
2211
|
+
element.value = element.defaultValue;
|
|
2212
|
+
return element.value !== oldValue;
|
|
2213
|
+
}
|
|
2214
|
+
return false;
|
|
2215
|
+
}
|
|
2216
|
+
if (element instanceof HTMLInputElement) {
|
|
2217
|
+
return resetInputElement(element, defaultValues);
|
|
2218
|
+
} else if (element instanceof HTMLSelectElement) {
|
|
2219
|
+
return resetSelectElement(element, defaultValues);
|
|
2220
|
+
} else if (element instanceof HTMLTextAreaElement) {
|
|
2221
|
+
const oldValue = element.value;
|
|
2222
|
+
element.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2223
|
+
return element.value !== oldValue;
|
|
2224
|
+
}
|
|
2225
|
+
return false;
|
|
2226
|
+
}
|
|
2227
|
+
function resetFieldElements(elements, defaultValues) {
|
|
2228
|
+
let hasChanged = false;
|
|
2229
|
+
if (elements instanceof RadioNodeList || elements instanceof HTMLCollection) {
|
|
2230
|
+
Array.from(elements).forEach((node, index) => {
|
|
2231
|
+
if (node instanceof Element && isFormElement(node)) {
|
|
2232
|
+
if (node instanceof HTMLInputElement && ["checkbox", "radio"].includes(node.type.toLowerCase())) {
|
|
2233
|
+
if (resetFormElement(node, defaultValues)) {
|
|
2234
|
+
hasChanged = true;
|
|
2235
|
+
}
|
|
2236
|
+
} else {
|
|
2237
|
+
const indexedDefaultValues = defaultValues[index] !== void 0 ? [defaultValues[index]] : [defaultValues[0] ?? null].filter(Boolean);
|
|
2238
|
+
if (resetFormElement(node, indexedDefaultValues)) {
|
|
2239
|
+
hasChanged = true;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
});
|
|
2244
|
+
} else if (isFormElement(elements)) {
|
|
2245
|
+
hasChanged = resetFormElement(elements, defaultValues);
|
|
2246
|
+
}
|
|
2247
|
+
return hasChanged;
|
|
2248
|
+
}
|
|
2249
|
+
function resetFormFields(formElement, defaults, fieldNames) {
|
|
2250
|
+
if (!fieldNames || fieldNames.length === 0) {
|
|
2251
|
+
const formData = new FormData(formElement);
|
|
2252
|
+
const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
|
|
2253
|
+
fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
|
|
2254
|
+
}
|
|
2255
|
+
let hasChanged = false;
|
|
2256
|
+
fieldNames.forEach((fieldName) => {
|
|
2257
|
+
const elements = formElement.elements.namedItem(fieldName);
|
|
2258
|
+
if (elements) {
|
|
2259
|
+
if (resetFieldElements(elements, defaults.getAll(fieldName))) {
|
|
2260
|
+
hasChanged = true;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
});
|
|
2264
|
+
if (hasChanged) {
|
|
2265
|
+
formElement.dispatchEvent(new Event("reset", { bubbles: true }));
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2120
2269
|
// src/head.ts
|
|
2121
2270
|
var Renderer = {
|
|
2122
2271
|
buildDOMElement(tag) {
|