fictioneer/js/follows.min.js
Tetrakern c35ea2a5ad Refactored mobile menu
That. Was. Not. Fun!
2023-06-11 01:17:04 +02:00

1 line
3.9 KiB
JavaScript

const fcn_desktopFollowList=_$$$("follow-menu-scroll"),fcn_mobileFollowList=_$$$("mobile-menu-follows-list"),fcn_followsMenuItem=_$$$("follow-menu-button");var fcn_userFollowsTimeout,fcn_follows;function fcn_initializeFollows(){fcn_follows=fcn_getFollows(),fcn_fetchFollowsFromDatabase()}function fcn_getFollows(){let o=localStorage.getItem("fcnStoryFollows");return o&&fcn_isValidJSONString(o)?JSON.parse(o):{lastLoaded:0,data:{},seen:Date.now(),new:!1}}function fcn_fetchFollowsFromDatabase(){fcn_ajaxLimitThreshold<fcn_follows.lastLoaded?fcn_updateFollowsView():fcn_ajaxGet({action:"fictioneer_ajax_get_follows"}).then((o=>{if(o.success){let t=o.data.follows;t=fcn_isValidJSONString(t)?t:"{}",t=JSON.parse(t),(fcn_follows="object"==typeof t&&t.data&&Object.keys(t.data).length>0?t:{data:{},seen:Date.now(),new:!1}).lastLoaded=Date.now()}})).catch((()=>{localStorage.removeItem("fcnStoryFollows"),fcn_follows=!1})).then((()=>{fcn_updateFollowsView(),localStorage.removeItem("fcnBookshelfContent")}))}function fcn_updateFollowsView(){if(!fcn_follows)return;_$$(".button-follow-story").forEach((o=>{o.classList.toggle("_followed",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))})),_$$(".card").forEach((o=>{o.classList.toggle("has-follow",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))})),localStorage.setItem("fcnStoryFollows",JSON.stringify(fcn_follows));let o=parseInt(fcn_follows.new)>0;_$$(".mark-follows-read, .follows-alert-number, .mobile-menu-button").forEach((t=>{t.classList.toggle("_new",o),o>0&&(t.dataset.newCount=fcn_follows.new)}))}function fcn_toggleFollow(o){let t=fcn_getFollows();if(localStorage.removeItem("fcnBookshelfContent"),JSON.stringify(fcn_follows.data[o])!==JSON.stringify(t.data[o]))return fcn_follows=t,fcn_showNotification(__("Follows re-synchronized.","fictioneer")),void fcn_updateFollowsView();(fcn_follows=t).data.hasOwnProperty(o)?delete fcn_follows.data[o]:fcn_follows.data[o]={timestamp:Date.now()},fcn_follows.lastLoaded=0,fcn_updateFollowsView();let e={action:"fictioneer_ajax_toggle_follow",story_id:o,set:fcn_follows.data.hasOwnProperty(o)};clearTimeout(fcn_userFollowsTimeout),fcn_userFollowsTimeout=setTimeout((()=>{fcn_ajaxPost(e).then((o=>{o.data.error&&fcn_showNotification(o.data.error,5,"warning")})).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")}))}),fictioneer_ajax.post_debounce_rate)}function fcn_setupFollowsHTML(){if(fcn_followsMenuItem.classList.contains("_loaded"))return;fcn_ajaxGet({action:"fictioneer_ajax_get_follows_notifications"}).then((o=>{o.data.html&&(fcn_desktopFollowList.innerHTML=o.data.html,fcn_mobileFollowList.innerHTML=o.data.html)})).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning"),fcn_desktopFollowList.remove(),fcn_mobileFollowList.remove()})).then((()=>{fcn_followsMenuItem.classList.add("_loaded")}))}function fcn_markFollowsRead(){fcn_followsMenuItem.classList.contains("_new")&&fcn_followsMenuItem.classList.contains("_loaded")&&(_$$(".mark-follows-read, .follows-alert-number, .follow-item, .mobile-menu-button").forEach((o=>{o.classList.remove("_new")})),fcn_follows.new=0,fcn_follows.lastLoaded=0,localStorage.setItem("fcnStoryFollows",JSON.stringify(fcn_follows)),fcn_ajaxPost({action:"fictioneer_ajax_mark_follows_read"}).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")})))}fcn_isLoggedIn&&fcn_initializeFollows(),fcn_followsMenuItem?.addEventListener("mouseover",(()=>{fcn_setupFollowsHTML()}),{once:!0}),fcn_followsMenuItem?.addEventListener("focus",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$('.mobile-menu__frame-button[data-frame-target="follows"]')?.addEventListener("click",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$$(".button-follow-story").forEach((o=>{o.addEventListener("click",(o=>{fcn_toggleFollow(o.currentTarget.dataset.storyId)}))})),_$$(".mark-follows-read").forEach((o=>{o.addEventListener("click",(()=>{fcn_markFollowsRead()}))}));