UNPKG

htmx.org

Version:

high power tools for html

1 lines 16.6 kB
var htmx=htmx||function(){"use strict";var e=["get","post","put","delete","patch"];function l(e){if(e==="null"||e==="false"||e===""){return null}else if(e.lastIndexOf("ms")===e.length-2){return parseFloat(e.substr(0,e.length-2))}else if(e.lastIndexOf("s")===e.length-1){return parseFloat(e.substr(0,e.length-1))*1e3}else{return parseFloat(e)}}function x(e,t){return e.getAttribute&&e.getAttribute(t)}function u(e,t){return x(e,t)||x(e,"data-"+t)}function n(e){return e.parentElement}function o(){return document}function s(e,t){if(t(e)){return e}else if(n(e)){return s(n(e),t)}else{return null}}function b(e,t){var r=null;s(e,function(e){return r=x(e,t)});return r}function f(e,t){var r=e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector;return r&&r.call(e,t)}function r(e){var t=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i;var r=t.exec(e);if(r){return r[1].toLowerCase()}else{return""}}function i(e,t){var r=new DOMParser;var n=r.parseFromString(e,"text/html");var i=n.body;while(t>0){t--;i=i.firstChild}return i}function c(e){var t=r(e);switch(t){case"thead":case"tbody":case"tfoot":case"colgroup":case"caption":return i("<table>"+e+"</table>",1);case"col":return i("<table><colgroup>"+e+"</colgroup></table>",2);case"tr":return i("<table><tbody>"+e+"</tbody></table>",2);case"td":case"th":return i("<table><tbody><tr>"+e+"</tr></tbody></table>",3);default:return i(e,0)}}function t(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function a(e){return t(e,"Function")}function h(e){return t(e,"Object")}function S(e){var t="htmx-internal-data";var r=e[t];if(!r){r=e[t]={}}return r}function v(e){var t=[];if(e){for(var r=0;r<e.length;r++){t.push(e[r])}}return t}function w(e,t){if(e){for(var r=0;r<e.length;r++){t(e[r])}}}function d(e){var t=e.getBoundingClientRect();var r=t.top;var n=t.bottom;return r<window.innerHeight&&n>=0}function m(e){return o().body.contains(e)}function g(e,t){return e.concat(t)}function p(e){return e.split(/\s+/)}function y(e,t){for(var r in t){if(t.hasOwnProperty(r)){e[r]=t[r]}}return e}function E(e){return eval(e)}function C(t){var e=htmx.on("load.htmx",function(e){t(e.detail.elt)});return e}function L(){htmx.logger=function(e,t,r){if(console){console.log(t,e,r)}}}function O(e,t){if(t){return e.querySelector(t)}else{return o().body.querySelector(e)}}function T(e,t){if(t){return e.querySelectorAll(t)}else{return o().body.querySelectorAll(e)}}function q(e,t){if(t){setTimeout(function(){q(e)},t)}else{e.parentElement.removeChild(e)}}function H(e,t,r){if(r){setTimeout(function(){H(e,t)},r)}else{e.classList.add(t)}}function X(e,t,r){if(r){setTimeout(function(){X(e,t)},r)}else{e.classList.remove(t)}}function A(e,t){e.classList.toggle(t)}function N(e,t){w(e.parentElement.children,function(e){X(e,t)});H(e,t)}function M(e,t){do{if(e==null||f(e,t))return e}while(e=e&&n(e))}function D(e,t,r){if(a(t)){return{target:o().body,event:e,listener:t}}else{return{target:e,event:t,listener:r}}}function I(t,r,n){Je(function(){var e=D(t,r,n);e.target.addEventListener(e.event,e.listener)});var e=a(r);return e?r:n}function R(t,r,n){Je(function(){var e=D(t,r,n);e.target.removeEventListener(e.event,e.listener)});return a(r)?r:n}function F(e){var t=s(e,function(e){return x(e,"hx-target")!==null});if(t){var r=x(t,"hx-target");if(r==="this"){return t}else if(r.indexOf("closest ")===0){return M(e,r.substr(8))}else{return o().querySelector(r)}}else{var n=S(e);if(n.boosted){return o().body}else{return e}}}function P(t,r){w(t.attributes,function(e){if(!r.hasAttribute(e.name)){t.removeAttribute(e.name)}});w(r.attributes,function(e){t.setAttribute(e.name,e.value)})}function k(e){var n=[];w(v(e.children),function(e){if(u(e,"hx-swap-oob")==="true"){var t=o().getElementById(e.id);if(t){var r=o().createDocumentFragment();r.appendChild(e);n=n.concat(U(t,r))}else{e.parentNode.removeChild(e);ge(o().body,"oobErrorNoTarget.htmx",{content:e})}}});return n}function j(n,e){var i=[];w(e.querySelectorAll("[id]"),function(e){var t=n.querySelector(e.tagName+"[id="+e.id+"]");if(t){var r=e.cloneNode();P(e,t);i.push(function(){P(e,r)})}});return i}function J(e,t,r){var n=j(e,r);while(r.childNodes.length>0){var i=r.firstChild;e.insertBefore(i,t);if(i.nodeType!==Node.TEXT_NODE){pe(i,"load.htmx",{});ve(i)}}return n}function U(e,t){if(e.tagName==="BODY"){return Y(e,t)}else{var r=J(n(e),e,t);n(e).removeChild(e);return r}}function z(e,t){return J(e,e.firstChild,t)}function B(e,t){return J(n(e),e,t)}function V(e,t){return J(e,null,t)}function G(e,t){return J(n(e),e.nextSibling,t)}function Y(e,t){var r=e.firstChild;var n=J(e,r,t);if(r){while(r.nextSibling){e.removeChild(r.nextSibling)}e.removeChild(r)}return n}function _(e,t){var r=b(e,"hx-select");if(r){var n=o().createDocumentFragment();w(t.querySelectorAll(r),function(e){n.appendChild(e)});t=n}return t}function K(e,t,r,n){var i=c(n);if(i){var a=k(i);i=_(r,i);switch(e){case"outerHTML":return g(a,U(t,i));case"afterbegin":return g(a,z(t,i));case"beforebegin":return g(a,B(t,i));case"beforeend":return g(a,V(t,i));case"afterend":return g(a,G(t,i));default:return g(a,Y(t,i))}}}function Q(e,t){if(t){if(t.indexOf("{")===0){var r=JSON.parse(t);for(var n in r){if(r.hasOwnProperty(n)){var i=r[n];if(!h(i)){i={value:i}}pe(e,n,i)}}}else{pe(e,t,[])}}}function W(e){var t={trigger:"click"};var r=u(e,"hx-trigger");if(r){var n=p(r);if(n.length>0){var i=n[0];if(i==="every"){t.pollInterval=l(n[1])}else if(i.indexOf("sse:")===0){t.sseEvent=i.substr(4)}else{t["trigger"]=i;for(var a=1;a<n.length;a++){var o=n[a].trim();if(o==="changed"){t.changed=true}if(o==="once"){t.once=true}if(o.indexOf("delay:")===0){t.delay=l(o.substr(6))}}}}}else{if(f(e,"form")){t["trigger"]="submit"}else if(f(e,"input, textarea, select")){t["trigger"]="change"}}return t}function Z(e){var t=p(e);if(t.length>1){var r=t[0];var n=t[1].trim();var i;var a;if(n.indexOf(":")>0){var o=n.split(":");i=o[0];a=l(o[1])}else{i=n;a=100}return{operation:r,cssClass:i,delay:a}}else{return null}}function $(i,e){w(e.split("&"),function(e){var n=0;w(e.split(","),function(e){var t=e.trim();var r=Z(t);if(r){if(r.operation==="toggle"){setTimeout(function(){setInterval(function(){i.classList[r.operation].call(i.classList,r.cssClass)},r.delay)},n);n=n+r.delay}else{n=n+r.delay;setTimeout(function(){i.classList[r.operation].call(i.classList,r.cssClass)},n)}}})})}function ee(e){S(e).cancelled=true}function te(e,t,r,n){var i=S(e);i.timeout=setTimeout(function(){if(m(e)&&i.cancelled!==true){je(e,t,r);te(e,t,u(e,"hx-"+t),n)}},n)}function re(e){return location.hostname===e.hostname&&x(e,"href")&&x(e,"href").indexOf("#")!==0}function ne(e,t,r){if(e.tagName==="A"&&re(e)||e.tagName==="FORM"){t.boosted=true;var n,i;if(e.tagName==="A"){n="get";i=x(e,"href")}else{var a=x(e,"method");n=a?a.toLowerCase():"get";i=x(e,"action")}ae(e,n,i,t,r,true)}}function ie(e){return e.tagName==="FORM"||f(e,'input[type="submit"], button')&&M(e,"form")!==null||e.tagName==="A"&&e.href&&e.href.indexOf("#")!==0}function ae(i,a,o,e,l,u){var t=function(e){if(u||ie(i))e.preventDefault();var t=S(e);var r=S(i);if(!t.handled){t.handled=true;if(l.once){if(r.triggeredOnce){return}else{r.triggeredOnce=true}}if(l.changed){if(r.lastValue===i.value){return}else{r.lastValue=i.value}}if(r.delayed){clearTimeout(r.delayed)}var n=function(){je(i,a,o,e.target)};if(l.delay){r.delayed=setTimeout(n,l.delay)}else{n()}}};e.trigger=l.trigger;e.eventListener=t;i.addEventListener(l.trigger,t)}function oe(){if(!window["htmxScrollHandler"]){var e=function(){w(o().querySelectorAll("[hx-trigger='revealed']"),function(e){le(e)})};window["htmxScrollHandler"]=e;window.addEventListener("scroll",e)}}function le(e){var t=S(e);if(!t.revealed&&d(e)){t.revealed=true;je(e,t.verb,t.path)}}function ue(e){if(!m(e)){e.sseSource.close();return true}}function se(t,e){var r={config:{withCredentials:true}};pe(t,"initSSE.htmx",r);var n=new EventSource(e,r.config);n.onerror=function(e){ge(t,"sseError.htmx",{error:e,source:n});ue(t)};S(t).sseSource=n}function fe(e,t,r,n){var i=s(e,function(e){return e.sseSource});if(i){var a=function(){if(!ue(i)){if(m(e)){je(e,t,r)}else{i.sseSource.removeEventListener(n,a)}}};i.sseSource.addEventListener(n,a)}else{ge(e,"noSSESourceError.htmx")}}function ce(e,t,r,n,i){var a=function(){if(!n.loaded){n.loaded=true;je(e,t,r)}};if(i){setTimeout(a,i)}else{a()}}function he(r,n,i){var a=false;w(e,function(e){var t=u(r,"hx-"+e);if(t){a=true;n.path=t;n.verb=e;if(i.sseEvent){fe(r,e,t,i.sseEvent)}else if(i.trigger==="revealed"){oe();le(r)}else if(i.trigger==="load"){ce(r,e,t,n,i.delay)}else if(i.pollInterval){n.polling=true;te(r,e,t,i.pollInterval)}else{ae(r,e,t,n,i)}}});return a}function ve(e){var t=S(e);if(!t.processed){t.processed=true;var r=W(e);var n=he(e,t,r);if(!n&&b(e,"hx-boost")==="true"){ne(e,t,r)}var i=u(e,"hx-sse-source");if(i){se(e,i)}var a=u(e,"hx-classes");if(a){$(e,a)}}if(e.children){w(e.children,function(e){ve(e)})}}function de(e,t,r){var n=b(e,"hx-error-url");if(n){var i=new XMLHttpRequest;i.open("POST",n);i.setRequestHeader("Content-Type","application/json;charset=UTF-8");i.send(JSON.stringify({elt:e.id,event:t,detail:r}))}}function me(e,t){var r;if(window.CustomEvent&&typeof window.CustomEvent==="function"){r=new CustomEvent(e,{bubbles:true,cancelable:true,detail:t})}else{r=o().createEvent("CustomEvent");r.initCustomEvent(e,true,true,t)}return r}function ge(e,t,r){pe(e,t,y({isError:true},r))}function pe(e,t,r){r["elt"]=e;var n=me(t,r);if(htmx.logger){htmx.logger(e,t,r);if(r.isError){de(e,t,r)}}var i=e.dispatchEvent(n);return i}var ye=null;function xe(){var e=o().querySelector("[hx-history-elt]");return e||o().body}function be(e,t,r,n){var i=JSON.parse(localStorage.getItem("htmx-history-cache"))||[];for(var a=0;a<i.length;a++){if(i[a].url===e){i=i.slice(a,1);break}}i.push({url:e,content:t,title:r,scroll:n});while(i.length>htmx.config.historyCacheSize){i.shift()}localStorage.setItem("htmx-history-cache",JSON.stringify(i))}function Se(e){var t=JSON.parse(localStorage.getItem("htmx-history-cache"))||[];for(var r=0;r<t.length;r++){if(t[r].url===e){return t[r]}}return null}function we(){var e=xe();var t=ye||location.pathname+location.search;pe(o().body,"beforeHistorySave.htmx",{path:t,historyElt:e});if(htmx.config.historyEnabled)history.replaceState({},o().title,window.location.href);be(t,e.innerHTML,o().title,window.scrollY)}function Ee(e){if(htmx.config.historyEnabled)history.pushState({},"",e);ye=e}function Ce(e){w(e,function(e){e.call()})}function Le(t){var e=new XMLHttpRequest;var r={path:t,xhr:e};pe(o().body,"historyCacheMiss.htmx",r);e.open("GET",t,true);e.onload=function(){if(this.status>=200&&this.status<400){pe(o().body,"historyCacheMissLoad.htmx",r);var e=c(this.response);e=e.querySelector("[hx-history-elt]")||e;Ce(Y(xe(),e));ye=t}else{ge(o().body,"historyCacheMissLoadError.htmx",r)}};e.send()}function Oe(e){we(ye);e=e||location.pathname+location.search;pe(o().body,"historyRestore.htmx",{path:e});var t=Se(e);if(t){Ce(Y(xe(),c(t.content)));document.title=t.title;window.scrollTo(0,t.scroll);ye=e}else{Le(e)}}function Te(e){return b(e,"hx-push-url")==="true"||e.tagName==="A"&&S(e).boosted}function qe(e){Xe(e,"add")}function He(e){Xe(e,"remove")}function Xe(e,t){var r=b(e,"hx-indicator");if(r){var n=o().querySelectorAll(r)}else{n=[e]}w(n,function(e){e.classList[t].call(e.classList,"htmx-request")})}function Ae(e,t){for(var r=0;r<e.length;r++){var n=e[r];if(n.isSameNode(t)){return true}}return false}function Ne(e){if(e.name===""||e.name==null||e.disabled){return false}if(e.type==="button"||e.type==="submit"||e.tagName==="image"||e.tagName==="reset"||e.tagName==="file"){return false}if(e.type==="checkbox"||e.type==="radio"){return e.checked}return true}function Me(t,r,e){if(e==null||Ae(t,e)){return}else{t.push(e)}if(Ne(e)){var n=x(e,"name");var i=e.value;if(n&&i){var a=r[n];if(a){if(Array.isArray(a)){a.push(i)}else{r[n]=[a,i]}}else{r[n]=i}}}if(f(e,"form")){var o=e.elements;w(o,function(e){Me(t,r,e)})}}function De(e,t){var r=[];var n={};Me(r,n,e);var i=b(e,"hx-include");if(i){var a=o().querySelectorAll(i);w(a,function(e){Me(r,n,e)})}if(t!=="get"){Me(r,n,M(e,"form"))}return n}function Ie(e,t,r){if(e!==""){e+="&"}e+=encodeURIComponent(t)+"="+encodeURIComponent(r);return e}function Re(e){var t="";for(var r in e){if(e.hasOwnProperty(r)){var n=e[r];if(Array.isArray(n)){w(n,function(e){t=Ie(t,r,e)})}else{t=Ie(t,r,n)}}}return t}function Fe(e,t,r,n){var i={"X-HX-Request":"true","X-HX-Trigger":x(e,"id"),"X-HX-Trigger-Name":x(e,"name"),"X-HX-Target":x(t,"id"),"Current-URL":o().location.href};if(r){i["X-HX-Prompt"]=r}if(n){i["X-HX-Event-Target"]=x(n,"id")}if(o().activeElement){i["X-HX-Active-Element"]=x(o().activeElement,"id");i["X-HX-Active-Element-Name"]=x(o().activeElement,"name");if(o().activeElement.value){i["X-HX-Active-Element-Value"]=x(o().activeElement,"value")}}return i}function Pe(t,e,r){var n=b(e,"hx-params");if(n){if(n==="none"){return{}}else if(n==="*"){return t}else if(n.indexOf("not ")===0){w(n.substr(4).split(","),function(e){e=e.trim();delete t[e]});return t}else{var i={};w(n.split(","),function(e){e=e.trim();i[e]=t[e]});return i}}else{return t}}function ke(e){var t=b(e,"hx-swap");var r={swapStyle:htmx.config.defaultSwapStyle,swapDelay:htmx.config.defaultSwapDelay,settleDelay:htmx.config.defaultSettleDelay};if(t){var n=p(t);if(n.length>0){r["swapStyle"]=n[0];for(var i=1;i<n.length;i++){var a=n[i];if(a.indexOf("swap:")===0){r["swapDelay"]=l(a.substr(5))}if(a.indexOf("settle:")===0){r["settleDelay"]=l(a.substr(7))}}}}return r}function je(o,e,t,r){var l=F(o);if(l==null){ge(o,"targetError.htmx",{target:x(o,"hx-target")});return}var n=S(o);if(n.requestInFlight){return}else{n.requestInFlight=true}var u=function(){n.requestInFlight=false};var i=b(o,"hx-prompt");if(i){var a=prompt(i);if(!pe(o,"prompt.htmx",{prompt:a,target:l}))return u()}var s=b(o,"hx-confirm");if(s){if(!confirm(s))return u()}var f=new XMLHttpRequest;var c=Fe(o,l,a,r);var h=De(o,e);var v=Pe(h,o,e);if(e!=="get"){c["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";if(e!=="post"){c["X-HTTP-Method-Override"]=e.toUpperCase()}}var d={parameters:v,unfilteredParameters:h,headers:c,target:l,verb:e};if(!pe(o,"configRequest.htmx",d))return u();var m;if(e==="get"){var g=Object.keys(v).length===0;m=t+(g?"":"?"+Re(v));f.open("GET",m,true)}else{m=t;f.open("POST",m,true)}f.overrideMimeType("text/html");for(var p in c){if(c.hasOwnProperty(p)){if(c[p])f.setRequestHeader(p,c[p])}}var y={xhr:f,target:l};f.onload=function(){try{if(!pe(o,"beforeOnLoad.htmx",y))return;Q(o,this.getResponseHeader("X-HX-Trigger"));var r=this.getResponseHeader("X-HX-Push");var n=Te(o)||r;if(this.status>=200&&this.status<400){if(this.status===286){ee(o)}if(this.status!==204){if(!pe(o,"beforeSwap.htmx",y))return;var i=this.response;if(n){we()}var a=ke(o);l.classList.add("htmx-swapping");var e=function(){try{var e=K(a.swapStyle,l,o,i);l.classList.remove("htmx-swapping");l.classList.add("htmx-settling");pe(o,"afterSwap.htmx",y);var t=function(){w(e,function(e){e.call()});l.classList.remove("htmx-settling");if(n){Ee(r||m)}pe(o,"afterSettle.htmx",y)};if(a.settleDelay>0){setTimeout(t,a.settleDelay)}else{t()}}catch(e){ge(o,"swapError.htmx",y);throw e}};if(a.swapDelay>0){setTimeout(e,a.swapDelay)}else{e()}}}else{ge(o,"responseError.htmx",y)}}catch(e){y["exception"]=e;ge(o,"onLoadError.htmx",y);throw e}finally{He(o);u();pe(o,"afterOnLoad.htmx",y)}};f.onerror=function(){He(o);ge(o,"sendError.htmx",y);u()};if(!pe(o,"beforeRequest.htmx",y))return u();qe(o);f.send(e==="get"?null:Re(v))}function Je(e){if(o().readyState!=="loading"){e()}else{o().addEventListener("DOMContentLoaded",e)}}(function(){var e=Ue();if(e===null||e.includeIndicatorStyles!==false){o().head.insertAdjacentHTML("beforeend","<style> .htmx-indicator{opacity:0;transition: opacity 200ms ease-in;} .htmx-request .htmx-indicator{opacity:1} .htmx-request.htmx-indicator{opacity:1} </style>")}})();function Ue(){var e=o().querySelector('meta[name="htmx-config"]');if(e){return JSON.parse(e.content)}else{return null}}function ze(){var e=Ue();if(e){htmx.config=y(htmx.config,e)}}Je(function(){ze();var e=o().body;ve(e);pe(e,"load.htmx",{});window.onpopstate=function(){Oe()}});return{onLoad:C,process:ve,on:I,off:R,trigger:pe,find:O,findAll:T,closest:M,remove:q,addClass:H,removeClass:X,toggleClass:A,takeClass:N,logAll:L,logger:null,config:{historyEnabled:true,historyCacheSize:10,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:100,includeIndicatorStyles:true},version:"0.0.2",_:E}}();