Cleaned up follows.js
This commit is contained in:
parent
90c1f4921d
commit
682ed8b136
2
js/follows.min.js
vendored
2
js/follows.min.js
vendored
@ -1 +1 @@
|
||||
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()}))}));
|
||||
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(){const 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));const 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){const 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(),clearTimeout(fcn_userFollowsTimeout),fcn_userFollowsTimeout=setTimeout((()=>{fcn_ajaxPost({action:"fictioneer_ajax_toggle_follow",story_id:o,set:fcn_follows.data.hasOwnProperty(o)}).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(){fcn_followsMenuItem.classList.contains("_loaded")||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()}))}));
|
@ -40,7 +40,7 @@ function fcn_initializeFollows() {
|
||||
|
||||
function fcn_getFollows() {
|
||||
// Get JSON string from local storage
|
||||
let f = localStorage.getItem('fcnStoryFollows');
|
||||
const f = localStorage.getItem('fcnStoryFollows');
|
||||
|
||||
// Parse and return JSON string if valid, otherwise return new JSON
|
||||
return (f && fcn_isValidJSONString(f)) ? JSON.parse(f) : { 'lastLoaded': 0, 'data': {}, 'seen': Date.now(), 'new': false };
|
||||
@ -138,7 +138,7 @@ function fcn_updateFollowsView() {
|
||||
localStorage.setItem('fcnStoryFollows', JSON.stringify(fcn_follows));
|
||||
|
||||
// Set "new" marker if there are new items
|
||||
let isNew = parseInt(fcn_follows['new']) > 0;
|
||||
const isNew = parseInt(fcn_follows['new']) > 0;
|
||||
|
||||
_$$('.mark-follows-read, .follows-alert-number, .mobile-menu-button').forEach(element => {
|
||||
element.classList.toggle('_new', isNew);
|
||||
@ -162,7 +162,7 @@ function fcn_updateFollowsView() {
|
||||
|
||||
function fcn_toggleFollow(storyId) {
|
||||
// Check local storage for outside changes
|
||||
let currentFollows = fcn_getFollows();
|
||||
const currentFollows = fcn_getFollows();
|
||||
|
||||
// Clear cached bookshelf content (if any)
|
||||
localStorage.removeItem('fcnBookshelfContent');
|
||||
@ -191,19 +191,16 @@ function fcn_toggleFollow(storyId) {
|
||||
// Update view and local storage
|
||||
fcn_updateFollowsView();
|
||||
|
||||
// Payload
|
||||
let payload = {
|
||||
'action': 'fictioneer_ajax_toggle_follow',
|
||||
'story_id': storyId,
|
||||
'set': fcn_follows.data.hasOwnProperty(storyId)
|
||||
}
|
||||
|
||||
// Clear previous timeout (if still pending)
|
||||
clearTimeout(fcn_userFollowsTimeout);
|
||||
|
||||
// Update in database; only one request every n seconds
|
||||
fcn_userFollowsTimeout = setTimeout(() => {
|
||||
fcn_ajaxPost(payload)
|
||||
fcn_ajaxPost({
|
||||
'action': 'fictioneer_ajax_toggle_follow',
|
||||
'story_id': storyId,
|
||||
'set': fcn_follows.data.hasOwnProperty(storyId)
|
||||
})
|
||||
.then((response) => {
|
||||
// Check for failure
|
||||
if (response.data.error) {
|
||||
@ -232,13 +229,10 @@ function fcn_setupFollowsHTML() {
|
||||
// Abort if already loaded
|
||||
if (fcn_followsMenuItem.classList.contains('_loaded')) return;
|
||||
|
||||
// Payload
|
||||
let payload = {
|
||||
'action': 'fictioneer_ajax_get_follows_notifications'
|
||||
}
|
||||
|
||||
// Request
|
||||
fcn_ajaxGet(payload)
|
||||
fcn_ajaxGet({
|
||||
'action': 'fictioneer_ajax_get_follows_notifications'
|
||||
})
|
||||
.then((response) => {
|
||||
// Any Follows HTML retrieved?
|
||||
if (response.data.html) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user