1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
const fcn_jumpToBookmarkButtons=_$$(".button--bookmark"),fcn_mobileBookmarkJump=_$$$("mobile-menu-bookmark-jump"),fcn_mobileBookmarkList=_$(".mobile-menu__bookmark-list"),fcn_mobileBookmarkTemplate=_$("#mobile-bookmark-template"),fcn_bookmarksSmallCardBlock=_$(".bookmarks-block"),fcn_bookmarksSmallCardTemplate=_$(".bookmark-small-card-template");var fcn_userBookmarksTimeout,fcn_bookmarks=fcn_getBookmarks();function fcn_saveBookmarksForUser(o){if(!fcn_isLoggedIn)return;fcn_bookmarks.lastLoaded=0,localStorage.setItem("fcnChapterBookmarks",JSON.stringify(fcn_bookmarks));const e={action:"fictioneer_ajax_save_bookmarks",bookmarks:o};clearTimeout(fcn_userBookmarksTimeout),fcn_userBookmarksTimeout=setTimeout((()=>{fcn_ajaxPost(e).then((o=>{o.data.error&&fcn_showNotification(o.data.error,3,"warning")})).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,3,"warning")}))}),fictioneer_ajax.post_debounce_rate)}function fcn_getBookmarksForUser(){fcn_isLoggedIn&&(fcn_ajaxLimitThreshold<fcn_bookmarks.lastLoaded?Object.keys(fcn_bookmarks.data).length>0&&fcn_updateBookmarkCards():fcn_ajaxGet({action:"fictioneer_ajax_get_bookmarks"}).then((o=>{if(o.success){let e=o.data.bookmarks;e=fcn_isValidJSONString(e)?e:"{}",e=JSON.parse(e),Object.keys(e).length>2&&(e={}),"object"==typeof e&&e.data&&(e.lastLoaded=Date.now(),fcn_setBookmarks(e,!0),fcn_showChapterBookmark())}fcn_updateBookmarkCards()})))}function fcn_getBookmarks(){let o=localStorage.getItem("fcnChapterBookmarks");o=o&&fcn_isValidJSONString(o)?JSON.parse(o):{lastLoaded:0,data:{}},fcn_setBookmarks(o,!0),fcn_isLoggedIn||fcn_updateBookmarkCards();const e=_$(".profile-bookmarks-stats");return e&&(e.innerHTML=e.innerHTML.replace("%s",Object.keys(o.data).length)),Object.keys(o.data).length>0&&_$$(".icon-menu-bookmarks").forEach((o=>{o.classList.remove("hidden")})),o}function fcn_setBookmarks(o,e=!1){"object"==typeof o&&(fcn_bookmarks=o,localStorage.setItem("fcnChapterBookmarks",JSON.stringify(o)),e||(window.clearTimeout(fcn_userBookmarksTimeout),fcn_saveBookmarksForUser(JSON.stringify(o))))}function fcn_toggleBookmark(o,e="none"){fcn_bookmarks=fcn_getBookmarks();const a=_$(".chapter__article"),r=_$(".current-bookmark");if(!a)return;const t=fcn_bookmarks.data[a.id];if(t&&t["paragraph-id"]==o&&r)"none"!=e&&e!=t.color?(_$(".current-bookmark").dataset.bookmarkColor=e,t.color=e):fcn_removeBookmark(a.id);else{Object.keys(fcn_bookmarks.data).length>=50&&fcn_removeBookmark(Object.keys(fcn_bookmarks.data)[0]);const t=_$(`[data-paragraph-id="${o}"]`),k=_$$$("chapter-bookmark-data").dataset;fcn_bookmarks.data[a.id]={"paragraph-id":o,progress:100*(fcn_offset(t).top-fcn_offset(t.parentElement).top)/t.parentElement.clientHeight,date:new Date,color:e,chapter:k.title.trim(),link:k.link,thumb:k.thumb,image:k.image,story:k.storyTitle.trim(),content:t.querySelector("span").innerHTML.substring(0,128)+"…"},fcn_jumpToBookmarkButtons.forEach((o=>{o.classList.remove("hidden")})),fcn_mobileBookmarkJump?.removeAttribute("hidden"),r?.classList.remove("current-bookmark"),t.classList.add("current-bookmark"),t.setAttribute("data-bookmark-color",e)}fcn_setMobileMenuBookmarks(),fcn_setBookmarks(fcn_bookmarks)}function fcn_showChapterBookmark(){_$(".current-bookmark")?.classList.remove("current-bookmark");const o=_$(".chapter__article");if(!o||!fcn_bookmarks.data[o.id])return;const e=fcn_bookmarks.data[o.id]["paragraph-id"],a=_$(`[data-paragraph-id="${e}"]`),r=fcn_bookmarks.data[o.id].color??"none";e&&a&&(fcn_jumpToBookmarkButtons.forEach((o=>{o.classList.remove("hidden")})),fcn_mobileBookmarkJump?.removeAttribute("hidden"),a.classList.add("current-bookmark"),a.setAttribute("data-bookmark-color",r))}function fcn_setMobileMenuBookmarks(){if(fcn_mobileBookmarkList.innerHTML="",Object.keys(fcn_bookmarks.data).length>0)Object.entries(fcn_bookmarks.data).forEach((o=>{const e=fcn_mobileBookmarkTemplate.content.cloneNode(!0);e.querySelector(".mobile-menu__bookmark").classList.add(`bookmark-${o[0]}`),e.querySelector(".mobile-menu__bookmark").dataset.color=o[1].color,e.querySelector(".mobile-menu__bookmark-progress > div > div").style.width=`${o[1].progress.toFixed(1)}%`,e.querySelector(".mobile-menu__bookmark a").href=`${o[1].link}#paragraph-${o[1]["paragraph-id"]}`,e.querySelector(".mobile-menu__bookmark a span").innerText=o[1].chapter,e.querySelector(".mobile-menu-bookmark-delete-button").setAttribute("data-bookmark-id",`${o[0]}`),fcn_mobileBookmarkList.appendChild(e)})),fcn_bookmarkDeleteHandler(_$$(".mobile-menu-bookmark-delete-button"));else{const o=document.createElement("li");o.classList.add("no-bookmarks"),o.appendChild(document.createTextNode(fcn_mobileBookmarkList.dataset.empty)),fcn_mobileBookmarkList.appendChild(o)}}function fcn_updateBookmarkCards(){if(!fcn_bookmarks||!fcn_bookmarksSmallCardBlock||!fcn_bookmarksSmallCardTemplate||Object.keys(fcn_bookmarks.data).length<1)return;fcn_bookmarksSmallCardBlock.classList.remove("hidden"),_$(".bookmarks-block__no-bookmarks")?.remove(),_$$(".show-if-bookmarks").forEach((o=>{o.classList.remove("hidden")}));let o=fcn_bookmarksSmallCardBlock.dataset.count;Object.entries(fcn_bookmarks.data).forEach((e=>{if(o>-1&&o--<1)return;const a=fcn_bookmarksSmallCardTemplate.content.cloneNode(!0),r=new Date(e[1].date);""!==e[1].image?(a.querySelector(".bookmark-card__image").href=e[1].image,a.querySelector(".bookmark-card__image img").src=`${e[1].thumb}`):a.querySelector(".bookmark-card__image").remove(),a.querySelector(".bookmark-card__excerpt").innerHTML+=e[1].content,a.querySelector(".bookmark-card").classList.add(`bookmark-${e[0]}`),a.querySelector(".bookmark-card").dataset.color=e[1].color,a.querySelector(".bookmark-card__title > a").href=`${e[1].link}#paragraph-${e[1]["paragraph-id"]}`,a.querySelector(".bookmark-card__title > a").innerText=e[1].chapter,a.querySelector(".bookmark-card__percentage").innerText=`${e[1].progress.toFixed(1)} %`,a.querySelector(".bookmark-card__progress").style.width=`${e[1].progress.toFixed(1)}%`,a.querySelector("time").innerText=`${r.toLocaleDateString("en-US",{year:"2-digit",month:"short",day:"numeric"})}`,a.querySelector(".button-delete-bookmark").setAttribute("data-bookmark-id",`${e[0]}`),fcn_bookmarksSmallCardBlock.querySelector("ul").appendChild(a)})),fcn_bookmarkDeleteHandler(_$$(".button-delete-bookmark"))}function fcn_bookmarkDeleteHandler(o){("object"==typeof o?o:[o]).forEach((o=>{o.addEventListener("click",(o=>{fcn_removeBookmark(o.currentTarget.dataset.bookmarkId),fcn_setBookmarks(fcn_bookmarks),Object.keys(fcn_bookmarks.data).length<1&&(_$(".bookmarks-block")?.classList.add("hidden"),_$$(".show-if-bookmarks").forEach((o=>{o.classList.add("hidden")})))}))}))}function fcn_removeBookmark(o){const e=_$(".chapter__article"),a=_$(".current-bookmark");delete fcn_bookmarks.data[o],e&&e.id==o&&(fcn_jumpToBookmarkButtons.forEach((o=>{o.classList.add("hidden")})),fcn_mobileBookmarkJump?.setAttribute("hidden",!0),a&&(a.classList.remove("current-bookmark"),a.removeAttribute("data-bookmark-color"))),_$$(`.bookmark-${o}`)?.forEach((o=>{o.remove()}))}fcn_getBookmarksForUser(),fcn_showChapterBookmark(),fcn_jumpToBookmarkButtons.forEach((o=>{o.addEventListener("click",(()=>{fcn_scrollTo(_$(`[data-paragraph-id="${fcn_bookmarks.data[_$("article").id]["paragraph-id"]}"]`))}))})); |