@contributte/datagrid 0.0.0-20251104-3ac07f3 → 0.0.0-20251126-8084900

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- const e=e=>{"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()};class t extends EventTarget{constructor(e){super(),this.naja=e,this.selector=".ajax",this.allowedOrigins=[window.location.origin],this.handler=this.handleUI.bind(this),e.addEventListener("init",this.initialize.bind(this))}initialize(){e(()=>this.bindUI(window.document.body)),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{snippet:t}=e.detail;this.bindUI(t)})}bindUI(e){const t=`a${this.selector}`,i=e=>{e.removeEventListener("click",this.handler),e.addEventListener("click",this.handler)};if(e.matches(t))return i(e);e.querySelectorAll(t).forEach(e=>i(e));const n=e=>{e.removeEventListener("submit",this.handler),e.addEventListener("submit",this.handler)};if(e instanceof HTMLFormElement)return n(e);e.querySelectorAll("form").forEach(e=>n(e))}handleUI(e){const t=e.currentTarget,i=this.naja.prepareOptions(),n=()=>{};if(e instanceof MouseEvent){if(e.altKey||e.ctrlKey||e.shiftKey||e.metaKey||e.button)return;return void this.clickElement(t,i,e).catch(n)}const{submitter:r}=e;(""===this.selector||t.matches(this.selector)||r?.matches(this.selector))&&this.submitForm(r??t,i,e).catch(n)}async clickElement(e,t={},i){if(e instanceof HTMLAnchorElement)return this.processInteraction(e,"GET",e.href,null,t,i);if((e instanceof HTMLInputElement||e instanceof HTMLButtonElement)&&e.form)return this.submitForm(e,t,i);throw new Error("Unsupported element in clickElement(): element must be an anchor or a submitter element attached to a form.")}async submitForm(e,t={},i){let n,r=null;if((e instanceof HTMLInputElement||e instanceof HTMLButtonElement)&&e.form)n=e.form,r=e;else{if(!(e instanceof HTMLFormElement))throw new Error("Unsupported element in submitForm(): formOrSubmitter must be either a form or a submitter element attached to a form.");n=e,r=i instanceof SubmitEvent?i.submitter:null}const s=(r?.getAttribute("formmethod")??n.getAttribute("method")??"GET").toUpperCase(),o=r?.getAttribute("formaction")??n.getAttribute("action")??window.location.pathname+window.location.search,a=new FormData(n,r);return this.processInteraction(r??n,s,o,a,t,i)}async processInteraction(e,t,i,n=null,r={},s){if(!this.dispatchEvent(new CustomEvent("interaction",{cancelable:!0,detail:{element:e,originalEvent:s,options:r}})))return s?.preventDefault(),{};if(!this.isUrlAllowed(`${i}`))throw new Error(`Cannot dispatch async request, URL is not allowed: ${i}`);return s?.preventDefault(),this.naja.makeRequest(t,i,n,r)}isUrlAllowed(e){const t=new URL(e,location.href);return"null"!==t.origin&&this.allowedOrigins.includes(t.origin)}}class i{constructor(e){this.naja=e,e.addEventListener("init",this.initialize.bind(this)),e.uiHandler.addEventListener("interaction",this.processForm.bind(this))}initialize(){e(()=>this.initForms(window.document.body)),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{snippet:t}=e.detail;this.initForms(t)})}initForms(e){const t=this.netteForms||window.Nette;if(!t)return;if(e instanceof HTMLFormElement)return void t.initForm(e);e.querySelectorAll("form").forEach(e=>t.initForm(e))}processForm(e){const{element:t,originalEvent:i}=e.detail,n=t instanceof HTMLFormElement,r=(t instanceof HTMLInputElement||t instanceof HTMLButtonElement)&&t.form;r&&(t.form["nette-submittedBy"]=t);const s=this.netteForms||window.Nette;(n||r)&&s&&!s.validateForm(t)&&(i?.stopImmediatePropagation(),i?.preventDefault(),e.preventDefault())}}class n extends EventTarget{constructor(e){super(),this.naja=e,e.uiHandler.addEventListener("interaction",e=>{const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-force-redirect")||t.form?.hasAttribute("data-naja-force-redirect")){const e=t.getAttribute("data-naja-force-redirect")??t.form?.getAttribute("data-naja-force-redirect");i.forceRedirect="off"!==e}}),e.addEventListener("success",e=>{const{payload:t,options:i}=e.detail;t.redirect&&(this.makeRedirect(t.redirect,i.forceRedirect??!1,i),e.stopImmediatePropagation())}),this.locationAdapter={assign:e=>window.location.assign(e)}}makeRedirect(e,t,i={}){e instanceof URL&&(e=e.href);let n=t||!this.naja.uiHandler.isUrlAllowed(e);this.dispatchEvent(new CustomEvent("redirect",{cancelable:!0,detail:{url:e,setUrl(t){e=t},isHardRedirect:n,setHardRedirect(e){n=!!e},options:i}}))&&(n?this.locationAdapter.assign(e):this.naja.makeRequest("GET",e,null,i))}}class r extends EventTarget{constructor(e){super(),this.op={replace:{updateElement(e,t){e.innerHTML=t},updateIndex:(e,t)=>t},prepend:{updateElement(e,t){e.insertAdjacentHTML("afterbegin",t)},updateIndex:(e,t)=>t+e},append:{updateElement(e,t){e.insertAdjacentHTML("beforeend",t)},updateIndex:(e,t)=>e+t}},e.addEventListener("success",e=>{const{options:t,payload:i}=e.detail;i.snippets&&this.updateSnippets(i.snippets,!1,t)})}static findSnippets(e,t=window.document){const i={};return t.querySelectorAll('[id^="snippet-"]').forEach(t=>{(e?.(t)??1)&&(i[t.id]=t.innerHTML)}),i}async updateSnippets(e,t=!1,i={}){await Promise.all(Object.keys(e).map(async n=>{const r=document.getElementById(n);r&&await this.updateSnippet(r,e[n],t,i)}))}async updateSnippet(e,t,i,n){let r=this.op.replace;!e.hasAttribute("data-naja-snippet-prepend")&&!e.hasAttribute("data-ajax-prepend")||i?!e.hasAttribute("data-naja-snippet-append")&&!e.hasAttribute("data-ajax-append")||i||(r=this.op.append):r=this.op.prepend;if(!this.dispatchEvent(new CustomEvent("beforeUpdate",{cancelable:!0,detail:{snippet:e,content:t,fromCache:i,operation:r,changeOperation(e){r=e},options:n}})))return;this.dispatchEvent(new CustomEvent("pendingUpdate",{detail:{snippet:e,content:t,fromCache:i,operation:r,options:n}}));const s=("function"==typeof r?r:r.updateElement)(e,t);s instanceof Promise&&await s,this.dispatchEvent(new CustomEvent("afterUpdate",{detail:{snippet:e,content:t,fromCache:i,operation:r,options:n}}))}}const s=Symbol();class o extends EventTarget{constructor(e){super(),this.naja=e,this.initialized=!1,this.cursor=0,this.popStateHandler=this.handlePopState.bind(this),e.addEventListener("init",this.initialize.bind(this)),e.addEventListener("before",this.saveUrl.bind(this)),e.addEventListener("before",this.saveOriginalTitle.bind(this)),e.addEventListener("before",this.replaceInitialStateBeforeRequest.bind(this)),e.addEventListener("success",this.pushNewState.bind(this)),e.redirectHandler.addEventListener("redirect",this.saveRedirectedUrl.bind(this)),e.uiHandler.addEventListener("interaction",this.configureMode.bind(this)),this.historyAdapter={get state(){return window.history.state},replaceState:(e,t,i)=>window.history.replaceState(e,t,i),pushState:(e,t,i)=>window.history.pushState(e,t,i)}}set uiCache(e){console.warn("Naja: HistoryHandler.uiCache is deprecated, use options.snippetCache instead."),this.naja.defaultOptions.snippetCache=e}handlePopState(e){const{state:t}=e;if("naja"!==t?.source)return;this.replaceInitialState();const i=t.cursor-this.cursor;this.cursor=t.cursor;const n=this.naja.prepareOptions();this.dispatchEvent(new CustomEvent("restoreState",{detail:{state:t,direction:i,options:n}}))}initialize(){window.addEventListener("popstate",this.popStateHandler),"naja"===this.historyAdapter.state?.source&&(this.cursor=this.historyAdapter.state.cursor,this.replaceInitialState())}saveOriginalTitle(e){const{options:t}=e.detail;t[s]=window.document.title}saveUrl(e){const{url:t,options:i}=e.detail;i.href??=t}saveRedirectedUrl(e){const{url:t,options:i}=e.detail;i.href=t}replaceInitialStateBeforeRequest(e){const{options:t}=e.detail;!1!==o.normalizeMode(t.history)&&this.replaceInitialState(t)}replaceInitialState(t=this.naja.prepareOptions()){this.initialized||(e(()=>this.historyAdapter.replaceState(this.buildState(window.location.href,"replace",this.cursor,t),window.document.title,window.location.href)),this.initialized=!0)}configureMode(e){const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-history")||t.form?.hasAttribute("data-naja-history")){const e=t.getAttribute("data-naja-history")??t.form?.getAttribute("data-naja-history");i.history=o.normalizeMode(e)}}static normalizeMode(e){return"off"!==e&&!1!==e&&("replace"!==e||"replace")}pushNewState(e){const{payload:t,options:i}=e.detail,n=o.normalizeMode(i.history);if(!1===n)return;t.postGet&&t.url&&(i.href=t.url);const r="replace"===n?"replaceState":"pushState",a="replace"===n?this.cursor:++this.cursor,l=this.buildState(i.href,n,a,i),c=window.document.title;window.document.title=i[s],this.historyAdapter[r](l,c,i.href),window.document.title=c}buildState(e,t,i,n){const r={source:"naja",cursor:i,href:e};return this.dispatchEvent(new CustomEvent("buildState",{detail:{state:r,operation:"replace"===t?"replaceState":"pushState",isInitial:!this.initialized,options:n}})),r}}class a extends EventTarget{constructor(e){super(),this.naja=e,this.currentSnippets=new Map,this.storages={off:new l(e),history:new c,session:new d},e.addEventListener("init",this.initializeIndex.bind(this)),e.snippetHandler.addEventListener("pendingUpdate",this.updateIndex.bind(this)),e.uiHandler.addEventListener("interaction",this.configureCache.bind(this)),e.historyHandler.addEventListener("buildState",this.buildHistoryState.bind(this)),e.historyHandler.addEventListener("restoreState",this.restoreHistoryState.bind(this))}resolveStorage(e){let t;return t=!0===e||void 0===e?"history":!1===e?"off":e,this.storages[t]}static shouldCacheSnippet(e){return!(e.hasAttribute("data-naja-history-nocache")||e.hasAttribute("data-history-nocache")||e.hasAttribute("data-naja-snippet-cache")&&"off"===e.getAttribute("data-naja-snippet-cache"))}initializeIndex(){e(()=>{const e=r.findSnippets(a.shouldCacheSnippet);this.currentSnippets=new Map(Object.entries(e))})}updateIndex(e){const{snippet:t,content:i,operation:n}=e.detail;if(!a.shouldCacheSnippet(t))return;const s=this.currentSnippets.get(t.id)??"",o="object"==typeof n?n.updateIndex:()=>i;this.currentSnippets.set(t.id,o(s,i));const l=a.parser.parseFromString(i,"text/html"),c=r.findSnippets(a.shouldCacheSnippet,l);for(const[e,t]of Object.entries(c))this.currentSnippets.set(e,t)}configureCache(e){const{element:t,options:i}=e.detail;if(t&&(t.hasAttribute("data-naja-snippet-cache")||t.form?.hasAttribute("data-naja-snippet-cache")||t.hasAttribute("data-naja-history-cache")||t.form?.hasAttribute("data-naja-history-cache"))){const e=t.getAttribute("data-naja-snippet-cache")??t.form?.getAttribute("data-naja-snippet-cache")??t.getAttribute("data-naja-history-cache")??t.form?.getAttribute("data-naja-history-cache");i.snippetCache=e}}buildHistoryState(e){const{state:t,options:i}=e.detail;"historyUiCache"in i&&(console.warn("Naja: options.historyUiCache is deprecated, use options.snippetCache instead."),i.snippetCache=i.historyUiCache);const n=Object.keys(r.findSnippets(a.shouldCacheSnippet)),s=Object.fromEntries(Array.from(this.currentSnippets).filter(([e])=>n.includes(e)));if(!this.dispatchEvent(new CustomEvent("store",{cancelable:!0,detail:{snippets:s,state:t,options:i}})))return;const o=this.resolveStorage(i.snippetCache);t.snippets={storage:o.type,key:o.store(s)}}restoreHistoryState(e){const{state:t,options:i}=e.detail;if(void 0===t.snippets)return;if(i.snippetCache=t.snippets.storage,!this.dispatchEvent(new CustomEvent("fetch",{cancelable:!0,detail:{state:t,options:i}})))return;const n=this.resolveStorage(i.snippetCache).fetch(t.snippets.key,t,i);null!==n&&this.dispatchEvent(new CustomEvent("restore",{cancelable:!0,detail:{snippets:n,state:t,options:i}}))&&this.naja.snippetHandler.updateSnippets(n,!0,i)}}a.parser=new DOMParser;class l{constructor(e){this.naja=e,this.type="off"}store(){return null}fetch(e,t,i){return this.naja.makeRequest("GET",t.href,null,{...i,history:!1,snippetCache:!1}),null}}class c{constructor(){this.type="history"}store(e){return e}fetch(e){return e}}class d{constructor(){this.type="session"}store(e){const t=Math.random().toString(36).substring(2,8);return window.sessionStorage.setItem(t,JSON.stringify(e)),t}fetch(e){const t=window.sessionStorage.getItem(e);return null===t?null:JSON.parse(t)}}class u{constructor(e){this.naja=e,this.loadedScripts=new Set,e.addEventListener("init",this.initialize.bind(this))}initialize(){e(()=>{document.querySelectorAll("script[data-naja-script-id]").forEach(e=>{const t=e.getAttribute("data-naja-script-id");null!==t&&""!==t&&this.loadedScripts.add(t)})}),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{content:t}=e.detail;this.loadScripts(t)})}loadScripts(e){"string"!=typeof e?Object.keys(e).forEach(t=>{const i=e[t];this.loadScriptsInSnippet(i)}):this.loadScriptsInSnippet(e)}loadScriptsInSnippet(e){if(!/<script/i.test(e))return;u.parser.parseFromString(e,"text/html").querySelectorAll("script").forEach(e=>{const t=e.getAttribute("data-naja-script-id");if(null!==t&&""!==t&&this.loadedScripts.has(t))return;const i=window.document.createElement("script");if(i.innerHTML=e.innerHTML,e.hasAttributes())for(const t of e.attributes)i.setAttribute(t.name,t.value);window.document.head.appendChild(i).parentNode.removeChild(i),null!==t&&""!==t&&this.loadedScripts.add(t)})}}u.parser=new DOMParser;class h extends EventTarget{constructor(e,s,l,c,d,h,p){super(),this.VERSION=3,this.initialized=!1,this.extensions=[],this.defaultOptions={},this.uiHandler=new(e??t)(this),this.redirectHandler=new(s??n)(this),this.snippetHandler=new(l??r)(this),this.formsHandler=new(c??i)(this),this.historyHandler=new(d??o)(this),this.snippetCache=new(h??a)(this),this.scriptLoader=new(p??u)(this)}registerExtension(e){this.initialized&&e.initialize(this),this.extensions.push(e)}initialize(e={}){if(this.initialized)throw new Error("Cannot initialize Naja, it is already initialized.");this.defaultOptions=this.prepareOptions(e),this.extensions.forEach(e=>e.initialize(this)),this.dispatchEvent(new CustomEvent("init",{detail:{defaultOptions:this.defaultOptions}})),this.initialized=!0}prepareOptions(e){return{...this.defaultOptions,...e,fetch:{...this.defaultOptions.fetch,...e?.fetch}}}async makeRequest(e,t,i=null,n={}){"string"==typeof t&&(t=new URL(t,location.href)),n=this.prepareOptions(n);const r=new Headers(n.fetch.headers||{}),s=this.transformData(t,e,i),o=new AbortController,a=new Request(t.toString(),{credentials:"same-origin",...n.fetch,method:e,headers:r,body:s,signal:o.signal});if(a.headers.set("X-Requested-With","XMLHttpRequest"),a.headers.set("Accept","application/json"),!this.dispatchEvent(new CustomEvent("before",{cancelable:!0,detail:{request:a,method:e,url:t.toString(),data:i,options:n}})))return{};const l=window.fetch(a);let c,d;this.dispatchEvent(new CustomEvent("start",{detail:{request:a,promise:l,abortController:o,options:n}}));try{if(c=await l,!c.ok)throw new p(c);d=await c.json()}catch(e){if("AbortError"===e.name)return this.dispatchEvent(new CustomEvent("abort",{detail:{request:a,error:e,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:void 0,error:e,options:n}})),{};throw this.dispatchEvent(new CustomEvent("error",{detail:{request:a,response:c,error:e,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:void 0,error:e,options:n}})),e}return this.dispatchEvent(new CustomEvent("payload",{detail:{request:a,response:c,payload:d,options:n}})),this.dispatchEvent(new CustomEvent("success",{detail:{request:a,response:c,payload:d,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:d,error:void 0,options:n}})),d}appendToQueryString(e,t,i){if(null!=i)if(Array.isArray(i)||Object.getPrototypeOf(i)===Object.prototype)for(const[n,r]of Object.entries(i))this.appendToQueryString(e,`${t}[${n}]`,r);else e.append(t,String(i))}transformData(e,t,i){const n=["GET","HEAD"].includes(t.toUpperCase());if(n&&i instanceof FormData){for(const[t,n]of i)null!=n&&e.searchParams.append(t,String(n));return null}if(null!==i&&Object.getPrototypeOf(i)===Object.prototype||Array.isArray(i)){const t=n?e.searchParams:new URLSearchParams;for(const[e,n]of Object.entries(i))this.appendToQueryString(t,e,n);return n?null:t}return i}}class p extends Error{constructor(e){const t=`HTTP ${e.status}: ${e.statusText}`;super(t),this.name=this.constructor.name,this.stack=new Error(t).stack,this.response=e}}const f=new h;function g(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}f.registerExtension(new class{constructor(){this.abortControllers=new Set}initialize(e){e.uiHandler.addEventListener("interaction",this.checkAbortable.bind(this)),e.addEventListener("init",this.onInitialize.bind(this)),e.addEventListener("start",this.saveAbortController.bind(this)),e.addEventListener("complete",this.removeAbortController.bind(this))}onInitialize(){document.addEventListener("keydown",e=>{if("Escape"===e.key&&!(e.ctrlKey||e.shiftKey||e.altKey||e.metaKey)){for(const e of this.abortControllers)e.abort();this.abortControllers.clear()}})}checkAbortable(e){const{element:t,options:i}=e.detail;(t.hasAttribute("data-naja-abort")||t.form?.hasAttribute("data-naja-abort"))&&(i.abort="off"!==(t.getAttribute("data-naja-abort")??t.form?.getAttribute("data-naja-abort")))}saveAbortController(e){const{abortController:t,options:i}=e.detail;!1!==i.abort&&(this.abortControllers.add(t),i.clearAbortExtension=()=>this.abortControllers.delete(t))}removeAbortController(e){const{options:t}=e.detail;!1!==t.abort&&t.clearAbortExtension&&t.clearAbortExtension()}}),f.registerExtension(new class{constructor(){this.abortControllers=new Map}initialize(e){e.uiHandler.addEventListener("interaction",this.checkUniqueness.bind(this)),e.addEventListener("start",this.abortPreviousRequest.bind(this)),e.addEventListener("complete",this.clearRequest.bind(this))}checkUniqueness(e){const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-unique")??t.form?.hasAttribute("data-naja-unique")){const e=t.getAttribute("data-naja-unique")??t.form?.getAttribute("data-naja-unique");i.unique="off"!==e&&(e??"default")}}abortPreviousRequest(e){const{abortController:t,options:i}=e.detail;!1!==i.unique&&(this.abortControllers.get(i.unique??"default")?.abort(),this.abortControllers.set(i.unique??"default",t))}clearRequest(e){const{request:t,options:i}=e.detail;t.signal.aborted||!1===i.unique||this.abortControllers.delete(i.unique??"default")}});var m,v={exports:{}};
1
+ const e=e=>{"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()};class t extends EventTarget{constructor(e){super(),this.naja=e,this.selector=".ajax",this.allowedOrigins=[window.location.origin],this.handler=this.handleUI.bind(this),e.addEventListener("init",this.initialize.bind(this))}initialize(){e(()=>this.bindUI(window.document.body)),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{snippet:t}=e.detail;this.bindUI(t)})}bindUI(e){const t=`a${this.selector}`,i=e=>{e.removeEventListener("click",this.handler),e.addEventListener("click",this.handler)};if(e.matches(t))return i(e);e.querySelectorAll(t).forEach(e=>i(e));const n=e=>{e.removeEventListener("submit",this.handler),e.addEventListener("submit",this.handler)};if(e instanceof HTMLFormElement)return n(e);e.querySelectorAll("form").forEach(e=>n(e))}handleUI(e){const t=e.currentTarget,i=this.naja.prepareOptions(),n=()=>{};if(e instanceof MouseEvent){if(e.altKey||e.ctrlKey||e.shiftKey||e.metaKey||e.button)return;return void this.clickElement(t,i,e).catch(n)}const{submitter:r}=e;(""===this.selector||t.matches(this.selector)||r?.matches(this.selector))&&this.submitForm(r??t,i,e).catch(n)}async clickElement(e,t={},i){if(e instanceof HTMLAnchorElement)return this.processInteraction(e,"GET",e.href,null,t,i);if((e instanceof HTMLInputElement||e instanceof HTMLButtonElement)&&e.form)return this.submitForm(e,t,i);throw new Error("Unsupported element in clickElement(): element must be an anchor or a submitter element attached to a form.")}async submitForm(e,t={},i){let n,r=null;if((e instanceof HTMLInputElement||e instanceof HTMLButtonElement)&&e.form)n=e.form,r=e;else{if(!(e instanceof HTMLFormElement))throw new Error("Unsupported element in submitForm(): formOrSubmitter must be either a form or a submitter element attached to a form.");n=e,r=i instanceof SubmitEvent?i.submitter:null}const s=(r?.getAttribute("formmethod")??n.getAttribute("method")??"GET").toUpperCase(),o=r?.getAttribute("formaction")??n.getAttribute("action")??window.location.pathname+window.location.search,a=new FormData(n,r);return this.processInteraction(r??n,s,o,a,t,i)}async processInteraction(e,t,i,n=null,r={},s){if(!this.dispatchEvent(new CustomEvent("interaction",{cancelable:!0,detail:{element:e,originalEvent:s,options:r}})))return s?.preventDefault(),{};if(!this.isUrlAllowed(`${i}`))throw new Error(`Cannot dispatch async request, URL is not allowed: ${i}`);return s?.preventDefault(),this.naja.makeRequest(t,i,n,r)}isUrlAllowed(e){const t=new URL(e,location.href);return"null"!==t.origin&&this.allowedOrigins.includes(t.origin)}}class i{constructor(e){this.naja=e,e.addEventListener("init",this.initialize.bind(this)),e.uiHandler.addEventListener("interaction",this.processForm.bind(this))}initialize(){e(()=>this.initForms(window.document.body)),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{snippet:t}=e.detail;this.initForms(t)})}initForms(e){const t=this.netteForms||window.Nette;if(!t)return;if(e instanceof HTMLFormElement)return void t.initForm(e);e.querySelectorAll("form").forEach(e=>t.initForm(e))}processForm(e){const{element:t,originalEvent:i}=e.detail,n=t instanceof HTMLFormElement,r=(t instanceof HTMLInputElement||t instanceof HTMLButtonElement)&&t.form;r&&(t.form["nette-submittedBy"]=t);const s=this.netteForms||window.Nette;(n||r)&&s&&!s.validateForm(t)&&(i?.stopImmediatePropagation(),i?.preventDefault(),e.preventDefault())}}class n extends EventTarget{constructor(e){super(),this.naja=e,e.uiHandler.addEventListener("interaction",e=>{const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-force-redirect")||t.form?.hasAttribute("data-naja-force-redirect")){const e=t.getAttribute("data-naja-force-redirect")??t.form?.getAttribute("data-naja-force-redirect");i.forceRedirect="off"!==e}}),e.addEventListener("success",e=>{const{payload:t,options:i}=e.detail;t.redirect&&(this.makeRedirect(t.redirect,i.forceRedirect??!1,i),e.stopImmediatePropagation())}),this.locationAdapter={assign:e=>window.location.assign(e)}}makeRedirect(e,t,i={}){e instanceof URL&&(e=e.href);let n=t||!this.naja.uiHandler.isUrlAllowed(e);this.dispatchEvent(new CustomEvent("redirect",{cancelable:!0,detail:{url:e,setUrl(t){e=t},isHardRedirect:n,setHardRedirect(e){n=!!e},options:i}}))&&(n?this.locationAdapter.assign(e):this.naja.makeRequest("GET",e,null,i))}}class r extends EventTarget{constructor(e){super(),this.op={replace:{updateElement(e,t){e.innerHTML=t},updateIndex:(e,t)=>t},prepend:{updateElement(e,t){e.insertAdjacentHTML("afterbegin",t)},updateIndex:(e,t)=>t+e},append:{updateElement(e,t){e.insertAdjacentHTML("beforeend",t)},updateIndex:(e,t)=>e+t}},e.addEventListener("success",e=>{const{options:t,payload:i}=e.detail;i.snippets&&this.updateSnippets(i.snippets,!1,t)})}static findSnippets(e,t=window.document){const i={};return t.querySelectorAll('[id^="snippet-"]').forEach(t=>{(e?.(t)??1)&&(i[t.id]=t.innerHTML)}),i}async updateSnippets(e,t=!1,i={}){await Promise.all(Object.keys(e).map(async n=>{const r=document.getElementById(n);r&&await this.updateSnippet(r,e[n],t,i)}))}async updateSnippet(e,t,i,n){let r=this.op.replace;!e.hasAttribute("data-naja-snippet-prepend")&&!e.hasAttribute("data-ajax-prepend")||i?!e.hasAttribute("data-naja-snippet-append")&&!e.hasAttribute("data-ajax-append")||i||(r=this.op.append):r=this.op.prepend;if(!this.dispatchEvent(new CustomEvent("beforeUpdate",{cancelable:!0,detail:{snippet:e,content:t,fromCache:i,operation:r,changeOperation(e){r=e},options:n}})))return;this.dispatchEvent(new CustomEvent("pendingUpdate",{detail:{snippet:e,content:t,fromCache:i,operation:r,options:n}}));const s=("function"==typeof r?r:r.updateElement)(e,t);s instanceof Promise&&await s,this.dispatchEvent(new CustomEvent("afterUpdate",{detail:{snippet:e,content:t,fromCache:i,operation:r,options:n}}))}}const s=Symbol();class o extends EventTarget{constructor(e){super(),this.naja=e,this.initialized=!1,this.cursor=0,this.popStateHandler=this.handlePopState.bind(this),e.addEventListener("init",this.initialize.bind(this)),e.addEventListener("before",this.saveUrl.bind(this)),e.addEventListener("before",this.saveOriginalTitle.bind(this)),e.addEventListener("before",this.replaceInitialStateBeforeRequest.bind(this)),e.addEventListener("success",this.pushNewState.bind(this)),e.redirectHandler.addEventListener("redirect",this.saveRedirectedUrl.bind(this)),e.uiHandler.addEventListener("interaction",this.configureMode.bind(this)),this.historyAdapter={get state(){return window.history.state},replaceState:(e,t,i)=>window.history.replaceState(e,t,i),pushState:(e,t,i)=>window.history.pushState(e,t,i)}}set uiCache(e){console.warn("Naja: HistoryHandler.uiCache is deprecated, use options.snippetCache instead."),this.naja.defaultOptions.snippetCache=e}handlePopState(e){const{state:t}=e;if("naja"!==t?.source)return;this.replaceInitialState();const i=t.cursor-this.cursor;this.cursor=t.cursor;const n=this.naja.prepareOptions();this.dispatchEvent(new CustomEvent("restoreState",{detail:{state:t,direction:i,options:n}}))}initialize(){window.addEventListener("popstate",this.popStateHandler),"naja"===this.historyAdapter.state?.source&&(this.cursor=this.historyAdapter.state.cursor,this.replaceInitialState())}saveOriginalTitle(e){const{options:t}=e.detail;t[s]=window.document.title}saveUrl(e){const{url:t,options:i}=e.detail;i.href??=t}saveRedirectedUrl(e){const{url:t,options:i}=e.detail;i.href=t}replaceInitialStateBeforeRequest(e){const{options:t}=e.detail;!1!==o.normalizeMode(t.history)&&this.replaceInitialState(t)}replaceInitialState(t=this.naja.prepareOptions()){this.initialized||(e(()=>this.historyAdapter.replaceState(this.buildState(window.location.href,"replace",this.cursor,t),window.document.title,window.location.href)),this.initialized=!0)}configureMode(e){const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-history")||t.form?.hasAttribute("data-naja-history")){const e=t.getAttribute("data-naja-history")??t.form?.getAttribute("data-naja-history");i.history=o.normalizeMode(e)}}static normalizeMode(e){return"off"!==e&&!1!==e&&("replace"!==e||"replace")}pushNewState(e){const{payload:t,options:i}=e.detail,n=o.normalizeMode(i.history);if(!1===n)return;t.postGet&&t.url&&(i.href=t.url);const r="replace"===n?"replaceState":"pushState",a="replace"===n?this.cursor:++this.cursor,l=this.buildState(i.href,n,a,i),c=window.document.title;window.document.title=i[s],this.historyAdapter[r](l,c,i.href),window.document.title=c}buildState(e,t,i,n){const r={source:"naja",cursor:i,href:e};return this.dispatchEvent(new CustomEvent("buildState",{detail:{state:r,operation:"replace"===t?"replaceState":"pushState",isInitial:!this.initialized,options:n}})),r}}class a extends EventTarget{constructor(e){super(),this.naja=e,this.initialized=!1,this.currentSnippets=new Map,this.storages={off:new l(e),history:new c,session:new d},e.addEventListener("init",this.initializeIndex.bind(this)),e.snippetHandler.addEventListener("pendingUpdate",this.updateIndex.bind(this)),e.uiHandler.addEventListener("interaction",this.configureCache.bind(this)),e.historyHandler.addEventListener("buildState",this.buildHistoryState.bind(this)),e.historyHandler.addEventListener("restoreState",this.restoreHistoryState.bind(this))}resolveStorage(e){let t;return t=!0===e||void 0===e?"history":!1===e?"off":e,this.storages[t]}static shouldCacheSnippet(e){return!(e.hasAttribute("data-naja-history-nocache")||e.hasAttribute("data-history-nocache")||e.hasAttribute("data-naja-snippet-cache")&&"off"===e.getAttribute("data-naja-snippet-cache"))}initializeIndex(){this.initialized||(e(()=>{const e=r.findSnippets(a.shouldCacheSnippet);this.currentSnippets=new Map(Object.entries(e))}),this.initialized=!0)}updateIndex(e){const{snippet:t,content:i,operation:n}=e.detail;if(!a.shouldCacheSnippet(t))return;const s=this.currentSnippets.get(t.id)??"",o="object"==typeof n?n.updateIndex:()=>i;this.currentSnippets.set(t.id,o(s,i));const l=a.parser.parseFromString(i,"text/html"),c=r.findSnippets(a.shouldCacheSnippet,l);for(const[e,t]of Object.entries(c))this.currentSnippets.set(e,t)}configureCache(e){const{element:t,options:i}=e.detail;if(t&&(t.hasAttribute("data-naja-snippet-cache")||t.form?.hasAttribute("data-naja-snippet-cache")||t.hasAttribute("data-naja-history-cache")||t.form?.hasAttribute("data-naja-history-cache"))){const e=t.getAttribute("data-naja-snippet-cache")??t.form?.getAttribute("data-naja-snippet-cache")??t.getAttribute("data-naja-history-cache")??t.form?.getAttribute("data-naja-history-cache");i.snippetCache=e}}buildHistoryState(e){const{isInitial:t,state:i,options:n}=e.detail;"historyUiCache"in n&&(console.warn("Naja: options.historyUiCache is deprecated, use options.snippetCache instead."),n.snippetCache=n.historyUiCache),t&&this.initializeIndex();const s=Object.keys(r.findSnippets(a.shouldCacheSnippet)),o=Object.fromEntries(Array.from(this.currentSnippets).filter(([e])=>s.includes(e)));if(!this.dispatchEvent(new CustomEvent("store",{cancelable:!0,detail:{snippets:o,state:i,options:n}})))return;const l=this.resolveStorage(n.snippetCache);i.snippets={storage:l.type,key:l.store(o)}}restoreHistoryState(e){const{state:t,options:i}=e.detail;if(void 0===t.snippets)return;if(i.snippetCache=t.snippets.storage,!this.dispatchEvent(new CustomEvent("fetch",{cancelable:!0,detail:{state:t,options:i}})))return;const n=this.resolveStorage(i.snippetCache).fetch(t.snippets.key,t,i);null!==n&&this.dispatchEvent(new CustomEvent("restore",{cancelable:!0,detail:{snippets:n,state:t,options:i}}))&&this.naja.snippetHandler.updateSnippets(n,!0,i)}}a.parser=new DOMParser;class l{constructor(e){this.naja=e,this.type="off"}store(){return null}fetch(e,t,i){return this.naja.makeRequest("GET",t.href,null,{...i,history:!1,snippetCache:!1}),null}}class c{constructor(){this.type="history"}store(e){return e}fetch(e){return e}}class d{constructor(){this.type="session"}store(e){const t=Math.random().toString(36).substring(2,8);return window.sessionStorage.setItem(t,JSON.stringify(e)),t}fetch(e){const t=window.sessionStorage.getItem(e);return null===t?null:JSON.parse(t)}}class u{constructor(e){this.naja=e,this.loadedScripts=new Set,e.addEventListener("init",this.initialize.bind(this))}initialize(){e(()=>{document.querySelectorAll("script[data-naja-script-id]").forEach(e=>{const t=e.getAttribute("data-naja-script-id");null!==t&&""!==t&&this.loadedScripts.add(t)})}),this.naja.snippetHandler.addEventListener("afterUpdate",e=>{const{content:t}=e.detail;this.loadScripts(t)})}loadScripts(e){"string"!=typeof e?Object.keys(e).forEach(t=>{const i=e[t];this.loadScriptsInSnippet(i)}):this.loadScriptsInSnippet(e)}loadScriptsInSnippet(e){if(!/<script/i.test(e))return;u.parser.parseFromString(e,"text/html").querySelectorAll("script").forEach(e=>{const t=e.getAttribute("data-naja-script-id");if(null!==t&&""!==t&&this.loadedScripts.has(t))return;const i=window.document.createElement("script");if(i.innerHTML=e.innerHTML,e.hasAttributes())for(const t of e.attributes)i.setAttribute(t.name,t.value);window.document.head.appendChild(i).parentNode.removeChild(i),null!==t&&""!==t&&this.loadedScripts.add(t)})}}u.parser=new DOMParser;class h extends EventTarget{constructor(e,s,l,c,d,h,p){super(),this.VERSION=3,this.initialized=!1,this.extensions=[],this.defaultOptions={},this.uiHandler=new(e??t)(this),this.redirectHandler=new(s??n)(this),this.snippetHandler=new(l??r)(this),this.formsHandler=new(c??i)(this),this.historyHandler=new(d??o)(this),this.snippetCache=new(h??a)(this),this.scriptLoader=new(p??u)(this)}registerExtension(e){this.initialized&&e.initialize(this),this.extensions.push(e)}initialize(e={}){if(this.initialized)throw new Error("Cannot initialize Naja, it is already initialized.");this.defaultOptions=this.prepareOptions(e),this.extensions.forEach(e=>e.initialize(this)),this.dispatchEvent(new CustomEvent("init",{detail:{defaultOptions:this.defaultOptions}})),this.initialized=!0}prepareOptions(e){return{...this.defaultOptions,...e,fetch:{...this.defaultOptions.fetch,...e?.fetch}}}async makeRequest(e,t,i=null,n={}){"string"==typeof t&&(t=new URL(t,location.href)),n=this.prepareOptions(n);const r=new Headers(n.fetch.headers||{}),s=this.transformData(t,e,i),o=new AbortController,a=new Request(t.toString(),{credentials:"same-origin",...n.fetch,method:e,headers:r,body:s,signal:o.signal});if(a.headers.set("X-Requested-With","XMLHttpRequest"),a.headers.set("Accept","application/json"),!this.dispatchEvent(new CustomEvent("before",{cancelable:!0,detail:{request:a,method:e,url:t.toString(),data:i,options:n}})))return{};const l=window.fetch(a);let c,d;this.dispatchEvent(new CustomEvent("start",{detail:{request:a,promise:l,abortController:o,options:n}}));try{if(c=await l,!c.ok)throw new p(c);d=await c.json()}catch(e){if("AbortError"===e.name)return this.dispatchEvent(new CustomEvent("abort",{detail:{request:a,error:e,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:void 0,error:e,options:n}})),{};throw this.dispatchEvent(new CustomEvent("error",{detail:{request:a,response:c,error:e,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:void 0,error:e,options:n}})),e}return this.dispatchEvent(new CustomEvent("payload",{detail:{request:a,response:c,payload:d,options:n}})),this.dispatchEvent(new CustomEvent("success",{detail:{request:a,response:c,payload:d,options:n}})),this.dispatchEvent(new CustomEvent("complete",{detail:{request:a,response:c,payload:d,error:void 0,options:n}})),d}appendToQueryString(e,t,i){if(null!=i)if(Array.isArray(i)||Object.getPrototypeOf(i)===Object.prototype)for(const[n,r]of Object.entries(i))this.appendToQueryString(e,`${t}[${n}]`,r);else e.append(t,String(i))}transformData(e,t,i){const n=["GET","HEAD"].includes(t.toUpperCase());if(n&&i instanceof FormData){for(const[t,n]of i)null!=n&&e.searchParams.append(t,String(n));return null}if(null!==i&&Object.getPrototypeOf(i)===Object.prototype||Array.isArray(i)){const t=n?e.searchParams:new URLSearchParams;for(const[e,n]of Object.entries(i))this.appendToQueryString(t,e,n);return n?null:t}return i}}class p extends Error{constructor(e){const t=`HTTP ${e.status}: ${e.statusText}`;super(t),this.name=this.constructor.name,this.stack=new Error(t).stack,this.response=e}}const f=new h;function g(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}f.registerExtension(new class{constructor(){this.abortControllers=new Set}initialize(e){e.uiHandler.addEventListener("interaction",this.checkAbortable.bind(this)),e.addEventListener("init",this.onInitialize.bind(this)),e.addEventListener("start",this.saveAbortController.bind(this)),e.addEventListener("complete",this.removeAbortController.bind(this))}onInitialize(){document.addEventListener("keydown",e=>{if("Escape"===e.key&&!(e.ctrlKey||e.shiftKey||e.altKey||e.metaKey)){for(const e of this.abortControllers)e.abort();this.abortControllers.clear()}})}checkAbortable(e){const{element:t,options:i}=e.detail;(t.hasAttribute("data-naja-abort")||t.form?.hasAttribute("data-naja-abort"))&&(i.abort="off"!==(t.getAttribute("data-naja-abort")??t.form?.getAttribute("data-naja-abort")))}saveAbortController(e){const{abortController:t,options:i}=e.detail;!1!==i.abort&&(this.abortControllers.add(t),i.clearAbortExtension=()=>this.abortControllers.delete(t))}removeAbortController(e){const{options:t}=e.detail;!1!==t.abort&&t.clearAbortExtension&&t.clearAbortExtension()}}),f.registerExtension(new class{constructor(){this.abortControllers=new Map}initialize(e){e.uiHandler.addEventListener("interaction",this.checkUniqueness.bind(this)),e.addEventListener("start",this.abortPreviousRequest.bind(this)),e.addEventListener("complete",this.clearRequest.bind(this))}checkUniqueness(e){const{element:t,options:i}=e.detail;if(t.hasAttribute("data-naja-unique")??t.form?.hasAttribute("data-naja-unique")){const e=t.getAttribute("data-naja-unique")??t.form?.getAttribute("data-naja-unique");i.unique="off"!==e&&(e??"default")}}abortPreviousRequest(e){const{abortController:t,options:i}=e.detail;!1!==i.unique&&(this.abortControllers.get(i.unique??"default")?.abort(),this.abortControllers.set(i.unique??"default",t))}clearRequest(e){const{request:t,options:i}=e.detail;t.signal.aborted||!1===i.unique||this.abortControllers.delete(i.unique??"default")}});var m,v={exports:{}};
2
2
  /*!
3
3
  * NetteForms - simple form validation.
4
4
  *