philomena/assets/js/ujs.ts

111 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

import { assertNotNull, assertNotUndefined } from './utils/assert';
2019-10-05 02:09:52 +02:00
import { $$, makeEl, findFirstTextNode } from './utils/dom';
2019-11-19 04:38:22 +01:00
import { fire, delegate, leftClick } from './utils/events';
2019-10-05 02:09:52 +02:00
const headers = () => ({
'x-csrf-token': window.booru.csrfToken,
2024-07-04 02:27:59 +02:00
'x-requested-with': 'XMLHttpRequest',
2019-10-05 02:09:52 +02:00
});
function confirm(event: Event, target: HTMLElement) {
2019-10-05 02:09:52 +02:00
if (!window.confirm(target.dataset.confirm)) {
event.preventDefault();
event.stopImmediatePropagation();
return false;
}
}
function disable(event: Event, target: HTMLAnchorElement | HTMLButtonElement | HTMLInputElement) {
2019-10-05 02:09:52 +02:00
// failed validations prevent the form from being submitted;
// stop here or the form will be permanently locked
if (target.type === 'submit' && target.closest(':invalid') !== null) return;
// Store what's already there so we don't lose it
const label = findFirstTextNode<Text>(target);
2019-10-05 02:09:52 +02:00
if (label) {
target.dataset.enableWith = assertNotNull(label.nodeValue);
2019-10-05 02:09:52 +02:00
label.nodeValue = ` ${target.dataset.disableWith}`;
2024-07-04 02:27:59 +02:00
} else {
2019-10-05 02:09:52 +02:00
target.dataset.enableWith = target.innerHTML;
target.innerHTML = assertNotUndefined(target.dataset.disableWith);
2019-10-05 02:09:52 +02:00
}
// delay is needed because Safari stops the submit if the button is immediately disabled
requestAnimationFrame(() => target.setAttribute('disabled', 'disabled'));
2019-10-05 02:09:52 +02:00
}
// you should use button_to instead of link_to[method]!
function linkMethod(event: Event, target: HTMLAnchorElement) {
2019-10-05 02:09:52 +02:00
event.preventDefault();
2024-07-04 02:27:59 +02:00
const form = makeEl('form', { action: target.href, method: 'POST' });
const csrf = makeEl('input', { type: 'hidden', name: '_csrf_token', value: window.booru.csrfToken });
2019-10-05 02:09:52 +02:00
const method = makeEl('input', { type: 'hidden', name: '_method', value: target.dataset.method });
document.body.appendChild(form);
form.appendChild(csrf);
form.appendChild(method);
form.submit();
}
function formRemote(event: Event, target: HTMLFormElement) {
2019-10-05 02:09:52 +02:00
event.preventDefault();
fetch(target.action, {
credentials: 'same-origin',
method: (target.dataset.method || target.method).toUpperCase(),
2019-10-05 02:09:52 +02:00
headers: headers(),
2024-07-04 02:27:59 +02:00
body: new FormData(target),
}).then(response => {
fire(target, 'fetchcomplete', response);
if (response && response.status === 300) {
window.location.reload();
}
});
2019-10-05 02:09:52 +02:00
}
function formReset(_event: Event | null, target: HTMLElement) {
$$<HTMLElement>('[disabled][data-disable-with][data-enable-with]', target).forEach(input => {
2019-10-05 02:09:52 +02:00
const label = findFirstTextNode(input);
if (label) {
label.nodeValue = ` ${input.dataset.enableWith}`;
2024-07-04 02:27:59 +02:00
} else {
input.innerHTML = assertNotUndefined(input.dataset.enableWith);
}
2019-10-05 02:09:52 +02:00
delete input.dataset.enableWith;
input.removeAttribute('disabled');
});
}
function linkRemote(event: Event, target: HTMLAnchorElement) {
2019-10-05 02:09:52 +02:00
event.preventDefault();
fetch(target.href, {
credentials: 'same-origin',
method: (target.dataset.method || 'get').toUpperCase(),
2024-07-04 02:27:59 +02:00
headers: headers(),
}).then(response => fire(target, 'fetchcomplete', response));
2019-10-05 02:09:52 +02:00
}
delegate(document, 'click', {
'a[data-confirm],button[data-confirm],input[data-confirm]': leftClick(confirm),
'a[data-disable-with],button[data-disable-with],input[data-disable-with]': leftClick(disable),
'a[data-method]:not([data-remote])': leftClick(linkMethod),
'a[data-remote]': leftClick(linkRemote),
});
delegate(document, 'submit', {
2024-07-04 02:27:59 +02:00
'form[data-remote]': formRemote,
2019-10-05 02:09:52 +02:00
});
delegate(document, 'reset', {
2024-07-04 02:27:59 +02:00
form: formReset,
2019-10-05 02:09:52 +02:00
});
window.addEventListener('pageshow', () => {
for (const form of document.forms) {
formReset(null, form);
}
});