Fix regressions introduced in previous JS refactoring

This commit is contained in:
Jebbs 2019-07-26 10:02:39 +08:00 committed by Frédéric Guillot
parent a634839678
commit 17aae72583
5 changed files with 45 additions and 51 deletions

File diff suppressed because one or more lines are too long

View file

@ -611,7 +611,7 @@ a.button {
overflow-x: hidden;
}
.hide-read-items .item-status-read {
.hide-read-items .item-status-read:not(.current-item) {
display: none;
}

View file

@ -59,16 +59,16 @@ let searchInputElement=document.getElementById("search-input");if(searchInputEle
function showKeyboardShortcuts(){let template=document.getElementById("keyboard-shortcuts");if(template!==null){ModalHandler.open(template.content);}}
function markPageAsRead(){let items=DomHelper.getVisibleElements(".items .item");let entryIDs=[];items.forEach((element)=>{element.classList.add("item-status-read");entryIDs.push(parseInt(element.dataset.id,10));});if(entryIDs.length>0){updateEntriesStatus(entryIDs,"read",()=>{let element=document.querySelector("a[data-action=markPageAsRead]");let showOnlyUnread=false;if(element){showOnlyUnread=element.dataset.showOnlyUnread||false;}
if(showOnlyUnread){window.location.reload();}else{goToPage("next",true);}});}}
function handleEntryStatus(){if(isListView()){let currentItem=document.querySelector(".current-item");if(currentItem!==null){goToNextListItem();toggleEntryStatus(currentItem);}}else{toggleEntryStatus(document.querySelector(".entry"));}}
function handleEntryStatus(element){let currentEntry=findEntry(element);if(currentEntry){toggleEntryStatus(currentEntry);if(isListView()&&currentEntry.classList.contains('current-item')){goToNextListItem();}}}
function toggleEntryStatus(element){let entryID=parseInt(element.dataset.id,10);let link=element.querySelector("a[data-toggle-status]");let currentStatus=link.dataset.value;let newStatus=currentStatus==="read"?"unread":"read";updateEntriesStatus([entryID],newStatus);if(currentStatus==="read"){link.innerHTML=link.dataset.labelRead;link.dataset.value="unread";}else{link.innerHTML=link.dataset.labelUnread;link.dataset.value="read";}
if(element.classList.contains("item-status-"+currentStatus)){element.classList.remove("item-status-"+currentStatus);element.classList.add("item-status-"+newStatus);}}
function markEntryAsRead(element){if(element.classList.contains("item-status-unread")){element.classList.remove("item-status-unread");element.classList.add("item-status-read");let entryID=parseInt(element.dataset.id,10);updateEntriesStatus([entryID],"read");}}
function updateEntriesStatus(entryIDs,status,callback){let url=document.body.dataset.entriesStatusUrl;let request=new RequestBuilder(url);request.withBody({entry_ids:entryIDs,status:status});request.withCallback(callback);request.execute();if(status==="read"){decrementUnreadCounter(1);}else{incrementUnreadCounter(1);}}
function handleSaveEntry(){if(isListView()){let currentItem=document.querySelector(".current-item");if(currentItem!==null){saveEntry(currentItem.querySelector("a[data-save-entry]"));}}else{saveEntry(document.querySelector("a[data-save-entry]"));}}
function handleSaveEntry(element){let currentEntry=findEntry(element);if(currentEntry){saveEntry(currentEntry.querySelector("a[data-save-entry]"));}}
function saveEntry(element){if(!element){return;}
if(element.dataset.completed){return;}
element.innerHTML=element.dataset.labelLoading;let request=new RequestBuilder(element.dataset.saveUrl);request.withCallback(()=>{element.innerHTML=element.dataset.labelDone;element.dataset.completed=true;});request.execute();}
function handleBookmark(){if(isListView()){let currentItem=document.querySelector(".current-item");if(currentItem!==null){toggleBookmark(currentItem);}}else{toggleBookmark(document.querySelector(".entry"));}}
function handleBookmark(element){let currentEntry=findEntry(element);if(currentEntry){toggleBookmark(currentEntry);}}
function toggleBookmark(parentElement){let element=parentElement.querySelector("a[data-toggle-bookmark]");if(!element){return;}
element.innerHTML=element.dataset.labelLoading;let request=new RequestBuilder(element.dataset.bookmarkUrl);request.withCallback(()=>{if(element.dataset.value==="star"){element.innerHTML=element.dataset.labelStar;element.dataset.value="unstar";}else{element.innerHTML=element.dataset.labelUnstar;element.dataset.value="star";}});request.execute();}
function handleFetchOriginalContent(){if(isListView()){return;}
@ -96,15 +96,15 @@ function incrementUnreadCounter(n){updateUnreadCounterValue((current)=>{return c
function updateUnreadCounterValue(callback){let counterElements=document.querySelectorAll("span.unread-counter");counterElements.forEach((element)=>{let oldValue=parseInt(element.textContent,10);element.innerHTML=callback(oldValue);});if(window.location.href.endsWith('/unread')){let oldValue=parseInt(document.title.split('(')[1],10);let newValue=callback(oldValue);document.title=document.title.replace(/(.*?)\(\d+\)(.*?)/,function(match,prefix,suffix,offset,string){return prefix+'('+newValue+')'+suffix;});}}
function isEntry(){return document.querySelector("section.entry")!==null;}
function isListView(){return document.querySelector(".items")!==null;}
function findEntry(element){if(isListView()){if(element){return DomHelper.findParent(element,"item");}else{return document.querySelector(".current-item");}}else{return document.querySelector(".entry");}}
function handleConfirmationMessage(linkElement,callback){linkElement.style.display="none";let containerElement=linkElement.parentNode;let questionElement=document.createElement("span");let yesElement=document.createElement("a");yesElement.href="#";yesElement.appendChild(document.createTextNode(linkElement.dataset.labelYes));yesElement.onclick=(event)=>{event.preventDefault();let loadingElement=document.createElement("span");loadingElement.className="loading";loadingElement.appendChild(document.createTextNode(linkElement.dataset.labelLoading));questionElement.remove();containerElement.appendChild(loadingElement);callback(linkElement.dataset.url,linkElement.dataset.redirectUrl);};let noElement=document.createElement("a");noElement.href="#";noElement.appendChild(document.createTextNode(linkElement.dataset.labelNo));noElement.onclick=(event)=>{event.preventDefault();linkElement.style.display="inline";questionElement.remove();};questionElement.className="confirm";questionElement.appendChild(document.createTextNode(linkElement.dataset.labelQuestion+" "));questionElement.appendChild(yesElement);questionElement.appendChild(document.createTextNode(", "));questionElement.appendChild(noElement);containerElement.appendChild(questionElement);}
document.addEventListener("DOMContentLoaded",function(){handleSubmitButtons();if(!document.querySelector("body[data-disable-keyboard-shortcuts=true]")){let keyboardHandler=new KeyboardHandler();keyboardHandler.on("g u",()=>goToPage("unread"));keyboardHandler.on("g b",()=>goToPage("starred"));keyboardHandler.on("g h",()=>goToPage("history"));keyboardHandler.on("g f",()=>goToFeedOrFeeds());keyboardHandler.on("g c",()=>goToPage("categories"));keyboardHandler.on("g s",()=>goToPage("settings"));keyboardHandler.on("ArrowLeft",()=>goToPrevious());keyboardHandler.on("ArrowRight",()=>goToNext());keyboardHandler.on("k",()=>goToPrevious());keyboardHandler.on("p",()=>goToPrevious());keyboardHandler.on("j",()=>goToNext());keyboardHandler.on("n",()=>goToNext());keyboardHandler.on("h",()=>goToPage("previous"));keyboardHandler.on("l",()=>goToPage("next"));keyboardHandler.on("o",()=>openSelectedItem());keyboardHandler.on("v",()=>openOriginalLink());keyboardHandler.on("m",()=>handleEntryStatus());keyboardHandler.on("A",()=>markPageAsRead());keyboardHandler.on("s",()=>handleSaveEntry());keyboardHandler.on("d",()=>handleFetchOriginalContent());keyboardHandler.on("f",()=>handleBookmark());keyboardHandler.on("?",()=>showKeyboardShortcuts());keyboardHandler.on("#",()=>unsubscribeFromFeed());keyboardHandler.on("/",(e)=>setFocusToSearchInput(e));keyboardHandler.on("Escape",()=>ModalHandler.close());keyboardHandler.listen();}
let touchHandler=new TouchHandler();touchHandler.listen();onClick("a[data-save-entry]",()=>handleSaveEntry());onClick("a[data-toggle-bookmark]",()=>handleBookmark());onClick("a[data-fetch-content-entry]",()=>handleFetchOriginalContent());onClick("a[data-action=search]",(event)=>setFocusToSearchInput(event));onClick("a[data-action=markPageAsRead]",()=>handleConfirmationMessage(event.target,()=>markPageAsRead()));onClick("a[data-toggle-status]",(event)=>{let currentItem=DomHelper.findParent(event.target,"entry");if(!currentItem){currentItem=DomHelper.findParent(event.target,"item");}
if(currentItem){toggleEntryStatus(currentItem);}});onClick("a[data-confirm]",(event)=>handleConfirmationMessage(event.target,(url,redirectURL)=>{let request=new RequestBuilder(url);request.withCallback(()=>{if(redirectURL){window.location.href=redirectURL;}else{window.location.reload();}});request.execute();}));if(document.documentElement.clientWidth<600){onClick(".logo",()=>toggleMainMenu());onClick(".header nav li",(event)=>onClickMainMenuListItem(event));}
let touchHandler=new TouchHandler();touchHandler.listen();onClick("a[data-save-entry]",(event)=>handleSaveEntry(event.target));onClick("a[data-toggle-bookmark]",(event)=>handleBookmark(event.target));onClick("a[data-fetch-content-entry]",()=>handleFetchOriginalContent());onClick("a[data-action=search]",(event)=>setFocusToSearchInput(event));onClick("a[data-action=markPageAsRead]",()=>handleConfirmationMessage(event.target,()=>markPageAsRead()));onClick("a[data-toggle-status]",(event)=>handleEntryStatus(event.target));onClick("a[data-confirm]",(event)=>handleConfirmationMessage(event.target,(url,redirectURL)=>{let request=new RequestBuilder(url);request.withCallback(()=>{if(redirectURL){window.location.href=redirectURL;}else{window.location.reload();}});request.execute();}));if(document.documentElement.clientWidth<600){onClick(".logo",()=>toggleMainMenu());onClick(".header nav li",(event)=>onClickMainMenuListItem(event));}
if("serviceWorker"in navigator){let scriptElement=document.getElementById("service-worker-script");if(scriptElement){navigator.serviceWorker.register(scriptElement.src);}}});})();`,
"sw": `'use strict';self.addEventListener("fetch",(event)=>{if(event.request.url.includes("/feed/icon/")){event.respondWith(caches.open("feed_icons").then((cache)=>{return cache.match(event.request).then((response)=>{return response||fetch(event.request).then((response)=>{cache.put(event.request,response.clone());return response;});});}));}});`,
}
var JavascriptsChecksums = map[string]string{
"app": "7220905bf67e3283f975da171548d2622160d52ba07ed17933a7c38f41d83caa",
"app": "4d95eed76ba60a01f0366ef36b399d2b25f8a553941236f262b9d62e0fe66688",
"sw": "55fffa223919cc18572788fb9c62fccf92166c0eb5d3a1d6f91c31f24d020be9",
}

View file

@ -115,17 +115,17 @@ function markPageAsRead() {
}
// Handle entry status changes from the list view and entry view.
function handleEntryStatus() {
if (isListView()) {
let currentItem = document.querySelector(".current-item");
if (currentItem !== null) {
// The order is important here,
// On the unread page, the read item will be hidden.
function handleEntryStatus(element) {
let currentEntry = findEntry(element);
if (currentEntry) {
// The order is important here,
// On the unread page, the read item will be hidden.
// If "goToNextListItem" first, it may go to an item about to hide:
// Imagine that user click 'mark as read' right below the '.current-item'
toggleEntryStatus(currentEntry);
if (isListView() && currentEntry.classList.contains('current-item')) {
goToNextListItem();
toggleEntryStatus(currentItem);
}
} else {
toggleEntryStatus(document.querySelector(".entry"));
}
}
@ -180,14 +180,10 @@ function updateEntriesStatus(entryIDs, status, callback) {
}
// Handle save entry from list view and entry view.
function handleSaveEntry() {
if (isListView()) {
let currentItem = document.querySelector(".current-item");
if (currentItem !== null) {
saveEntry(currentItem.querySelector("a[data-save-entry]"));
}
} else {
saveEntry(document.querySelector("a[data-save-entry]"));
function handleSaveEntry(element) {
let currentEntry = findEntry(element);
if (currentEntry) {
saveEntry(currentEntry.querySelector("a[data-save-entry]"));
}
}
@ -212,14 +208,10 @@ function saveEntry(element) {
}
// Handle bookmark from the list view and entry view.
function handleBookmark() {
if (isListView()) {
let currentItem = document.querySelector(".current-item");
if (currentItem !== null) {
toggleBookmark(currentItem);
}
} else {
toggleBookmark(document.querySelector(".entry"));
function handleBookmark(element) {
let currentEntry = findEntry(element);
if (currentEntry) {
toggleBookmark(currentEntry);
}
}
@ -454,6 +446,18 @@ function isListView() {
return document.querySelector(".items") !== null;
}
function findEntry(element) {
if (isListView()) {
if (element) {
return DomHelper.findParent(element, "item");
} else {
return document.querySelector(".current-item");
}
} else {
return document.querySelector(".entry");
}
}
function handleConfirmationMessage(linkElement, callback) {
linkElement.style.display = "none";

View file

@ -34,22 +34,12 @@ document.addEventListener("DOMContentLoaded", function () {
let touchHandler = new TouchHandler();
touchHandler.listen();
onClick("a[data-save-entry]", () => handleSaveEntry());
onClick("a[data-toggle-bookmark]", () => handleBookmark());
onClick("a[data-save-entry]", (event) => handleSaveEntry(event.target));
onClick("a[data-toggle-bookmark]", (event) => handleBookmark(event.target));
onClick("a[data-fetch-content-entry]", () => handleFetchOriginalContent());
onClick("a[data-action=search]", (event) => setFocusToSearchInput(event));
onClick("a[data-action=markPageAsRead]", () => handleConfirmationMessage(event.target, () => markPageAsRead()));
onClick("a[data-toggle-status]", (event) => {
let currentItem = DomHelper.findParent(event.target, "entry");
if (!currentItem) {
currentItem = DomHelper.findParent(event.target, "item");
}
if (currentItem) {
toggleEntryStatus(currentItem);
}
});
onClick("a[data-toggle-status]", (event) => handleEntryStatus(event.target));
onClick("a[data-confirm]", (event) => handleConfirmationMessage(event.target, (url, redirectURL) => {
let request = new RequestBuilder(url);