Skip to main content

max / makenotwork

4.2 KB · 2 lines History Blame Raw
1 (()=>{let t,e=null;const r="htmx-history-index";function o(){return htmx.config.historyCache}function i(t){let e=[t];return htmx.config.prefix&&e.push(t.replaceAll("hx-",htmx.config.prefix)),e.join(",")}function s(){try{return sessionStorage.setItem("__htmx_test__","1"),sessionStorage.removeItem("__htmx_test__"),!0}catch(t){return!1}}function l(){return htmx.find(i("[hx-history-elt]"))||document.body}const n="data-htmx-history-value",a="data-htmx-history-checked",h="data-htmx-history-scroll";function c(){let t=history.state||{htmx:!0},r=t.htmxId;r||(r=crypto.randomUUID?.()??Math.random().toString(36).slice(2),history.replaceState({...t,htmxId:r},"",location.href)),e=r}function m(t){sessionStorage.setItem(r,JSON.stringify(t))}function u(t,e){if(!s()||o().size<=0)return!1;let i=function(){try{return JSON.parse(sessionStorage.getItem(r))||[]}catch{return[]}}().filter(e=>e!==t);for(;i.length>=o().size;)sessionStorage.removeItem("htmx-history-"+i.shift());let l=JSON.stringify(e);for(;;)try{return sessionStorage.setItem("htmx-history-"+t,l),i.push(t),m(i),!0}catch(t){if(0===i.length)return!1;sessionStorage.removeItem("htmx-history-"+i.shift())}}function f(r){if(!(r=r||e))return;if(htmx.find(i('[hx-history="false"]')))return;let o=l();var s;(s=o).querySelectorAll("input, textarea, select").forEach(t=>{let e=t.type?.toLowerCase();if("file"!==e&&"password"!==e)if("checkbox"===e||"radio"===e)t.checked&&t.setAttribute(a,"1");else if("SELECT"===t.tagName&&t.multiple){let e=Array.from(t.options).filter(t=>t.selected).map(t=>t.value);e.length&&t.setAttribute(n,JSON.stringify(e))}else t.value&&t.setAttribute(n,t.value)}),s.querySelectorAll("*").forEach(t=>{(t.scrollTop>0||t.scrollLeft>0)&&t.setAttribute(h,`${t.scrollTop},${t.scrollLeft}`)});let c=document.head.outerHTML,m=window.scrollY,f=document.title,d={target:o,head:c};if(!1===t.triggerHtmxEvent(document,"htmx:history:cache:before:save",d))return;let y=function(t){let e=t.cloneNode(!0);return e.querySelectorAll(".htmx-request").forEach(t=>t.classList.remove("htmx-request")),e.querySelectorAll("[disabled][data-disabled-by-htmx]").forEach(t=>{t.removeAttribute("disabled"),t.removeAttribute("data-disabled-by-htmx")}),e.outerHTML}(o);c=d.head,u(r,{content:y,head:c,scroll:m,title:f}),t.triggerHtmxEvent(document,"htmx:history:cache:after:save",{content:y,head:c,scroll:m,title:f})}function d(){let t=history.state?.htmxId;return t&&s()?function(t){try{return JSON.parse(sessionStorage.getItem("htmx-history-"+t))}catch{return null}}(t):null}async function y(e){let r={head:e.head,ready:null};t.triggerHtmxEvent(document,"htmx:history:cache:before:restore",r),r.ready&&await r.ready;let i=e.content,s=l(),c=o().swapStyle,m={sourceElement:document.body,target:s,swap:c,text:i,transition:!1,_deferredHeadScripts:r._deferredHeadScripts};await htmx.swap(m),document.title=e.title||document.title,requestAnimationFrame(()=>{var r;window.scrollTo(0,e.scroll||0),(r=l()).querySelectorAll(`[${a}]`).forEach(t=>{t.checked=!0,t.removeAttribute(a)}),r.querySelectorAll(`[${n}]`).forEach(t=>{let e=t.getAttribute(n);if("SELECT"===t.tagName&&t.multiple){let r=JSON.parse(e);Array.from(t.options).forEach(t=>{t.selected=r.includes(t.value)})}else t.value=e;t.removeAttribute(n)}),r.querySelectorAll(`[${h}]`).forEach(t=>{let[e,r]=t.getAttribute(h).split(",").map(Number);t.scrollTop=e,t.scrollLeft=r,t.removeAttribute(h)}),t.triggerHtmxEvent(document,"htmx:history:cache:after:restore",{item:e})})}htmx.registerExtension("history-cache",{init:e=>{t=e,htmx.config.historyCache??={},htmx.config.historyCache.size??=10,htmx.config.historyCache.refreshOnMiss??=!1,htmx.config.historyCache.disable??=!1,htmx.config.historyCache.swapStyle??="outerSync",c()},htmx_before_history_update:(t,e)=>{o().disable||(c(),f())},htmx_after_history_update:()=>{o().disable||c()},htmx_before_history_restore:(r,i)=>{if(o().disable)return;let s=e,l=history.state?.htmxId;s&&s!==l&&f(s),l?e=l:c();let n=d();if(!n){let e={path:i.path,refreshOnMiss:o().refreshOnMiss};return t.triggerHtmxEvent(document,"htmx:history:cache:miss",e),e.refreshOnMiss?(location.reload(),!1):void 0}let a={path:i.path,item:n};return!1!==t.triggerHtmxEvent(document,"htmx:history:cache:hit",a)?(i.cancelled=!0,y(a.item),!1):void 0}})})();
2