Replace a bunch of let with const

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

> Many style guides (including MDN's) recommend using const over let whenever a
variable is not reassigned in its scope. This makes the intent clear that a
variable's type (or value, in the case of a primitive) can never change.
This commit is contained in:
jvoisin 2024-03-20 23:59:37 +01:00 committed by Frédéric Guillot
parent fc4bdf3ab0
commit beb8c80787
6 changed files with 41 additions and 44 deletions

View file

@ -2,7 +2,7 @@ document.addEventListener("DOMContentLoaded", () => {
handleSubmitButtons(); handleSubmitButtons();
if (!document.querySelector("body[data-disable-keyboard-shortcuts=true]")) { if (!document.querySelector("body[data-disable-keyboard-shortcuts=true]")) {
let keyboardHandler = new KeyboardHandler(); const keyboardHandler = new KeyboardHandler();
keyboardHandler.on("g u", () => goToPage("unread")); keyboardHandler.on("g u", () => goToPage("unread"));
keyboardHandler.on("g b", () => goToPage("starred")); keyboardHandler.on("g b", () => goToPage("starred"));
keyboardHandler.on("g h", () => goToPage("history")); keyboardHandler.on("g h", () => goToPage("history"));
@ -48,7 +48,7 @@ document.addEventListener("DOMContentLoaded", () => {
keyboardHandler.listen(); keyboardHandler.listen();
} }
let touchHandler = new TouchHandler(); const touchHandler = new TouchHandler();
touchHandler.listen(); touchHandler.listen();
if (WebAuthnHandler.isWebAuthnSupported()) { if (WebAuthnHandler.isWebAuthnSupported()) {
@ -56,7 +56,7 @@ document.addEventListener("DOMContentLoaded", () => {
onClick("#webauthn-delete", () => { webauthnHandler.removeAllCredentials(); }); onClick("#webauthn-delete", () => { webauthnHandler.removeAllCredentials(); });
let registerButton = document.getElementById("webauthn-register"); const registerButton = document.getElementById("webauthn-register");
if (registerButton != null) { if (registerButton != null) {
registerButton.disabled = false; registerButton.disabled = false;
@ -65,13 +65,13 @@ document.addEventListener("DOMContentLoaded", () => {
}); });
} }
let loginButton = document.getElementById("webauthn-login"); const loginButton = document.getElementById("webauthn-login");
if (loginButton != null) { if (loginButton != null) {
const abortController = new AbortController(); const abortController = new AbortController();
loginButton.disabled = false; loginButton.disabled = false;
onClick("#webauthn-login", () => { onClick("#webauthn-login", () => {
let usernameField = document.getElementById("form-username"); const usernameField = document.getElementById("form-username");
if (usernameField != null) { if (usernameField != null) {
abortController.abort(); abortController.abort();
webauthnHandler.login(usernameField.value).catch(err => WebAuthnHandler.showErrorMessage(err)); webauthnHandler.login(usernameField.value).catch(err => WebAuthnHandler.showErrorMessage(err));
@ -89,7 +89,7 @@ document.addEventListener("DOMContentLoaded", () => {
onClick(":is(a, button)[data-action=markPageAsRead]", (event) => handleConfirmationMessage(event.target, markPageAsRead)); onClick(":is(a, button)[data-action=markPageAsRead]", (event) => handleConfirmationMessage(event.target, markPageAsRead));
onClick(":is(a, button)[data-toggle-status]", (event) => handleEntryStatus("next", event.target)); onClick(":is(a, button)[data-toggle-status]", (event) => handleEntryStatus("next", event.target));
onClick(":is(a, button)[data-confirm]", (event) => handleConfirmationMessage(event.target, (url, redirectURL) => { onClick(":is(a, button)[data-confirm]", (event) => handleConfirmationMessage(event.target, (url, redirectURL) => {
let request = new RequestBuilder(url); const request = new RequestBuilder(url);
request.withCallback((response) => { request.withCallback((response) => {
if (redirectURL) { if (redirectURL) {
@ -127,7 +127,7 @@ document.addEventListener("DOMContentLoaded", () => {
onClick(".header nav li", (event) => onClickMainMenuListItem(event)); onClick(".header nav li", (event) => onClickMainMenuListItem(event));
if ("serviceWorker" in navigator) { if ("serviceWorker" in navigator) {
let scriptElement = document.getElementById("service-worker-script"); const scriptElement = document.getElementById("service-worker-script");
if (scriptElement) { if (scriptElement) {
navigator.serviceWorker.register(scriptElement.src); navigator.serviceWorker.register(scriptElement.src);
} }

View file

@ -4,17 +4,17 @@ class DomHelper {
} }
static openNewTab(url) { static openNewTab(url) {
let win = window.open(""); const win = window.open("");
win.opener = null; win.opener = null;
win.location = url; win.location = url;
win.focus(); win.focus();
} }
static scrollPageTo(element, evenIfOnScreen) { static scrollPageTo(element, evenIfOnScreen) {
let windowScrollPosition = window.pageYOffset; const windowScrollPosition = window.pageYOffset;
let windowHeight = document.documentElement.clientHeight; const windowHeight = document.documentElement.clientHeight;
let viewportPosition = windowScrollPosition + windowHeight; const viewportPosition = windowScrollPosition + windowHeight;
let itemBottomPosition = element.offsetTop + element.offsetHeight; const itemBottomPosition = element.offsetTop + element.offsetHeight;
if (evenIfOnScreen || viewportPosition - itemBottomPosition < 0 || viewportPosition - element.offsetTop > windowHeight) { if (evenIfOnScreen || viewportPosition - itemBottomPosition < 0 || viewportPosition - element.offsetTop > windowHeight) {
window.scrollTo(0, element.offsetTop - 10); window.scrollTo(0, element.offsetTop - 10);

View file

@ -12,7 +12,7 @@ class KeyboardHandler {
listen() { listen() {
document.onkeydown = (event) => { document.onkeydown = (event) => {
let key = this.getKey(event); const key = this.getKey(event);
if (this.isEventIgnored(event, key) || this.isModifierKeyDown(event)) { if (this.isEventIgnored(event, key) || this.isModifierKeyDown(event)) {
return; return;
} }
@ -23,8 +23,8 @@ class KeyboardHandler {
this.queue.push(key); this.queue.push(key);
for (let combination in this.shortcuts) { for (const combination in this.shortcuts) {
let keys = combination.split(" "); const keys = combination.split(" ");
if (keys.every((value, index) => value === this.queue[index])) { if (keys.every((value, index) => value === this.queue[index])) {
this.queue = []; this.queue = [];
@ -64,7 +64,7 @@ class KeyboardHandler {
'Right': 'ArrowRight' 'Right': 'ArrowRight'
}; };
for (let key in mapping) { for (const key in mapping) {
if (mapping.hasOwnProperty(key) && key === event.key) { if (mapping.hasOwnProperty(key) && key === event.key) {
return mapping[key]; return mapping[key];
} }

View file

@ -8,7 +8,7 @@ class ModalHandler {
} }
static getFocusableElements() { static getFocusableElements() {
let container = this.getModalContainer(); const container = this.getModalContainer();
if (container === null) { if (container === null) {
return null; return null;
@ -18,14 +18,14 @@ class ModalHandler {
} }
static setupFocusTrap() { static setupFocusTrap() {
let focusableElements = this.getFocusableElements(); const focusableElements = this.getFocusableElements();
if (focusableElements === null) { if (focusableElements === null) {
return; return;
} }
let firstFocusableElement = focusableElements[0]; const firstFocusableElement = focusableElements[0];
let lastFocusableElement = focusableElements[focusableElements.length - 1]; const lastFocusableElement = focusableElements[focusableElements.length - 1];
this.getModalContainer().onkeydown = (e) => { this.getModalContainer().onkeydown = (e) => {
if (e.key !== 'Tab') { if (e.key !== 'Tab') {
@ -57,13 +57,13 @@ class ModalHandler {
this.activeElement = document.activeElement; this.activeElement = document.activeElement;
let container = document.createElement("div"); const container = document.createElement("div");
container.id = "modal-container"; container.id = "modal-container";
container.setAttribute("role", "dialog"); container.setAttribute("role", "dialog");
container.appendChild(document.importNode(fragment, true)); container.appendChild(document.importNode(fragment, true));
document.body.appendChild(container); document.body.appendChild(container);
let closeButton = document.querySelector("button.btn-close-modal"); const closeButton = document.querySelector("button.btn-close-modal");
if (closeButton !== null) { if (closeButton !== null) {
closeButton.onclick = (event) => { closeButton.onclick = (event) => {
event.preventDefault(); event.preventDefault();
@ -89,7 +89,7 @@ class ModalHandler {
} }
static close() { static close() {
let container = this.getModalContainer(); const container = this.getModalContainer();
if (container !== null) { if (container !== null) {
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
} }

View file

@ -15,8 +15,8 @@ class TouchHandler {
calculateDistance() { calculateDistance() {
if (this.touch.start.x >= -1 && this.touch.move.x >= -1) { if (this.touch.start.x >= -1 && this.touch.move.x >= -1) {
let horizontalDistance = Math.abs(this.touch.move.x - this.touch.start.x); const horizontalDistance = Math.abs(this.touch.move.x - this.touch.start.x);
let verticalDistance = Math.abs(this.touch.move.y - this.touch.start.y); const verticalDistance = Math.abs(this.touch.move.y - this.touch.start.y);
if (horizontalDistance > 30 && verticalDistance < 70 || this.touch.moved) { if (horizontalDistance > 30 && verticalDistance < 70 || this.touch.moved) {
return this.touch.move.x - this.touch.start.x; return this.touch.move.x - this.touch.start.x;
@ -54,8 +54,8 @@ class TouchHandler {
this.touch.move.x = event.touches[0].clientX; this.touch.move.x = event.touches[0].clientX;
this.touch.move.y = event.touches[0].clientY; this.touch.move.y = event.touches[0].clientY;
let distance = this.calculateDistance(); const distance = this.calculateDistance();
let absDistance = Math.abs(distance); const absDistance = Math.abs(distance);
if (absDistance > 0) { if (absDistance > 0) {
this.touch.moved = true; this.touch.moved = true;
@ -78,7 +78,7 @@ class TouchHandler {
} }
if (this.touch.element !== null) { if (this.touch.element !== null) {
let absDistance = Math.abs(this.calculateDistance()); const absDistance = Math.abs(this.calculateDistance());
if (absDistance > 75) { if (absDistance > 75) {
toggleEntryStatus(this.touch.element); toggleEntryStatus(this.touch.element);
@ -118,9 +118,9 @@ class TouchHandler {
return; return;
} }
let distance = this.calculateDistance(); const distance = this.calculateDistance();
let absDistance = Math.abs(distance); const absDistance = Math.abs(distance);
let now = Date.now(); const now = Date.now();
if (now - this.touch.time <= 1000 && absDistance > 75) { if (now - this.touch.time <= 1000 && absDistance > 75) {
if (distance > 0) { if (distance > 0) {
@ -138,10 +138,10 @@ class TouchHandler {
return; return;
} }
let now = Date.now(); const now = Date.now();
if (this.touch.start.x !== -1 && now - this.touch.time <= 200) { if (this.touch.start.x !== -1 && now - this.touch.time <= 200) {
let innerWidthHalf = window.innerWidth / 2; const innerWidthHalf = window.innerWidth / 2;
if (this.touch.start.x >= innerWidthHalf && event.changedTouches[0].clientX >= innerWidthHalf) { if (this.touch.start.x >= innerWidthHalf && event.changedTouches[0].clientX >= innerWidthHalf) {
goToPage("next"); goToPage("next");
@ -158,19 +158,16 @@ class TouchHandler {
} }
listen() { listen() {
let hasPassiveOption = DomHelper.hasPassiveEventListenerOption(); const hasPassiveOption = DomHelper.hasPassiveEventListenerOption();
let elements = document.querySelectorAll(".entry-swipe"); document.querySelectorAll(".entry-swipe").forEach((element) => {
elements.forEach((element) => {
element.addEventListener("touchstart", (e) => this.onItemTouchStart(e), hasPassiveOption ? { passive: true } : false); element.addEventListener("touchstart", (e) => this.onItemTouchStart(e), hasPassiveOption ? { passive: true } : false);
element.addEventListener("touchmove", (e) => this.onItemTouchMove(e), hasPassiveOption ? { passive: false } : false); element.addEventListener("touchmove", (e) => this.onItemTouchMove(e), hasPassiveOption ? { passive: false } : false);
element.addEventListener("touchend", (e) => this.onItemTouchEnd(e), hasPassiveOption ? { passive: true } : false); element.addEventListener("touchend", (e) => this.onItemTouchEnd(e), hasPassiveOption ? { passive: true } : false);
element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false); element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false);
}); });
let element = document.querySelector(".entry-content"); const element = document.querySelector(".entry-content");
if (element) { if (element) {
if (element.classList.contains("gesture-nav-tap")) { if (element.classList.contains("gesture-nav-tap")) {
element.addEventListener("touchend", (e) => this.onTapEnd(e), hasPassiveOption ? { passive: true } : false); element.addEventListener("touchend", (e) => this.onTapEnd(e), hasPassiveOption ? { passive: true } : false);

View file

@ -5,7 +5,7 @@ class WebAuthnHandler {
static showErrorMessage(errorMessage) { static showErrorMessage(errorMessage) {
console.log("webauthn error: " + errorMessage); console.log("webauthn error: " + errorMessage);
let alertElement = document.getElementById("webauthn-error"); const alertElement = document.getElementById("webauthn-error");
if (alertElement) { if (alertElement) {
alertElement.textContent += " (" + errorMessage + ")"; alertElement.textContent += " (" + errorMessage + ")";
alertElement.classList.remove("hidden"); alertElement.classList.remove("hidden");
@ -79,14 +79,14 @@ class WebAuthnHandler {
return; return;
} }
let credentialCreationOptions = await registerBeginResponse.json(); const credentialCreationOptions = await registerBeginResponse.json();
credentialCreationOptions.publicKey.challenge = this.decodeBuffer(credentialCreationOptions.publicKey.challenge); credentialCreationOptions.publicKey.challenge = this.decodeBuffer(credentialCreationOptions.publicKey.challenge);
credentialCreationOptions.publicKey.user.id = this.decodeBuffer(credentialCreationOptions.publicKey.user.id); credentialCreationOptions.publicKey.user.id = this.decodeBuffer(credentialCreationOptions.publicKey.user.id);
if (Object.hasOwn(credentialCreationOptions.publicKey, 'excludeCredentials')) { if (Object.hasOwn(credentialCreationOptions.publicKey, 'excludeCredentials')) {
credentialCreationOptions.publicKey.excludeCredentials.forEach((credential) => credential.id = this.decodeBuffer(credential.id)); credentialCreationOptions.publicKey.excludeCredentials.forEach((credential) => credential.id = this.decodeBuffer(credential.id));
} }
let attestation = await navigator.credentials.create(credentialCreationOptions); const attestation = await navigator.credentials.create(credentialCreationOptions);
let registrationFinishResponse; let registrationFinishResponse;
try { try {
@ -108,7 +108,7 @@ class WebAuthnHandler {
throw new Error("Login failed with HTTP status code " + response.status); throw new Error("Login failed with HTTP status code " + response.status);
} }
let jsonData = await registrationFinishResponse.json(); const jsonData = await registrationFinishResponse.json();
window.location.href = jsonData.redirect; window.location.href = jsonData.redirect;
} }
@ -121,7 +121,7 @@ class WebAuthnHandler {
return; return;
} }
let credentialRequestOptions = await loginBeginResponse.json(); const credentialRequestOptions = await loginBeginResponse.json();
credentialRequestOptions.publicKey.challenge = this.decodeBuffer(credentialRequestOptions.publicKey.challenge); credentialRequestOptions.publicKey.challenge = this.decodeBuffer(credentialRequestOptions.publicKey.challenge);
if (Object.hasOwn(credentialRequestOptions.publicKey, 'allowCredentials')) { if (Object.hasOwn(credentialRequestOptions.publicKey, 'allowCredentials')) {