1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-11 17:51:01 +00:00

refactor(js): simplify some functions using modern JavaScript

This commit is contained in:
Frédéric Guillot 2025-08-02 13:59:58 -07:00
parent b116da85a9
commit bbe3c2ea71

View file

@ -451,30 +451,23 @@ function showKeyboardShortcuts() {
*/ */
function markPageAsRead() { function markPageAsRead() {
const items = getVisibleEntries(); const items = getVisibleEntries();
const entryIDs = []; if (items.length === 0) return;
items.forEach((element) => { const entryIDs = items.map((element) => {
element.classList.add("item-status-read"); element.classList.add("item-status-read");
entryIDs.push(parseInt(element.dataset.id, 10)); return parseInt(element.dataset.id, 10);
}); });
if (entryIDs.length > 0) { updateEntriesStatus(entryIDs, "read", () => {
updateEntriesStatus(entryIDs, "read", () => { const element = document.querySelector(":is(a, button)[data-action=markPageAsRead]");
// Make sure the Ajax request reach the server before we reload the page. const showOnlyUnread = element?.dataset.showOnlyUnread || false;
const element = document.querySelector(":is(a, button)[data-action=markPageAsRead]"); if (showOnlyUnread) {
let showOnlyUnread = false; window.location.reload();
if (element) { } else {
showOnlyUnread = element.dataset.showOnlyUnread || false; goToPage("next", true);
} }
});
if (showOnlyUnread) {
window.location.href = window.location.href;
} else {
goToPage("next", true);
}
});
}
} }
/** /**
@ -600,9 +593,9 @@ function updateEntriesStatus(entryIDs, status, callback) {
} }
if (status === "read") { if (status === "read") {
decrementUnreadCounter(count); updateUnreadCounterValue(-count);
} else { } else {
incrementUnreadCounter(count); updateUnreadCounterValue(count);
} }
}); });
}); });
@ -672,9 +665,7 @@ function handleBookmark(element) {
*/ */
function toggleBookmark(parentElement, toasting) { function toggleBookmark(parentElement, toasting) {
const buttonElement = parentElement.querySelector(":is(a, button)[data-toggle-bookmark]"); const buttonElement = parentElement.querySelector(":is(a, button)[data-toggle-bookmark]");
if (!buttonElement) { if (!buttonElement) return;
return;
}
buttonElement.textContent = ""; buttonElement.textContent = "";
insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading); insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading);
@ -683,20 +674,14 @@ function toggleBookmark(parentElement, toasting) {
request.withCallback(() => { request.withCallback(() => {
const currentStarStatus = buttonElement.dataset.value; const currentStarStatus = buttonElement.dataset.value;
const newStarStatus = currentStarStatus === "star" ? "unstar" : "star"; const newStarStatus = currentStarStatus === "star" ? "unstar" : "star";
const isStarred = currentStarStatus === "star";
let iconElement, label; const iconElement = document.querySelector(isStarred ? "template#icon-star" : "template#icon-unstar");
if (currentStarStatus === "star") { const label = isStarred ? buttonElement.dataset.labelStar : buttonElement.dataset.labelUnstar;
iconElement = document.querySelector("template#icon-star");
label = buttonElement.dataset.labelStar; if (toasting) {
if (toasting) { const toastKey = isStarred ? "toastUnstar" : "toastStar";
showToast(buttonElement.dataset.toastUnstar, iconElement); showToast(buttonElement.dataset[toastKey], iconElement);
}
} else {
iconElement = document.querySelector("template#icon-unstar");
label = buttonElement.dataset.labelUnstar;
if (toasting) {
showToast(buttonElement.dataset.toastStar, iconElement);
}
} }
buttonElement.replaceChildren(iconElement.content.cloneNode(true)); buttonElement.replaceChildren(iconElement.content.cloneNode(true));
@ -712,14 +697,10 @@ function toggleBookmark(parentElement, toasting) {
* @returns {void} * @returns {void}
*/ */
function handleFetchOriginalContent() { function handleFetchOriginalContent() {
if (isListView()) { if (isListView()) return;
return;
}
const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]"); const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]");
if (!buttonElement) { if (!buttonElement) return;
return;
}
const previousElement = buttonElement.cloneNode(true); const previousElement = buttonElement.cloneNode(true);
@ -732,7 +713,7 @@ function handleFetchOriginalContent() {
buttonElement.appendChild(previousElement); buttonElement.appendChild(previousElement);
response.json().then((data) => { response.json().then((data) => {
if (data.hasOwnProperty("content") && data.hasOwnProperty("reading_time")) { if (data.content && data.reading_time) {
document.querySelector(".entry-content").innerHTML = ttpolicy.createHTML(data.content); document.querySelector(".entry-content").innerHTML = ttpolicy.createHTML(data.content);
const entryReadingtimeElement = document.querySelector(".entry-reading-time"); const entryReadingtimeElement = document.querySelector(".entry-reading-time");
if (entryReadingtimeElement) { if (entryReadingtimeElement) {
@ -752,7 +733,7 @@ function handleFetchOriginalContent() {
*/ */
function openOriginalLink(openLinkInCurrentTab) { function openOriginalLink(openLinkInCurrentTab) {
const entryLink = document.querySelector(".entry h1 a"); const entryLink = document.querySelector(".entry h1 a");
if (entryLink !== null) { if (entryLink) {
if (openLinkInCurrentTab) { if (openLinkInCurrentTab) {
window.location.href = entryLink.getAttribute("href"); window.location.href = entryLink.getAttribute("href");
} else { } else {
@ -762,7 +743,7 @@ function openOriginalLink(openLinkInCurrentTab) {
} }
const currentItemOriginalLink = document.querySelector(".current-item :is(a, button)[data-original-link]"); const currentItemOriginalLink = document.querySelector(".current-item :is(a, button)[data-original-link]");
if (currentItemOriginalLink !== null) { if (currentItemOriginalLink) {
openNewTab(currentItemOriginalLink.getAttribute("href")); openNewTab(currentItemOriginalLink.getAttribute("href"));
const currentItem = document.querySelector(".current-item"); const currentItem = document.querySelector(".current-item");
@ -781,19 +762,13 @@ function openOriginalLink(openLinkInCurrentTab) {
* @returns {void} * @returns {void}
*/ */
function openCommentLink(openLinkInCurrentTab) { function openCommentLink(openLinkInCurrentTab) {
if (!isListView()) { const entryLink = document.querySelector(isListView() ? ".current-item :is(a, button)[data-comments-link]" : ":is(a, button)[data-comments-link]");
const entryLink = document.querySelector(":is(a, button)[data-comments-link]");
if (entryLink !== null) { if (entryLink) {
if (openLinkInCurrentTab) { if (openLinkInCurrentTab) {
window.location.href = entryLink.getAttribute("href"); window.location.href = entryLink.getAttribute("href");
} else { } else {
openNewTab(entryLink.getAttribute("href")); openNewTab(entryLink.getAttribute("href"));
}
}
} else {
const currentItemCommentsLink = document.querySelector(".current-item :is(a, button)[data-comments-link]");
if (currentItemCommentsLink !== null) {
openNewTab(currentItemCommentsLink.getAttribute("href"));
} }
} }
} }
@ -806,7 +781,7 @@ function openCommentLink(openLinkInCurrentTab) {
*/ */
function openSelectedItem() { function openSelectedItem() {
const currentItemLink = document.querySelector(".current-item .item-title a"); const currentItemLink = document.querySelector(".current-item .item-title a");
if (currentItemLink !== null) { if (currentItemLink) {
window.location.href = currentItemLink.getAttribute("href"); window.location.href = currentItemLink.getAttribute("href");
} }
} }
@ -815,17 +790,11 @@ function openSelectedItem() {
* Unsubscribe from the feed of the currently selected item. * Unsubscribe from the feed of the currently selected item.
*/ */
function unsubscribeFromFeed() { function unsubscribeFromFeed() {
const unsubscribeLinks = document.querySelectorAll("[data-action=remove-feed]"); const unsubscribeLink = document.querySelector("[data-action=remove-feed]");
if (unsubscribeLinks.length === 1) { if (unsubscribeLink) {
const unsubscribeLink = unsubscribeLinks[0];
const request = new RequestBuilder(unsubscribeLink.dataset.url); const request = new RequestBuilder(unsubscribeLink.dataset.url);
request.withCallback(() => { request.withCallback(() => {
if (unsubscribeLink.dataset.redirectUrl) { window.location.href = unsubscribeLink.dataset.redirectUrl || window.location.href;
window.location.href = unsubscribeLink.dataset.redirectUrl;
} else {
window.location.reload();
}
}); });
request.execute(); request.execute();
} }
@ -836,54 +805,26 @@ function unsubscribeFromFeed() {
*/ */
function scrollToCurrentItem() { function scrollToCurrentItem() {
const currentItem = document.querySelector(".current-item"); const currentItem = document.querySelector(".current-item");
if (currentItem !== null) { if (currentItem) {
scrollPageTo(currentItem, true); scrollPageTo(currentItem, true);
} }
} }
/**
* Decrement the unread counter by a specified amount.
*
* @param {number} n - The amount to decrement the counter by.
*/
function decrementUnreadCounter(n) {
updateUnreadCounterValue((current) => {
return current - n;
});
}
/**
* Increment the unread counter by a specified amount.
*
* @param {number} n - The amount to increment the counter by.
*/
function incrementUnreadCounter(n) {
updateUnreadCounterValue((current) => {
return current + n;
});
}
/** /**
* Update the unread counter value. * Update the unread counter value.
* *
* @param {function} callback - The function to call with the old value. * @param {number} delta - The amount to change the counter by.
*/ */
function updateUnreadCounterValue(callback) { function updateUnreadCounterValue(delta) {
document.querySelectorAll("span.unread-counter").forEach((element) => { document.querySelectorAll("span.unread-counter").forEach((element) => {
const oldValue = parseInt(element.textContent, 10); const oldValue = parseInt(element.textContent, 10);
element.textContent = callback(oldValue); element.textContent = oldValue + delta;
}); });
if (window.location.href.endsWith('/unread')) { if (window.location.href.endsWith('/unread')) {
const oldValue = parseInt(document.title.split('(')[1], 10); const oldValue = parseInt(document.title.split('(')[1], 10);
const newValue = callback(oldValue); const newValue = oldValue + delta;
document.title = document.title.replace(/(.*?)\(\d+\)(.*?)/, `$1(${newValue})$2`);
document.title = document.title.replace(
/(.*?)\(\d+\)(.*?)/,
function (match, prefix, suffix, offset, string) {
return prefix + '(' + newValue + ')' + suffix;
}
);
} }
} }
@ -970,9 +911,7 @@ function showToast(toastMessage, iconElement) {
const toastElementWrapper = document.getElementById("toast-wrapper"); const toastElementWrapper = document.getElementById("toast-wrapper");
toastElementWrapper.classList.remove('toast-animate'); toastElementWrapper.classList.remove('toast-animate');
setTimeout(() => { setTimeout(() => toastElementWrapper.classList.add('toast-animate'), 100);
toastElementWrapper.classList.add('toast-animate');
}, 100);
} }
/** /**