Merge branch 'laravel-8-bs-import' into 'laravel-8'

Laravel 8 bs import

See merge request ldc-web/projets/laravel-base-project!7
This commit is contained in:
Vincent BRIDIER 2023-03-13 14:12:35 +00:00
commit 279c8104c6
2 changed files with 51 additions and 49 deletions

View file

@ -1,73 +1,76 @@
const route = {
methods: {
/**
* Define a route by the data given,
* ex: route('name', { 'parameter': 'value' }).
* Same behavior as laravel route
*
* ! window.__SYSTEM._routes has to be defined
*
* @param routeName The route name.
* @param parameters Parameters, if a parameter name matches a route path level,
* it will be replaced, otherwise it will appear in query.
* @param keepActualQueries Pass this to keep current url queries.
* @param keepActualHashes Pass this to keep current url fragments
* @returns The newly formated string
*/
route(
routeName: string,
parameters: Record<
string,
string | number | Array<string | number>
> = {}
> = {},
keepActualQueries: boolean = false,
keepActualHashes: boolean = false
): string | null {
const routes = route.flattenProperties(window.__SYSTEM._routes);
const routeUrl =
Object.keys(routes).indexOf(routeName) !== -1
? (routes[routeName] as string)
: null;
// * Return null if route url could not be found
if (!routeUrl) {
return routeUrl;
return null;
}
// flatten parameter array
for (const parameterIndex in parameters) {
const parameter = parameters[parameterIndex];
if (!Array.isArray(parameter)) {
continue;
const queryParameters: Record<string, number | string> = {},
url = new URL(window.location.origin),
searchParams = new URL(String(window.location)).searchParams,
hash = window.location.hash;
let pathname = routeUrl.replace(origin, "");
// * Replace parameters in path name.
for (const paramName in parameters) {
const paramValue = String(parameters[paramName]);
if (pathname.indexOf(paramName) !== -1) {
// * Replace pathname part with parameter
pathname = pathname.replace(paramName, paramValue);
} else {
// * push parameter to be in queries
queryParameters[paramName] = paramValue;
}
delete parameters[parameterIndex];
parameter.forEach(
(parameter, index) =>
(parameters[`${parameterIndex}[${index}]`] = parameter)
}
url.pathname = pathname;
// * Build query
if (keepActualQueries) {
// * Push actual queries
for (const param of searchParams) {
url.searchParams.set(param[0], param[1]);
}
}
// * Push request queries
for (const paramName in queryParameters) {
url.searchParams.set(
paramName,
String(queryParameters[paramName])
);
// still append empty entry if empty array
if (!parameter.length) {
parameters[`${parameterIndex}`] = "";
}
}
const flattenParameters = parameters as Record<
string,
string | number
>;
// copy parameters
const iParameters = Object.assign({}, flattenParameters),
origin = window.location.origin,
url = new URL(
`${origin}${routeUrl.replace(origin, "")}${
window.location.search
}${window.location.hash}`
);
// Replace in path name.
for (const paramName in iParameters) {
const paramValue = flattenParameters[paramName];
if (url.pathname.indexOf(paramName) !== -1) {
url.pathname = url.pathname.replace(
paramName,
paramValue as string
);
delete flattenParameters[paramName];
}
if (keepActualHashes) {
url.hash = hash;
}
// Append remaining parameters to url
for (const paramName in flattenParameters) {
const paramValue = flattenParameters[paramName];
url.searchParams.set(paramName, String(paramValue));
}
return url.toString();
return String(url);
},
},
flattenProperties(

View file

@ -1,8 +1,7 @@
import axios from "axios";
import * as bootstrap from "bootstrap";
import bsTooltips from "../modules/bsTooltips";
const bootstrap = require("bootstrap");
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the