convert misc scripts to ts

This commit is contained in:
Luna D. 2024-06-23 19:59:06 +02:00
parent 6e1cb36eb6
commit b329f6d5aa
No known key found for this signature in database
GPG key ID: 4B1C63448394F688

View file

@ -4,35 +4,40 @@
import store from './utils/store'; import store from './utils/store';
import { $, $$ } from './utils/dom'; import { $, $$ } from './utils/dom';
import { assertNotNull } from './utils/assert';
import '../types/ujs';
let touchMoved = false; let touchMoved = false;
function formResult({target, detail}) { function formResult({target, detail}: FetchcompleteEvent) {
const elements: {[key: string]: string} = {
const elements = {
'#description-form': '.image-description', '#description-form': '.image-description',
'#uploader-form': '.image-uploader' '#uploader-form': '.image-uploader'
}; };
function showResult(resultEl, formEl, response) { function showResult(resultEl: HTMLElement, formEl: HTMLFormElement, response: string) {
resultEl.innerHTML = response; resultEl.innerHTML = response;
resultEl.classList.remove('hidden'); resultEl.classList.remove('hidden');
formEl.classList.add('hidden'); formEl.classList.add('hidden');
formEl.querySelector('input[type="submit"],button').disabled = false; const inputEl = $<HTMLInputElement>('input[type="submit"]', formEl);
const buttonEl = $<HTMLButtonElement>('button', formEl);
if (inputEl) inputEl.disabled = false;
if (buttonEl) buttonEl.disabled = false;
} }
for (const element in elements) { for (const element in elements) {
if (target.matches(element)) detail.text().then(text => showResult($(elements[element]), target, text)); if (target.matches(element)) {
detail.text().then(text => showResult(assertNotNull($<HTMLElement>(elements[element])), target as HTMLFormElement, text));
}
} }
} }
function revealSpoiler(event) { function revealSpoiler(event: MouseEvent | TouchEvent) {
const target = assertNotNull(event.target) as HTMLElement;
const { target } = event;
const spoiler = target.closest('.spoiler'); const spoiler = target.closest('.spoiler');
let imgspoiler = target.closest('.spoiler .imgspoiler, .spoiler-revealed .imgspoiler');
const showContainer = target.closest('.image-show-container'); const showContainer = target.closest('.image-show-container');
let imgspoiler = target.closest('.spoiler .imgspoiler, .spoiler-revealed .imgspoiler');
// Prevent reveal if touchend came after touchmove event // Prevent reveal if touchend came after touchmove event
if (touchMoved) { if (touchMoved) {
@ -42,7 +47,8 @@ function revealSpoiler(event) {
if (spoiler) { if (spoiler) {
if (showContainer) { if (showContainer) {
const imageShow = showContainer.querySelector('.image-show'); const imageShow = assertNotNull(showContainer.querySelector('.image-show'));
if (!imageShow.classList.contains('hidden') && imageShow.classList.contains('spoiler-pending')) { if (!imageShow.classList.contains('hidden') && imageShow.classList.contains('spoiler-pending')) {
imageShow.classList.remove('spoiler-pending'); imageShow.classList.remove('spoiler-pending');
return; return;
@ -62,19 +68,22 @@ function revealSpoiler(event) {
if (imgspoiler) { if (imgspoiler) {
imgspoiler.classList.remove('imgspoiler'); imgspoiler.classList.remove('imgspoiler');
imgspoiler.classList.add('imgspoiler-revealed'); imgspoiler.classList.add('imgspoiler-revealed');
if (event.type === 'touchend' && !event.defaultPrevented) { if (event.type === 'touchend' && !event.defaultPrevented) {
event.preventDefault(); event.preventDefault();
} }
} }
} }
function setupEvents() { export function setupEvents() {
const extrameta = $('#extrameta'); const extrameta = $<HTMLElement>('#extrameta');
if (extrameta && store.get('hide_uploader')) {
extrameta.classList.add('hidden');
}
if (store.get('hide_uploader') && extrameta) extrameta.classList.add('hidden');
if (store.get('hide_score')) { if (store.get('hide_score')) {
$$('.upvotes,.score,.downvotes').forEach(s => s.classList.add('hidden')); $$<HTMLElement>('.upvotes,.score,.downvotes').forEach(s => s.classList.add('hidden'));
} }
document.addEventListener('fetchcomplete', formResult); document.addEventListener('fetchcomplete', formResult);
@ -82,5 +91,3 @@ function setupEvents() {
document.addEventListener('touchend', revealSpoiler); document.addEventListener('touchend', revealSpoiler);
document.addEventListener('touchmove', () => touchMoved = true); document.addEventListener('touchmove', () => touchMoved = true);
} }
export { setupEvents };