Fix checkmarks initialization
This commit is contained in:
parent
1500fcab43
commit
6b907f5079
2
js/chapter.min.js
vendored
2
js/chapter.min.js
vendored
File diff suppressed because one or more lines are too long
2
js/checkmarks.min.js
vendored
2
js/checkmarks.min.js
vendored
@ -1 +1 @@
|
|||||||
function fcn_toggleCheckmark(e,t=null,a=null){const r=window.FictioneerApp.Controllers.fictioneerCheckmarks;if(!r)return void fcn_showNotification("Error: Checkmarks Controller not connected.",3,"warning");const s=FcnUtils.userData();let i="story";if((e=parseInt(e??0))<1)return void fcn_showNotification("Error: Invalid story ID.",3,"warning");if(null!==t){if((t=parseInt(t))<1)return void fcn_showNotification("Error: Invalid chapter ID.",3,"warning");i="chapter"}s.checkmarks.data[e]||(s.checkmarks.data[e]=[]),null===a&&(a="chapter"===i?!s.checkmarks.data[e]?.includes(t):!s.checkmarks.data[e]?.includes(e));const c="chapter"===i?t:e;a?s.checkmarks.data[e].push(c):(FcnUtils.removeArrayItemOnce(s.checkmarks.data[e],c),"chapter"===i&&FcnUtils.removeArrayItemOnce(s.checkmarks.data[e],e)),s.checkmarks.data[e]=s.checkmarks.data[e].filter(((e,t,a)=>a.indexOf(e)==t)),s.lastLoaded=0,FcnUtils.setUserData(s),r.refreshView(),clearTimeout(r.timeout),r.timeout=setTimeout((()=>{FcnUtils.remoteAction("fictioneer_ajax_set_checkmark",{payload:{update:s.checkmarks.data[e].join(" "),story_id:e}})}),FcnGlobals.debounceRate)}application.register("fictioneer-checkmarks",class extends Stimulus.Controller{static get targets(){return["chapterCheck","storyCheck","ribbon"]}timeout=0;initialize(){fcn()?.userReady?this.#e=!0:document.addEventListener("fcnUserDataReady",(()=>{this.refreshView(),this.#e=!0,this.#t()}))}connect(){window.FictioneerApp.Controllers.fictioneerCheckmarks=this,this.#e&&(this.refreshView(),this.#t())}data(){return this.checkmarksCachedData=FcnUtils.userData().checkmarks?.data,Array.isArray(this.checkmarksCachedData)&&0===this.checkmarksCachedData.length&&(this.checkmarksCachedData={}),this.checkmarksCachedData}toggleChapter({params:{chapter:e,story:t}}){fcn_toggleCheckmark(t,e)}toggleStory({params:{story:e}}){fcn_toggleCheckmark(e)}clear(){const e=FcnUtils.userData();e.checkmarks={data:{},updated:Date.now()},fcn().setUserData(e),this.refreshView()}refreshView(){const e=this.data();!e||Object.keys(e).length<1?this.uncheckAll():Object.entries(e).forEach((([e,t])=>{e=parseInt(e);const a=t?.includes(e);this.hasChapterCheckTarget&&(a?this.chapterCheckTargets.forEach((e=>{e.classList.toggle("marked",!0),e.setAttribute("aria-checked",!0)})):this.chapterCheckTargets.forEach((a=>{const r=parseInt(a.dataset.fictioneerCheckmarksChapterParam);if(parseInt(a.dataset.fictioneerCheckmarksStoryParam)===e){const e=t?.includes(r);a.classList.toggle("marked",e),a.setAttribute("aria-checked",e)}}))),this.hasStoryCheckTarget&&(this.storyCheckTarget.classList.toggle("marked",a),this.storyCheckTarget.setAttribute("aria-checked",a)),this.hasRibbonTarget&&this.ribbonTarget.classList.toggle("hidden",!a)}))}uncheckAll(){this.hasChapterCheckTarget&&this.chapterCheckTargets.forEach((e=>{e.classList.toggle("marked",!1),e.setAttribute("aria-checked",!1)})),this.hasStoryCheckTarget&&(this.storyCheckTarget.classList.toggle("marked",!1),this.storyCheckTarget.setAttribute("aria-checked",!1))}#e=!1;#a=!1;#r(){const e=FcnUtils.loggedIn();return e||(this.#s(),this.#a=!0),e}#i(){return this.#r()&&JSON.stringify(this.checkmarksCachedData??0)!==JSON.stringify(this.data())}#c(){this.refreshInterval||(this.refreshInterval=setInterval((()=>{!this.#a&&this.#i()&&this.refreshView()}),3e4+1e3*Math.random()))}#t(){this.#c(),this.visibilityStateCheck=()=>{this.#r()&&("visible"===document.visibilityState?(this.#a=!1,this.refreshView(),this.#c()):(this.#a=!0,clearInterval(this.refreshInterval),this.refreshInterval=null))},document.addEventListener("visibilitychange",this.visibilityStateCheck)}#s(){clearInterval(this.refreshInterval),document.removeEventListener("visibilitychange",this.visibilityStateCheck)}});
|
function fcn_toggleCheckmark(e,t=null,a=null){const r=window.FictioneerApp.Controllers.fictioneerCheckmarks;if(!r)return void fcn_showNotification("Error: Checkmarks Controller not connected.",3,"warning");const s=FcnUtils.userData();let i="story";if((e=parseInt(e??0))<1)return void fcn_showNotification("Error: Invalid story ID.",3,"warning");if(null!==t){if((t=parseInt(t))<1)return void fcn_showNotification("Error: Invalid chapter ID.",3,"warning");i="chapter"}("object"!=typeof s.checkmarks.data||null===s.checkmarks.data||Array.isArray(s.checkmarks.data))&&(s.checkmarks.data={}),s.checkmarks.data[e]||(s.checkmarks.data[e]=[]),null===a&&(a="chapter"===i?!s.checkmarks.data[e]?.includes(t):!s.checkmarks.data[e]?.includes(e));const c="chapter"===i?t:e;a?s.checkmarks.data[e].push(c):(FcnUtils.removeArrayItemOnce(s.checkmarks.data[e],c),"chapter"===i&&FcnUtils.removeArrayItemOnce(s.checkmarks.data[e],e)),s.checkmarks.data[e]=s.checkmarks.data[e].filter(((e,t,a)=>a.indexOf(e)==t)),s.lastLoaded=0,FcnUtils.setUserData(s),r.refreshView(),clearTimeout(r.timeout),r.timeout=setTimeout((()=>{FcnUtils.remoteAction("fictioneer_ajax_set_checkmark",{payload:{update:s.checkmarks.data[e].join(" "),story_id:e}})}),FcnGlobals.debounceRate)}application.register("fictioneer-checkmarks",class extends Stimulus.Controller{static get targets(){return["chapterCheck","storyCheck","ribbon"]}timeout=0;initialize(){fcn()?.userReady?this.#e=!0:document.addEventListener("fcnUserDataReady",(()=>{this.refreshView(),this.#e=!0,this.#t()}))}connect(){window.FictioneerApp.Controllers.fictioneerCheckmarks=this,this.#e&&(this.refreshView(),this.#t())}data(){return this.checkmarksCachedData=FcnUtils.userData().checkmarks?.data,Array.isArray(this.checkmarksCachedData)&&0===this.checkmarksCachedData.length&&(this.checkmarksCachedData={}),this.checkmarksCachedData}toggleChapter({params:{chapter:e,story:t}}){fcn_toggleCheckmark(t,e)}toggleStory({params:{story:e}}){fcn_toggleCheckmark(e)}clear(){const e=FcnUtils.userData();e.checkmarks={data:{},updated:Date.now()},fcn().setUserData(e),this.refreshView()}refreshView(){const e=this.data();!e||Object.keys(e).length<1?this.uncheckAll():Object.entries(e).forEach((([e,t])=>{e=parseInt(e);const a=t?.includes(e);this.hasChapterCheckTarget&&(a?this.chapterCheckTargets.forEach((e=>{e.classList.toggle("marked",!0),e.setAttribute("aria-checked",!0)})):this.chapterCheckTargets.forEach((a=>{const r=parseInt(a.dataset.fictioneerCheckmarksChapterParam);if(parseInt(a.dataset.fictioneerCheckmarksStoryParam)===e){const e=t?.includes(r);a.classList.toggle("marked",e),a.setAttribute("aria-checked",e)}}))),this.hasStoryCheckTarget&&(this.storyCheckTarget.classList.toggle("marked",a),this.storyCheckTarget.setAttribute("aria-checked",a)),this.hasRibbonTarget&&this.ribbonTarget.classList.toggle("hidden",!a)}))}uncheckAll(){this.hasChapterCheckTarget&&this.chapterCheckTargets.forEach((e=>{e.classList.toggle("marked",!1),e.setAttribute("aria-checked",!1)})),this.hasStoryCheckTarget&&(this.storyCheckTarget.classList.toggle("marked",!1),this.storyCheckTarget.setAttribute("aria-checked",!1))}#e=!1;#a=!1;#r(){const e=FcnUtils.loggedIn();return e||(this.#s(),this.#a=!0),e}#i(){return this.#r()&&JSON.stringify(this.checkmarksCachedData??0)!==JSON.stringify(this.data())}#c(){this.refreshInterval||(this.refreshInterval=setInterval((()=>{!this.#a&&this.#i()&&this.refreshView()}),3e4+1e3*Math.random()))}#t(){this.#c(),this.visibilityStateCheck=()=>{this.#r()&&("visible"===document.visibilityState?(this.#a=!1,this.refreshView(),this.#c()):(this.#a=!0,clearInterval(this.refreshInterval),this.refreshInterval=null))},document.addEventListener("visibilitychange",this.visibilityStateCheck)}#s(){clearInterval(this.refreshInterval),document.removeEventListener("visibilitychange",this.visibilityStateCheck)}});
|
6
js/complete.min.js
vendored
6
js/complete.min.js
vendored
File diff suppressed because one or more lines are too long
@ -269,11 +269,11 @@ application.register('fictioneer-chapter', class extends Stimulus.Controller {
|
|||||||
this.progressBar.style.width = `${p}%`;
|
this.progressBar.style.width = `${p}%`;
|
||||||
|
|
||||||
// If end of chapter has been reached and the user is logged in...
|
// If end of chapter has been reached and the user is logged in...
|
||||||
if (!this.checkmarkUpdated && !!this.storyIdValue && p >= 100 && FcnUtils.loggedIn()) {
|
if (!this.checkmarkUpdated && this.hasStoryIdValue && !!this.storyIdValue && p >= 100 && FcnUtils.loggedIn()) {
|
||||||
this.checkmarkUpdated = true;
|
this.checkmarkUpdated = true;
|
||||||
|
|
||||||
// Make sure necessary data is available
|
// Make sure necessary data is available
|
||||||
if (!this.chapterIdValue || typeof fcn_toggleCheckmark !== 'function') {
|
if (!this.hasChapterIdValue || !this.chapterIdValue || typeof fcn_toggleCheckmark !== 'function') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,6 +222,14 @@ function fcn_toggleCheckmark(storyId, chapterId = null, set = null) {
|
|||||||
type = 'chapter';
|
type = 'chapter';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof userData.checkmarks.data !== 'object' ||
|
||||||
|
userData.checkmarks.data === null ||
|
||||||
|
Array.isArray(userData.checkmarks.data)
|
||||||
|
) {
|
||||||
|
userData.checkmarks.data = {};
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize if story is not yet tracked
|
// Initialize if story is not yet tracked
|
||||||
if (!userData.checkmarks.data[storyId]) {
|
if (!userData.checkmarks.data[storyId]) {
|
||||||
userData.checkmarks.data[storyId] = [];
|
userData.checkmarks.data[storyId] = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user