HummingBirdAnimeClient/public/js/scripts.min.js

38 lines
9.2 KiB
JavaScript
Raw Permalink Normal View History

"serviceWorker"in navigator&&navigator.serviceWorker.register("/sw.js").then(e=>{console.log("Service worker registered",e.scope)}).catch(e=>{console.error("Failed to register service worker",e)});let e=(t,n)=>{let o=(t.document||t.ownerDocument).querySelectorAll(n),r=e.length;for(;--r>=0&&o.item(r)!==t;);return r>-1},t={noop:()=>{},$(e,t=null){if("string"!=typeof e)return e;t=null!==t&&1===t.nodeType?t:document;let n=[];return e.match(/^#([\w]+$)/)?n.push(document.getElementById(e.split("#")[1])):n=[].slice.apply(t.querySelectorAll(e)),n},hasElement:e=>t.$(e).length>0,scrollToTop(){t.$("header")[0].scrollIntoView(!0)},hide(e){"string"==typeof e&&(e=t.$(e)),Array.isArray(e)?e.forEach(e=>e.setAttribute("hidden","hidden")):e.setAttribute("hidden","hidden")},show(e){"string"==typeof e&&(e=t.$(e)),Array.isArray(e)?e.forEach(e=>e.removeAttribute("hidden")):e.removeAttribute("hidden")},showMessage(e,n){let o=`<div class='message ${e}'>
<span class='icon'></span>
${n}
<span class='close'></span>
</div>`,r=t.$(".message");void 0!==r[0]&&r[0].remove(),t.$("header")[0].insertAdjacentHTML("beforeend",o)},closestParent(t,n){if(void 0!==Element.prototype.closest)return t.closest(n);for(;t!==document.documentElement;){if(e(t,n))return t;t=t.parentElement}return null},url:e=>`//${document.location.host}`+("/"===e.charAt(0)?e:`/${e}`),throttle(e,t,n){let o=!1;return function(...r){o||(t.apply(n||this,r),o=!0,setTimeout(function(){o=!1},e))}}};function n(e,t,o){t.match(/^([\w\-]+)$/)||t.split(" ").forEach(t=>{n(e,t,o)}),e.addEventListener(t,o,!1)}function o(e){let t=[];return Object.keys(e).forEach(n=>{let o=e[n].toString();n=encodeURIComponent(n),o=encodeURIComponent(o),t.push(`${n}=${o}`)}),t.join("&")}t.on=(e,o,r,a)=>{void 0===a?(a=r,t.$(e).forEach(e=>{n(e,o,a)})):t.$(e).forEach(e=>{var s;s=a,n(e,o,n=>{t.$(r,e).forEach(e=>{n.target==e&&(s.call(e,n),n.stopPropagation())})})})},t.ajax=(e,n)=>{n={data:{},type:"GET",dataType:"",success:t.noop,mimeType:"application/x-www-form-urlencoded",error:t.noop,...n};let r=new XMLHttpRequest,a=String(n.type).toUpperCase();return"GET"===a&&(e+=e.match(/\?/)?o(n.data):`?${o(n.data)}`),r.open(a,e),r.onreadystatechange=()=>{if(4===r.readyState){let e="";e="json"===r.responseType?JSON.parse(r.responseText):r.responseText,r.status>299?n.error.call(null,r.status,e,r.response):n.success.call(null,e,r.status)}},"json"===n.dataType?(n.data=JSON.stringify(n.data),n.mimeType="application/json"):n.data=o(n.data),r.setRequestHeader("Content-Type",n.mimeType),"GET"===a?r.send(null):r.send(n.data),r},t.get=(e,n,o=null)=>(null===o&&(o=n,n={}),t.ajax(e,{data:n,success:o}));t.on("header","click",".message",function(e){t.hide(e.target)}),t.on("form.js-delete","submit",function(t){!1===confirm("Are you ABSOLUTELY SURE you want to delete this item?")&&(t.preventDefault(),t.stopPropagation())}),t.on(".js-clear-cache","click",function(){t.get("/cache_purge",()=>{t.showMessage("success","Successfully purged api cache")})}),t.on(".vertical-tabs input","change",function(t){let e=t.currentTarget.parentElement.getBoundingClientRect().top+window.pageYOffset;window.scrollTo({top:e,behavior:"smooth"})}),t.on(".media-filter","input",function(e){let i=e.target.value,n=RegExp(i,"i");""!==i?(t.$("article.media").forEach(e=>{let i=String(t.$(".name a",e)[0].textContent).trim();n.test(i)?t.show(e):t.hide(e)}),t.$("table.media-wrap tbody tr").forEach(e=>{let i=t.$("td.align-left",e)[0];n.test(String(t.$("a",i)[0].textContent).trim())||n.test(String(i.textContent).trim())?t.show(e):t.hide(e)})):(t.show("article.media"),t.show("table.media-wrap tbody tr"))});(()=>{let i=null,n=null;void 0!==document.hidden?(i="hidden",n="visibilitychange"):void 0!==document.msHidden?(i="msHidden",n="msvisibilitychange"):void 0!==document.webkitHidden&&(i="webkitHidden",n="webkitvisibilitychange"),null===i?console.info("Page visibility API not supported, JS session check will not work"):document.addEventListener(n,function t1(){document[i]||t.get("/heartbeat",e=>{!0!==JSON.parse(e).hasAuth&&(document.removeEventListener(n,t1,!1),location.reload())})},!1)})();t.on("main","change",".big-check",a=>{let i=a.target.id;document.getElementById(`mal_${i}`).checked=!0,document.getElementById(`anilist_${i}`).checked=!0});function renderSearchResults(a,i,e=!1){return i.map(i=>{let l=i.titles.join("<br />"),s=null!==i.libraryEntry?"disabled":"",t=function(a,i,e=!1){return e||null===i.libraryEntry?"":`
<div class="row">
<span class="edit"><big>[ Already in List ]</big></span>
</div>
<div class="row">
<span class="edit">
<a class="bracketed" href="/${a}/edit/${i.libraryEntry.id}/${i.libraryEntry.status}">Edit</a>
</span>
</div>
<div class="row"><span class="edit">&nbsp;</span></div>
`}(a,i,e);return e&&(s=""),`
<article class="media search ${s}">
<div class="name">
<input type="radio" class="mal-check" id="anilist_${i.slug}" name="anilist_id" value="${i.anilist_id}" ${s} />
<input type="radio" class="mal-check" id="mal_${i.slug}" name="mal_id" value="${i.mal_id}" ${s} />
<input type="radio" class="big-check" id="${i.slug}" name="id" value="${i.id}" ${s} />
<label for="${i.slug}">
<img src="${i.coverImage}" alt="" width="220" />
<span class="name">
${i.canonicalTitle}<br />
<small>${l}</small>
</span>
</label>
</div>
<div class="table">
${t}
<div class="row">
<span class="edit">
<a class="bracketed" href="/${a}/details/${i.slug}">Info Page</a>
</span>
</div>
</div>
</article>
`}).join("")}function e1(e,t="."){return e.replace(/\[(.*?)]/g,t+"$1")}function hasNestedProperty(t,r){if(t&&"object"==typeof t){if("string"==typeof r&&""!==r)return(r=e1(r)).split(".").reduce((e,t,r,n)=>r===n.length-1?!!(e&&e.hasOwnProperty(t)):e&&e[t],t);if("number"==typeof r)return r in t}return!1}function getNestedProperty(t,r,n="."){if(isType("string",r)&&""!==r){let o=(r=e1(r,n)).split(n);try{return o.reduce((e,t)=>e[t],t)}catch(e){return}}return null}function getType(e){return Array.isArray(e)?"array":"object"!=typeof e?(typeof e).toLowerCase():(function(){return Object.prototype.toString.call(this).slice(8,-1)}).call(e).toLowerCase()}function isType(e,t){return getType(t)===String(e).toLowerCase()}let s=(a,r=!1)=>(t.show(".cssload-loader"),t.get(t.url("/anime-collection/search"),{query:a},(a,s)=>{a=JSON.parse(a),t.hide(".cssload-loader"),t.$("#series-list")[0].innerHTML=renderSearchResults("anime",a,r)}));if(t.hasElement(".anime #search")){let t1=null;t.on("#search","input",t.throttle(250,e=>{let a=encodeURIComponent(e.target.value);""!==a&&(null!==t1&&t1.abort(),t1=s(a))}))}if(t.hasElement("#search-anime-collection")){let t1=null;t.on("#search-anime-collection","input",t.throttle(250,e=>{let a=encodeURIComponent(e.target.value);""!==a&&(null!==t1&&t1.abort(),t1=s(a,!0))}))}t.on("body.anime.list","click",".plus-one",t1=>{let s=t.closestParent(t1.target,"article"),n=parseInt(t.$(".completed_number",s)[0].textContent,10)||0,l=parseInt(t.$(".total_number",s)[0].textContent,10),o=t.$(".name a",s)[0].textContent,i={id:s.dataset.kitsuId,anilist_id:s.dataset.anilistId,mal_id:s.dataset.malId,data:{progress:n+1}},d=(t1,a)=>{t.hide("#loading-shadow"),t.showMessage(t1,`${a} ${o}`),t.scrollToTop()},c=()=>d("error","Failed to update");(isNaN(n)||0===n)&&(i.data.status="CURRENT"),!isNaN(n)&&n+1===l&&(i.data.status="COMPLETED"),t.show("#loading-shadow"),t.ajax(t.url("/anime/increment"),{data:i,dataType:"json",type:"POST",success:t1=>{try{let l=JSON.parse(t1),o=getNestedProperty(l,"data.libraryEntry.update.libraryEntry.progress");if(hasNestedProperty(l,"error")||o!==i.data.progress){c();return}if("COMPLETED"===getNestedProperty(l,"data.libraryEntry.update.libraryEntry.status")){t.hide(s),d("success","Completed");return}t.$(".completed_number",s)[0].textContent=++n,d("success","Updated")}catch(e){c()}},error:c})});let r=e=>(t.show(".cssload-loader"),t.get(t.url("/manga/search"),{query:e},(e,s)=>{e=JSON.parse(e),t.hide(".cssload-loader"),t.$("#series-list")[0].innerHTML=renderSearchResults("manga",e)}));if(t.hasElement(".manga #search")){let a=null;t.on("#search","input",t.throttle(250,t=>{let e=encodeURIComponent(t.target.value);""!==e&&(null!==a&&a.abort(),a=r(e))}))}t.on(".manga.list","click",".edit-buttons button",a=>{let r=a.target,n=t.closestParent(a.target,"article"),o=r.classList.contains("plus-one-chapter")?"chapter":"volume",l=parseInt(t.$(`.${o}s_read`,n)[0].textContent,10)||0,i=parseInt(t.$(`.${o}_count`,n)[0].textContent,10),d=t.$(".name",n)[0].textContent;isNaN(l)&&(l=0);let p={id:n.dataset.kitsuId,anilist_id:n.dataset.anilistId,mal_id:n.dataset.malId,data:{progress:l}},c=(a,e)=>{t.hide("#loading-shadow"),t.showMessage(a,`${e} ${d}`),t.scrollToTop()},m=()=>c("error","Failed to update");(isNaN(l)||0===l)&&(p.data.status="CURRENT"),!isNaN(l)&&l+1===i&&(p.data.status="COMPLETED"),p.data.progress=++l,t.show("#loading-shadow"),t.ajax(t.url("/manga/increment"),{data:p,dataType:"json",type:"POST",mimeType:"application/json",success:a=>{try{let r=JSON.parse(a),i=getNestedProperty(r,"data.libraryEntry.update.libraryEntry.progress");if(hasNestedProperty(r,"error")||i!==p.data.progress){m();return}if("COMPLETED"===getNestedProperty(r,"data.libraryEntry.update.libraryEntry.status")){t.hide(n),c("success","Completed");return}t.$(`.${o}s_read`,n)[0].textContent=String(l),c("success","Updated")}catch(t){m()}},error:m})});