2024-04-16 14:02:03 +02:00
|
|
|
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 = () => ({
|
2020-04-11 20:23:55 +02:00
|
|
|
'x-csrf-token': window.booru.csrfToken,
|
|
|
|
'x-requested-with': 'XMLHttpRequest'
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
|
|
|
|
2024-04-16 14:02:03 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 14:02:03 +02:00
|
|
|
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
|
2024-04-16 14:02:03 +02:00
|
|
|
const label = findFirstTextNode<Text>(target);
|
2019-10-05 02:09:52 +02:00
|
|
|
if (label) {
|
2024-04-16 14:02:03 +02:00
|
|
|
target.dataset.enableWith = assertNotNull(label.nodeValue);
|
2019-10-05 02:09:52 +02:00
|
|
|
label.nodeValue = ` ${target.dataset.disableWith}`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target.dataset.enableWith = target.innerHTML;
|
2024-04-16 14:02:03 +02:00
|
|
|
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
|
2024-04-16 14:02:03 +02:00
|
|
|
requestAnimationFrame(() => target.setAttribute('disabled', 'disabled'));
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// you should use button_to instead of link_to[method]!
|
2024-04-16 14:02:03 +02:00
|
|
|
function linkMethod(event: Event, target: HTMLAnchorElement) {
|
2019-10-05 02:09:52 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const form = makeEl('form', { action: target.href, method: 'POST' });
|
2019-11-13 05:49:37 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-04-16 14:02:03 +02:00
|
|
|
function formRemote(event: Event, target: HTMLFormElement) {
|
2019-10-05 02:09:52 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
fetch(target.action, {
|
|
|
|
credentials: 'same-origin',
|
2024-04-16 14:02:03 +02:00
|
|
|
method: (target.dataset.method || target.method).toUpperCase(),
|
2019-10-05 02:09:52 +02:00
|
|
|
headers: headers(),
|
|
|
|
body: new FormData(target)
|
2020-04-11 20:23:55 +02:00
|
|
|
}).then(response => {
|
2024-04-16 14:02:03 +02:00
|
|
|
fire(target, 'fetchcomplete', response);
|
2021-10-09 21:58:07 +02:00
|
|
|
if (response && response.status === 300) {
|
2024-04-16 14:02:03 +02:00
|
|
|
window.location.reload();
|
2020-04-11 20:23:55 +02:00
|
|
|
}
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 14:02:03 +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-04-16 14:02:03 +02:00
|
|
|
else {
|
|
|
|
input.innerHTML = assertNotUndefined(input.dataset.enableWith);
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
delete input.dataset.enableWith;
|
|
|
|
input.removeAttribute('disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-16 14:02:03 +02:00
|
|
|
function linkRemote(event: Event, target: HTMLAnchorElement) {
|
2019-10-05 02:09:52 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
fetch(target.href, {
|
|
|
|
credentials: 'same-origin',
|
2024-04-16 14:02:03 +02:00
|
|
|
method: (target.dataset.method || 'get').toUpperCase(),
|
2019-10-05 02:09:52 +02:00
|
|
|
headers: headers()
|
|
|
|
}).then(response =>
|
|
|
|
fire(target, 'fetchcomplete', response)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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', {
|
|
|
|
'form[data-remote]': formRemote
|
|
|
|
});
|
|
|
|
|
|
|
|
delegate(document, 'reset', {
|
|
|
|
form: formReset
|
|
|
|
});
|
2020-09-15 02:53:11 +02:00
|
|
|
|
|
|
|
window.addEventListener('pageshow', () => {
|
2024-04-16 14:02:03 +02:00
|
|
|
for (const form of document.forms) {
|
|
|
|
formReset(null, form);
|
|
|
|
}
|
2020-09-15 02:53:11 +02:00
|
|
|
});
|