From c555da4dc903fe58bbef36b8a1d402e100b06c9b Mon Sep 17 00:00:00 2001 From: maddalax Date: Fri, 20 Sep 2024 22:59:07 -0500 Subject: [PATCH] cleaning up the api a bit --- cli/htmgo/tasks/run/build.go | 4 +- framework-ui/ui/input.go | 4 +- framework/assets/dist/htmgo.js | 2 +- .../assets/js/htmxextensions/livereload.ts | 17 +- framework/h/base.go | 4 +- framework/h/conditionals.go | 32 +++ framework/h/events.go | 24 -- framework/h/lifecycle.go | 73 ++++-- framework/h/renderer.go | 48 +++- framework/h/tag.go | 234 ++++++------------ framework/h/xhr.go | 43 ++++ framework/hx/event.go | 26 ++ framework/hx/htmx.go | 138 +++++++++++ framework/hx/htmx_test.go | 19 ++ framework/hx/modifiers.go | 49 ++++ framework/hx/trigger.go | 80 ++++++ framework/hx/triggers.go | 16 ++ htmgo-site/go.mod | 6 +- htmgo-site/go.sum | 1 + htmgo-site/pages/markdown.go | 5 + htmgo-site/partials/load/generated.go | 4 + htmgo-site/partials/navbar.go | 30 ++- 22 files changed, 641 insertions(+), 218 deletions(-) create mode 100644 framework/h/conditionals.go delete mode 100644 framework/h/events.go create mode 100644 framework/h/xhr.go create mode 100644 framework/hx/event.go create mode 100644 framework/hx/htmx.go create mode 100644 framework/hx/htmx_test.go create mode 100644 framework/hx/modifiers.go create mode 100644 framework/hx/trigger.go create mode 100644 framework/hx/triggers.go diff --git a/cli/htmgo/tasks/run/build.go b/cli/htmgo/tasks/run/build.go index c721a0a..deaaad1 100644 --- a/cli/htmgo/tasks/run/build.go +++ b/cli/htmgo/tasks/run/build.go @@ -22,6 +22,8 @@ func Build() { // }, //) - process.RunOrExit("env GOOS=linux GOARCH=amd64 go build -o ./dist .") + process.RunOrExit("env GOOS=linux GOARCH=amd64 go build -o ./dist/app-linux-amd64 .") + process.RunOrExit("go build -o ./dist/app .") + process.RunOrExit("echo \"Build successful\"") } diff --git a/framework-ui/ui/input.go b/framework-ui/ui/input.go index 94fff5b..585fc3d 100644 --- a/framework-ui/ui/input.go +++ b/framework-ui/ui/input.go @@ -2,6 +2,7 @@ package ui import ( "github.com/maddalax/htmgo/framework/h" + "github.com/maddalax/htmgo/framework/hx" ) type InputProps struct { @@ -16,8 +17,7 @@ type InputProps struct { func Input(props InputProps) h.Ren { validation := h.If(props.ValidationPath != "", h.Children( - h.Post(props.ValidationPath), - h.Trigger("change"), + h.Post(props.ValidationPath, hx.ChangeEvent), h.Attribute("hx-swap", "innerHTML transition:true"), h.Attribute("hx-target", "next div"), )) diff --git a/framework/assets/dist/htmgo.js b/framework/assets/dist/htmgo.js index 9d5afa9..d29d2d7 100644 --- a/framework/assets/dist/htmgo.js +++ b/framework/assets/dist/htmgo.js @@ -1 +1 @@ -var K=function(){let htmx={onLoad:null,process:null,on:null,off:null,trigger:null,ajax:null,find:null,findAll:null,closest:null,values:function(e,t){return getInputValues(e,t||"post").values},remove:null,addClass:null,removeClass:null,toggleClass:null,takeClass:null,swap:null,defineExtension:null,removeExtension:null,logAll:null,logNone:null,logger:null,config:{historyEnabled:!0,historyCacheSize:10,refreshOnHistoryMiss:!1,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:20,includeIndicatorStyles:!0,indicatorClass:"htmx-indicator",requestClass:"htmx-request",addedClass:"htmx-added",settlingClass:"htmx-settling",swappingClass:"htmx-swapping",allowEval:!0,allowScriptTags:!0,inlineScriptNonce:"",inlineStyleNonce:"",attributesToSettle:["class","style","width","height"],withCredentials:!1,timeout:0,wsReconnectDelay:"full-jitter",wsBinaryType:"blob",disableSelector:"[hx-disable], [data-hx-disable]",scrollBehavior:"instant",defaultFocusScroll:!1,getCacheBusterParam:!1,globalViewTransitions:!1,methodsThatUseUrlParams:["get","delete"],selfRequestsOnly:!0,ignoreTitle:!1,scrollIntoViewOnBoost:!0,triggerSpecsCache:null,disableInheritance:!1,responseHandling:[{code:"204",swap:!1},{code:"[23]..",swap:!0},{code:"[45]..",swap:!1,error:!0}],allowNestedOobSwaps:!0},parseInterval:null,_:null,version:"2.0.2"};htmx.onLoad=onLoadHelper,htmx.process=processNode,htmx.on=addEventListenerImpl,htmx.off=removeEventListenerImpl,htmx.trigger=triggerEvent,htmx.ajax=ajaxHelper,htmx.find=find,htmx.findAll=findAll,htmx.closest=closest,htmx.remove=removeElement,htmx.addClass=addClassToElement,htmx.removeClass=removeClassFromElement,htmx.toggleClass=toggleClassOnElement,htmx.takeClass=takeClassForElement,htmx.swap=swap,htmx.defineExtension=defineExtension,htmx.removeExtension=removeExtension,htmx.logAll=logAll,htmx.logNone=logNone,htmx.parseInterval=parseInterval,htmx._=internalEval;let internalAPI={addTriggerHandler,bodyContains,canAccessLocalStorage,findThisElement,filterValues,swap,hasAttribute,getAttributeValue,getClosestAttributeValue,getClosestMatch,getExpressionVars,getHeaders,getInputValues,getInternalData,getSwapSpecification,getTriggerSpecs,getTarget,makeFragment,mergeObjects,makeSettleInfo,oobSwap,querySelectorExt,settleImmediately,shouldCancel,triggerEvent,triggerErrorEvent,withExtensions},VERBS=["get","post","put","delete","patch"],VERB_SELECTOR=VERBS.map(function(e){return "[hx-"+e+"], [data-hx-"+e+"]"}).join(", "),HEAD_TAG_REGEX=makeTagRegEx("head");function makeTagRegEx(e,t=!1){return new RegExp(`<${e}(\\s[^>]*>|>)([\\s\\S]*?)<\\/${e}>`,t?"gim":"im")}function parseInterval(e){if(e==null)return;let t=NaN;return e.slice(-2)=="ms"?t=parseFloat(e.slice(0,-2)):e.slice(-1)=="s"?t=parseFloat(e.slice(0,-1))*1e3:e.slice(-1)=="m"?t=parseFloat(e.slice(0,-1))*1e3*60:t=parseFloat(e),isNaN(t)?void 0:t}function getRawAttribute(e,t){return e instanceof Element&&e.getAttribute(t)}function hasAttribute(e,t){return !!e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function getAttributeValue(e,t){return getRawAttribute(e,t)||getRawAttribute(e,"data-"+t)}function parentElt(e){let t=e.parentElement;return !t&&e.parentNode instanceof ShadowRoot?e.parentNode:t}function getDocument(){return document}function getRootNode(e,t){return e.getRootNode?e.getRootNode({composed:t}):getDocument()}function getClosestMatch(e,t){for(;e&&!t(e);)e=parentElt(e);return e||null}function getAttributeValueWithDisinheritance(e,t,n){let r=getAttributeValue(t,n),o=getAttributeValue(t,"hx-disinherit");var i=getAttributeValue(t,"hx-inherit");if(e!==t){if(htmx.config.disableInheritance)return i&&(i==="*"||i.split(" ").indexOf(n)>=0)?r:null;if(o&&(o==="*"||o.split(" ").indexOf(n)>=0))return "unset"}return r}function getClosestAttributeValue(e,t){let n=null;if(getClosestMatch(e,function(r){return !!(n=getAttributeValueWithDisinheritance(e,asElement(r),t))}),n!=="unset")return n}function matches(e,t){let n=e instanceof Element&&(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector);return !!n&&n.call(e,t)}function getStartTag(e){let n=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i.exec(e);return n?n[1].toLowerCase():""}function parseHTML(e){return new DOMParser().parseFromString(e,"text/html")}function takeChildrenFor(e,t){for(;t.childNodes.length>0;)e.append(t.childNodes[0]);}function duplicateScript(e){let t=getDocument().createElement("script");return forEach(e.attributes,function(n){t.setAttribute(n.name,n.value);}),t.textContent=e.textContent,t.async=!1,htmx.config.inlineScriptNonce&&(t.nonce=htmx.config.inlineScriptNonce),t}function isJavaScriptScriptNode(e){return e.matches("script")&&(e.type==="text/javascript"||e.type==="module"||e.type==="")}function normalizeScriptTags(e){Array.from(e.querySelectorAll("script")).forEach(t=>{if(isJavaScriptScriptNode(t)){let n=duplicateScript(t),r=t.parentNode;try{r.insertBefore(n,t);}catch(o){logError(o);}finally{t.remove();}}});}function makeFragment(e){let t=e.replace(HEAD_TAG_REGEX,""),n=getStartTag(t),r;if(n==="html"){r=new DocumentFragment;let i=parseHTML(e);takeChildrenFor(r,i.body),r.title=i.title;}else if(n==="body"){r=new DocumentFragment;let i=parseHTML(t);takeChildrenFor(r,i.body),r.title=i.title;}else {let i=parseHTML('");r=i.querySelector("template").content,r.title=i.title;var o=r.querySelector("title");o&&o.parentNode===r&&(o.remove(),r.title=o.innerText);}return r&&(htmx.config.allowScriptTags?normalizeScriptTags(r):r.querySelectorAll("script").forEach(i=>i.remove())),r}function maybeCall(e){e&&e();}function isType(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function isFunction(e){return typeof e=="function"}function isRawObject(e){return isType(e,"Object")}function getInternalData(e){let t="htmx-internal-data",n=e[t];return n||(n=e[t]={}),n}function toArray(e){let t=[];if(e)for(let n=0;n=0}function bodyContains(e){let t=e.getRootNode&&e.getRootNode();return t&&t instanceof window.ShadowRoot?getDocument().body.contains(t.host):getDocument().body.contains(e)}function splitOnWhitespace(e){return e.trim().split(/\s+/)}function mergeObjects(e,t){for(let n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}function parseJSON(e){try{return JSON.parse(e)}catch(t){return logError(t),null}}function canAccessLocalStorage(){let e="htmx:localStorageTest";try{return localStorage.setItem(e,e),localStorage.removeItem(e),!0}catch{return !1}}function normalizePath(e){try{let t=new URL(e);return t&&(e=t.pathname+t.search),/^\/$/.test(e)||(e=e.replace(/\/+$/,"")),e}catch{return e}}function internalEval(str){return maybeEval(getDocument().body,function(){return eval(str)})}function onLoadHelper(e){return htmx.on("htmx:load",function(n){e(n.detail.elt);})}function logAll(){htmx.logger=function(e,t,n){console&&console.log(t,e,n);};}function logNone(){htmx.logger=null;}function find(e,t){return typeof e!="string"?e.querySelector(t):find(getDocument(),e)}function findAll(e,t){return typeof e!="string"?e.querySelectorAll(t):findAll(getDocument(),e)}function getWindow(){return window}function removeElement(e,t){e=resolveTarget(e),t?getWindow().setTimeout(function(){removeElement(e),e=null;},t):parentElt(e).removeChild(e);}function asElement(e){return e instanceof Element?e:null}function asHtmlElement(e){return e instanceof HTMLElement?e:null}function asString(e){return typeof e=="string"?e:null}function asParentNode(e){return e instanceof Element||e instanceof Document||e instanceof DocumentFragment?e:null}function addClassToElement(e,t,n){e=asElement(resolveTarget(e)),e&&(n?getWindow().setTimeout(function(){addClassToElement(e,t),e=null;},n):e.classList&&e.classList.add(t));}function removeClassFromElement(e,t,n){let r=asElement(resolveTarget(e));r&&(n?getWindow().setTimeout(function(){removeClassFromElement(r,t),r=null;},n):r.classList&&(r.classList.remove(t),r.classList.length===0&&r.removeAttribute("class")));}function toggleClassOnElement(e,t){e=resolveTarget(e),e.classList.toggle(t);}function takeClassForElement(e,t){e=resolveTarget(e),forEach(e.parentElement.children,function(n){removeClassFromElement(n,t);}),addClassToElement(asElement(e),t);}function closest(e,t){if(e=asElement(resolveTarget(e)),e&&e.closest)return e.closest(t);do if(e==null||matches(e,t))return e;while(e=e&&asElement(parentElt(e)));return null}function startsWith(e,t){return e.substring(0,t.length)===t}function endsWith(e,t){return e.substring(e.length-t.length)===t}function normalizeSelector(e){let t=e.trim();return startsWith(t,"<")&&endsWith(t,"/>")?t.substring(1,t.length-2):t}function querySelectorAllExt(e,t,n){return e=resolveTarget(e),t.indexOf("closest ")===0?[closest(asElement(e),normalizeSelector(t.substr(8)))]:t.indexOf("find ")===0?[find(asParentNode(e),normalizeSelector(t.substr(5)))]:t==="next"?[asElement(e).nextElementSibling]:t.indexOf("next ")===0?[scanForwardQuery(e,normalizeSelector(t.substr(5)),!!n)]:t==="previous"?[asElement(e).previousElementSibling]:t.indexOf("previous ")===0?[scanBackwardsQuery(e,normalizeSelector(t.substr(9)),!!n)]:t==="document"?[document]:t==="window"?[window]:t==="body"?[document.body]:t==="root"?[getRootNode(e,!!n)]:t.indexOf("global ")===0?querySelectorAllExt(e,t.slice(7),!0):toArray(asParentNode(getRootNode(e,!!n)).querySelectorAll(normalizeSelector(t)))}var scanForwardQuery=function(e,t,n){let r=asParentNode(getRootNode(e,n)).querySelectorAll(t);for(let o=0;o=0;o--){let i=r[o];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_FOLLOWING)return i}};function querySelectorExt(e,t){return typeof e!="string"?querySelectorAllExt(e,t)[0]:querySelectorAllExt(getDocument().body,e)[0]}function resolveTarget(e,t){return typeof e=="string"?find(asParentNode(t)||document,e):e}function processEventArgs(e,t,n){return isFunction(t)?{target:getDocument().body,event:asString(e),listener:t}:{target:resolveTarget(e),event:asString(t),listener:n}}function addEventListenerImpl(e,t,n){return ready(function(){let o=processEventArgs(e,t,n);o.target.addEventListener(o.event,o.listener);}),isFunction(t)?t:n}function removeEventListenerImpl(e,t,n){return ready(function(){let r=processEventArgs(e,t,n);r.target.removeEventListener(r.event,r.listener);}),isFunction(t)?t:n}let DUMMY_ELT=getDocument().createElement("output");function findAttributeTargets(e,t){let n=getClosestAttributeValue(e,t);if(n){if(n==="this")return [findThisElement(e,t)];{let r=querySelectorAllExt(e,n);return r.length===0?(logError('The selector "'+n+'" on '+t+" returned no matches!"),[DUMMY_ELT]):r}}}function findThisElement(e,t){return asElement(getClosestMatch(e,function(n){return getAttributeValue(asElement(n),t)!=null}))}function getTarget(e){let t=getClosestAttributeValue(e,"hx-target");return t?t==="this"?findThisElement(e,"hx-target"):querySelectorExt(e,t):getInternalData(e).boosted?getDocument().body:e}function shouldSettleAttribute(e){let t=htmx.config.attributesToSettle;for(let n=0;n0?(o=e.substr(0,e.indexOf(":")),r=e.substr(e.indexOf(":")+1,e.length)):o=e);let i=getDocument().querySelectorAll(r);return i?(forEach(i,function(s){let l,a=t.cloneNode(!0);l=getDocument().createDocumentFragment(),l.appendChild(a),isInlineSwap(o,s)||(l=asParentNode(a));let u={shouldSwap:!0,target:s,fragment:l};triggerEvent(s,"htmx:oobBeforeSwap",u)&&(s=u.target,u.shouldSwap&&swapWithStyle(o,s,s,l,n),forEach(n.elts,function(f){triggerEvent(f,"htmx:oobAfterSwap",u);}));}),t.parentNode.removeChild(t)):(t.parentNode.removeChild(t),triggerErrorEvent(getDocument().body,"htmx:oobErrorNoTarget",{content:t})),e}function handlePreservedElements(e){forEach(findAll(e,"[hx-preserve], [data-hx-preserve]"),function(t){let n=getAttributeValue(t,"id"),r=getDocument().getElementById(n);r!=null&&t.parentNode.replaceChild(r,t);});}function handleAttributes(e,t,n){forEach(t.querySelectorAll("[id]"),function(r){let o=getRawAttribute(r,"id");if(o&&o.length>0){let i=o.replace("'","\\'"),s=r.tagName.replace(":","\\:"),l=asParentNode(e),a=l&&l.querySelector(s+"[id='"+i+"']");if(a&&a!==l){let u=r.cloneNode();cloneAttributes(r,a),n.tasks.push(function(){cloneAttributes(r,u);});}}});}function makeAjaxLoadTask(e){return function(){removeClassFromElement(e,htmx.config.addedClass),processNode(asElement(e)),processFocus(asParentNode(e)),triggerEvent(e,"htmx:load");}}function processFocus(e){let t="[autofocus]",n=asHtmlElement(matches(e,t)?e:e.querySelector(t));n?.focus();}function insertNodesBefore(e,t,n,r){for(handleAttributes(e,n,r);n.childNodes.length>0;){let o=n.firstChild;addClassToElement(asElement(o),htmx.config.addedClass),e.insertBefore(o,t),o.nodeType!==Node.TEXT_NODE&&o.nodeType!==Node.COMMENT_NODE&&r.tasks.push(makeAjaxLoadTask(o));}}function stringHash(e,t){let n=0;for(;n0}function swap(e,t,n,r){r||(r={}),e=resolveTarget(e);let o=document.activeElement,i={};try{i={elt:o,start:o?o.selectionStart:null,end:o?o.selectionEnd:null};}catch{}let s=makeSettleInfo(e);if(n.swapStyle==="textContent")e.textContent=t;else {let a=makeFragment(t);if(s.title=a.title,r.selectOOB){let u=r.selectOOB.split(",");for(let f=0;f0?getWindow().setTimeout(l,n.settleDelay):l();}function handleTriggerHeader(e,t,n){let r=e.getResponseHeader(t);if(r.indexOf("{")===0){let o=parseJSON(r);for(let i in o)if(o.hasOwnProperty(i)){let s=o[i];isRawObject(s)?n=s.target!==void 0?s.target:n:s={value:s},triggerEvent(n,i,s);}}else {let o=r.split(",");for(let i=0;i0;){let s=t[0];if(s==="]"){if(r--,r===0){i===null&&(o=o+"true"),t.shift(),o+=")})";try{let l=maybeEval(e,function(){return Function(o)()},function(){return !0});return l.source=o,l}catch(l){return triggerErrorEvent(getDocument().body,"htmx:syntax:error",{error:l,source:o}),null}}}else s==="["&&r++;isPossibleRelativeReference(s,i,n)?o+="(("+n+"."+s+") ? ("+n+"."+s+") : (window."+s+"))":o=o+s,i=t.shift();}}}function consumeUntil(e,t){let n="";for(;e.length>0&&!t.test(e[0]);)n+=e.shift();return n}function consumeCSSSelector(e){let t;return e.length>0&&COMBINED_SELECTOR_START.test(e[0])?(e.shift(),t=consumeUntil(e,COMBINED_SELECTOR_END).trim(),e.shift()):t=consumeUntil(e,WHITESPACE_OR_COMMA),t}let INPUT_SELECTOR="input, textarea, select";function parseAndCacheTrigger(e,t,n){let r=[],o=tokenizeString(t);do{consumeUntil(o,NOT_WHITESPACE);let l=o.length,a=consumeUntil(o,/[,\[\s]/);if(a!=="")if(a==="every"){let u={trigger:"every"};consumeUntil(o,NOT_WHITESPACE),u.pollInterval=parseInterval(consumeUntil(o,/[,\[\s]/)),consumeUntil(o,NOT_WHITESPACE);var i=maybeGenerateConditional(e,o,"event");i&&(u.eventFilter=i),r.push(u);}else {let u={trigger:a};var i=maybeGenerateConditional(e,o,"event");for(i&&(u.eventFilter=i);o.length>0&&o[0]!==",";){consumeUntil(o,NOT_WHITESPACE);let c=o.shift();if(c==="changed")u.changed=!0;else if(c==="once")u.once=!0;else if(c==="consume")u.consume=!0;else if(c==="delay"&&o[0]===":")o.shift(),u.delay=parseInterval(consumeUntil(o,WHITESPACE_OR_COMMA));else if(c==="from"&&o[0]===":"){if(o.shift(),COMBINED_SELECTOR_START.test(o[0]))var s=consumeCSSSelector(o);else {var s=consumeUntil(o,WHITESPACE_OR_COMMA);if(s==="closest"||s==="find"||s==="next"||s==="previous"){o.shift();let b=consumeCSSSelector(o);b.length>0&&(s+=" "+b);}}u.from=s;}else c==="target"&&o[0]===":"?(o.shift(),u.target=consumeCSSSelector(o)):c==="throttle"&&o[0]===":"?(o.shift(),u.throttle=parseInterval(consumeUntil(o,WHITESPACE_OR_COMMA))):c==="queue"&&o[0]===":"?(o.shift(),u.queue=consumeUntil(o,WHITESPACE_OR_COMMA)):c==="root"&&o[0]===":"?(o.shift(),u[c]=consumeCSSSelector(o)):c==="threshold"&&o[0]===":"?(o.shift(),u[c]=consumeUntil(o,WHITESPACE_OR_COMMA)):triggerErrorEvent(e,"htmx:syntax:error",{token:o.shift()});}r.push(u);}o.length===l&&triggerErrorEvent(e,"htmx:syntax:error",{token:o.shift()}),consumeUntil(o,NOT_WHITESPACE);}while(o[0]===","&&o.shift());return n&&(n[t]=r),r}function getTriggerSpecs(e){let t=getAttributeValue(e,"hx-trigger"),n=[];if(t){let r=htmx.config.triggerSpecsCache;n=r&&r[t]||parseAndCacheTrigger(e,t,r);}return n.length>0?n:matches(e,"form")?[{trigger:"submit"}]:matches(e,'input[type="button"], input[type="submit"]')?[{trigger:"click"}]:matches(e,INPUT_SELECTOR)?[{trigger:"change"}]:[{trigger:"click"}]}function cancelPolling(e){getInternalData(e).cancelled=!0;}function processPolling(e,t,n){let r=getInternalData(e);r.timeout=getWindow().setTimeout(function(){bodyContains(e)&&r.cancelled!==!0&&(maybeFilterEvent(n,e,makeEvent("hx:poll:trigger",{triggerSpec:n,target:e}))||t(e),processPolling(e,t,n));},n.pollInterval);}function isLocalLink(e){return location.hostname===e.hostname&&getRawAttribute(e,"href")&&getRawAttribute(e,"href").indexOf("#")!==0}function eltIsDisabled(e){return closest(e,htmx.config.disableSelector)}function boostElement(e,t,n){if(e instanceof HTMLAnchorElement&&isLocalLink(e)&&(e.target===""||e.target==="_self")||e.tagName==="FORM"&&String(getRawAttribute(e,"method")).toLowerCase()!=="dialog"){t.boosted=!0;let r,o;if(e.tagName==="A")r="get",o=getRawAttribute(e,"href");else {let i=getRawAttribute(e,"method");r=i?i.toLowerCase():"get",o=getRawAttribute(e,"action");}n.forEach(function(i){addEventListener(e,function(s,l){let a=asElement(s);if(eltIsDisabled(a)){cleanUpElement(a);return}issueAjaxRequest(r,o,a,l);},t,i,!0);});}}function shouldCancel(e,t){let n=asElement(t);return n?!!((e.type==="submit"||e.type==="click")&&(n.tagName==="FORM"||matches(n,'input[type="submit"], button')&&closest(n,"form")!==null||n instanceof HTMLAnchorElement&&n.href&&(n.getAttribute("href")==="#"||n.getAttribute("href").indexOf("#")!==0))):!1}function ignoreBoostedAnchorCtrlClick(e,t){return getInternalData(e).boosted&&e instanceof HTMLAnchorElement&&t.type==="click"&&(t.ctrlKey||t.metaKey)}function maybeFilterEvent(e,t,n){let r=e.eventFilter;if(r)try{return r.call(t,n)!==!0}catch(o){let i=r.source;return triggerErrorEvent(getDocument().body,"htmx:eventFilter:error",{error:o,source:i}),!0}return !1}function addEventListener(e,t,n,r,o){let i=getInternalData(e),s;r.from?s=querySelectorAllExt(e,r.from):s=[e],r.changed&&s.forEach(function(l){let a=getInternalData(l);a.lastValue=l.value;}),forEach(s,function(l){let a=function(u){if(!bodyContains(e)){l.removeEventListener(r.trigger,a);return}if(ignoreBoostedAnchorCtrlClick(e,u)||((o||shouldCancel(u,e))&&u.preventDefault(),maybeFilterEvent(r,e,u)))return;let f=getInternalData(u);if(f.triggerSpec=r,f.handledFor==null&&(f.handledFor=[]),f.handledFor.indexOf(e)<0){if(f.handledFor.push(e),r.consume&&u.stopPropagation(),r.target&&u.target&&!matches(asElement(u.target),r.target))return;if(r.once){if(i.triggeredOnce)return;i.triggeredOnce=!0;}if(r.changed){let c=getInternalData(l),d=l.value;if(c.lastValue===d)return;c.lastValue=d;}if(i.delayed&&clearTimeout(i.delayed),i.throttle)return;r.throttle>0?i.throttle||(triggerEvent(e,"htmx:trigger"),t(e,u),i.throttle=getWindow().setTimeout(function(){i.throttle=null;},r.throttle)):r.delay>0?i.delayed=getWindow().setTimeout(function(){triggerEvent(e,"htmx:trigger"),t(e,u);},r.delay):(triggerEvent(e,"htmx:trigger"),t(e,u));}};n.listenerInfos==null&&(n.listenerInfos=[]),n.listenerInfos.push({trigger:r.trigger,listener:a,on:l}),l.addEventListener(r.trigger,a);});}let windowIsScrolling=!1,scrollHandler=null;function initScrollHandler(){scrollHandler||(scrollHandler=function(){windowIsScrolling=!0;},window.addEventListener("scroll",scrollHandler),setInterval(function(){windowIsScrolling&&(windowIsScrolling=!1,forEach(getDocument().querySelectorAll("[hx-trigger*='revealed'],[data-hx-trigger*='revealed']"),function(e){maybeReveal(e);}));},200));}function maybeReveal(e){!hasAttribute(e,"data-hx-revealed")&&isScrolledIntoView(e)&&(e.setAttribute("data-hx-revealed","true"),getInternalData(e).initHash?triggerEvent(e,"revealed"):e.addEventListener("htmx:afterProcessNode",function(){triggerEvent(e,"revealed");},{once:!0}));}function loadImmediately(e,t,n,r){let o=function(){n.loaded||(n.loaded=!0,t(e));};r>0?getWindow().setTimeout(o,r):o();}function processVerbs(e,t,n){let r=!1;return forEach(VERBS,function(o){if(hasAttribute(e,"hx-"+o)){let i=getAttributeValue(e,"hx-"+o);r=!0,t.path=i,t.verb=o,n.forEach(function(s){addTriggerHandler(e,s,t,function(l,a){let u=asElement(l);if(closest(u,htmx.config.disableSelector)){cleanUpElement(u);return}issueAjaxRequest(o,i,u,a);});});}}),r}function addTriggerHandler(e,t,n,r){if(t.trigger==="revealed")initScrollHandler(),addEventListener(e,r,n,t),maybeReveal(asElement(e));else if(t.trigger==="intersect"){let o={};t.root&&(o.root=querySelectorExt(e,t.root)),t.threshold&&(o.threshold=parseFloat(t.threshold)),new IntersectionObserver(function(s){for(let l=0;l0?(n.polling=!0,processPolling(asElement(e),r,t)):addEventListener(e,r,n,t);}function shouldProcessHxOn(e){let t=asElement(e);if(!t)return !1;let n=t.attributes;for(let r=0;r", "+i).join(""))}else return []}function maybeSetLastButtonClicked(e){let t=closest(asElement(e.target),"button, input[type='submit']"),n=getRelatedFormData(e);n&&(n.lastButtonClicked=t);}function maybeUnsetLastButtonClicked(e){let t=getRelatedFormData(e);t&&(t.lastButtonClicked=null);}function getRelatedFormData(e){let t=closest(asElement(e.target),"button, input[type='submit']");if(!t)return;let n=resolveTarget("#"+getRawAttribute(t,"form"),t.getRootNode())||closest(t,"form");if(n)return getInternalData(n)}function initButtonTracking(e){e.addEventListener("click",maybeSetLastButtonClicked),e.addEventListener("focusin",maybeSetLastButtonClicked),e.addEventListener("focusout",maybeUnsetLastButtonClicked);}function addHxOnEventHandler(e,t,n){let r=getInternalData(e);Array.isArray(r.onHandlers)||(r.onHandlers=[]);let o,i=function(s){maybeEval(e,function(){eltIsDisabled(e)||(o||(o=new Function("event",n)),o.call(e,s));});};e.addEventListener(t,i),r.onHandlers.push({event:t,listener:i});}function processHxOnWildcard(e){deInitOnHandlers(e);for(let t=0;thtmx.config.historyCacheSize;)i.shift();for(;i.length>0;)try{localStorage.setItem("htmx-history-cache",JSON.stringify(i));break}catch(l){triggerErrorEvent(getDocument().body,"htmx:historyCacheError",{cause:l,cache:i}),i.shift();}}function getCachedHistory(e){if(!canAccessLocalStorage())return null;e=normalizePath(e);let t=parseJSON(localStorage.getItem("htmx-history-cache"))||[];for(let n=0;n=200&&this.status<400){triggerEvent(getDocument().body,"htmx:historyCacheMissLoad",n);let r=makeFragment(this.response),o=r.querySelector("[hx-history-elt],[data-hx-history-elt]")||r,i=getHistoryElement(),s=makeSettleInfo(i);handleTitle(r.title),swapInnerHTML(i,o,s),settleImmediately(s.tasks),currentPathForHistory=e,triggerEvent(getDocument().body,"htmx:historyRestore",{path:e,cacheMiss:!0,serverResponse:this.response});}else triggerErrorEvent(getDocument().body,"htmx:historyCacheMissLoadError",n);},t.send();}function restoreHistory(e){saveCurrentPageToHistory(),e=e||location.pathname+location.search;let t=getCachedHistory(e);if(t){let n=makeFragment(t.content),r=getHistoryElement(),o=makeSettleInfo(r);handleTitle(n.title),swapInnerHTML(r,n,o),settleImmediately(o.tasks),getWindow().setTimeout(function(){window.scrollTo(0,t.scroll);},0),currentPathForHistory=e,triggerEvent(getDocument().body,"htmx:historyRestore",{path:e,item:t});}else htmx.config.refreshOnHistoryMiss?window.location.reload(!0):loadHistoryFromServer(e);}function addRequestIndicatorClasses(e){let t=findAttributeTargets(e,"hx-indicator");return t==null&&(t=[e]),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)+1,n.classList.add.call(n.classList,htmx.config.requestClass);}),t}function disableElements(e){let t=findAttributeTargets(e,"hx-disabled-elt");return t==null&&(t=[]),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)+1,n.setAttribute("disabled",""),n.setAttribute("data-disabled-by-htmx","");}),t}function removeRequestIndicators(e,t){forEach(e,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)-1,r.requestCount===0&&n.classList.remove.call(n.classList,htmx.config.requestClass);}),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)-1,r.requestCount===0&&(n.removeAttribute("disabled"),n.removeAttribute("data-disabled-by-htmx"));});}function haveSeenNode(e,t){for(let n=0;nt.indexOf(o)<0):r=r.filter(o=>o!==t),n.delete(e),forEach(r,o=>n.append(e,o));}}function processInputValue(e,t,n,r,o){if(!(r==null||haveSeenNode(e,r))){if(e.push(r),shouldInclude(r)){let i=getRawAttribute(r,"name"),s=r.value;r instanceof HTMLSelectElement&&r.multiple&&(s=toArray(r.querySelectorAll("option:checked")).map(function(l){return l.value})),r instanceof HTMLInputElement&&r.files&&(s=toArray(r.files)),addValueToFormData(i,s,t),o&&validateElement(r,n);}r instanceof HTMLFormElement&&(forEach(r.elements,function(i){e.indexOf(i)>=0?removeValueFromFormData(i.name,i.value,t):e.push(i),o&&validateElement(i,n);}),new FormData(r).forEach(function(i,s){i instanceof File&&i.name===""||addValueToFormData(s,i,t);}));}}function validateElement(e,t){let n=e;n.willValidate&&(triggerEvent(n,"htmx:validation:validate"),n.checkValidity()||(t.push({elt:n,message:n.validationMessage,validity:n.validity}),triggerEvent(n,"htmx:validation:failed",{message:n.validationMessage,validity:n.validity})));}function overrideFormData(e,t){for(let n of t.keys())e.delete(n);return t.forEach(function(n,r){e.append(r,n);}),e}function getInputValues(e,t){let n=[],r=new FormData,o=new FormData,i=[],s=getInternalData(e);s.lastButtonClicked&&!bodyContains(s.lastButtonClicked)&&(s.lastButtonClicked=null);let l=e instanceof HTMLFormElement&&e.noValidate!==!0||getAttributeValue(e,"hx-validate")==="true";if(s.lastButtonClicked&&(l=l&&s.lastButtonClicked.formNoValidate!==!0),t!=="get"&&processInputValue(n,o,i,closest(e,"form"),l),processInputValue(n,r,i,e,l),s.lastButtonClicked||e.tagName==="BUTTON"||e.tagName==="INPUT"&&getRawAttribute(e,"type")==="submit"){let u=s.lastButtonClicked||e,f=getRawAttribute(u,"name");addValueToFormData(f,u.value,o);}let a=findAttributeTargets(e,"hx-include");return forEach(a,function(u){processInputValue(n,r,i,asElement(u),l),matches(u,"form")||forEach(asParentNode(u).querySelectorAll(INPUT_SELECTOR),function(f){processInputValue(n,r,i,f,l);});}),overrideFormData(r,o),{errors:i,formData:r,values:formDataProxy(r)}}function appendParam(e,t,n){e!==""&&(e+="&"),String(n)==="[object Object]"&&(n=JSON.stringify(n));let r=encodeURIComponent(n);return e+=encodeURIComponent(t)+"="+r,e}function urlEncode(e){e=formDataFromObject(e);let t="";return e.forEach(function(n,r){t=appendParam(t,r,n);}),t}function getHeaders(e,t,n){let r={"HX-Request":"true","HX-Trigger":getRawAttribute(e,"id"),"HX-Trigger-Name":getRawAttribute(e,"name"),"HX-Target":getAttributeValue(t,"id"),"HX-Current-URL":getDocument().location.href};return getValuesForElement(e,"hx-headers",!1,r),n!==void 0&&(r["HX-Prompt"]=n),getInternalData(e).boosted&&(r["HX-Boosted"]="true"),r}function filterValues(e,t){let n=getClosestAttributeValue(t,"hx-params");if(n){if(n==="none")return new FormData;if(n==="*")return e;if(n.indexOf("not ")===0)return forEach(n.substr(4).split(","),function(r){r=r.trim(),e.delete(r);}),e;{let r=new FormData;return forEach(n.split(","),function(o){o=o.trim(),e.has(o)&&e.getAll(o).forEach(function(i){r.append(o,i);});}),r}}else return e}function isAnchorLink(e){return !!getRawAttribute(e,"href")&&getRawAttribute(e,"href").indexOf("#")>=0}function getSwapSpecification(e,t){let n=t||getClosestAttributeValue(e,"hx-swap"),r={swapStyle:getInternalData(e).boosted?"innerHTML":htmx.config.defaultSwapStyle,swapDelay:htmx.config.defaultSwapDelay,settleDelay:htmx.config.defaultSettleDelay};if(htmx.config.scrollIntoViewOnBoost&&getInternalData(e).boosted&&!isAnchorLink(e)&&(r.show="top"),n){let s=splitOnWhitespace(n);if(s.length>0)for(let l=0;l0?o.join(":"):null;r.scroll=f,r.scrollTarget=i;}else if(a.indexOf("show:")===0){var o=a.substr(5).split(":");let c=o.pop();var i=o.length>0?o.join(":"):null;r.show=c,r.showTarget=i;}else if(a.indexOf("focus-scroll:")===0){let u=a.substr(13);r.focusScroll=u=="true";}else l==0?r.swapStyle=a:logError("Unknown modifier in hx-swap: "+a);}}return r}function usesFormData(e){return getClosestAttributeValue(e,"hx-encoding")==="multipart/form-data"||matches(e,"form")&&getRawAttribute(e,"enctype")==="multipart/form-data"}function encodeParamsForBody(e,t,n){let r=null;return withExtensions(t,function(o){r==null&&(r=o.encodeParameters(e,n,t));}),r??(usesFormData(t)?overrideFormData(new FormData,formDataFromObject(n)):urlEncode(n))}function makeSettleInfo(e){return {tasks:[],elts:[e]}}function updateScrollState(e,t){let n=e[0],r=e[e.length-1];if(t.scroll){var o=null;t.scrollTarget&&(o=asElement(querySelectorExt(n,t.scrollTarget))),t.scroll==="top"&&(n||o)&&(o=o||n,o.scrollTop=0),t.scroll==="bottom"&&(r||o)&&(o=o||r,o.scrollTop=o.scrollHeight);}if(t.show){var o=null;if(t.showTarget){let s=t.showTarget;t.showTarget==="window"&&(s="body"),o=asElement(querySelectorExt(n,s));}t.show==="top"&&(n||o)&&(o=o||n,o.scrollIntoView({block:"start",behavior:htmx.config.scrollBehavior})),t.show==="bottom"&&(r||o)&&(o=o||r,o.scrollIntoView({block:"end",behavior:htmx.config.scrollBehavior}));}}function getValuesForElement(e,t,n,r){if(r==null&&(r={}),e==null)return r;let o=getAttributeValue(e,t);if(o){let i=o.trim(),s=n;if(i==="unset")return null;i.indexOf("javascript:")===0?(i=i.substr(11),s=!0):i.indexOf("js:")===0&&(i=i.substr(3),s=!0),i.indexOf("{")!==0&&(i="{"+i+"}");let l;s?l=maybeEval(e,function(){return Function("return ("+i+")")()},{}):l=parseJSON(i);for(let a in l)l.hasOwnProperty(a)&&r[a]==null&&(r[a]=l[a]);}return getValuesForElement(asElement(parentElt(e)),t,n,r)}function maybeEval(e,t,n){return htmx.config.allowEval?t():(triggerErrorEvent(e,"htmx:evalDisallowedError"),n)}function getHXVarsForElement(e,t){return getValuesForElement(e,"hx-vars",!0,t)}function getHXValsForElement(e,t){return getValuesForElement(e,"hx-vals",!1,t)}function getExpressionVars(e){return mergeObjects(getHXVarsForElement(e),getHXValsForElement(e))}function safelySetHeaderValue(e,t,n){if(n!==null)try{e.setRequestHeader(t,n);}catch{e.setRequestHeader(t,encodeURIComponent(n)),e.setRequestHeader(t+"-URI-AutoEncoded","true");}}function getPathFromResponse(e){if(e.responseURL&&typeof URL<"u")try{let t=new URL(e.responseURL);return t.pathname+t.search}catch{triggerErrorEvent(getDocument().body,"htmx:badResponseUrl",{url:e.responseURL});}}function hasHeader(e,t){return t.test(e.getAllResponseHeaders())}function ajaxHelper(e,t,n){return e=e.toLowerCase(),n?n instanceof Element||typeof n=="string"?issueAjaxRequest(e,t,null,null,{targetOverride:resolveTarget(n),returnPromise:!0}):issueAjaxRequest(e,t,resolveTarget(n.source),n.event,{handler:n.handler,headers:n.headers,values:n.values,targetOverride:resolveTarget(n.target),swapOverride:n.swap,select:n.select,returnPromise:!0}):issueAjaxRequest(e,t,null,null,{returnPromise:!0})}function hierarchyForElt(e){let t=[];for(;e;)t.push(e),e=e.parentElement;return t}function verifyPath(e,t,n){let r,o;return typeof URL=="function"?(o=new URL(t,document.location.href),r=document.location.origin===o.origin):(o=t,r=startsWith(t,document.location.origin)),htmx.config.selfRequestsOnly&&!r?!1:triggerEvent(e,"htmx:validateUrl",mergeObjects({url:o,sameHost:r},n))}function formDataFromObject(e){if(e instanceof FormData)return e;let t=new FormData;for(let n in e)e.hasOwnProperty(n)&&(typeof e[n].forEach=="function"?e[n].forEach(function(r){t.append(n,r);}):typeof e[n]=="object"&&!(e[n]instanceof Blob)?t.append(n,JSON.stringify(e[n])):t.append(n,e[n]));return t}function formDataArrayProxy(e,t,n){return new Proxy(n,{get:function(r,o){return typeof o=="number"?r[o]:o==="length"?r.length:o==="push"?function(i){r.push(i),e.append(t,i);}:typeof r[o]=="function"?function(){r[o].apply(r,arguments),e.delete(t),r.forEach(function(i){e.append(t,i);});}:r[o]&&r[o].length===1?r[o][0]:r[o]},set:function(r,o,i){return r[o]=i,e.delete(t),r.forEach(function(s){e.append(t,s);}),!0}})}function formDataProxy(e){return new Proxy(e,{get:function(t,n){if(typeof n=="symbol")return Reflect.get(t,n);if(n==="toJSON")return ()=>Object.fromEntries(e);if(n in t)return typeof t[n]=="function"?function(){return e[n].apply(e,arguments)}:t[n];let r=e.getAll(n);if(r.length!==0)return r.length===1?r[0]:formDataArrayProxy(t,n,r)},set:function(t,n,r){return typeof n!="string"?!1:(t.delete(n),typeof r.forEach=="function"?r.forEach(function(o){t.append(n,o);}):typeof r=="object"&&!(r instanceof Blob)?t.append(n,JSON.stringify(r)):t.append(n,r),!0)},deleteProperty:function(t,n){return typeof n=="string"&&t.delete(n),!0},ownKeys:function(t){return Reflect.ownKeys(Object.fromEntries(t))},getOwnPropertyDescriptor:function(t,n){return Reflect.getOwnPropertyDescriptor(Object.fromEntries(t),n)}})}function issueAjaxRequest(e,t,n,r,o,i){let s=null,l=null;if(o=o??{},o.returnPromise&&typeof Promise<"u")var a=new Promise(function(h,E){s=h,l=E;});n==null&&(n=getDocument().body);let u=o.handler||handleAjaxResponse,f=o.select||null;if(!bodyContains(n))return maybeCall(s),a;let c=o.targetOverride||asElement(getTarget(n));if(c==null||c==DUMMY_ELT)return triggerErrorEvent(n,"htmx:targetError",{target:getAttributeValue(n,"hx-target")}),maybeCall(l),a;let d=getInternalData(n),b=d.lastButtonClicked;if(b){let h=getRawAttribute(b,"formaction");h!=null&&(t=h);let E=getRawAttribute(b,"formmethod");E!=null&&E.toLowerCase()!=="dialog"&&(e=E);}let S=getClosestAttributeValue(n,"hx-confirm");if(i===void 0&&triggerEvent(n,"htmx:confirm",{target:c,elt:n,path:t,verb:e,triggeringEvent:r,etc:o,issueRequest:function(O){return issueAjaxRequest(e,t,n,r,o,!!O)},question:S})===!1)return maybeCall(s),a;let A=n,p=getClosestAttributeValue(n,"hx-sync"),x=null,H=!1;if(p){let h=p.split(":"),E=h[0].trim();if(E==="this"?A=findThisElement(n,"hx-sync"):A=asElement(querySelectorExt(n,E)),p=(h[1]||"drop").trim(),d=getInternalData(A),p==="drop"&&d.xhr&&d.abortable!==!0)return maybeCall(s),a;if(p==="abort"){if(d.xhr)return maybeCall(s),a;H=!0;}else p==="replace"?triggerEvent(A,"htmx:abort"):p.indexOf("queue")===0&&(x=(p.split(" ")[1]||"last").trim());}if(d.xhr)if(d.abortable)triggerEvent(A,"htmx:abort");else {if(x==null){if(r){let h=getInternalData(r);h&&h.triggerSpec&&h.triggerSpec.queue&&(x=h.triggerSpec.queue);}x==null&&(x="last");}return d.queuedRequests==null&&(d.queuedRequests=[]),x==="first"&&d.queuedRequests.length===0?d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);}):x==="all"?d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);}):x==="last"&&(d.queuedRequests=[],d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);})),maybeCall(s),a}let m=new XMLHttpRequest;d.xhr=m,d.abortable=H;let T=function(){d.xhr=null,d.abortable=!1,d.queuedRequests!=null&&d.queuedRequests.length>0&&d.queuedRequests.shift()();},P=getClosestAttributeValue(n,"hx-prompt");if(P){var I=prompt(P);if(I===null||!triggerEvent(n,"htmx:prompt",{prompt:I,target:c}))return maybeCall(s),T(),a}if(S&&!i&&!confirm(S))return maybeCall(s),T(),a;let R=getHeaders(n,c,I);e!=="get"&&!usesFormData(n)&&(R["Content-Type"]="application/x-www-form-urlencoded"),o.headers&&(R=mergeObjects(R,o.headers));let v=getInputValues(n,e),L=v.errors,N=v.formData;o.values&&overrideFormData(N,formDataFromObject(o.values));let B=formDataFromObject(getExpressionVars(n)),_=overrideFormData(N,B),q=filterValues(_,n);htmx.config.getCacheBusterParam&&e==="get"&&q.set("org.htmx.cache-buster",getRawAttribute(c,"id")||"true"),(t==null||t==="")&&(t=getDocument().location.href);let W=getValuesForElement(n,"hx-request"),z=getInternalData(n).boosted,F=htmx.config.methodsThatUseUrlParams.indexOf(e)>=0,w={boosted:z,useUrlParams:F,formData:q,parameters:formDataProxy(q),unfilteredFormData:_,unfilteredParameters:formDataProxy(_),headers:R,target:c,verb:e,errors:L,withCredentials:o.credentials||W.credentials||htmx.config.withCredentials,timeout:o.timeout||W.timeout||htmx.config.timeout,path:t,triggeringEvent:r};if(!triggerEvent(n,"htmx:configRequest",w))return maybeCall(s),T(),a;if(t=w.path,e=w.verb,R=w.headers,q=formDataFromObject(w.parameters),L=w.errors,F=w.useUrlParams,L&&L.length>0)return triggerEvent(n,"htmx:validation:halted",w),maybeCall(s),T(),a;let J=t.split("#"),$=J[0],X=J[1],D=t;if(F&&(D=$,!q.keys().next().done&&(D.indexOf("?")<0?D+="?":D+="&",D+=urlEncode(q),X&&(D+="#"+X))),!verifyPath(n,D,w))return triggerErrorEvent(n,"htmx:invalidPath",w),maybeCall(l),a;if(m.open(e.toUpperCase(),D,!0),m.overrideMimeType("text/html"),m.withCredentials=w.withCredentials,m.timeout=w.timeout,!W.noHeaders){for(let h in R)if(R.hasOwnProperty(h)){let E=R[h];safelySetHeaderValue(m,h,E);}}let y={xhr:m,target:c,requestConfig:w,etc:o,boosted:z,select:f,pathInfo:{requestPath:t,finalRequestPath:D,responsePath:null,anchor:X}};if(m.onload=function(){try{let h=hierarchyForElt(n);if(y.pathInfo.responsePath=getPathFromResponse(m),u(n,y),y.keepIndicators!==!0&&removeRequestIndicators(M,V),triggerEvent(n,"htmx:afterRequest",y),triggerEvent(n,"htmx:afterOnLoad",y),!bodyContains(n)){let E=null;for(;h.length>0&&E==null;){let O=h.shift();bodyContains(O)&&(E=O);}E&&(triggerEvent(E,"htmx:afterRequest",y),triggerEvent(E,"htmx:afterOnLoad",y));}maybeCall(s),T();}catch(h){throw triggerErrorEvent(n,"htmx:onLoadError",mergeObjects({error:h},y)),h}},m.onerror=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:sendError",y),maybeCall(l),T();},m.onabort=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:sendAbort",y),maybeCall(l),T();},m.ontimeout=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:timeout",y),maybeCall(l),T();},!triggerEvent(n,"htmx:beforeRequest",y))return maybeCall(s),T(),a;var M=addRequestIndicatorClasses(n),V=disableElements(n);forEach(["loadstart","loadend","progress","abort"],function(h){forEach([m,m.upload],function(E){E.addEventListener(h,function(O){triggerEvent(n,"htmx:xhr:"+h,{lengthComputable:O.lengthComputable,loaded:O.loaded,total:O.total});});});}),triggerEvent(n,"htmx:beforeSend",y);let Q=F?null:encodeParamsForBody(m,n,q);return m.send(Q),a}function determineHistoryUpdates(e,t){let n=t.xhr,r=null,o=null;if(hasHeader(n,/HX-Push:/i)?(r=n.getResponseHeader("HX-Push"),o="push"):hasHeader(n,/HX-Push-Url:/i)?(r=n.getResponseHeader("HX-Push-Url"),o="push"):hasHeader(n,/HX-Replace-Url:/i)&&(r=n.getResponseHeader("HX-Replace-Url"),o="replace"),r)return r==="false"?{}:{type:o,path:r};let i=t.pathInfo.finalRequestPath,s=t.pathInfo.responsePath,l=getClosestAttributeValue(e,"hx-push-url"),a=getClosestAttributeValue(e,"hx-replace-url"),u=getInternalData(e).boosted,f=null,c=null;return l?(f="push",c=l):a?(f="replace",c=a):u&&(f="push",c=s||i),c?c==="false"?{}:(c==="true"&&(c=s||i),t.pathInfo.anchor&&c.indexOf("#")===-1&&(c=c+"#"+t.pathInfo.anchor),{type:f,path:c}):{}}function codeMatches(e,t){var n=new RegExp(e.code);return n.test(t.toString(10))}function resolveResponseHandling(e){for(var t=0;t0?getWindow().setTimeout(I,x.swapDelay):I();}c&&triggerErrorEvent(e,"htmx:responseError",mergeObjects({error:"Response Status Error Code "+n.status+" from "+t.pathInfo.requestPath},t));}}let extensions={};function extensionBase(){return {init:function(e){return null},getSelectors:function(){return null},onEvent:function(e,t){return !0},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return !1},handleSwap:function(e,t,n,r){return !1},encodeParameters:function(e,t,n){return null}}}function defineExtension(e,t){t.init&&t.init(internalAPI),extensions[e]=mergeObjects(extensionBase(),t);}function removeExtension(e){delete extensions[e];}function getExtensions(e,t,n){if(t==null&&(t=[]),e==null)return t;n==null&&(n=[]);let r=getAttributeValue(e,"hx-ext");return r&&forEach(r.split(","),function(o){if(o=o.replace(/ /g,""),o.slice(0,7)=="ignore:"){n.push(o.slice(7));return}if(n.indexOf(o)<0){let i=extensions[o];i&&t.indexOf(i)<0&&t.push(i);}}),getExtensions(asElement(parentElt(e)),t,n)}var isReady=!1;getDocument().addEventListener("DOMContentLoaded",function(){isReady=!0;});function ready(e){isReady||getDocument().readyState==="complete"?e():getDocument().addEventListener("DOMContentLoaded",e);}function insertIndicatorStyles(){if(htmx.config.includeIndicatorStyles!==!1){let e=htmx.config.inlineStyleNonce?` nonce="${htmx.config.inlineStyleNonce}"`:"";getDocument().head.insertAdjacentHTML("beforeend"," ."+htmx.config.indicatorClass+"{opacity:0} ."+htmx.config.requestClass+" ."+htmx.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ."+htmx.config.requestClass+"."+htmx.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ");}}function getMetaConfig(){let e=getDocument().querySelector('meta[name="htmx-config"]');return e?parseJSON(e.content):null}function mergeMetaConfig(){let e=getMetaConfig();e&&(htmx.config=mergeObjects(htmx.config,e));}return ready(function(){mergeMetaConfig(),insertIndicatorStyles();let e=getDocument().body;processNode(e);let t=getDocument().querySelectorAll("[hx-trigger='restored'],[data-hx-trigger='restored']");e.addEventListener("htmx:abort",function(r){let o=r.target,i=getInternalData(o);i&&i.xhr&&i.xhr.abort();});let n=window.onpopstate?window.onpopstate.bind(window):null;window.onpopstate=function(r){r.state&&r.state.htmx?(restoreHistory(),forEach(t,function(o){triggerEvent(o,"htmx:restored",{document:getDocument(),triggerEvent});})):n&&n(r);},getWindow().setTimeout(function(){triggerEvent(e,"htmx:load",{}),e=null;},0);}),htmx}(),g=K;function Z(e,t){if(e==="ignore")return !1;let n=e.split("/"),r=t.split("/");for(let o=0;o{if(!(t instanceof CustomEvent))return !1;let n=t.detail.target;return n&&n.children&&Array.from(n.children).forEach(r=>{g.trigger(r,e,null);}),!0},init:function(e){},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return !1},handleSwap:function(e,t,n,r){return !1},encodeParameters:function(e,t,n){},getSelectors:function(){return null}});g.defineExtension("debug",{onEvent:function(e,t){console.debug?console.debug(e):console&&console.log("DEBUG:",e);}});var C=g.config,k,te="hx-target-";function G(e,t){return e.substring(0,t.length)===t}function ne(e,t){if(!e||!t)return null;let n=t.toString(),r=[n,n.substr(0,2)+"*",n.substr(0,2)+"x",n.substr(0,1)+"*",n.substr(0,1)+"x",n.substr(0,1)+"**",n.substr(0,1)+"xx","*","x","***","xxx"];(G(n,"4")||G(n,"5"))&&r.push("error");for(let o=0;o{k=e,C.responseTargetUnsetsError===void 0&&(C.responseTargetUnsetsError=!0),C.responseTargetSetsError===void 0&&(C.responseTargetSetsError=!1),C.responseTargetPrefersExisting===void 0&&(C.responseTargetPrefersExisting=!1),C.responseTargetPrefersRetargetHeader===void 0&&(C.responseTargetPrefersRetargetHeader=!0);},onEvent:(e,t)=>{if(!(t instanceof CustomEvent))return !1;if(e==="htmx:beforeSwap"&&t.detail.xhr&&t.detail.xhr.status!==200){if(t.detail.target&&(C.responseTargetPrefersExisting||C.responseTargetPrefersRetargetHeader&&t.detail.xhr.getAllResponseHeaders().match(/HX-Retarget:/i)))return t.detail.shouldSwap=!0,j(t),!0;if(!t.detail.requestConfig)return !0;let n=ne(t.detail.requestConfig.elt,t.detail.xhr.status);return n&&(j(t),t.detail.shouldSwap=!0,t.detail.target=n),!0}}});g.defineExtension("mutation-error",{onEvent:(e,t)=>{if(!(t instanceof CustomEvent))return !1;if(e==="htmx:afterRequest"){if(!t.detail||!t.detail.xhr)return;let n=t.detail.xhr.status;n>=400&&g.findAll("[hx-on\\:\\:mutation-error]").forEach(r=>{g.trigger(r,"htmx:mutation-error",{status:n});});}}});function Y(e){let t=null,n=o=>{console.log("connecting to ws",e.url,"attempt",o),t=new WebSocket(e.url),t.onmessage=i=>{e.onMessage(i.data);},t.onerror=i=>{try{t?.close();}catch{}t=null;let s=o*(e.reconnectInterval||1e3);setTimeout(()=>n(o+1),s);},t.onclose=()=>{t=null;let i=o*(e.reconnectInterval||1e3);setTimeout(()=>n(o+1),i);};};n(1);let r=o=>{t&&t.readyState===WebSocket.OPEN?t.send(o):setTimeout(()=>r(o),100);};return {sendMessage:r}}var U="";g.defineExtension("livereload",{init:function(){let e=window.location.host;console.log("livereload extension initialized."),Y({url:`ws://${e}/dev/livereload`,onOpen:()=>{},onMessage:t=>{U===""&&(U=t),U!==t&&(U=t,re());},onError:t=>{},onClose:()=>{}});},onEvent:function(e,t){}});function re(){window.location.reload();}function oe(e){let t=window.location.href;setInterval(()=>{window.location.href!==t&&(e(t,window.location.href),t=window.location.href);},100);}oe((e,t)=>{ie(t);});function ie(e){let t=new URL(e);document.querySelectorAll("[hx-trigger]").forEach(function(n){let r=n.getAttribute("hx-trigger");if(!r)return;if(r.split(", ").find(i=>i==="url"))g.swap(n,"url",{swapStyle:"outerHTML",swapDelay:0,settleDelay:0});else for(let[i,s]of t.searchParams){let l="qs:"+i;if(r.includes(l)){console.log("triggering",l),g.trigger(n,l,null);break}}}),document.querySelectorAll("[hx-match-qp]").forEach(n=>{let r=!1;for(let o of n.getAttributeNames())if(o.startsWith("hx-match-qp-mapping:")){let i=o.replace("hx-match-qp-mapping:","");if(t.searchParams.get(i)){g.swap(n,n.getAttribute(o)??"",{swapStyle:"innerHTML",swapDelay:0,settleDelay:0}),r=!0;break}}if(!r){let o=n.getAttribute("hx-match-qp-default");o&&g.swap(n,n.getAttribute("hx-match-qp-mapping:"+o)??"",{swapStyle:"innerHTML",swapDelay:0,settleDelay:0});}});} +var K=function(){let htmx={onLoad:null,process:null,on:null,off:null,trigger:null,ajax:null,find:null,findAll:null,closest:null,values:function(e,t){return getInputValues(e,t||"post").values},remove:null,addClass:null,removeClass:null,toggleClass:null,takeClass:null,swap:null,defineExtension:null,removeExtension:null,logAll:null,logNone:null,logger:null,config:{historyEnabled:!0,historyCacheSize:10,refreshOnHistoryMiss:!1,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:20,includeIndicatorStyles:!0,indicatorClass:"htmx-indicator",requestClass:"htmx-request",addedClass:"htmx-added",settlingClass:"htmx-settling",swappingClass:"htmx-swapping",allowEval:!0,allowScriptTags:!0,inlineScriptNonce:"",inlineStyleNonce:"",attributesToSettle:["class","style","width","height"],withCredentials:!1,timeout:0,wsReconnectDelay:"full-jitter",wsBinaryType:"blob",disableSelector:"[hx-disable], [data-hx-disable]",scrollBehavior:"instant",defaultFocusScroll:!1,getCacheBusterParam:!1,globalViewTransitions:!1,methodsThatUseUrlParams:["get","delete"],selfRequestsOnly:!0,ignoreTitle:!1,scrollIntoViewOnBoost:!0,triggerSpecsCache:null,disableInheritance:!1,responseHandling:[{code:"204",swap:!1},{code:"[23]..",swap:!0},{code:"[45]..",swap:!1,error:!0}],allowNestedOobSwaps:!0},parseInterval:null,_:null,version:"2.0.2"};htmx.onLoad=onLoadHelper,htmx.process=processNode,htmx.on=addEventListenerImpl,htmx.off=removeEventListenerImpl,htmx.trigger=triggerEvent,htmx.ajax=ajaxHelper,htmx.find=find,htmx.findAll=findAll,htmx.closest=closest,htmx.remove=removeElement,htmx.addClass=addClassToElement,htmx.removeClass=removeClassFromElement,htmx.toggleClass=toggleClassOnElement,htmx.takeClass=takeClassForElement,htmx.swap=swap,htmx.defineExtension=defineExtension,htmx.removeExtension=removeExtension,htmx.logAll=logAll,htmx.logNone=logNone,htmx.parseInterval=parseInterval,htmx._=internalEval;let internalAPI={addTriggerHandler,bodyContains,canAccessLocalStorage,findThisElement,filterValues,swap,hasAttribute,getAttributeValue,getClosestAttributeValue,getClosestMatch,getExpressionVars,getHeaders,getInputValues,getInternalData,getSwapSpecification,getTriggerSpecs,getTarget,makeFragment,mergeObjects,makeSettleInfo,oobSwap,querySelectorExt,settleImmediately,shouldCancel,triggerEvent,triggerErrorEvent,withExtensions},VERBS=["get","post","put","delete","patch"],VERB_SELECTOR=VERBS.map(function(e){return "[hx-"+e+"], [data-hx-"+e+"]"}).join(", "),HEAD_TAG_REGEX=makeTagRegEx("head");function makeTagRegEx(e,t=!1){return new RegExp(`<${e}(\\s[^>]*>|>)([\\s\\S]*?)<\\/${e}>`,t?"gim":"im")}function parseInterval(e){if(e==null)return;let t=NaN;return e.slice(-2)=="ms"?t=parseFloat(e.slice(0,-2)):e.slice(-1)=="s"?t=parseFloat(e.slice(0,-1))*1e3:e.slice(-1)=="m"?t=parseFloat(e.slice(0,-1))*1e3*60:t=parseFloat(e),isNaN(t)?void 0:t}function getRawAttribute(e,t){return e instanceof Element&&e.getAttribute(t)}function hasAttribute(e,t){return !!e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function getAttributeValue(e,t){return getRawAttribute(e,t)||getRawAttribute(e,"data-"+t)}function parentElt(e){let t=e.parentElement;return !t&&e.parentNode instanceof ShadowRoot?e.parentNode:t}function getDocument(){return document}function getRootNode(e,t){return e.getRootNode?e.getRootNode({composed:t}):getDocument()}function getClosestMatch(e,t){for(;e&&!t(e);)e=parentElt(e);return e||null}function getAttributeValueWithDisinheritance(e,t,n){let r=getAttributeValue(t,n),o=getAttributeValue(t,"hx-disinherit");var i=getAttributeValue(t,"hx-inherit");if(e!==t){if(htmx.config.disableInheritance)return i&&(i==="*"||i.split(" ").indexOf(n)>=0)?r:null;if(o&&(o==="*"||o.split(" ").indexOf(n)>=0))return "unset"}return r}function getClosestAttributeValue(e,t){let n=null;if(getClosestMatch(e,function(r){return !!(n=getAttributeValueWithDisinheritance(e,asElement(r),t))}),n!=="unset")return n}function matches(e,t){let n=e instanceof Element&&(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector);return !!n&&n.call(e,t)}function getStartTag(e){let n=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i.exec(e);return n?n[1].toLowerCase():""}function parseHTML(e){return new DOMParser().parseFromString(e,"text/html")}function takeChildrenFor(e,t){for(;t.childNodes.length>0;)e.append(t.childNodes[0]);}function duplicateScript(e){let t=getDocument().createElement("script");return forEach(e.attributes,function(n){t.setAttribute(n.name,n.value);}),t.textContent=e.textContent,t.async=!1,htmx.config.inlineScriptNonce&&(t.nonce=htmx.config.inlineScriptNonce),t}function isJavaScriptScriptNode(e){return e.matches("script")&&(e.type==="text/javascript"||e.type==="module"||e.type==="")}function normalizeScriptTags(e){Array.from(e.querySelectorAll("script")).forEach(t=>{if(isJavaScriptScriptNode(t)){let n=duplicateScript(t),r=t.parentNode;try{r.insertBefore(n,t);}catch(o){logError(o);}finally{t.remove();}}});}function makeFragment(e){let t=e.replace(HEAD_TAG_REGEX,""),n=getStartTag(t),r;if(n==="html"){r=new DocumentFragment;let i=parseHTML(e);takeChildrenFor(r,i.body),r.title=i.title;}else if(n==="body"){r=new DocumentFragment;let i=parseHTML(t);takeChildrenFor(r,i.body),r.title=i.title;}else {let i=parseHTML('");r=i.querySelector("template").content,r.title=i.title;var o=r.querySelector("title");o&&o.parentNode===r&&(o.remove(),r.title=o.innerText);}return r&&(htmx.config.allowScriptTags?normalizeScriptTags(r):r.querySelectorAll("script").forEach(i=>i.remove())),r}function maybeCall(e){e&&e();}function isType(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function isFunction(e){return typeof e=="function"}function isRawObject(e){return isType(e,"Object")}function getInternalData(e){let t="htmx-internal-data",n=e[t];return n||(n=e[t]={}),n}function toArray(e){let t=[];if(e)for(let n=0;n=0}function bodyContains(e){let t=e.getRootNode&&e.getRootNode();return t&&t instanceof window.ShadowRoot?getDocument().body.contains(t.host):getDocument().body.contains(e)}function splitOnWhitespace(e){return e.trim().split(/\s+/)}function mergeObjects(e,t){for(let n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}function parseJSON(e){try{return JSON.parse(e)}catch(t){return logError(t),null}}function canAccessLocalStorage(){let e="htmx:localStorageTest";try{return localStorage.setItem(e,e),localStorage.removeItem(e),!0}catch{return !1}}function normalizePath(e){try{let t=new URL(e);return t&&(e=t.pathname+t.search),/^\/$/.test(e)||(e=e.replace(/\/+$/,"")),e}catch{return e}}function internalEval(str){return maybeEval(getDocument().body,function(){return eval(str)})}function onLoadHelper(e){return htmx.on("htmx:load",function(n){e(n.detail.elt);})}function logAll(){htmx.logger=function(e,t,n){console&&console.log(t,e,n);};}function logNone(){htmx.logger=null;}function find(e,t){return typeof e!="string"?e.querySelector(t):find(getDocument(),e)}function findAll(e,t){return typeof e!="string"?e.querySelectorAll(t):findAll(getDocument(),e)}function getWindow(){return window}function removeElement(e,t){e=resolveTarget(e),t?getWindow().setTimeout(function(){removeElement(e),e=null;},t):parentElt(e).removeChild(e);}function asElement(e){return e instanceof Element?e:null}function asHtmlElement(e){return e instanceof HTMLElement?e:null}function asString(e){return typeof e=="string"?e:null}function asParentNode(e){return e instanceof Element||e instanceof Document||e instanceof DocumentFragment?e:null}function addClassToElement(e,t,n){e=asElement(resolveTarget(e)),e&&(n?getWindow().setTimeout(function(){addClassToElement(e,t),e=null;},n):e.classList&&e.classList.add(t));}function removeClassFromElement(e,t,n){let r=asElement(resolveTarget(e));r&&(n?getWindow().setTimeout(function(){removeClassFromElement(r,t),r=null;},n):r.classList&&(r.classList.remove(t),r.classList.length===0&&r.removeAttribute("class")));}function toggleClassOnElement(e,t){e=resolveTarget(e),e.classList.toggle(t);}function takeClassForElement(e,t){e=resolveTarget(e),forEach(e.parentElement.children,function(n){removeClassFromElement(n,t);}),addClassToElement(asElement(e),t);}function closest(e,t){if(e=asElement(resolveTarget(e)),e&&e.closest)return e.closest(t);do if(e==null||matches(e,t))return e;while(e=e&&asElement(parentElt(e)));return null}function startsWith(e,t){return e.substring(0,t.length)===t}function endsWith(e,t){return e.substring(e.length-t.length)===t}function normalizeSelector(e){let t=e.trim();return startsWith(t,"<")&&endsWith(t,"/>")?t.substring(1,t.length-2):t}function querySelectorAllExt(e,t,n){return e=resolveTarget(e),t.indexOf("closest ")===0?[closest(asElement(e),normalizeSelector(t.substr(8)))]:t.indexOf("find ")===0?[find(asParentNode(e),normalizeSelector(t.substr(5)))]:t==="next"?[asElement(e).nextElementSibling]:t.indexOf("next ")===0?[scanForwardQuery(e,normalizeSelector(t.substr(5)),!!n)]:t==="previous"?[asElement(e).previousElementSibling]:t.indexOf("previous ")===0?[scanBackwardsQuery(e,normalizeSelector(t.substr(9)),!!n)]:t==="document"?[document]:t==="window"?[window]:t==="body"?[document.body]:t==="root"?[getRootNode(e,!!n)]:t.indexOf("global ")===0?querySelectorAllExt(e,t.slice(7),!0):toArray(asParentNode(getRootNode(e,!!n)).querySelectorAll(normalizeSelector(t)))}var scanForwardQuery=function(e,t,n){let r=asParentNode(getRootNode(e,n)).querySelectorAll(t);for(let o=0;o=0;o--){let i=r[o];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_FOLLOWING)return i}};function querySelectorExt(e,t){return typeof e!="string"?querySelectorAllExt(e,t)[0]:querySelectorAllExt(getDocument().body,e)[0]}function resolveTarget(e,t){return typeof e=="string"?find(asParentNode(t)||document,e):e}function processEventArgs(e,t,n){return isFunction(t)?{target:getDocument().body,event:asString(e),listener:t}:{target:resolveTarget(e),event:asString(t),listener:n}}function addEventListenerImpl(e,t,n){return ready(function(){let o=processEventArgs(e,t,n);o.target.addEventListener(o.event,o.listener);}),isFunction(t)?t:n}function removeEventListenerImpl(e,t,n){return ready(function(){let r=processEventArgs(e,t,n);r.target.removeEventListener(r.event,r.listener);}),isFunction(t)?t:n}let DUMMY_ELT=getDocument().createElement("output");function findAttributeTargets(e,t){let n=getClosestAttributeValue(e,t);if(n){if(n==="this")return [findThisElement(e,t)];{let r=querySelectorAllExt(e,n);return r.length===0?(logError('The selector "'+n+'" on '+t+" returned no matches!"),[DUMMY_ELT]):r}}}function findThisElement(e,t){return asElement(getClosestMatch(e,function(n){return getAttributeValue(asElement(n),t)!=null}))}function getTarget(e){let t=getClosestAttributeValue(e,"hx-target");return t?t==="this"?findThisElement(e,"hx-target"):querySelectorExt(e,t):getInternalData(e).boosted?getDocument().body:e}function shouldSettleAttribute(e){let t=htmx.config.attributesToSettle;for(let n=0;n0?(o=e.substr(0,e.indexOf(":")),r=e.substr(e.indexOf(":")+1,e.length)):o=e);let i=getDocument().querySelectorAll(r);return i?(forEach(i,function(s){let l,a=t.cloneNode(!0);l=getDocument().createDocumentFragment(),l.appendChild(a),isInlineSwap(o,s)||(l=asParentNode(a));let u={shouldSwap:!0,target:s,fragment:l};triggerEvent(s,"htmx:oobBeforeSwap",u)&&(s=u.target,u.shouldSwap&&swapWithStyle(o,s,s,l,n),forEach(n.elts,function(f){triggerEvent(f,"htmx:oobAfterSwap",u);}));}),t.parentNode.removeChild(t)):(t.parentNode.removeChild(t),triggerErrorEvent(getDocument().body,"htmx:oobErrorNoTarget",{content:t})),e}function handlePreservedElements(e){forEach(findAll(e,"[hx-preserve], [data-hx-preserve]"),function(t){let n=getAttributeValue(t,"id"),r=getDocument().getElementById(n);r!=null&&t.parentNode.replaceChild(r,t);});}function handleAttributes(e,t,n){forEach(t.querySelectorAll("[id]"),function(r){let o=getRawAttribute(r,"id");if(o&&o.length>0){let i=o.replace("'","\\'"),s=r.tagName.replace(":","\\:"),l=asParentNode(e),a=l&&l.querySelector(s+"[id='"+i+"']");if(a&&a!==l){let u=r.cloneNode();cloneAttributes(r,a),n.tasks.push(function(){cloneAttributes(r,u);});}}});}function makeAjaxLoadTask(e){return function(){removeClassFromElement(e,htmx.config.addedClass),processNode(asElement(e)),processFocus(asParentNode(e)),triggerEvent(e,"htmx:load");}}function processFocus(e){let t="[autofocus]",n=asHtmlElement(matches(e,t)?e:e.querySelector(t));n?.focus();}function insertNodesBefore(e,t,n,r){for(handleAttributes(e,n,r);n.childNodes.length>0;){let o=n.firstChild;addClassToElement(asElement(o),htmx.config.addedClass),e.insertBefore(o,t),o.nodeType!==Node.TEXT_NODE&&o.nodeType!==Node.COMMENT_NODE&&r.tasks.push(makeAjaxLoadTask(o));}}function stringHash(e,t){let n=0;for(;n0}function swap(e,t,n,r){r||(r={}),e=resolveTarget(e);let o=document.activeElement,i={};try{i={elt:o,start:o?o.selectionStart:null,end:o?o.selectionEnd:null};}catch{}let s=makeSettleInfo(e);if(n.swapStyle==="textContent")e.textContent=t;else {let a=makeFragment(t);if(s.title=a.title,r.selectOOB){let u=r.selectOOB.split(",");for(let f=0;f0?getWindow().setTimeout(l,n.settleDelay):l();}function handleTriggerHeader(e,t,n){let r=e.getResponseHeader(t);if(r.indexOf("{")===0){let o=parseJSON(r);for(let i in o)if(o.hasOwnProperty(i)){let s=o[i];isRawObject(s)?n=s.target!==void 0?s.target:n:s={value:s},triggerEvent(n,i,s);}}else {let o=r.split(",");for(let i=0;i0;){let s=t[0];if(s==="]"){if(r--,r===0){i===null&&(o=o+"true"),t.shift(),o+=")})";try{let l=maybeEval(e,function(){return Function(o)()},function(){return !0});return l.source=o,l}catch(l){return triggerErrorEvent(getDocument().body,"htmx:syntax:error",{error:l,source:o}),null}}}else s==="["&&r++;isPossibleRelativeReference(s,i,n)?o+="(("+n+"."+s+") ? ("+n+"."+s+") : (window."+s+"))":o=o+s,i=t.shift();}}}function consumeUntil(e,t){let n="";for(;e.length>0&&!t.test(e[0]);)n+=e.shift();return n}function consumeCSSSelector(e){let t;return e.length>0&&COMBINED_SELECTOR_START.test(e[0])?(e.shift(),t=consumeUntil(e,COMBINED_SELECTOR_END).trim(),e.shift()):t=consumeUntil(e,WHITESPACE_OR_COMMA),t}let INPUT_SELECTOR="input, textarea, select";function parseAndCacheTrigger(e,t,n){let r=[],o=tokenizeString(t);do{consumeUntil(o,NOT_WHITESPACE);let l=o.length,a=consumeUntil(o,/[,\[\s]/);if(a!=="")if(a==="every"){let u={trigger:"every"};consumeUntil(o,NOT_WHITESPACE),u.pollInterval=parseInterval(consumeUntil(o,/[,\[\s]/)),consumeUntil(o,NOT_WHITESPACE);var i=maybeGenerateConditional(e,o,"event");i&&(u.eventFilter=i),r.push(u);}else {let u={trigger:a};var i=maybeGenerateConditional(e,o,"event");for(i&&(u.eventFilter=i);o.length>0&&o[0]!==",";){consumeUntil(o,NOT_WHITESPACE);let c=o.shift();if(c==="changed")u.changed=!0;else if(c==="once")u.once=!0;else if(c==="consume")u.consume=!0;else if(c==="delay"&&o[0]===":")o.shift(),u.delay=parseInterval(consumeUntil(o,WHITESPACE_OR_COMMA));else if(c==="from"&&o[0]===":"){if(o.shift(),COMBINED_SELECTOR_START.test(o[0]))var s=consumeCSSSelector(o);else {var s=consumeUntil(o,WHITESPACE_OR_COMMA);if(s==="closest"||s==="find"||s==="next"||s==="previous"){o.shift();let b=consumeCSSSelector(o);b.length>0&&(s+=" "+b);}}u.from=s;}else c==="target"&&o[0]===":"?(o.shift(),u.target=consumeCSSSelector(o)):c==="throttle"&&o[0]===":"?(o.shift(),u.throttle=parseInterval(consumeUntil(o,WHITESPACE_OR_COMMA))):c==="queue"&&o[0]===":"?(o.shift(),u.queue=consumeUntil(o,WHITESPACE_OR_COMMA)):c==="root"&&o[0]===":"?(o.shift(),u[c]=consumeCSSSelector(o)):c==="threshold"&&o[0]===":"?(o.shift(),u[c]=consumeUntil(o,WHITESPACE_OR_COMMA)):triggerErrorEvent(e,"htmx:syntax:error",{token:o.shift()});}r.push(u);}o.length===l&&triggerErrorEvent(e,"htmx:syntax:error",{token:o.shift()}),consumeUntil(o,NOT_WHITESPACE);}while(o[0]===","&&o.shift());return n&&(n[t]=r),r}function getTriggerSpecs(e){let t=getAttributeValue(e,"hx-trigger"),n=[];if(t){let r=htmx.config.triggerSpecsCache;n=r&&r[t]||parseAndCacheTrigger(e,t,r);}return n.length>0?n:matches(e,"form")?[{trigger:"submit"}]:matches(e,'input[type="button"], input[type="submit"]')?[{trigger:"click"}]:matches(e,INPUT_SELECTOR)?[{trigger:"change"}]:[{trigger:"click"}]}function cancelPolling(e){getInternalData(e).cancelled=!0;}function processPolling(e,t,n){let r=getInternalData(e);r.timeout=getWindow().setTimeout(function(){bodyContains(e)&&r.cancelled!==!0&&(maybeFilterEvent(n,e,makeEvent("hx:poll:trigger",{triggerSpec:n,target:e}))||t(e),processPolling(e,t,n));},n.pollInterval);}function isLocalLink(e){return location.hostname===e.hostname&&getRawAttribute(e,"href")&&getRawAttribute(e,"href").indexOf("#")!==0}function eltIsDisabled(e){return closest(e,htmx.config.disableSelector)}function boostElement(e,t,n){if(e instanceof HTMLAnchorElement&&isLocalLink(e)&&(e.target===""||e.target==="_self")||e.tagName==="FORM"&&String(getRawAttribute(e,"method")).toLowerCase()!=="dialog"){t.boosted=!0;let r,o;if(e.tagName==="A")r="get",o=getRawAttribute(e,"href");else {let i=getRawAttribute(e,"method");r=i?i.toLowerCase():"get",o=getRawAttribute(e,"action");}n.forEach(function(i){addEventListener(e,function(s,l){let a=asElement(s);if(eltIsDisabled(a)){cleanUpElement(a);return}issueAjaxRequest(r,o,a,l);},t,i,!0);});}}function shouldCancel(e,t){let n=asElement(t);return n?!!((e.type==="submit"||e.type==="click")&&(n.tagName==="FORM"||matches(n,'input[type="submit"], button')&&closest(n,"form")!==null||n instanceof HTMLAnchorElement&&n.href&&(n.getAttribute("href")==="#"||n.getAttribute("href").indexOf("#")!==0))):!1}function ignoreBoostedAnchorCtrlClick(e,t){return getInternalData(e).boosted&&e instanceof HTMLAnchorElement&&t.type==="click"&&(t.ctrlKey||t.metaKey)}function maybeFilterEvent(e,t,n){let r=e.eventFilter;if(r)try{return r.call(t,n)!==!0}catch(o){let i=r.source;return triggerErrorEvent(getDocument().body,"htmx:eventFilter:error",{error:o,source:i}),!0}return !1}function addEventListener(e,t,n,r,o){let i=getInternalData(e),s;r.from?s=querySelectorAllExt(e,r.from):s=[e],r.changed&&s.forEach(function(l){let a=getInternalData(l);a.lastValue=l.value;}),forEach(s,function(l){let a=function(u){if(!bodyContains(e)){l.removeEventListener(r.trigger,a);return}if(ignoreBoostedAnchorCtrlClick(e,u)||((o||shouldCancel(u,e))&&u.preventDefault(),maybeFilterEvent(r,e,u)))return;let f=getInternalData(u);if(f.triggerSpec=r,f.handledFor==null&&(f.handledFor=[]),f.handledFor.indexOf(e)<0){if(f.handledFor.push(e),r.consume&&u.stopPropagation(),r.target&&u.target&&!matches(asElement(u.target),r.target))return;if(r.once){if(i.triggeredOnce)return;i.triggeredOnce=!0;}if(r.changed){let c=getInternalData(l),d=l.value;if(c.lastValue===d)return;c.lastValue=d;}if(i.delayed&&clearTimeout(i.delayed),i.throttle)return;r.throttle>0?i.throttle||(triggerEvent(e,"htmx:trigger"),t(e,u),i.throttle=getWindow().setTimeout(function(){i.throttle=null;},r.throttle)):r.delay>0?i.delayed=getWindow().setTimeout(function(){triggerEvent(e,"htmx:trigger"),t(e,u);},r.delay):(triggerEvent(e,"htmx:trigger"),t(e,u));}};n.listenerInfos==null&&(n.listenerInfos=[]),n.listenerInfos.push({trigger:r.trigger,listener:a,on:l}),l.addEventListener(r.trigger,a);});}let windowIsScrolling=!1,scrollHandler=null;function initScrollHandler(){scrollHandler||(scrollHandler=function(){windowIsScrolling=!0;},window.addEventListener("scroll",scrollHandler),setInterval(function(){windowIsScrolling&&(windowIsScrolling=!1,forEach(getDocument().querySelectorAll("[hx-trigger*='revealed'],[data-hx-trigger*='revealed']"),function(e){maybeReveal(e);}));},200));}function maybeReveal(e){!hasAttribute(e,"data-hx-revealed")&&isScrolledIntoView(e)&&(e.setAttribute("data-hx-revealed","true"),getInternalData(e).initHash?triggerEvent(e,"revealed"):e.addEventListener("htmx:afterProcessNode",function(){triggerEvent(e,"revealed");},{once:!0}));}function loadImmediately(e,t,n,r){let o=function(){n.loaded||(n.loaded=!0,t(e));};r>0?getWindow().setTimeout(o,r):o();}function processVerbs(e,t,n){let r=!1;return forEach(VERBS,function(o){if(hasAttribute(e,"hx-"+o)){let i=getAttributeValue(e,"hx-"+o);r=!0,t.path=i,t.verb=o,n.forEach(function(s){addTriggerHandler(e,s,t,function(l,a){let u=asElement(l);if(closest(u,htmx.config.disableSelector)){cleanUpElement(u);return}issueAjaxRequest(o,i,u,a);});});}}),r}function addTriggerHandler(e,t,n,r){if(t.trigger==="revealed")initScrollHandler(),addEventListener(e,r,n,t),maybeReveal(asElement(e));else if(t.trigger==="intersect"){let o={};t.root&&(o.root=querySelectorExt(e,t.root)),t.threshold&&(o.threshold=parseFloat(t.threshold)),new IntersectionObserver(function(s){for(let l=0;l0?(n.polling=!0,processPolling(asElement(e),r,t)):addEventListener(e,r,n,t);}function shouldProcessHxOn(e){let t=asElement(e);if(!t)return !1;let n=t.attributes;for(let r=0;r", "+i).join(""))}else return []}function maybeSetLastButtonClicked(e){let t=closest(asElement(e.target),"button, input[type='submit']"),n=getRelatedFormData(e);n&&(n.lastButtonClicked=t);}function maybeUnsetLastButtonClicked(e){let t=getRelatedFormData(e);t&&(t.lastButtonClicked=null);}function getRelatedFormData(e){let t=closest(asElement(e.target),"button, input[type='submit']");if(!t)return;let n=resolveTarget("#"+getRawAttribute(t,"form"),t.getRootNode())||closest(t,"form");if(n)return getInternalData(n)}function initButtonTracking(e){e.addEventListener("click",maybeSetLastButtonClicked),e.addEventListener("focusin",maybeSetLastButtonClicked),e.addEventListener("focusout",maybeUnsetLastButtonClicked);}function addHxOnEventHandler(e,t,n){let r=getInternalData(e);Array.isArray(r.onHandlers)||(r.onHandlers=[]);let o,i=function(s){maybeEval(e,function(){eltIsDisabled(e)||(o||(o=new Function("event",n)),o.call(e,s));});};e.addEventListener(t,i),r.onHandlers.push({event:t,listener:i});}function processHxOnWildcard(e){deInitOnHandlers(e);for(let t=0;thtmx.config.historyCacheSize;)i.shift();for(;i.length>0;)try{localStorage.setItem("htmx-history-cache",JSON.stringify(i));break}catch(l){triggerErrorEvent(getDocument().body,"htmx:historyCacheError",{cause:l,cache:i}),i.shift();}}function getCachedHistory(e){if(!canAccessLocalStorage())return null;e=normalizePath(e);let t=parseJSON(localStorage.getItem("htmx-history-cache"))||[];for(let n=0;n=200&&this.status<400){triggerEvent(getDocument().body,"htmx:historyCacheMissLoad",n);let r=makeFragment(this.response),o=r.querySelector("[hx-history-elt],[data-hx-history-elt]")||r,i=getHistoryElement(),s=makeSettleInfo(i);handleTitle(r.title),swapInnerHTML(i,o,s),settleImmediately(s.tasks),currentPathForHistory=e,triggerEvent(getDocument().body,"htmx:historyRestore",{path:e,cacheMiss:!0,serverResponse:this.response});}else triggerErrorEvent(getDocument().body,"htmx:historyCacheMissLoadError",n);},t.send();}function restoreHistory(e){saveCurrentPageToHistory(),e=e||location.pathname+location.search;let t=getCachedHistory(e);if(t){let n=makeFragment(t.content),r=getHistoryElement(),o=makeSettleInfo(r);handleTitle(n.title),swapInnerHTML(r,n,o),settleImmediately(o.tasks),getWindow().setTimeout(function(){window.scrollTo(0,t.scroll);},0),currentPathForHistory=e,triggerEvent(getDocument().body,"htmx:historyRestore",{path:e,item:t});}else htmx.config.refreshOnHistoryMiss?window.location.reload(!0):loadHistoryFromServer(e);}function addRequestIndicatorClasses(e){let t=findAttributeTargets(e,"hx-indicator");return t==null&&(t=[e]),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)+1,n.classList.add.call(n.classList,htmx.config.requestClass);}),t}function disableElements(e){let t=findAttributeTargets(e,"hx-disabled-elt");return t==null&&(t=[]),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)+1,n.setAttribute("disabled",""),n.setAttribute("data-disabled-by-htmx","");}),t}function removeRequestIndicators(e,t){forEach(e,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)-1,r.requestCount===0&&n.classList.remove.call(n.classList,htmx.config.requestClass);}),forEach(t,function(n){let r=getInternalData(n);r.requestCount=(r.requestCount||0)-1,r.requestCount===0&&(n.removeAttribute("disabled"),n.removeAttribute("data-disabled-by-htmx"));});}function haveSeenNode(e,t){for(let n=0;nt.indexOf(o)<0):r=r.filter(o=>o!==t),n.delete(e),forEach(r,o=>n.append(e,o));}}function processInputValue(e,t,n,r,o){if(!(r==null||haveSeenNode(e,r))){if(e.push(r),shouldInclude(r)){let i=getRawAttribute(r,"name"),s=r.value;r instanceof HTMLSelectElement&&r.multiple&&(s=toArray(r.querySelectorAll("option:checked")).map(function(l){return l.value})),r instanceof HTMLInputElement&&r.files&&(s=toArray(r.files)),addValueToFormData(i,s,t),o&&validateElement(r,n);}r instanceof HTMLFormElement&&(forEach(r.elements,function(i){e.indexOf(i)>=0?removeValueFromFormData(i.name,i.value,t):e.push(i),o&&validateElement(i,n);}),new FormData(r).forEach(function(i,s){i instanceof File&&i.name===""||addValueToFormData(s,i,t);}));}}function validateElement(e,t){let n=e;n.willValidate&&(triggerEvent(n,"htmx:validation:validate"),n.checkValidity()||(t.push({elt:n,message:n.validationMessage,validity:n.validity}),triggerEvent(n,"htmx:validation:failed",{message:n.validationMessage,validity:n.validity})));}function overrideFormData(e,t){for(let n of t.keys())e.delete(n);return t.forEach(function(n,r){e.append(r,n);}),e}function getInputValues(e,t){let n=[],r=new FormData,o=new FormData,i=[],s=getInternalData(e);s.lastButtonClicked&&!bodyContains(s.lastButtonClicked)&&(s.lastButtonClicked=null);let l=e instanceof HTMLFormElement&&e.noValidate!==!0||getAttributeValue(e,"hx-validate")==="true";if(s.lastButtonClicked&&(l=l&&s.lastButtonClicked.formNoValidate!==!0),t!=="get"&&processInputValue(n,o,i,closest(e,"form"),l),processInputValue(n,r,i,e,l),s.lastButtonClicked||e.tagName==="BUTTON"||e.tagName==="INPUT"&&getRawAttribute(e,"type")==="submit"){let u=s.lastButtonClicked||e,f=getRawAttribute(u,"name");addValueToFormData(f,u.value,o);}let a=findAttributeTargets(e,"hx-include");return forEach(a,function(u){processInputValue(n,r,i,asElement(u),l),matches(u,"form")||forEach(asParentNode(u).querySelectorAll(INPUT_SELECTOR),function(f){processInputValue(n,r,i,f,l);});}),overrideFormData(r,o),{errors:i,formData:r,values:formDataProxy(r)}}function appendParam(e,t,n){e!==""&&(e+="&"),String(n)==="[object Object]"&&(n=JSON.stringify(n));let r=encodeURIComponent(n);return e+=encodeURIComponent(t)+"="+r,e}function urlEncode(e){e=formDataFromObject(e);let t="";return e.forEach(function(n,r){t=appendParam(t,r,n);}),t}function getHeaders(e,t,n){let r={"HX-Request":"true","HX-Trigger":getRawAttribute(e,"id"),"HX-Trigger-Name":getRawAttribute(e,"name"),"HX-Target":getAttributeValue(t,"id"),"HX-Current-URL":getDocument().location.href};return getValuesForElement(e,"hx-headers",!1,r),n!==void 0&&(r["HX-Prompt"]=n),getInternalData(e).boosted&&(r["HX-Boosted"]="true"),r}function filterValues(e,t){let n=getClosestAttributeValue(t,"hx-params");if(n){if(n==="none")return new FormData;if(n==="*")return e;if(n.indexOf("not ")===0)return forEach(n.substr(4).split(","),function(r){r=r.trim(),e.delete(r);}),e;{let r=new FormData;return forEach(n.split(","),function(o){o=o.trim(),e.has(o)&&e.getAll(o).forEach(function(i){r.append(o,i);});}),r}}else return e}function isAnchorLink(e){return !!getRawAttribute(e,"href")&&getRawAttribute(e,"href").indexOf("#")>=0}function getSwapSpecification(e,t){let n=t||getClosestAttributeValue(e,"hx-swap"),r={swapStyle:getInternalData(e).boosted?"innerHTML":htmx.config.defaultSwapStyle,swapDelay:htmx.config.defaultSwapDelay,settleDelay:htmx.config.defaultSettleDelay};if(htmx.config.scrollIntoViewOnBoost&&getInternalData(e).boosted&&!isAnchorLink(e)&&(r.show="top"),n){let s=splitOnWhitespace(n);if(s.length>0)for(let l=0;l0?o.join(":"):null;r.scroll=f,r.scrollTarget=i;}else if(a.indexOf("show:")===0){var o=a.substr(5).split(":");let c=o.pop();var i=o.length>0?o.join(":"):null;r.show=c,r.showTarget=i;}else if(a.indexOf("focus-scroll:")===0){let u=a.substr(13);r.focusScroll=u=="true";}else l==0?r.swapStyle=a:logError("Unknown modifier in hx-swap: "+a);}}return r}function usesFormData(e){return getClosestAttributeValue(e,"hx-encoding")==="multipart/form-data"||matches(e,"form")&&getRawAttribute(e,"enctype")==="multipart/form-data"}function encodeParamsForBody(e,t,n){let r=null;return withExtensions(t,function(o){r==null&&(r=o.encodeParameters(e,n,t));}),r??(usesFormData(t)?overrideFormData(new FormData,formDataFromObject(n)):urlEncode(n))}function makeSettleInfo(e){return {tasks:[],elts:[e]}}function updateScrollState(e,t){let n=e[0],r=e[e.length-1];if(t.scroll){var o=null;t.scrollTarget&&(o=asElement(querySelectorExt(n,t.scrollTarget))),t.scroll==="top"&&(n||o)&&(o=o||n,o.scrollTop=0),t.scroll==="bottom"&&(r||o)&&(o=o||r,o.scrollTop=o.scrollHeight);}if(t.show){var o=null;if(t.showTarget){let s=t.showTarget;t.showTarget==="window"&&(s="body"),o=asElement(querySelectorExt(n,s));}t.show==="top"&&(n||o)&&(o=o||n,o.scrollIntoView({block:"start",behavior:htmx.config.scrollBehavior})),t.show==="bottom"&&(r||o)&&(o=o||r,o.scrollIntoView({block:"end",behavior:htmx.config.scrollBehavior}));}}function getValuesForElement(e,t,n,r){if(r==null&&(r={}),e==null)return r;let o=getAttributeValue(e,t);if(o){let i=o.trim(),s=n;if(i==="unset")return null;i.indexOf("javascript:")===0?(i=i.substr(11),s=!0):i.indexOf("js:")===0&&(i=i.substr(3),s=!0),i.indexOf("{")!==0&&(i="{"+i+"}");let l;s?l=maybeEval(e,function(){return Function("return ("+i+")")()},{}):l=parseJSON(i);for(let a in l)l.hasOwnProperty(a)&&r[a]==null&&(r[a]=l[a]);}return getValuesForElement(asElement(parentElt(e)),t,n,r)}function maybeEval(e,t,n){return htmx.config.allowEval?t():(triggerErrorEvent(e,"htmx:evalDisallowedError"),n)}function getHXVarsForElement(e,t){return getValuesForElement(e,"hx-vars",!0,t)}function getHXValsForElement(e,t){return getValuesForElement(e,"hx-vals",!1,t)}function getExpressionVars(e){return mergeObjects(getHXVarsForElement(e),getHXValsForElement(e))}function safelySetHeaderValue(e,t,n){if(n!==null)try{e.setRequestHeader(t,n);}catch{e.setRequestHeader(t,encodeURIComponent(n)),e.setRequestHeader(t+"-URI-AutoEncoded","true");}}function getPathFromResponse(e){if(e.responseURL&&typeof URL<"u")try{let t=new URL(e.responseURL);return t.pathname+t.search}catch{triggerErrorEvent(getDocument().body,"htmx:badResponseUrl",{url:e.responseURL});}}function hasHeader(e,t){return t.test(e.getAllResponseHeaders())}function ajaxHelper(e,t,n){return e=e.toLowerCase(),n?n instanceof Element||typeof n=="string"?issueAjaxRequest(e,t,null,null,{targetOverride:resolveTarget(n),returnPromise:!0}):issueAjaxRequest(e,t,resolveTarget(n.source),n.event,{handler:n.handler,headers:n.headers,values:n.values,targetOverride:resolveTarget(n.target),swapOverride:n.swap,select:n.select,returnPromise:!0}):issueAjaxRequest(e,t,null,null,{returnPromise:!0})}function hierarchyForElt(e){let t=[];for(;e;)t.push(e),e=e.parentElement;return t}function verifyPath(e,t,n){let r,o;return typeof URL=="function"?(o=new URL(t,document.location.href),r=document.location.origin===o.origin):(o=t,r=startsWith(t,document.location.origin)),htmx.config.selfRequestsOnly&&!r?!1:triggerEvent(e,"htmx:validateUrl",mergeObjects({url:o,sameHost:r},n))}function formDataFromObject(e){if(e instanceof FormData)return e;let t=new FormData;for(let n in e)e.hasOwnProperty(n)&&(typeof e[n].forEach=="function"?e[n].forEach(function(r){t.append(n,r);}):typeof e[n]=="object"&&!(e[n]instanceof Blob)?t.append(n,JSON.stringify(e[n])):t.append(n,e[n]));return t}function formDataArrayProxy(e,t,n){return new Proxy(n,{get:function(r,o){return typeof o=="number"?r[o]:o==="length"?r.length:o==="push"?function(i){r.push(i),e.append(t,i);}:typeof r[o]=="function"?function(){r[o].apply(r,arguments),e.delete(t),r.forEach(function(i){e.append(t,i);});}:r[o]&&r[o].length===1?r[o][0]:r[o]},set:function(r,o,i){return r[o]=i,e.delete(t),r.forEach(function(s){e.append(t,s);}),!0}})}function formDataProxy(e){return new Proxy(e,{get:function(t,n){if(typeof n=="symbol")return Reflect.get(t,n);if(n==="toJSON")return ()=>Object.fromEntries(e);if(n in t)return typeof t[n]=="function"?function(){return e[n].apply(e,arguments)}:t[n];let r=e.getAll(n);if(r.length!==0)return r.length===1?r[0]:formDataArrayProxy(t,n,r)},set:function(t,n,r){return typeof n!="string"?!1:(t.delete(n),typeof r.forEach=="function"?r.forEach(function(o){t.append(n,o);}):typeof r=="object"&&!(r instanceof Blob)?t.append(n,JSON.stringify(r)):t.append(n,r),!0)},deleteProperty:function(t,n){return typeof n=="string"&&t.delete(n),!0},ownKeys:function(t){return Reflect.ownKeys(Object.fromEntries(t))},getOwnPropertyDescriptor:function(t,n){return Reflect.getOwnPropertyDescriptor(Object.fromEntries(t),n)}})}function issueAjaxRequest(e,t,n,r,o,i){let s=null,l=null;if(o=o??{},o.returnPromise&&typeof Promise<"u")var a=new Promise(function(h,E){s=h,l=E;});n==null&&(n=getDocument().body);let u=o.handler||handleAjaxResponse,f=o.select||null;if(!bodyContains(n))return maybeCall(s),a;let c=o.targetOverride||asElement(getTarget(n));if(c==null||c==DUMMY_ELT)return triggerErrorEvent(n,"htmx:targetError",{target:getAttributeValue(n,"hx-target")}),maybeCall(l),a;let d=getInternalData(n),b=d.lastButtonClicked;if(b){let h=getRawAttribute(b,"formaction");h!=null&&(t=h);let E=getRawAttribute(b,"formmethod");E!=null&&E.toLowerCase()!=="dialog"&&(e=E);}let S=getClosestAttributeValue(n,"hx-confirm");if(i===void 0&&triggerEvent(n,"htmx:confirm",{target:c,elt:n,path:t,verb:e,triggeringEvent:r,etc:o,issueRequest:function(O){return issueAjaxRequest(e,t,n,r,o,!!O)},question:S})===!1)return maybeCall(s),a;let A=n,p=getClosestAttributeValue(n,"hx-sync"),x=null,H=!1;if(p){let h=p.split(":"),E=h[0].trim();if(E==="this"?A=findThisElement(n,"hx-sync"):A=asElement(querySelectorExt(n,E)),p=(h[1]||"drop").trim(),d=getInternalData(A),p==="drop"&&d.xhr&&d.abortable!==!0)return maybeCall(s),a;if(p==="abort"){if(d.xhr)return maybeCall(s),a;H=!0;}else p==="replace"?triggerEvent(A,"htmx:abort"):p.indexOf("queue")===0&&(x=(p.split(" ")[1]||"last").trim());}if(d.xhr)if(d.abortable)triggerEvent(A,"htmx:abort");else {if(x==null){if(r){let h=getInternalData(r);h&&h.triggerSpec&&h.triggerSpec.queue&&(x=h.triggerSpec.queue);}x==null&&(x="last");}return d.queuedRequests==null&&(d.queuedRequests=[]),x==="first"&&d.queuedRequests.length===0?d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);}):x==="all"?d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);}):x==="last"&&(d.queuedRequests=[],d.queuedRequests.push(function(){issueAjaxRequest(e,t,n,r,o);})),maybeCall(s),a}let m=new XMLHttpRequest;d.xhr=m,d.abortable=H;let T=function(){d.xhr=null,d.abortable=!1,d.queuedRequests!=null&&d.queuedRequests.length>0&&d.queuedRequests.shift()();},P=getClosestAttributeValue(n,"hx-prompt");if(P){var I=prompt(P);if(I===null||!triggerEvent(n,"htmx:prompt",{prompt:I,target:c}))return maybeCall(s),T(),a}if(S&&!i&&!confirm(S))return maybeCall(s),T(),a;let R=getHeaders(n,c,I);e!=="get"&&!usesFormData(n)&&(R["Content-Type"]="application/x-www-form-urlencoded"),o.headers&&(R=mergeObjects(R,o.headers));let v=getInputValues(n,e),L=v.errors,N=v.formData;o.values&&overrideFormData(N,formDataFromObject(o.values));let B=formDataFromObject(getExpressionVars(n)),_=overrideFormData(N,B),q=filterValues(_,n);htmx.config.getCacheBusterParam&&e==="get"&&q.set("org.htmx.cache-buster",getRawAttribute(c,"id")||"true"),(t==null||t==="")&&(t=getDocument().location.href);let W=getValuesForElement(n,"hx-request"),z=getInternalData(n).boosted,F=htmx.config.methodsThatUseUrlParams.indexOf(e)>=0,w={boosted:z,useUrlParams:F,formData:q,parameters:formDataProxy(q),unfilteredFormData:_,unfilteredParameters:formDataProxy(_),headers:R,target:c,verb:e,errors:L,withCredentials:o.credentials||W.credentials||htmx.config.withCredentials,timeout:o.timeout||W.timeout||htmx.config.timeout,path:t,triggeringEvent:r};if(!triggerEvent(n,"htmx:configRequest",w))return maybeCall(s),T(),a;if(t=w.path,e=w.verb,R=w.headers,q=formDataFromObject(w.parameters),L=w.errors,F=w.useUrlParams,L&&L.length>0)return triggerEvent(n,"htmx:validation:halted",w),maybeCall(s),T(),a;let J=t.split("#"),$=J[0],X=J[1],D=t;if(F&&(D=$,!q.keys().next().done&&(D.indexOf("?")<0?D+="?":D+="&",D+=urlEncode(q),X&&(D+="#"+X))),!verifyPath(n,D,w))return triggerErrorEvent(n,"htmx:invalidPath",w),maybeCall(l),a;if(m.open(e.toUpperCase(),D,!0),m.overrideMimeType("text/html"),m.withCredentials=w.withCredentials,m.timeout=w.timeout,!W.noHeaders){for(let h in R)if(R.hasOwnProperty(h)){let E=R[h];safelySetHeaderValue(m,h,E);}}let y={xhr:m,target:c,requestConfig:w,etc:o,boosted:z,select:f,pathInfo:{requestPath:t,finalRequestPath:D,responsePath:null,anchor:X}};if(m.onload=function(){try{let h=hierarchyForElt(n);if(y.pathInfo.responsePath=getPathFromResponse(m),u(n,y),y.keepIndicators!==!0&&removeRequestIndicators(M,V),triggerEvent(n,"htmx:afterRequest",y),triggerEvent(n,"htmx:afterOnLoad",y),!bodyContains(n)){let E=null;for(;h.length>0&&E==null;){let O=h.shift();bodyContains(O)&&(E=O);}E&&(triggerEvent(E,"htmx:afterRequest",y),triggerEvent(E,"htmx:afterOnLoad",y));}maybeCall(s),T();}catch(h){throw triggerErrorEvent(n,"htmx:onLoadError",mergeObjects({error:h},y)),h}},m.onerror=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:sendError",y),maybeCall(l),T();},m.onabort=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:sendAbort",y),maybeCall(l),T();},m.ontimeout=function(){removeRequestIndicators(M,V),triggerErrorEvent(n,"htmx:afterRequest",y),triggerErrorEvent(n,"htmx:timeout",y),maybeCall(l),T();},!triggerEvent(n,"htmx:beforeRequest",y))return maybeCall(s),T(),a;var M=addRequestIndicatorClasses(n),V=disableElements(n);forEach(["loadstart","loadend","progress","abort"],function(h){forEach([m,m.upload],function(E){E.addEventListener(h,function(O){triggerEvent(n,"htmx:xhr:"+h,{lengthComputable:O.lengthComputable,loaded:O.loaded,total:O.total});});});}),triggerEvent(n,"htmx:beforeSend",y);let Q=F?null:encodeParamsForBody(m,n,q);return m.send(Q),a}function determineHistoryUpdates(e,t){let n=t.xhr,r=null,o=null;if(hasHeader(n,/HX-Push:/i)?(r=n.getResponseHeader("HX-Push"),o="push"):hasHeader(n,/HX-Push-Url:/i)?(r=n.getResponseHeader("HX-Push-Url"),o="push"):hasHeader(n,/HX-Replace-Url:/i)&&(r=n.getResponseHeader("HX-Replace-Url"),o="replace"),r)return r==="false"?{}:{type:o,path:r};let i=t.pathInfo.finalRequestPath,s=t.pathInfo.responsePath,l=getClosestAttributeValue(e,"hx-push-url"),a=getClosestAttributeValue(e,"hx-replace-url"),u=getInternalData(e).boosted,f=null,c=null;return l?(f="push",c=l):a?(f="replace",c=a):u&&(f="push",c=s||i),c?c==="false"?{}:(c==="true"&&(c=s||i),t.pathInfo.anchor&&c.indexOf("#")===-1&&(c=c+"#"+t.pathInfo.anchor),{type:f,path:c}):{}}function codeMatches(e,t){var n=new RegExp(e.code);return n.test(t.toString(10))}function resolveResponseHandling(e){for(var t=0;t0?getWindow().setTimeout(I,x.swapDelay):I();}c&&triggerErrorEvent(e,"htmx:responseError",mergeObjects({error:"Response Status Error Code "+n.status+" from "+t.pathInfo.requestPath},t));}}let extensions={};function extensionBase(){return {init:function(e){return null},getSelectors:function(){return null},onEvent:function(e,t){return !0},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return !1},handleSwap:function(e,t,n,r){return !1},encodeParameters:function(e,t,n){return null}}}function defineExtension(e,t){t.init&&t.init(internalAPI),extensions[e]=mergeObjects(extensionBase(),t);}function removeExtension(e){delete extensions[e];}function getExtensions(e,t,n){if(t==null&&(t=[]),e==null)return t;n==null&&(n=[]);let r=getAttributeValue(e,"hx-ext");return r&&forEach(r.split(","),function(o){if(o=o.replace(/ /g,""),o.slice(0,7)=="ignore:"){n.push(o.slice(7));return}if(n.indexOf(o)<0){let i=extensions[o];i&&t.indexOf(i)<0&&t.push(i);}}),getExtensions(asElement(parentElt(e)),t,n)}var isReady=!1;getDocument().addEventListener("DOMContentLoaded",function(){isReady=!0;});function ready(e){isReady||getDocument().readyState==="complete"?e():getDocument().addEventListener("DOMContentLoaded",e);}function insertIndicatorStyles(){if(htmx.config.includeIndicatorStyles!==!1){let e=htmx.config.inlineStyleNonce?` nonce="${htmx.config.inlineStyleNonce}"`:"";getDocument().head.insertAdjacentHTML("beforeend"," ."+htmx.config.indicatorClass+"{opacity:0} ."+htmx.config.requestClass+" ."+htmx.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ."+htmx.config.requestClass+"."+htmx.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ");}}function getMetaConfig(){let e=getDocument().querySelector('meta[name="htmx-config"]');return e?parseJSON(e.content):null}function mergeMetaConfig(){let e=getMetaConfig();e&&(htmx.config=mergeObjects(htmx.config,e));}return ready(function(){mergeMetaConfig(),insertIndicatorStyles();let e=getDocument().body;processNode(e);let t=getDocument().querySelectorAll("[hx-trigger='restored'],[data-hx-trigger='restored']");e.addEventListener("htmx:abort",function(r){let o=r.target,i=getInternalData(o);i&&i.xhr&&i.xhr.abort();});let n=window.onpopstate?window.onpopstate.bind(window):null;window.onpopstate=function(r){r.state&&r.state.htmx?(restoreHistory(),forEach(t,function(o){triggerEvent(o,"htmx:restored",{document:getDocument(),triggerEvent});})):n&&n(r);},getWindow().setTimeout(function(){triggerEvent(e,"htmx:load",{}),e=null;},0);}),htmx}(),g=K;function Z(e,t){if(e==="ignore")return !1;let n=e.split("/"),r=t.split("/");for(let o=0;o{if(!(t instanceof CustomEvent))return !1;let n=t.detail.target;return n&&n.children&&Array.from(n.children).forEach(r=>{g.trigger(r,e,null);}),!0},init:function(e){},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return !1},handleSwap:function(e,t,n,r){return !1},encodeParameters:function(e,t,n){},getSelectors:function(){return null}});g.defineExtension("debug",{onEvent:function(e,t){console.debug?console.debug(e):console&&console.log("DEBUG:",e);}});var C=g.config,k,te="hx-target-";function G(e,t){return e.substring(0,t.length)===t}function ne(e,t){if(!e||!t)return null;let n=t.toString(),r=[n,n.substr(0,2)+"*",n.substr(0,2)+"x",n.substr(0,1)+"*",n.substr(0,1)+"x",n.substr(0,1)+"**",n.substr(0,1)+"xx","*","x","***","xxx"];(G(n,"4")||G(n,"5"))&&r.push("error");for(let o=0;o{k=e,C.responseTargetUnsetsError===void 0&&(C.responseTargetUnsetsError=!0),C.responseTargetSetsError===void 0&&(C.responseTargetSetsError=!1),C.responseTargetPrefersExisting===void 0&&(C.responseTargetPrefersExisting=!1),C.responseTargetPrefersRetargetHeader===void 0&&(C.responseTargetPrefersRetargetHeader=!0);},onEvent:(e,t)=>{if(!(t instanceof CustomEvent))return !1;if(e==="htmx:beforeSwap"&&t.detail.xhr&&t.detail.xhr.status!==200){if(t.detail.target&&(C.responseTargetPrefersExisting||C.responseTargetPrefersRetargetHeader&&t.detail.xhr.getAllResponseHeaders().match(/HX-Retarget:/i)))return t.detail.shouldSwap=!0,j(t),!0;if(!t.detail.requestConfig)return !0;let n=ne(t.detail.requestConfig.elt,t.detail.xhr.status);return n&&(j(t),t.detail.shouldSwap=!0,t.detail.target=n),!0}}});g.defineExtension("mutation-error",{onEvent:(e,t)=>{if(!(t instanceof CustomEvent))return !1;if(e==="htmx:afterRequest"){if(!t.detail||!t.detail.xhr)return;let n=t.detail.xhr.status;n>=400&&g.findAll("[hx-on\\:\\:mutation-error]").forEach(r=>{g.trigger(r,"htmx:mutation-error",{status:n});});}}});function Y(e){let t=null,n=o=>{console.log("connecting to ws",e.url,"attempt",o),t=new WebSocket(e.url),t.onmessage=i=>{e.onMessage(i.data);},t.onerror=i=>{try{t?.close();}catch{}t=null;let s=o*(e.reconnectInterval||1e3);setTimeout(()=>n(o+1),s);},t.onclose=()=>{t=null;let i=o*(e.reconnectInterval||1e3);setTimeout(()=>n(o+1),i);};};n(1);let r=o=>{t&&t.readyState===WebSocket.OPEN?t.send(o):setTimeout(()=>r(o),100);};return {sendMessage:r}}var U="";g.defineExtension("livereload",{init:function(){let e=window.location.host,t=!1;for(let n of Array.from(g.findAll("[hx-ext]")))if(n.getAttribute("hx-ext")?.split(" ").includes("livereload")){t=!0;break}t&&(console.log("livereload extension initialized."),Y({url:`${window.location.protocol==="https:"?"wss":"ws"}://${e}/dev/livereload`,onOpen:()=>{},onMessage:n=>{U===""&&(U=n),U!==n&&(U=n,re());},onError:n=>{},onClose:()=>{}}));},onEvent:function(e,t){}});function re(){window.location.reload();}function oe(e){let t=window.location.href;setInterval(()=>{window.location.href!==t&&(e(t,window.location.href),t=window.location.href);},100);}oe((e,t)=>{ie(t);});function ie(e){let t=new URL(e);document.querySelectorAll("[hx-trigger]").forEach(function(n){let r=n.getAttribute("hx-trigger");if(!r)return;if(r.split(", ").find(i=>i==="url"))g.swap(n,"url",{swapStyle:"outerHTML",swapDelay:0,settleDelay:0});else for(let[i,s]of t.searchParams){let l="qs:"+i;if(r.includes(l)){console.log("triggering",l),g.trigger(n,l,null);break}}}),document.querySelectorAll("[hx-match-qp]").forEach(n=>{let r=!1;for(let o of n.getAttributeNames())if(o.startsWith("hx-match-qp-mapping:")){let i=o.replace("hx-match-qp-mapping:","");if(t.searchParams.get(i)){g.swap(n,n.getAttribute(o)??"",{swapStyle:"innerHTML",swapDelay:0,settleDelay:0}),r=!0;break}}if(!r){let o=n.getAttribute("hx-match-qp-default");o&&g.swap(n,n.getAttribute("hx-match-qp-mapping:"+o)??"",{swapStyle:"innerHTML",swapDelay:0,settleDelay:0});}});} diff --git a/framework/assets/js/htmxextensions/livereload.ts b/framework/assets/js/htmxextensions/livereload.ts index f8544f3..3d4b25f 100644 --- a/framework/assets/js/htmxextensions/livereload.ts +++ b/framework/assets/js/htmxextensions/livereload.ts @@ -6,9 +6,24 @@ let lastVersion = ""; htmx.defineExtension("livereload", { init: function () { const host = window.location.host; + + let enabled = false + for (const element of Array.from(htmx.findAll("[hx-ext]"))) { + const value = element.getAttribute("hx-ext"); + if(value?.split(" ").includes("livereload")) { + enabled = true + break; + } + } + + if(!enabled) { + return + } + console.log('livereload extension initialized.'); + createWebSocketClient({ - url: `ws://${host}/dev/livereload`, + url: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${host}/dev/livereload`, onOpen: () => { }, onMessage: (message) => { diff --git a/framework/h/base.go b/framework/h/base.go index ac7851b..7074428 100644 --- a/framework/h/base.go +++ b/framework/h/base.go @@ -55,6 +55,6 @@ func GetPartialPath(partial func(ctx *RequestContext) *Partial) string { return runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name() } -func GetPartialPathWithQs(partial func(ctx *RequestContext) *Partial, qs string) string { - return html.EscapeString(GetPartialPath(partial) + "?" + qs) +func GetPartialPathWithQs(partial func(ctx *RequestContext) *Partial, qs *Qs) string { + return html.EscapeString(GetPartialPath(partial) + "?" + qs.ToString()) } diff --git a/framework/h/conditionals.go b/framework/h/conditionals.go new file mode 100644 index 0000000..9455fa6 --- /dev/null +++ b/framework/h/conditionals.go @@ -0,0 +1,32 @@ +package h + +func If(condition bool, node Ren) Ren { + if condition { + return node + } else { + return Empty() + } +} + +func IfElse(condition bool, node Ren, node2 Ren) Ren { + if condition { + return node + } else { + return node2 + } +} + +func IfElseLazy(condition bool, cb1 func() Ren, cb2 func() Ren) Ren { + if condition { + return cb1() + } else { + return cb2() + } +} + +func IfHtmxRequest(ctx *RequestContext, node Ren) Ren { + if ctx.Get("HX-Request") != "" { + return node + } + return Empty() +} diff --git a/framework/h/events.go b/framework/h/events.go deleted file mode 100644 index 457a010..0000000 --- a/framework/h/events.go +++ /dev/null @@ -1,24 +0,0 @@ -package h - -type HxEvent = string -type HxTriggerName = string - -var ( - HxBeforeRequest HxEvent = "hx-on::before-request" - HxAfterRequest HxEvent = "hx-on::after-request" - HxOnMutationError HxEvent = "hx-on::mutation-error" - HxOnLoad HxEvent = "hx-on::load" - HxOnLoadError HxEvent = "hx-on::load-error" - HxRequestTimeout HxEvent = "hx-on::request-timeout" - HxTrigger HxEvent = "hx-on::trigger" - HxRequestStart HxEvent = "hx-on::xhr:loadstart" - HxRequestProgress HxEvent = "hx-on::xhr:progress" -) - -const ( - TriggerLoad HxTriggerName = "load" - TriggerClick HxTriggerName = "click" - TriggerDblClick HxTriggerName = "dblclick" - TriggerKeyUpEnter HxTriggerName = "keyup[keyCode==13]" - TriggerBlur HxTriggerName = "blur" -) diff --git a/framework/h/lifecycle.go b/framework/h/lifecycle.go index abe13fd..c75dddf 100644 --- a/framework/h/lifecycle.go +++ b/framework/h/lifecycle.go @@ -2,61 +2,104 @@ package h import ( "fmt" + "github.com/maddalax/htmgo/framework/hx" + "strings" ) type LifeCycle struct { - handlers map[HxEvent][]JsCommand + handlers map[hx.Event][]Command } func NewLifeCycle() *LifeCycle { return &LifeCycle{ - handlers: make(map[HxEvent][]JsCommand), + handlers: make(map[hx.Event][]Command), } } -func (l *LifeCycle) OnEvent(event HxEvent, cmd ...JsCommand) *LifeCycle { - if l.handlers[event] == nil { - l.handlers[event] = []JsCommand{} +func validateCommands(cmds []Command) { + for _, cmd := range cmds { + switch t := cmd.(type) { + case JsCommand: + break + case *AttributeMap: + break + case *Element: + panic(fmt.Sprintf("element is not allowed in lifecycle events. Got: %v", t)) + default: + panic(fmt.Sprintf("type is not allowed in lifecycle events. Got: %v", t)) + + } } +} + +func (l *LifeCycle) OnEvent(event hx.Event, cmd ...Command) *LifeCycle { + validateCommands(cmd) + + if l.handlers[event] == nil { + l.handlers[event] = []Command{} + } + l.handlers[event] = append(l.handlers[event], cmd...) return l } -func (l *LifeCycle) BeforeRequest(cmd ...JsCommand) *LifeCycle { - l.OnEvent(HxBeforeRequest, cmd...) +func (l *LifeCycle) BeforeRequest(cmd ...Command) *LifeCycle { + l.OnEvent(hx.BeforeRequestEvent, cmd...) return l } -func OnEvent(event HxEvent, cmd ...JsCommand) *LifeCycle { +func OnLoad(cmd ...Command) *LifeCycle { + return NewLifeCycle().OnEvent(hx.LoadEvent, cmd...) +} + +func OnAfterSwap(cmd ...Command) *LifeCycle { + return NewLifeCycle().OnEvent(hx.AfterSwapEvent, cmd...) +} + +func OnTrigger(trigger string, cmd ...Command) *LifeCycle { + return NewLifeCycle().OnEvent(hx.NewStringTrigger(trigger).ToString(), cmd...) +} + +func OnClick(cmd ...Command) *LifeCycle { + return NewLifeCycle().OnEvent(hx.ClickEvent, cmd...) +} + +func OnEvent(event hx.Event, cmd ...Command) *LifeCycle { return NewLifeCycle().OnEvent(event, cmd...) } -func BeforeRequest(cmd ...JsCommand) *LifeCycle { +func BeforeRequest(cmd ...Command) *LifeCycle { return NewLifeCycle().BeforeRequest(cmd...) } -func AfterRequest(cmd ...JsCommand) *LifeCycle { +func AfterRequest(cmd ...Command) *LifeCycle { return NewLifeCycle().AfterRequest(cmd...) } -func OnMutationError(cmd ...JsCommand) *LifeCycle { +func OnMutationError(cmd ...Command) *LifeCycle { return NewLifeCycle().OnMutationError(cmd...) } -func (l *LifeCycle) AfterRequest(cmd ...JsCommand) *LifeCycle { - l.OnEvent(HxAfterRequest, cmd...) +func (l *LifeCycle) AfterRequest(cmd ...Command) *LifeCycle { + l.OnEvent(hx.AfterRequestEvent, cmd...) return l } -func (l *LifeCycle) OnMutationError(cmd ...JsCommand) *LifeCycle { - l.OnEvent(HxOnMutationError, cmd...) +func (l *LifeCycle) OnMutationError(cmd ...Command) *LifeCycle { + l.OnEvent(hx.OnMutationErrorEvent, cmd...) return l } +type Command = Ren + type JsCommand struct { Command string } +func (j JsCommand) Render(builder *strings.Builder) { + builder.WriteString(j.Command) +} + func SetText(text string) JsCommand { // language=JavaScript return JsCommand{Command: fmt.Sprintf("this.innerText = '%s'", text)} diff --git a/framework/h/renderer.go b/framework/h/renderer.go index 1a7e4ea..374342f 100644 --- a/framework/h/renderer.go +++ b/framework/h/renderer.go @@ -2,6 +2,7 @@ package h import ( "fmt" + "github.com/maddalax/htmgo/framework/hx" "strings" ) @@ -89,20 +90,61 @@ func (m *AttributeMap) Render(builder *strings.Builder) { } } +func toHtmxTriggerName(event string) string { + if strings.HasPrefix(event, "htmx:") { + return event[5:] + } + if strings.HasPrefix(event, "on") { + return event[2:] + } + return event +} + +func formatEventName(event string, isDomEvent bool) string { + raw := toHtmxTriggerName(event) + if isDomEvent { + return "on" + raw + } + return event +} + +func (l *LifeCycle) fromAttributeMap(event string, key string, value string, builder *strings.Builder) { + + if key == hx.GetAttr || key == hx.PatchAttr || key == hx.PostAttr { + TriggerString(toHtmxTriggerName(event)).Render(builder) + } + + Attribute(key, value).Render(builder) +} + func (l *LifeCycle) Render(builder *strings.Builder) { m := make(map[string]string) for event, commands := range l.handlers { m[event] = "" for _, command := range commands { - m[event] += fmt.Sprintf("%s;", command.Command) + switch c := command.(type) { + case JsCommand: + eventName := formatEventName(event, true) + m[eventName] += fmt.Sprintf("%s;", c.Command) + case *AttributeMap: + for k, v := range c.ToMap() { + l.fromAttributeMap(event, k, v, builder) + } + } } } children := make([]Ren, 0) - for event, js := range m { - children = append(children, Attribute(event, js)) + for event, value := range m { + if value != "" { + children = append(children, Attribute(event, value)) + } + } + + if len(children) == 0 { + return } Children(children...).Render(builder) diff --git a/framework/h/tag.go b/framework/h/tag.go index 1ba54b9..ec47f70 100644 --- a/framework/h/tag.go +++ b/framework/h/tag.go @@ -3,11 +3,51 @@ package h import ( "encoding/json" "fmt" - "html" + "github.com/maddalax/htmgo/framework/hx" "net/url" "strings" ) +type Qs struct { + m map[string]string +} + +func NewQs(pairs ...string) *Qs { + q := &Qs{ + m: make(map[string]string), + } + if len(pairs)%2 != 0 { + return q + } + for i := 0; i < len(pairs); i++ { + q.m[pairs[i]] = pairs[i+1] + i++ + } + return q +} + +func (q *Qs) Add(key string, value string) *Qs { + q.m[key] = value + return q +} + +func (q *Qs) ToString() string { + builder := strings.Builder{} + index := 0 + for k, v := range q.m { + builder.WriteString(k) + builder.WriteString("=") + builder.WriteString(v) + if index < len(q.m)-1 { + builder.WriteString("&") + } + index++ + } + return builder.String() +} + +type PartialFunc = func(ctx *RequestContext) *Partial + type Element struct { tag string attributes map[string]string @@ -76,12 +116,24 @@ func Attributes(attrs *AttributeMap) *AttributeMap { return attrs } +func AttributePairs(pairs ...string) *AttributeMap { + if len(pairs)%2 != 0 { + return &AttributeMap{} + } + m := make(AttributeMap) + for i := 0; i < len(pairs); i++ { + m[pairs[i]] = pairs[i+1] + i++ + } + return &m +} + func Checked() Ren { return Attribute("checked", "true") } func Boost() Ren { - return Attribute("hx-boost", "true") + return Attribute(hx.BoostAttr, "true") } func Attribute(key string, value string) *AttributeMap { @@ -93,90 +145,24 @@ func TriggerChildren() Ren { } func HxExtension(value string) Ren { - return Attribute("hx-ext", value) + return Attribute(hx.ExtAttr, value) } func Disabled() Ren { return Attribute("disabled", "") } -func Get(path string) Ren { - return Attribute("hx-get", path) +func TriggerString(triggers ...string) *AttributeMap { + trigger := hx.NewStringTrigger(strings.Join(triggers, ", ")) + return Attribute(hx.TriggerAttr, trigger.ToString()) } -func GetPartial(partial func(ctx *RequestContext) *Partial) Ren { - return Get(GetPartialPath(partial)) +func Trigger(opts ...hx.TriggerEvent) *AttributeMap { + return Attribute(hx.TriggerAttr, hx.NewTrigger(opts...).ToString()) } -func GetPartialWithQs(partial func(ctx *RequestContext) *Partial, qs string) Ren { - return Get(GetPartialPathWithQs(partial, qs)) -} - -func CreateTriggers(triggers ...string) []string { - return triggers -} - -type ReloadParams struct { - Triggers []string - Target string - Children Ren -} - -func ViewOnLoad(partial func(ctx *RequestContext) *Partial) Ren { - return View(partial, ReloadParams{ - Triggers: CreateTriggers("load"), - }) -} - -func View(partial func(ctx *RequestContext) *Partial, params ReloadParams) Ren { - return Div(Attributes(&AttributeMap{ - "hx-get": GetPartialPath(partial), - "hx-trigger": strings.Join(params.Triggers, ", "), - "hx-target": params.Target, - }), params.Children) -} - -func PartialWithTriggers(partial func(ctx *RequestContext) *Partial, triggers ...string) Ren { - return Div(Attributes(&AttributeMap{ - "hx-get": GetPartialPath(partial), - "hx-trigger": strings.Join(triggers, ", "), - })) -} - -func GetWithQs(path string, qs map[string]string) Ren { - return Get(SetQueryParams(path, qs)) -} - -func PostPartialOnTrigger(partial func(ctx *RequestContext) *Partial, triggers ...string) Ren { - return PostOnTrigger(GetPartialPath(partial), strings.Join(triggers, ", ")) -} - -func PostPartialWithQsOnTrigger(partial func(ctx *RequestContext) *Partial, qs string, trigger string) Ren { - return PostOnTrigger(GetPartialPathWithQs(partial, qs), trigger) -} - -func Post(url string) Ren { - return Attribute("hx-post", url) -} - -func PostOnTrigger(url string, trigger string) Ren { - return AttributeList(Attribute("hx-post", url), Trigger(trigger)) -} - -func PostOnClick(url string) Ren { - return PostOnTrigger(url, "click") -} - -func PostPartialOnClick(partial func(ctx *RequestContext) *Partial) Ren { - return PostOnClick(GetPartialPath(partial)) -} - -func PostPartialOnClickQs(partial func(ctx *RequestContext) *Partial, qs string) Ren { - return PostOnClick(GetPartialPathWithQs(partial, qs)) -} - -func Trigger(trigger string) *AttributeMap { - return Attribute("hx-trigger", trigger) +func TriggerClick(opts ...hx.Modifier) *AttributeMap { + return Trigger(hx.OnClick(opts...)) } func TextF(format string, args ...interface{}) Ren { @@ -192,7 +178,7 @@ func Pf(format string, args ...interface{}) Ren { } func Target(target string) Ren { - return Attribute("hx-target", target) + return Attribute(hx.TargetAttr, target) } func Name(name string) Ren { @@ -200,7 +186,7 @@ func Name(name string) Ren { } func Confirm(message string) Ren { - return Attribute("hx-confirm", message) + return Attribute(hx.ConfirmAttr, message) } func Href(path string) Ren { @@ -216,7 +202,7 @@ func Placeholder(placeholder string) Ren { } func OutOfBandSwap(selector string) Ren { - return Attribute("hx-swap-oob", + return Attribute(hx.SwapOobAttr, Ternary(selector == "", "true", selector)) } @@ -307,7 +293,7 @@ func Article(children ...Ren) *Element { } func ReplaceUrlHeader(url string) *Headers { - return NewHeaders("HX-Replace-Url", url) + return NewHeaders(hx.ReplaceUrlHeader, url) } func CombineHeaders(headers ...*Headers) *Headers { @@ -321,7 +307,7 @@ func CombineHeaders(headers ...*Headers) *Headers { } func CurrentPath(ctx *RequestContext) string { - current := ctx.Request().Header.Get("Hx-Current-Url") + current := ctx.Request().Header.Get(hx.CurrentUrlHeader) parsed, err := url.Parse(current) if err != nil { return "" @@ -330,12 +316,12 @@ func CurrentPath(ctx *RequestContext) string { } func PushQsHeader(ctx *RequestContext, key string, value string) *Headers { - current := ctx.Request().Header.Get("Hx-Current-Url") + current := ctx.Request().Header.Get(hx.CurrentUrlHeader) parsed, err := url.Parse(current) if err != nil { return NewHeaders() } - return NewHeaders("HX-Replace-Url", SetQueryParams(parsed.Path, map[string]string{ + return NewHeaders(hx.ReplaceUrlHeader, SetQueryParams(parsed.Path, map[string]string{ key: value, })) } @@ -402,8 +388,8 @@ func Button(children ...Ren) *Element { return Tag("button", children...) } -func Indicator(tag string) Ren { - return Attribute("hx-indicator", tag) +func Indicator(tag string) *AttributeMap { + return Attribute(hx.IndicatorAttr, tag) } func P(children ...Ren) *Element { @@ -460,31 +446,6 @@ func Empty() *Element { } } -func BeforeRequestSetHtml(children ...Ren) Ren { - serialized := Render(Fragment(children...)) - return Attribute("hx-on::before-request", `this.innerHTML = '`+html.EscapeString(serialized)+`'`) -} - -func BeforeRequestSetAttribute(key string, value string) Ren { - return Attribute("hx-on::before-request", `this.setAttribute('`+key+`', '`+value+`')`) -} - -func OnMutationErrorSetText(text string) Ren { - return Attribute("hx-on::mutation-error", `this.innerText = '`+text+`'`) -} - -func BeforeRequestSetText(text string) Ren { - return Attribute("hx-on::before-request", `this.innerText = '`+text+`'`) -} - -func AfterRequestSetText(text string) Ren { - return Attribute("hx-on::after-request", `this.innerText = '`+text+`'`) -} - -func AfterRequestRemoveAttribute(key string, value string) Ren { - return Attribute("hx-on::after-request", `this.removeAttribute('`+key+`')`) -} - func IfQueryParam(key string, node *Element) Ren { return Fragment(Attribute("hx-if-qp:"+key, "true"), node) } @@ -493,11 +454,6 @@ func Hidden() Ren { return Attribute("style", "display:none") } -func AfterRequestSetHtml(children ...Ren) Ren { - serialized := Render(Fragment(children...)) - return Attribute("hx-on::after-request", `this.innerHTML = '`+html.EscapeString(serialized)+`'`) -} - func Children(children ...Ren) *ChildList { return NewChildList(children...) } @@ -506,41 +462,10 @@ func Label(text string) *Element { return Tag("label", Text(text)) } -func If(condition bool, node Ren) Ren { - if condition { - return node - } else { - return Empty() - } -} - -func IfElse(condition bool, node Ren, node2 Ren) Ren { - if condition { - return node - } else { - return node2 - } -} - -func IfElseLazy(condition bool, cb1 func() Ren, cb2 func() Ren) Ren { - if condition { - return cb1() - } else { - return cb2() - } -} - func GetTriggerName(ctx *RequestContext) string { return ctx.Request().Header.Get("HX-Trigger-Name") } -func IfHtmxRequest(ctx *RequestContext, node Ren) Ren { - if ctx.Get("HX-Request") != "" { - return node - } - return Empty() -} - type SwapArg struct { Selector string Content *Element @@ -575,20 +500,3 @@ func SwapMany(ctx *RequestContext, args ...SwapArg) Ren { return arg.Content })...) } - -type OnRequestSwapArgs struct { - Target string - Get string - Default *Element - BeforeRequest *Element - AfterRequest *Element -} - -func OnRequestSwap(args OnRequestSwapArgs) Ren { - return Div(args.Default, - BeforeRequestSetHtml(args.BeforeRequest), - AfterRequestSetHtml(args.AfterRequest), - Get(args.Get), - Target(args.Target), - ) -} diff --git a/framework/h/xhr.go b/framework/h/xhr.go new file mode 100644 index 0000000..a31fa24 --- /dev/null +++ b/framework/h/xhr.go @@ -0,0 +1,43 @@ +package h + +import "github.com/maddalax/htmgo/framework/hx" + +func Get(path string, trigger ...string) *AttributeMap { + return AttributeList(Attribute(hx.GetAttr, path), TriggerString(trigger...)) +} + +func GetPartial(partial PartialFunc, trigger ...string) *AttributeMap { + return Get(GetPartialPath(partial), trigger...) +} + +func GetPartialWithQs(partial PartialFunc, qs *Qs, trigger string) *AttributeMap { + return Get(GetPartialPathWithQs(partial, qs), trigger) +} + +func GetWithQs(path string, qs map[string]string, trigger string) *AttributeMap { + return Get(SetQueryParams(path, qs), trigger) +} + +func PostPartial(partial PartialFunc, triggers ...string) *AttributeMap { + return Post(GetPartialPath(partial), triggers...) +} + +func PostPartialWithQs(partial PartialFunc, qs *Qs, trigger ...string) *AttributeMap { + return Post(GetPartialPathWithQs(partial, qs), trigger...) +} + +func Post(url string, trigger ...string) *AttributeMap { + return AttributeList(Attribute(hx.PostAttr, url), TriggerString(trigger...)) +} + +func PostOnClick(url string) *AttributeMap { + return Post(url, hx.ClickEvent) +} + +func PostPartialOnClick(partial PartialFunc) *AttributeMap { + return PostOnClick(GetPartialPath(partial)) +} + +func PostPartialOnClickQs(partial PartialFunc, qs *Qs) *AttributeMap { + return PostOnClick(GetPartialPathWithQs(partial, qs)) +} diff --git a/framework/hx/event.go b/framework/hx/event.go new file mode 100644 index 0000000..c30864b --- /dev/null +++ b/framework/hx/event.go @@ -0,0 +1,26 @@ +package hx + +import "fmt" + +func OnEvent(event Event, modifiers ...Modifier) TriggerEvent { + return TriggerEvent{ + event: event, + modifiers: modifiers, + } +} + +func OnClick(modifiers ...Modifier) TriggerEvent { + return OnEvent(ClickEvent, modifiers...) +} + +func OnLoad(modifiers ...Modifier) TriggerEvent { + return OnEvent(LoadEvent, modifiers...) +} + +func OnChange(modifiers ...Modifier) TriggerEvent { + return OnEvent(ChangeEvent, modifiers...) +} + +func OnPoll(durationSeconds int) TriggerEvent { + return OnEvent(PollingEvent, StringModifier(fmt.Sprintf("%ds", durationSeconds))) +} diff --git a/framework/hx/htmx.go b/framework/hx/htmx.go new file mode 100644 index 0000000..5d3adee --- /dev/null +++ b/framework/hx/htmx.go @@ -0,0 +1,138 @@ +package hx + +type Attribute = string +type Header = string +type Event = string + +// https://htmx.org/reference/#events +const ( + GetAttr Attribute = "hx-get" + PostAttr Attribute = "hx-post" + PushUrlAttr Attribute = "hx-push-url" + SelectAttr Attribute = "hx-select" + SelectOobAttr Attribute = "hx-select-oob" + SwapAttr Attribute = "hx-swap" + SwapOobAttr Attribute = "hx-swap-oob" + TargetAttr Attribute = "hx-target" + TriggerAttr Attribute = "hx-trigger" + ValsAttr Attribute = "hx-vals" + BoostAttr Attribute = "hx-boost" + ConfirmAttr Attribute = "hx-confirm" + DeleteAttr Attribute = "hx-delete" + DisableAttr Attribute = "hx-disable" + DisabledEltAttr Attribute = "hx-disabled-elt" + DisinheritAttr Attribute = "hx-disinherit" + EncodingAttr Attribute = "hx-encoding" + ExtAttr Attribute = "hx-ext" + HeadersAttr Attribute = "hx-headers" + HistoryAttr Attribute = "hx-history" + HistoryEltAttr Attribute = "hx-history-elt" + IncludeAttr Attribute = "hx-include" + IndicatorAttr Attribute = "hx-indicator" + InheritAttr Attribute = "hx-inherit" + ParamsAttr Attribute = "hx-params" + PatchAttr Attribute = "hx-patch" + PreserveAttr Attribute = "hx-preserve" + PromptAttr Attribute = "hx-prompt" + PutAttr Attribute = "hx-put" + ReplaceUrlAttr Attribute = "hx-replace-url" + RequestAttr Attribute = "hx-request" + SyncAttr Attribute = "hx-sync" + ValidateAttr Attribute = "hx-validate" +) + +const ( + LocationHeader Header = "HX-Location" + PushUrlHeader Header = "HX-Push-Url" + RedirectHeader Header = "HX-Redirect" + RefreshHeader Header = "HX-Refresh" + ReplaceUrlHeader Header = "HX-Replace-Url" + CurrentUrlHeader Header = "HX-Current-Url" + ReswapHeader Header = "HX-Reswap" + RetargetHeader Header = "HX-Retarget" + ReselectHeader Header = "HX-Reselect" + TriggerHeader Header = "HX-Trigger" + TriggerAfterSettleHeader Header = "HX-Trigger-After-Settle" + TriggerAfterSwapHeader Header = "HX-Trigger-After-Swap" +) + +const ( + // AbortEvent Htmx Events + AbortEvent Event = "htmx:abort" + AfterOnLoadEvent Event = "htmx:afterOnLoad" + AfterProcessNodeEvent Event = "htmx:afterProcessNode" + AfterRequestEvent Event = "htmx:afterRequest" + OnMutationErrorEvent Event = "htmx:onMutationError" + AfterSettleEvent Event = "htmx:afterSettle" + AfterSwapEvent Event = "htmx:afterSwap" + BeforeCleanupElementEvent Event = "htmx:beforeCleanupElement" + BeforeOnLoadEvent Event = "htmx:beforeOnLoad" + BeforeProcessNodeEvent Event = "htmx:beforeProcessNode" + BeforeRequestEvent Event = "htmx:beforeRequest" + BeforeSwapEvent Event = "htmx:beforeSwap" + BeforeSendEvent Event = "htmx:beforeSend" + ConfigRequestEvent Event = "htmx:configRequest" + ConfirmEvent Event = "htmx:confirm" + HistoryCacheErrorEvent Event = "htmx:historyCacheError" + HistoryCacheMissEvent Event = "htmx:historyCacheMiss" + HistoryCacheMissErrorEvent Event = "htmx:historyCacheMissError" + HistoryCacheMissLoadEvent Event = "htmx:historyCacheMissLoad" + HistoryRestoreEvent Event = "htmx:historyRestore" + BeforeHistorySaveEvent Event = "htmx:beforeHistorySave" + LoadEvent Event = "htmx:load" + NoSSESourceErrorEvent Event = "htmx:noSSESourceError" + OnLoadErrorEvent Event = "htmx:onLoadError" + OobAfterSwapEvent Event = "htmx:oobAfterSwap" + OobBeforeSwapEvent Event = "htmx:oobBeforeSwap" + OobErrorNoTargetEvent Event = "htmx:oobErrorNoTarget" + PromptEvent Event = "htmx:prompt" + PushedIntoHistoryEvent Event = "htmx:pushedIntoHistory" + ResponseErrorEvent Event = "htmx:responseError" + SendErrorEvent Event = "htmx:sendError" + SSEErrorEvent Event = "htmx:sseError" + SSEOpenEvent Event = "htmx:sseOpen" + SwapErrorEvent Event = "htmx:swapError" + TargetErrorEvent Event = "htmx:targetError" + TimeoutEvent Event = "htmx:timeout" + ValidationValidateEvent Event = "htmx:validation:validate" + ValidationFailedEvent Event = "htmx:validation:failed" + ValidationHaltedEvent Event = "htmx:validation:halted" + XhrAbortEvent Event = "htmx:xhr:abort" + XhrLoadEndEvent Event = "htmx:xhr:loadend" + XhrLoadStartEvent Event = "htmx:xhr:loadstart" + XhrProgressEvent Event = "htmx:xhr:progress" + + // RevealedEvent Misc Events + RevealedEvent Event = "revealed" + InstersectEvent Event = "intersect" + PollingEvent Event = "every" + + // ClickEvent Dom Events + ClickEvent Event = "onclick" + ChangeEvent Event = "onchange" + InputEvent Event = "oninput" + FocusEvent Event = "onfocus" + BlurEvent Event = "onblur" + KeyDownEvent Event = "onkeydown" + KeyUpEvent Event = "onkeyup" + KeyPressEvent Event = "onkeypress" + SubmitEvent Event = "onsubmit" + LoadDomEvent Event = "onload" + UnloadEvent Event = "onunload" + ResizeEvent Event = "onresize" + ScrollEvent Event = "onscroll" + DblClickEvent Event = "ondblclick" + MouseOverEvent Event = "onmouseover" + MouseOutEvent Event = "onmouseout" + MouseMoveEvent Event = "onmousemove" + MouseDownEvent Event = "onmousedown" + MouseUpEvent Event = "onmouseup" + ContextMenuEvent Event = "oncontextmenu" + DragStartEvent Event = "ondragstart" + DragEvent Event = "ondrag" + DragEnterEvent Event = "ondragenter" + DragLeaveEvent Event = "ondragleave" + DragOverEvent Event = "ondragover" + DropEvent Event = "ondrop" + DragEndEvent Event = "ondragend" +) diff --git a/framework/hx/htmx_test.go b/framework/hx/htmx_test.go new file mode 100644 index 0000000..2bf7811 --- /dev/null +++ b/framework/hx/htmx_test.go @@ -0,0 +1,19 @@ +package hx + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewStringTrigger(t *testing.T) { + trigger := "click once, htmx:click throttle:5, load delay:10" + tgr := NewStringTrigger(trigger) + assert.Equal(t, len(tgr.events), 3) + assert.Equal(t, tgr.events[0].event, "click") + assert.Equal(t, tgr.events[0].modifiers[0].Modifier(), "once") + assert.Equal(t, tgr.events[1].event, "click") + assert.Equal(t, tgr.events[1].modifiers[0].Modifier(), "throttle:5") + assert.Equal(t, tgr.events[2].event, "load") + assert.Equal(t, tgr.events[2].modifiers[0].Modifier(), "delay:10") + assert.Equal(t, "click once, click throttle:5, load delay:10", tgr.ToString()) +} diff --git a/framework/hx/modifiers.go b/framework/hx/modifiers.go new file mode 100644 index 0000000..acdba6d --- /dev/null +++ b/framework/hx/modifiers.go @@ -0,0 +1,49 @@ +package hx + +import "fmt" + +type Modifier interface { + Modifier() string +} + +type RawModifier struct { + modifier string +} + +func StringModifier(modifier string) RawModifier { + return RawModifier{modifier} +} + +func (r RawModifier) Modifier() string { + return r.modifier +} + +type OnceModifier struct{} + +func (o OnceModifier) Modifier() string { + return "once" +} + +type ThrottleModifier struct { + durationSeconds int +} + +func (t ThrottleModifier) Modifier() string { + return fmt.Sprintf("throttle:%ds", t.durationSeconds) +} + +func Throttle(durationSeconds int) ThrottleModifier { + return ThrottleModifier{durationSeconds} +} + +type DelayModifier struct { + durationSeconds int +} + +func (t DelayModifier) Modifier() string { + return fmt.Sprintf("delay:%ds", t.durationSeconds) +} + +func Delay(durationSeconds int) DelayModifier { + return DelayModifier{durationSeconds} +} diff --git a/framework/hx/trigger.go b/framework/hx/trigger.go new file mode 100644 index 0000000..5dedefe --- /dev/null +++ b/framework/hx/trigger.go @@ -0,0 +1,80 @@ +package hx + +import ( + "strings" +) + +type Trigger struct { + events []TriggerEvent +} + +type TriggerEvent struct { + event Event + modifiers []Modifier +} + +func NewTrigger(opts ...TriggerEvent) *Trigger { + t := Trigger{ + events: make([]TriggerEvent, 0), + } + if len(opts) > 0 { + t.events = opts + } + return &t +} + +func NewStringTrigger(trigger string) Trigger { + t := Trigger{ + events: make([]TriggerEvent, 0), + } + + split := strings.Split(trigger, ", ") + for _, s := range split { + parts := strings.Split(s, " ") + event := parts[0] + + if strings.HasPrefix(event, "htmx:") { + event = event[5:] + } + + modifiers := make([]Modifier, 0) + if len(parts) > 1 { + for _, m := range parts[1:] { + modifiers = append(modifiers, RawModifier{modifier: m}) + } + } + t.events = append(t.events, TriggerEvent{ + event: event, + modifiers: modifiers, + }) + } + return t +} + +func (t Trigger) AddEvent(event TriggerEvent) Trigger { + t.events = append(t.events, event) + return t +} + +func (t Trigger) ToString() string { + builder := strings.Builder{} + for i, e := range t.events { + eventName := e.event + if strings.HasPrefix(eventName, "htmx:") { + eventName = eventName[5:] + } + builder.WriteString(eventName) + for _, m := range e.modifiers { + builder.WriteString(" ") + builder.WriteString(m.Modifier()) + } + if i < len(t.events)-1 { + builder.WriteString(", ") + } + } + return builder.String() +} + +func (t Trigger) Render(builder *strings.Builder) { + builder.WriteString(t.ToString()) +} diff --git a/framework/hx/triggers.go b/framework/hx/triggers.go new file mode 100644 index 0000000..10c16bc --- /dev/null +++ b/framework/hx/triggers.go @@ -0,0 +1,16 @@ +package hx + +// TriggerClick Common trigger events +const TriggerClick = ClickEvent +const TriggerClickOnce = TriggerClick + " once" +const TriggerDblClick = DblClickEvent +const TriggerKeyUpEnter = "keyup[keyCode==13]" +const TriggerEnterPressed = TriggerKeyUpEnter +const TriggerBlur = "blur" +const TriggerEvery1s = "every:1s" +const TriggerEvery2s = "every:2s" +const TriggerEvery5s = "every:5s" +const TriggerEvery10s = "every:10s" +const TriggerEvery30s = "every:30s" +const TriggerEvery1m = "every:1m" +const TriggerLoad = LoadEvent diff --git a/htmgo-site/go.mod b/htmgo-site/go.mod index 7c0aae5..ed4d988 100644 --- a/htmgo-site/go.mod +++ b/htmgo-site/go.mod @@ -3,22 +3,22 @@ module htmgo-site go 1.23.0 require ( - github.com/google/uuid v1.6.0 github.com/labstack/echo/v4 v4.12.0 github.com/maddalax/htmgo/framework v0.0.0-20240920021308-279a3c716342 github.com/mattn/go-sqlite3 v1.14.16 + github.com/yuin/goldmark v1.7.4 + github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc ) require ( github.com/alecthomas/chroma/v2 v2.2.0 // indirect github.com/dlclark/regexp2 v1.7.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - github.com/yuin/goldmark v1.7.4 // indirect - github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/sys v0.25.0 // indirect diff --git a/htmgo-site/go.sum b/htmgo-site/go.sum index 7fd62ef..4dad40b 100644 --- a/htmgo-site/go.sum +++ b/htmgo-site/go.sum @@ -1,5 +1,6 @@ github.com/alecthomas/chroma/v2 v2.2.0 h1:Aten8jfQwUqEdadVFFjNyjx7HTexhKP0XuqBG67mRDY= github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs= +github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae h1:zzGwJfFlFGD94CyyYwCJeSuD32Gj9GTaSi5y9hoVzdY= github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/htmgo-site/pages/markdown.go b/htmgo-site/pages/markdown.go index bc1dfd1..f9f8b39 100644 --- a/htmgo-site/pages/markdown.go +++ b/htmgo-site/pages/markdown.go @@ -4,8 +4,10 @@ import ( "embed" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/htmgo/service" + "github.com/maddalax/htmgo/framework/hx" "htmgo-site/internal/markdown" "htmgo-site/pages/base" + "htmgo-site/partials" ) func MarkdownHandler(ctx *h.RequestContext, path string) error { @@ -15,6 +17,9 @@ func MarkdownHandler(ctx *h.RequestContext, path string) error { func MarkdownPage(ctx *h.RequestContext, path string) *h.Element { return base.RootPage( h.Div( + h.Div( + h.GetPartial(partials.TestPartial, hx.LoadEvent), + ), h.Class("w-full p-4 flex flex-col justify-center items-center"), MarkdownContent(ctx, path), h.Div( diff --git a/htmgo-site/partials/load/generated.go b/htmgo-site/partials/load/generated.go index e63b0bf..73055f6 100644 --- a/htmgo-site/partials/load/generated.go +++ b/htmgo-site/partials/load/generated.go @@ -11,6 +11,10 @@ func GetPartialFromContext(ctx echo.Context) *h.Partial { cc := ctx.(*h.RequestContext) return partials.ToggleNavbar(cc) } + if path == "TestPartial" || path == "/htmgo-site/partials.TestPartial" { + cc := ctx.(*h.RequestContext) + return partials.TestPartial(cc) + } return nil } diff --git a/htmgo-site/partials/navbar.go b/htmgo-site/partials/navbar.go index 1d9c6c8..63f0973 100644 --- a/htmgo-site/partials/navbar.go +++ b/htmgo-site/partials/navbar.go @@ -1,6 +1,8 @@ package partials -import "github.com/maddalax/htmgo/framework/h" +import ( + "github.com/maddalax/htmgo/framework/h" +) type NavItem struct { Name string @@ -13,6 +15,12 @@ func ToggleNavbar(ctx *h.RequestContext) *h.Partial { ) } +func TestPartial(ctx *h.RequestContext) *h.Partial { + return h.NewPartial( + h.Div(h.Text("This is a test")), + ) +} + var navItems = []NavItem{ {Name: "Docs", Url: "/docs"}, {Name: "Examples", Url: "/examples"}, @@ -37,6 +45,7 @@ func NavBar(expanded bool) *h.Element { ) desktopNav := h.Nav( + h.Script("https://buttons.github.io/buttons.js"), h.Class("hidden sm:block bg-neutral-100 border border-b-slate-300 p-4 md:p-3"), h.Div( h.Class("max-w-[95%] md:max-w-prose mx-auto"), @@ -45,6 +54,7 @@ func NavBar(expanded bool) *h.Element { h.Div( h.Class("flex items-center"), h.A( + h.Boost(), h.Class("text-2xl"), h.Href("/"), h.Text("htmgo"), @@ -55,6 +65,7 @@ func NavBar(expanded bool) *h.Element { return h.Div( h.Class("flex items-center"), h.A( + h.Boost(), h.Class(""), h.Href(item.Url), h.Text(item.Name), @@ -87,6 +98,7 @@ func MobileNav(expanded bool) *h.Element { h.Div( h.Class("flex items-center"), h.A( + h.Boost(), h.Class("text-2xl"), h.Href("/"), h.Text("htmgo"), @@ -94,8 +106,19 @@ func MobileNav(expanded bool) *h.Element { h.Div( h.Class("flex items-center"), h.Button( - h.GetPartialWithQs(ToggleNavbar, h.Ternary(expanded, "expanded=false", "expanded=true")), - h.Trigger(h.TriggerClick), + h.Boost(), + + h.GetPartialWithQs( + ToggleNavbar, + h.NewQs("expanded", h.Ternary(expanded, "false", "true"), "test", "true"), + "click", + ), + + h.AttributePairs( + "class", "text-2xl", + "aria-expanded", h.Ternary(expanded, "true", "false"), + ), + h.Class("text-2xl"), h.Text("☰"), ), @@ -109,6 +132,7 @@ func MobileNav(expanded bool) *h.Element { return h.Div( h.Class("flex items-center"), h.A( + h.Boost(), h.Class(""), h.Href(item.Url), h.Text(item.Name),