Clean up ajax-bookshelf.js
This commit is contained in:
parent
b71cd494b5
commit
81ee31c9fe
2
js/ajax-bookshelf.min.js
vendored
2
js/ajax-bookshelf.min.js
vendored
@ -1 +1 @@
|
||||
const fcn_bookshelfTarget=_$$$("ajax-bookshelf-target");function fcn_getBookshelfContent(){let e=sessionStorage.getItem("fcnBookshelfContent");return e&&fcn_isValidJSONString(e)?JSON.parse(e):{html:{},count:{}}}function fcn_updateBookshelfView(e=null,t=null,o=null,n=!1){let a=fcn_getBookshelfContent(),s=(e=e??fcn_bookshelfTarget.dataset.action)+(t=t??fcn_bookshelfTarget.dataset.page)+(o=o??fcn_bookshelfTarget.dataset.order);if(!a.hasOwnProperty("timestamp")||a.timestamp+6e4<Date.now())return sessionStorage.removeItem("fcnBookshelfContent"),a={html:{},count:{}},void fcn_fetchBookshelfPart(e,t,o,n);a.hasOwnProperty("html")&&a.html.hasOwnProperty(s)?(fcn_bookshelfTarget.innerHTML=a.html[s],fcn_bookshelfTarget.classList.remove("ajax-in-progress"),fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${a.count[e]})`,n&&_$$$("main").scrollIntoView({behavior:"smooth"})):fcn_fetchBookshelfPart(e,t,o,n)}function fcn_browseBookshelfPage(e){fcn_bookshelfTarget.classList.add("ajax-in-progress"),fcn_updateBookshelfView(null,e,null,!0);let t=fcn_buildUrl({tab:fcn_bookshelfTarget.dataset.tab,pg:e,order:fcn_bookshelfTarget.dataset.order});history.pushState({},"",t.href)}function fcn_fetchBookshelfPart(e,t,o,n=!1){let a=fcn_getBookshelfContent(),s=e+t+o;fcn_ajaxGet({action:e,page:t,order:o}).then((o=>{o.success?(a.timestamp=Date.now(),a.html[s]=o.data.html,a.count[e]=o.data.count,sessionStorage.setItem("fcnBookshelfContent",JSON.stringify(a)),fcn_bookshelfTarget.innerHTML=o.data.html,fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${o.data.count})`):(fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(o.data.error)))})).catch((e=>{_$(".item-number").innerHTML="",fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`))})).then((()=>{fcn_bookshelfTarget.classList.remove("ajax-in-progress"),n&&_$$$("main").scrollIntoView({behavior:"smooth"})}))}fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_updateBookshelfView()})):fcn_updateBookshelfView();
|
||||
const fcn_bookshelfTarget=_$$$("ajax-bookshelf-target");function fcn_getBookshelfContent(){const e=sessionStorage.getItem("fcnBookshelfContent");return e&&fcn_isValidJSONString(e)?JSON.parse(e):{html:{},count:{}}}function fcn_updateBookshelfView(e=null,t=null,o=null,n=!1){let a=fcn_getBookshelfContent();const s=(e=e??fcn_bookshelfTarget.dataset.action)+(t=t??fcn_bookshelfTarget.dataset.page)+(o=o??fcn_bookshelfTarget.dataset.order);if(!a.hasOwnProperty("timestamp")||a.timestamp+6e4<Date.now())return sessionStorage.removeItem("fcnBookshelfContent"),a={html:{},count:{}},void fcn_fetchBookshelfPart(e,t,o,n);a.hasOwnProperty("html")&&a.html.hasOwnProperty(s)?(fcn_bookshelfTarget.innerHTML=a.html[s],fcn_bookshelfTarget.classList.remove("ajax-in-progress"),fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${a.count[e]})`,n&&_$$$("main").scrollIntoView({behavior:"smooth"})):fcn_fetchBookshelfPart(e,t,o,n)}function fcn_browseBookshelfPage(e){fcn_bookshelfTarget.classList.add("ajax-in-progress"),fcn_updateBookshelfView(null,e,null,!0),history.pushState({},"",fcn_buildUrl({tab:fcn_bookshelfTarget.dataset.tab,pg:e,order:fcn_bookshelfTarget.dataset.order}).href)}function fcn_fetchBookshelfPart(e,t,o,n=!1){const a=e+t+o,s=fcn_getBookshelfContent();fcn_ajaxGet({action:e,page:t,order:o}).then((o=>{o.success?(s.timestamp=Date.now(),s.html[a]=o.data.html,s.count[e]=o.data.count,sessionStorage.setItem("fcnBookshelfContent",JSON.stringify(s)),fcn_bookshelfTarget.innerHTML=o.data.html,fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${o.data.count})`):(fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(o.data.error)))})).catch((e=>{_$(".item-number").innerHTML="",fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`))})).then((()=>{fcn_bookshelfTarget.classList.remove("ajax-in-progress"),n&&_$$$("main").scrollIntoView({behavior:"smooth"})}))}fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_updateBookshelfView()})):fcn_updateBookshelfView();
|
@ -26,7 +26,7 @@ if (fcn_theRoot.dataset.ajaxNonce && !_$$$('fictioneer-ajax-nonce')) {
|
||||
|
||||
function fcn_getBookshelfContent() {
|
||||
// Get JSON string from session storage
|
||||
let c = sessionStorage.getItem('fcnBookshelfContent');
|
||||
const c = sessionStorage.getItem('fcnBookshelfContent');
|
||||
|
||||
// Parse and return JSON string if valid, otherwise return new JSON
|
||||
return (c && fcn_isValidJSONString(c)) ? JSON.parse(c) : { html: {}, count: {} };
|
||||
@ -36,13 +36,26 @@ function fcn_getBookshelfContent() {
|
||||
// UPDATE VIEW
|
||||
// =============================================================================
|
||||
|
||||
function fcn_updateBookshelfView(action = null, page = null, order = null, update = false) {
|
||||
/**
|
||||
* Update the bookshelf view with fetched/cached content.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param {string|null} action - The action to perform (default: null).
|
||||
* @param {string|null} page - The page to display (default: null).
|
||||
* @param {string|null} order - The order of the bookshelf (default: null).
|
||||
* @param {boolean} scroll - Whether to scroll to the content (default: false).
|
||||
*/
|
||||
|
||||
function fcn_updateBookshelfView(action = null, page = null, order = null, scroll = false) {
|
||||
// Setup
|
||||
let fcn_bookshelfStorage = fcn_getBookshelfContent();
|
||||
|
||||
action = action ?? fcn_bookshelfTarget.dataset.action,
|
||||
page = page ?? fcn_bookshelfTarget.dataset.page,
|
||||
order = order ?? fcn_bookshelfTarget.dataset.order;
|
||||
let htmlKey = action + page + order;
|
||||
|
||||
const htmlKey = action + page + order;
|
||||
|
||||
// Storage item valid for 60 seconds
|
||||
if (
|
||||
@ -51,7 +64,7 @@ function fcn_updateBookshelfView(action = null, page = null, order = null, updat
|
||||
) {
|
||||
sessionStorage.removeItem('fcnBookshelfContent');
|
||||
fcn_bookshelfStorage = { html: {}, count: {} };
|
||||
fcn_fetchBookshelfPart(action, page, order, update);
|
||||
fcn_fetchBookshelfPart(action, page, order, scroll);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -64,9 +77,9 @@ function fcn_updateBookshelfView(action = null, page = null, order = null, updat
|
||||
fcn_bookshelfTarget.classList.remove('ajax-in-progress');
|
||||
fcn_bookshelfTarget.dataset.page = page;
|
||||
_$('.item-number').innerHTML = `(${fcn_bookshelfStorage['count'][action]})`;
|
||||
if (update) _$$$('main').scrollIntoView({behavior: 'smooth'});
|
||||
if (scroll) _$$$('main').scrollIntoView({behavior: 'smooth'});
|
||||
} else {
|
||||
fcn_fetchBookshelfPart(action, page, order, update);
|
||||
fcn_fetchBookshelfPart(action, page, order, scroll);
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,6 +87,14 @@ function fcn_updateBookshelfView(action = null, page = null, order = null, updat
|
||||
// CHANGE PAGE
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Change current bookshelf page.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param {string} page - The page to browse to.
|
||||
*/
|
||||
|
||||
function fcn_browseBookshelfPage(page) {
|
||||
// Indicate loading
|
||||
fcn_bookshelfTarget.classList.add('ajax-in-progress');
|
||||
@ -82,23 +103,34 @@ function fcn_browseBookshelfPage(page) {
|
||||
fcn_updateBookshelfView(null, page, null, true);
|
||||
|
||||
// Update URL
|
||||
let url = fcn_buildUrl({
|
||||
tab: fcn_bookshelfTarget.dataset.tab,
|
||||
pg: page,
|
||||
order: fcn_bookshelfTarget.dataset.order
|
||||
});
|
||||
|
||||
history.pushState({}, '', url.href);
|
||||
history.pushState(
|
||||
{},
|
||||
'',
|
||||
fcn_buildUrl({
|
||||
tab: fcn_bookshelfTarget.dataset.tab,
|
||||
pg: page,
|
||||
order: fcn_bookshelfTarget.dataset.order
|
||||
}).href
|
||||
);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// REQUEST BOOKSHELF VIA AJAX
|
||||
// =============================================================================
|
||||
|
||||
function fcn_fetchBookshelfPart(action, page, order, update = false) {
|
||||
/**
|
||||
* Fetch a part of the bookshelf via AJAX.
|
||||
*
|
||||
* @param {string} action - The action to perform.
|
||||
* @param {string} page - The page to retrieve.
|
||||
* @param {string} order - The order of the bookshelf.
|
||||
* @param {boolean} [scroll=false] - Whether to scroll to the top after rendering.
|
||||
*/
|
||||
|
||||
function fcn_fetchBookshelfPart(action, page, order, scroll = false) {
|
||||
// Setup
|
||||
let fcn_bookshelfStorage = fcn_getBookshelfContent(),
|
||||
htmlKey = action + page + order;
|
||||
const htmlKey = action + page + order,
|
||||
fcn_bookshelfStorage = fcn_getBookshelfContent();
|
||||
|
||||
// Request
|
||||
fcn_ajaxGet({
|
||||
@ -135,6 +167,6 @@ function fcn_fetchBookshelfPart(action, page, order, update = false) {
|
||||
.then(() => {
|
||||
// Regardless of outcome
|
||||
fcn_bookshelfTarget.classList.remove('ajax-in-progress');
|
||||
if (update) _$$$('main').scrollIntoView({behavior: 'smooth'});
|
||||
if (scroll) _$$$('main').scrollIntoView({behavior: 'smooth'});
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user