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();
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 b", () => goToPage("starred"));
keyboardHandler.on("g h", () => goToPage("history"));
@ -48,7 +48,7 @@ document.addEventListener("DOMContentLoaded", () => {
keyboardHandler.listen();
}
let touchHandler = new TouchHandler();
const touchHandler = new TouchHandler();
touchHandler.listen();
if (WebAuthnHandler.isWebAuthnSupported()) {
@ -56,7 +56,7 @@ document.addEventListener("DOMContentLoaded", () => {
onClick("#webauthn-delete", () => { webauthnHandler.removeAllCredentials(); });
let registerButton = document.getElementById("webauthn-register");
const registerButton = document.getElementById("webauthn-register");
if (registerButton != null) {
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) {
const abortController = new AbortController();
loginButton.disabled = false;
onClick("#webauthn-login", () => {
let usernameField = document.getElementById("form-username");
const usernameField = document.getElementById("form-username");
if (usernameField != null) {
abortController.abort();
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-toggle-status]", (event) => handleEntryStatus("next", event.target));
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) => {
if (redirectURL) {
@ -127,7 +127,7 @@ document.addEventListener("DOMContentLoaded", () => {
onClick(".header nav li", (event) => onClickMainMenuListItem(event));
if ("serviceWorker" in navigator) {
let scriptElement = document.getElementById("service-worker-script");
const scriptElement = document.getElementById("service-worker-script");
if (scriptElement) {
navigator.serviceWorker.register(scriptElement.src);
}

View file

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

View file

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

View file

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

View file

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

View file

@ -5,7 +5,7 @@ class WebAuthnHandler {
static showErrorMessage(errorMessage) {
console.log("webauthn error: " + errorMessage);
let alertElement = document.getElementById("webauthn-error");
const alertElement = document.getElementById("webauthn-error");
if (alertElement) {
alertElement.textContent += " (" + errorMessage + ")";
alertElement.classList.remove("hidden");
@ -79,14 +79,14 @@ class WebAuthnHandler {
return;
}
let credentialCreationOptions = await registerBeginResponse.json();
const credentialCreationOptions = await registerBeginResponse.json();
credentialCreationOptions.publicKey.challenge = this.decodeBuffer(credentialCreationOptions.publicKey.challenge);
credentialCreationOptions.publicKey.user.id = this.decodeBuffer(credentialCreationOptions.publicKey.user.id);
if (Object.hasOwn(credentialCreationOptions.publicKey, 'excludeCredentials')) {
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;
try {
@ -108,7 +108,7 @@ class WebAuthnHandler {
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;
}
@ -121,7 +121,7 @@ class WebAuthnHandler {
return;
}
let credentialRequestOptions = await loginBeginResponse.json();
const credentialRequestOptions = await loginBeginResponse.json();
credentialRequestOptions.publicKey.challenge = this.decodeBuffer(credentialRequestOptions.publicKey.challenge);
if (Object.hasOwn(credentialRequestOptions.publicKey, 'allowCredentials')) {