2022-04-28 19:10:21 +02:00
|
|
|
import { session } from '@hotwired/turbo';
|
2022-07-07 11:39:03 +02:00
|
|
|
import { z } from 'zod';
|
2018-10-09 11:32:27 +02:00
|
|
|
|
2022-07-07 11:39:03 +02:00
|
|
|
const Gon = z
|
|
|
|
.object({
|
|
|
|
autosave: z
|
|
|
|
.object({
|
|
|
|
debounce_delay: z.number().default(0),
|
|
|
|
status_visible_duration: z.number().default(0)
|
|
|
|
})
|
|
|
|
.default({}),
|
|
|
|
autocomplete: z
|
|
|
|
.object({
|
|
|
|
api_geo_url: z.string().optional(),
|
|
|
|
api_adresse_url: z.string().optional(),
|
|
|
|
api_education_url: z.string().optional()
|
|
|
|
})
|
|
|
|
.default({}),
|
2023-02-22 17:08:36 +01:00
|
|
|
locale: z.string().default('fr'),
|
2022-07-07 11:39:03 +02:00
|
|
|
matomo: z
|
|
|
|
.object({
|
|
|
|
cookieDomain: z.string().optional(),
|
|
|
|
domain: z.string().optional(),
|
|
|
|
enabled: z.boolean().default(false),
|
|
|
|
host: z.string().optional(),
|
2022-07-18 16:08:29 +02:00
|
|
|
key: z.string().or(z.number()).nullish()
|
2022-07-07 11:39:03 +02:00
|
|
|
})
|
|
|
|
.default({}),
|
|
|
|
sentry: z
|
|
|
|
.object({
|
|
|
|
key: z.string().nullish(),
|
|
|
|
enabled: z.boolean().default(false),
|
|
|
|
environment: z.string().optional(),
|
|
|
|
user: z.object({ id: z.string() }).default({ id: '' }),
|
2023-05-23 14:25:52 +02:00
|
|
|
browser: z.object({ modern: z.boolean() }).default({ modern: false }),
|
|
|
|
release: z.string().nullish()
|
2022-07-07 11:39:03 +02:00
|
|
|
})
|
|
|
|
.default({}),
|
|
|
|
crisp: z
|
|
|
|
.object({
|
|
|
|
key: z.string().nullish(),
|
|
|
|
enabled: z.boolean().default(false),
|
|
|
|
administrateur: z
|
|
|
|
.object({
|
|
|
|
email: z.string(),
|
|
|
|
DS_SIGN_IN_COUNT: z.number(),
|
|
|
|
DS_NB_DEMARCHES_BROUILLONS: z.number(),
|
|
|
|
DS_NB_DEMARCHES_ACTIVES: z.number(),
|
|
|
|
DS_NB_DEMARCHES_ARCHIVES: z.number(),
|
|
|
|
DS_ID: z.number()
|
|
|
|
})
|
|
|
|
.default({
|
|
|
|
email: '',
|
|
|
|
DS_SIGN_IN_COUNT: 0,
|
|
|
|
DS_NB_DEMARCHES_BROUILLONS: 0,
|
|
|
|
DS_NB_DEMARCHES_ACTIVES: 0,
|
|
|
|
DS_NB_DEMARCHES_ARCHIVES: 0,
|
|
|
|
DS_ID: 0
|
|
|
|
})
|
|
|
|
})
|
2023-03-18 12:57:44 +01:00
|
|
|
.default({}),
|
|
|
|
defaultQuery: z.string().optional(),
|
|
|
|
defaultVariables: z.string().optional()
|
2022-07-07 11:39:03 +02:00
|
|
|
})
|
|
|
|
.default({});
|
|
|
|
declare const window: Window & typeof globalThis & { gon: unknown };
|
|
|
|
|
|
|
|
export function getConfig() {
|
|
|
|
return Gon.parse(window.gon);
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:01:53 +02:00
|
|
|
export function show(el: Element | null) {
|
2022-05-06 19:35:14 +02:00
|
|
|
el?.classList.remove('hidden');
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 19:01:53 +02:00
|
|
|
export function hide(el: Element | null) {
|
2022-05-06 19:35:14 +02:00
|
|
|
el?.classList.add('hidden');
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 19:01:53 +02:00
|
|
|
export function toggle(el: Element | null, force?: boolean) {
|
2020-03-30 15:34:56 +02:00
|
|
|
if (force == undefined) {
|
2022-05-06 19:35:14 +02:00
|
|
|
el?.classList.toggle('hidden');
|
2020-03-30 15:34:56 +02:00
|
|
|
} else if (force) {
|
2022-05-06 19:35:14 +02:00
|
|
|
el?.classList.remove('hidden');
|
2020-03-30 15:34:56 +02:00
|
|
|
} else {
|
2022-05-06 19:35:14 +02:00
|
|
|
el?.classList.add('hidden');
|
2020-03-30 15:34:56 +02:00
|
|
|
}
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
2018-10-09 11:32:27 +02:00
|
|
|
|
2022-10-24 16:43:18 +02:00
|
|
|
export function toggleExpandIcon(icon: Element | null) {
|
2023-04-28 10:33:01 +02:00
|
|
|
icon?.classList.toggle('fr-icon-arrow-down-s-line');
|
|
|
|
icon?.classList?.toggle('fr-icon-arrow-up-s-line');
|
2022-10-24 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 19:01:53 +02:00
|
|
|
export function enable(
|
|
|
|
el: HTMLSelectElement | HTMLInputElement | HTMLButtonElement | null
|
|
|
|
) {
|
2019-11-19 17:55:30 +01:00
|
|
|
el && (el.disabled = false);
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:01:53 +02:00
|
|
|
export function disable(
|
|
|
|
el: HTMLSelectElement | HTMLInputElement | HTMLButtonElement | null
|
|
|
|
) {
|
2019-11-19 17:55:30 +01:00
|
|
|
el && (el.disabled = true);
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:14 +02:00
|
|
|
export function hasClass(el: HTMLElement | null, cssClass: string) {
|
|
|
|
return el?.classList.contains(cssClass);
|
2019-11-19 17:55:30 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:14 +02:00
|
|
|
export function addClass(el: HTMLElement | null, cssClass: string) {
|
|
|
|
el?.classList.add(cssClass);
|
2019-11-19 17:55:30 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:14 +02:00
|
|
|
export function removeClass(el: HTMLElement | null, cssClass: string) {
|
|
|
|
el?.classList.remove(cssClass);
|
2019-11-19 17:55:30 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
export function delegate<E extends Event = Event>(
|
2022-02-02 17:16:50 +01:00
|
|
|
eventNames: string,
|
|
|
|
selector: string,
|
2022-04-15 13:05:46 +02:00
|
|
|
callback: (event: E) => void
|
2022-10-20 18:13:36 +02:00
|
|
|
): () => void {
|
|
|
|
const subscriptions = eventNames
|
2018-10-09 11:32:27 +02:00
|
|
|
.split(' ')
|
2022-10-20 18:13:36 +02:00
|
|
|
.map((eventName) =>
|
|
|
|
delegateEvent(
|
2022-04-15 13:05:46 +02:00
|
|
|
document,
|
|
|
|
selector,
|
|
|
|
eventName,
|
|
|
|
callback as (event: Event) => void
|
|
|
|
)
|
2018-10-09 11:32:27 +02:00
|
|
|
);
|
2022-10-20 18:13:36 +02:00
|
|
|
return () => subscriptions.forEach((unsubscribe) => unsubscribe());
|
2018-10-09 11:32:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:10:21 +02:00
|
|
|
export class ResponseError extends Error {
|
2022-05-06 19:37:43 +02:00
|
|
|
readonly response: Response;
|
|
|
|
readonly jsonBody?: unknown;
|
|
|
|
readonly textBody?: string;
|
2022-02-23 13:08:02 +01:00
|
|
|
|
2022-05-06 19:37:43 +02:00
|
|
|
constructor(response: Response, jsonBody?: unknown, textBody?: string) {
|
2022-02-23 13:08:02 +01:00
|
|
|
super(String(response.statusText || response.status));
|
|
|
|
this.response = response;
|
2022-05-06 19:37:43 +02:00
|
|
|
this.jsonBody = jsonBody;
|
|
|
|
this.textBody = textBody;
|
2022-02-23 13:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:10:21 +02:00
|
|
|
const FETCH_TIMEOUT = 30 * 1000; // 30 sec
|
|
|
|
|
|
|
|
// Perform an HTTP request using `fetch` API,
|
|
|
|
// and handle the result depending on the MIME type.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// Execute a GET request, and return the response as parsed JSON
|
|
|
|
// const parsedJson = await httpRequest(url).json();
|
|
|
|
//
|
|
|
|
// Execute a POST request with some JSON payload
|
|
|
|
// const parsedJson = await httpRequest(url, { method: 'POST', json: '{ "foo": 1 }').json();
|
|
|
|
//
|
|
|
|
// Execute a GET request, and apply the Turbo stream in the Response
|
|
|
|
// await httpRequest(url).turbo();
|
|
|
|
//
|
|
|
|
export function httpRequest(
|
|
|
|
url: string,
|
|
|
|
{
|
|
|
|
csrf = true,
|
|
|
|
timeout = FETCH_TIMEOUT,
|
|
|
|
json,
|
|
|
|
controller,
|
|
|
|
...init
|
|
|
|
}: RequestInit & {
|
|
|
|
csrf?: boolean;
|
|
|
|
json?: unknown;
|
|
|
|
timeout?: number | false;
|
|
|
|
controller?: AbortController;
|
|
|
|
} = {}
|
|
|
|
) {
|
2022-05-05 09:35:53 +02:00
|
|
|
const headers = init.headers ? new Headers(init.headers) : new Headers();
|
2022-04-28 19:10:21 +02:00
|
|
|
if (csrf) {
|
|
|
|
headers.set('x-csrf-token', csrfToken() ?? '');
|
|
|
|
headers.set('x-requested-with', 'XMLHttpRequest');
|
|
|
|
init.credentials = 'same-origin';
|
|
|
|
}
|
|
|
|
init.headers = headers;
|
|
|
|
init.method = init.method?.toUpperCase() ?? 'GET';
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
headers.set('content-type', 'application/json');
|
|
|
|
init.body = JSON.stringify(json);
|
|
|
|
}
|
2020-10-08 11:19:16 +02:00
|
|
|
|
2022-07-05 13:38:00 +02:00
|
|
|
let timer: ReturnType<typeof setTimeout>;
|
2022-04-28 19:10:21 +02:00
|
|
|
if (!init.signal) {
|
|
|
|
controller = createAbortController(controller);
|
|
|
|
if (controller) {
|
|
|
|
init.signal = controller.signal;
|
|
|
|
if (timeout != false) {
|
|
|
|
timer = setTimeout(() => controller?.abort(), timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:37:43 +02:00
|
|
|
const request = async (
|
|
|
|
init: RequestInit,
|
|
|
|
accept?: string
|
|
|
|
): Promise<Response> => {
|
2022-04-28 19:10:21 +02:00
|
|
|
if (accept && init.headers instanceof Headers) {
|
|
|
|
init.headers.set('accept', accept);
|
|
|
|
}
|
2022-05-06 19:37:43 +02:00
|
|
|
try {
|
|
|
|
const response = await fetch(url, init);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
return response;
|
|
|
|
} else if (response.status == 401) {
|
|
|
|
location.reload(); // reload whole page so Devise will redirect to sign-in
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
|
|
let jsonBody: unknown;
|
|
|
|
let textBody: string | undefined;
|
|
|
|
try {
|
|
|
|
if (contentType?.match('json')) {
|
2022-05-13 18:13:52 +02:00
|
|
|
jsonBody = await response.clone().json();
|
2022-05-06 19:37:43 +02:00
|
|
|
} else {
|
2022-05-13 18:13:52 +02:00
|
|
|
textBody = await response.clone().text();
|
2022-04-28 19:10:21 +02:00
|
|
|
}
|
2022-05-06 19:37:43 +02:00
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw new ResponseError(response, jsonBody, textBody);
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
2022-04-28 19:10:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
async json<T>(): Promise<T | null> {
|
|
|
|
const response = await request(init, 'application/json');
|
|
|
|
if (response.status == 204) {
|
2020-10-08 11:19:16 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return response.json();
|
2022-04-28 19:10:21 +02:00
|
|
|
},
|
|
|
|
async turbo(): Promise<void> {
|
|
|
|
const response = await request(init, 'text/vnd.turbo-stream.html');
|
|
|
|
if (response.status != 204) {
|
|
|
|
const stream = await response.text();
|
|
|
|
session.renderStreamMessage(stream);
|
|
|
|
}
|
2020-10-08 11:19:16 +02:00
|
|
|
}
|
2022-04-28 19:10:21 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAbortController(controller?: AbortController) {
|
|
|
|
if (controller) {
|
|
|
|
return controller;
|
|
|
|
} else if (window.AbortController) {
|
|
|
|
return new AbortController();
|
|
|
|
}
|
|
|
|
return;
|
2018-10-09 11:32:27 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 13:07:23 +01:00
|
|
|
export function isNumeric(s: string) {
|
|
|
|
const n = parseFloat(s);
|
|
|
|
return !isNaN(n) && isFinite(n);
|
2022-07-12 11:41:38 +02:00
|
|
|
}
|
2022-10-20 18:13:36 +02:00
|
|
|
|
|
|
|
export function fire<T>(obj: EventTarget, name: string, data?: T) {
|
|
|
|
const event = new CustomEvent(name, {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
detail: data
|
|
|
|
});
|
|
|
|
obj.dispatchEvent(event);
|
|
|
|
return !event.defaultPrevented;
|
|
|
|
}
|
|
|
|
|
2023-03-18 12:57:44 +01:00
|
|
|
export function csrfToken() {
|
2022-10-20 18:13:36 +02:00
|
|
|
const meta = document.querySelector<HTMLMetaElement>('meta[name=csrf-token]');
|
|
|
|
return meta?.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function delegateEvent<E extends Event = Event>(
|
|
|
|
element: EventTarget,
|
|
|
|
selector: string,
|
|
|
|
eventType: string,
|
|
|
|
handler: (event: E) => void | boolean
|
|
|
|
): () => void {
|
|
|
|
const listener = function (event: Event) {
|
|
|
|
let { target } = event;
|
|
|
|
while (!!(target instanceof Element) && !target.matches(selector)) {
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
target instanceof Element &&
|
|
|
|
handler.call(target, event as E) === false
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
element.addEventListener(eventType, listener);
|
|
|
|
return () => element.removeEventListener(eventType, listener);
|
|
|
|
}
|