const instead of let where possible

This commit is contained in:
rhiannon morris 2024-07-07 20:44:05 +02:00
parent 0de54d15d4
commit b2e2db77dd
3 changed files with 39 additions and 41 deletions

View File

@ -18,30 +18,30 @@ function fillSets(): [Set<string>, Set<string>] {
} }
function updateItems() { function updateItems() {
let [reqTags, excTags] = fillSets(); const [reqTags, excTags] = fillSets();
let anyReq = reqTags.size > 0; const anyReq = reqTags.size > 0;
for (let [year, items] of itemsByYear) { for (const [year, items] of itemsByYear) {
let hide = true; let hide = true;
for (let item of items) { for (const item of items) {
let req = tags.get(item)?.some(x => reqTags.has(x)) ?? false; const req = tags.get(item)?.some(x => reqTags.has(x)) ?? false;
let exc = tags.get(item)?.some(x => excTags.has(x)) ?? false; const exc = tags.get(item)?.some(x => excTags.has(x)) ?? false;
let hidden = exc || (anyReq && !req); const hidden = exc || (anyReq && !req);
item.hidden = hidden; item.hidden = hidden;
hide &&= hidden; hide &&= hidden;
} }
let marker = document.getElementById(`marker-${year}`); const marker = document.getElementById(`marker-${year}`);
if (marker !== null) marker.hidden = hide; if (marker !== null) marker.hidden = hide;
} }
function disp(pfx: string, tags: Iterable<string>) { function disp(pfx: string, tags: Iterable<string>) {
return [...tags].map(x => pfx + x).join('\u2003'); // em space return [...tags].map(x => pfx + x).join('\u2003'); // em space
} }
let plus = disp('+\u2009', Array.from(reqTags)); // thin space const plus = disp('+\u2009', reqTags); // thin space
let minus = disp('-\u2009', Array.from(excTags)); const minus = disp('-\u2009', excTags);
document.getElementById('filters-details')!.dataset.filters = document.getElementById('filters-details')!.dataset.filters =
`${plus}\u2003${minus}`.trim(); `${plus}\u2003${minus}`.trim();
} }
@ -61,7 +61,7 @@ function converseId(id: string) {
function toggle(checkbox: HTMLInputElement) { function toggle(checkbox: HTMLInputElement) {
if (checkbox.checked) { if (checkbox.checked) {
let converse = document.getElementById(converseId(checkbox.id)) as HTMLInputElement; const converse = document.getElementById(converseId(checkbox.id)) as HTMLInputElement;
converse.checked = false; converse.checked = false;
} }
update(); update();
@ -81,10 +81,10 @@ function clear(e: Event) {
function toggleSingles(e: Event) { function toggleSingles(e: Event) {
showSingles = !showSingles; showSingles = !showSingles;
let elems = Array.from(document.querySelectorAll('.filterlist li')); const elems = Array.from(document.querySelectorAll('.filterlist li')) as HTMLElement[];
for (let li of elems) { for (const li of elems) {
let countStr = li.querySelector('label')?.dataset.count; const countStr = li.querySelector('label')?.dataset.count;
let count = countStr ? +countStr : 0; const count = countStr ? +countStr : 0;
if (count <= 1 && li instanceof HTMLElement) { if (count <= 1 && li instanceof HTMLElement) {
li.hidden = !showSingles; li.hidden = !showSingles;
} }
@ -95,8 +95,8 @@ function toggleSingles(e: Event) {
function makeFragment() { function makeFragment() {
let allBoxesArr = Array.from(allBoxes); const allBoxesArr = Array.from(allBoxes);
let ids = allBoxesArr.filter(b => b.checked).map(b => b.id); const ids = allBoxesArr.filter(b => b.checked).map(b => b.id);
if (ids.length == 0) { if (ids.length == 0) {
return '#all'; return '#all';
} else if (allBoxesArr.every(b => b.checked == b.defaultChecked)) { } else if (allBoxesArr.every(b => b.checked == b.defaultChecked)) {
@ -107,8 +107,8 @@ function makeFragment() {
} }
function useFragment() { function useFragment() {
let frag = decodeURIComponent(location.hash).replace(/^#/, ''); const frag = decodeURIComponent(location.hash).replace(/^#/, '');
let details = document.getElementById('filters-details') as HTMLDetailsElement; const details = document.getElementById('filters-details') as HTMLDetailsElement;
if (!frag) { if (!frag) {
clearForm(); clearForm();
@ -116,8 +116,8 @@ function useFragment() {
allBoxes.forEach(b => b.checked = false); allBoxes.forEach(b => b.checked = false);
details.open = false; details.open = false;
} else { } else {
let set = new Set(frag.split(';')); const set = new Set(frag.split(';'));
let re = /^(require|exclude)_|hide_filters/; const re = /^(require|exclude)_|hide_filters/;
if (Array.from(set).every(x => re.test(x))) { if (Array.from(set).every(x => re.test(x))) {
allBoxes.forEach(b => b.checked = set.has(b.id)); allBoxes.forEach(b => b.checked = set.has(b.id));
details.open = !frag.match(/hide_filters|example\b/); details.open = !frag.match(/hide_filters|example\b/);
@ -130,12 +130,12 @@ function useFragment() {
function sortFilters(cmp: (a: Node, b: Node) => number) { function sortFilters(cmp: (a: Node, b: Node) => number) {
function sort1(id: string) { function sort1(id: string) {
let elt = document.getElementById(id); const elt = document.getElementById(id);
if (elt === null) return; if (elt === null) return;
let children = Array.from(elt.childNodes); const children = Array.from(elt.childNodes);
children.sort(cmp); children.sort(cmp);
for (let c of children) { for (const c of children) {
elt.removeChild(c); elt.removeChild(c);
elt.appendChild(c); elt.appendChild(c);
} }
@ -175,15 +175,15 @@ function sortFiltersUses(e: Event) {
function setup() { function setup() {
function inputs(id: string): Boxes { function inputs(id: string): Boxes {
let iter = document.getElementById(id)!.getElementsByTagName('input'); const iter = document.getElementById(id)!.getElementsByTagName('input');
return new Set(Array.from(iter)); return new Set(Array.from(iter));
} }
let items = Array.from(document.getElementsByClassName('post')) as HTMLElement[]; const items = Array.from(document.getElementsByClassName('post')) as HTMLElement[];
itemsByYear = new Map; itemsByYear = new Map;
for (let item of items) { for (const item of items) {
let year = item.dataset.year; const year = item.dataset.year;
if (year !== undefined) { if (year !== undefined) {
if (!itemsByYear.has(year)) { if (!itemsByYear.has(year)) {
itemsByYear.set(year, new Set([item])); itemsByYear.set(year, new Set([item]));

View File

@ -5,7 +5,7 @@ function alreadyYes() {
} }
function dismiss() { function dismiss() {
let dialog = document.getElementById('nsfw-dialog')!; const dialog = document.getElementById('nsfw-dialog')!;
dialog.parentElement?.removeChild(dialog); dialog.parentElement?.removeChild(dialog);
} }

View File

@ -4,7 +4,7 @@ let mainlink: HTMLAnchorElement;
let altButtons: HTMLInputElement[]; let altButtons: HTMLInputElement[];
let skipAll: HTMLInputElement; let skipAll: HTMLInputElement;
let opened: Set<string> = new Set; const opened: Set<string> = new Set;
function openCW(id: string | null, caption: HTMLElement | null, function openCW(id: string | null, caption: HTMLElement | null,
focusLink = false) { focusLink = false) {
@ -27,13 +27,11 @@ function setImage(id: string | null,
src: string, src: string,
href: string, href: string,
cw: string) { cw: string) {
let caption = document.getElementById('cw'); const caption = document.getElementById('cw');
let newCaption: HTMLElement | null = null; const checked = skipAll ? skipAll.checked : false;
let checked = skipAll ? skipAll.checked : false;
if (!checked && !opened.has(id) && cw) { if (!checked && !opened.has(id) && cw) {
let template = document.getElementById('cw-template') as HTMLTemplateElement; const template = document.getElementById('cw-template') as HTMLTemplateElement;
newCaption = template.content.firstElementChild!.cloneNode(true) as HTMLElement; newCaption = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
newCaption.querySelector('#cw-text')!.innerHTML = cw; newCaption.querySelector('#cw-text')!.innerHTML = cw;
addCWListeners(id, newCaption); addCWListeners(id, newCaption);
@ -62,9 +60,9 @@ function activateButton(button: HTMLInputElement, doPush = true): void {
function useFragment(firstLoad = false): void { function useFragment(firstLoad = false): void {
let button = altButtons[0]; let button = altButtons[0];
let frag = decodeURIComponent(location.hash).replace(/^#/, ''); const frag = decodeURIComponent(location.hash).replace(/^#/, '');
if (frag) { if (frag) {
let selected = document.getElementById(frag) as HTMLInputElement; const selected = document.getElementById(frag) as HTMLInputElement;
if (selected) button = selected; if (selected) button = selected;
} }
@ -85,21 +83,21 @@ function setup() {
mainlink = document.getElementById('mainlink') as HTMLAnchorElement; mainlink = document.getElementById('mainlink') as HTMLAnchorElement;
skipAll = document.getElementById('skipAll') as HTMLInputElement; skipAll = document.getElementById('skipAll') as HTMLInputElement;
let alts = document.getElementById('alts'); const alts = document.getElementById('alts');
if (alts) { if (alts) {
let inputs = Array.from(alts.getElementsByTagName('input')); const inputs = Array.from(alts.getElementsByTagName('input'));
altButtons = inputs.filter(e => e.name == 'variant'); altButtons = inputs.filter(e => e.name == 'variant');
} else { } else {
altButtons = []; altButtons = [];
} }
for (let button of altButtons) { for (const button of altButtons) {
button.onchange = _e => { if (button.checked) activateButton(button); }; button.onchange = _e => { if (button.checked) activateButton(button); };
} }
if (skipAll) { if (skipAll) {
skipAll.onchange = _e => { if (skipAll.checked) { skipAll.onchange = _e => { if (skipAll.checked) {
let caption = document.getElementById('cw'); const caption = document.getElementById('cw');
if (caption) { openCW(null, caption, false); } if (caption) { openCW(null, caption, false); }
} }; } };
} }