mirror of https://gitgud.io/fatchan/jschan.git
commit
2cbab576a7
10 changed files with 113 additions and 12 deletions
After Width: | Height: | Size: 6.2 KiB |
After Width: | Height: | Size: 648 B |
@ -0,0 +1,92 @@ |
||||
window.addEventListener('DOMContentLoaded', () => { |
||||
|
||||
let loading = {}; |
||||
|
||||
const hideOmitted = (e) => { |
||||
e.target.nextSibling.style.display = 'unset'; |
||||
const thread = e.target.closest('.thread'); |
||||
let replies = Array.from(thread.querySelectorAll('.post-container:not(.op)')); |
||||
if (e.target.dataset.shown > 0) { |
||||
replies = replies.slice(0, -parseInt(e.target.dataset.shown)); |
||||
} |
||||
replies.forEach(r => { |
||||
r.previousSibling.remove(); |
||||
r.remove(); |
||||
}); |
||||
e.target.removeAttribute('data-open'); |
||||
e.target.src = '/file/plus.png'; |
||||
}; |
||||
|
||||
const expandOmitted = async (e) => { |
||||
const threadId = e.target.dataset.thread; |
||||
const board = e.target.dataset.board; |
||||
const parentPost = e.target.closest('.post-container'); |
||||
const firstPreviewReply = parentPost.nextSibling && parentPost.nextSibling.nextSibling; //the first preview reply
|
||||
const jsonPath = `/${board}/thread/${threadId}.json`; |
||||
let hovercache = localStorage.getItem(`hovercache-${jsonPath}`); |
||||
let replies; |
||||
if (hovercache) { |
||||
hovercache = JSON.parse(hovercache); |
||||
//if we have the first preview reply in cache, the cache is fresh enough to show the omitted posts
|
||||
if (firstPreviewReply && hovercache.replies.find(r => r.postId == firstPreviewReply.dataset.postId)) { |
||||
replies = hovercache.replies; |
||||
} |
||||
//note: will always fetch (on new page load) for sticky preview replies = 0
|
||||
} |
||||
if (!replies) { |
||||
e.target.style.cursor = 'wait'; |
||||
e.target.classList.add('spin'); |
||||
let json; |
||||
try { |
||||
//same as hovering post replies
|
||||
if (!loading[jsonPath]) { |
||||
loading[jsonPath] = fetch(jsonPath).then(res => res.json()); |
||||
} |
||||
json = await loading[jsonPath]; |
||||
} catch (e) { |
||||
return console.error(e); |
||||
} finally { |
||||
e.target.style.cursor = ''; |
||||
e.target.classList.remove('spin'); |
||||
} |
||||
if (json) { |
||||
setLocalStorage(`hovercache-${jsonPath}`, JSON.stringify(json)); |
||||
hoverCacheList.value = Object.keys(localStorage).filter(k => k.startsWith('hovercache')); |
||||
replies = json.replies; |
||||
} else { |
||||
return localStorage.removeItem(`hovercache-${jsonPath}`); //thread deleted
|
||||
} |
||||
} |
||||
if (!replies) { |
||||
return; |
||||
} |
||||
e.target.nextSibling.style.display = 'none'; |
||||
e.target.src = '/file/minus.png'; |
||||
e.target.dataset.open = true; |
||||
replies = replies.reverse(); |
||||
if (firstPreviewReply) { |
||||
replies = replies.filter(r => r.postId < firstPreviewReply.dataset.postId); |
||||
} |
||||
replies.forEach(r => { |
||||
newPost(r, { |
||||
insertPoint: firstPreviewReply ? parentPost.nextSibling : parentPost, |
||||
insertPosition: firstPreviewReply ? 'beforebegin' : 'afterend', |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
const handleExpandClick = (e) => { |
||||
if (e.target.dataset.open) { |
||||
hideOmitted(e); |
||||
} else { |
||||
expandOmitted(e); |
||||
} |
||||
}; |
||||
|
||||
const expandOmittedButtons = document.getElementsByClassName('expand-omitted'); |
||||
|
||||
for (let i = 0; i < expandOmittedButtons.length; i++) { |
||||
expandOmittedButtons[i].addEventListener('click', handleExpandClick, false); |
||||
} |
||||
|
||||
}); |
Loading…
Reference in new issue