mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 15:25:26 +00:00
1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
var HTMx=HTMx||function(){"use strict";var n=["get","post","put","delete","patch"];function h(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 d(e,t){return e.getAttribute&&e.getAttribute(t)}function g(e,t){return d(e,t)||d(e,"data-"+t)}function s(e){return e.parentElement}function p(){return document}function l(e,t){if(t(e)){return e}else if(s(e)){return l(s(e),t)}else{return null}}function m(e,t){var r=null;l(e,function(e){return r=d(e,t)});return r}function u(e,t){return e!=null&&(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector).call(e,t)}function a(e,t){do{if(e==null||u(e,t))return e}while(e=e&&s(e))}function f(e){var t=p().createRange();return t.createContextualFragment(e)}function t(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function o(e){return t(e,"Function")}function c(e){return t(e,"Object")}function x(e){var t="hx-data-internal";var r=e[t];if(!r){r=e[t]={}}return r}function v(e){var t=[];y(e,function(e){t.push(e)});return t}function y(e,t){for(var r=0;r<e.length;r++){t(e[r])}}function r(e){var t=e.getBoundingClientRect();var r=t.top;var n=t.bottom;return r<window.innerHeight&&n>=0}function S(e){return p().body.contains(e)}function b(e){var t=l(e,function(e){return d(e,"hx-target")!==null});if(t){var r=d(t,"hx-target");if(r==="this"){return t}else{return p().querySelector(r)}}else{var n=x(e);if(n.boosted){return p().body}else{return e}}}function w(e){var t=g(e,"hx-swap-direct");if(t){var r=p().getElementById(d(e,"id"));if(r){if(t==="merge"){C(r,e)}else{var n=s(r);n.insertBefore(e,r);n.removeChild(r);return true}}}return false}function E(t,r,e,n,i){var a=f(e);var o;if(i){o=v(a.querySelectorAll(i))}else{o=v(a.childNodes)}y(o,function(e){if(!w(e)){t.insertBefore(e,r)}if(e.nodeType!==Node.TEXT_NODE){J(e,"load.hx",{parent:s(e)});U(e)}});if(n){n.call()}}function T(e,t){for(var r=0;r<t.length;r++){var n=t[r];if(e.hasAttribute("id")&&e.id===n.id){return n}if(!n.hasAttribute("id")&&e.tagName===n.tagName){return n}}return null}function i(t,r){y(t.attributes,function(e){if(!r.hasAttribute(e.name)){t.removeAttribute(e.name)}});y(r.attributes,function(e){t.setAttribute(e.name,e.value)})}function O(r,e){var n=v(r.children);var i=p().createElement("span");r.insertBefore(i,r.firstChild);y(e.childNodes,function(e){var t=T(e,n);if(t){while(i.nextSibling&&i.nextSibling!==t){r.removeChild(i.nextSibling)}r.insertBefore(i,t.nextSibling);C(t,e)}else{r.insertBefore(e,i)}});while(i.nextSibling){r.removeChild(i.nextSibling)}r.removeChild(i)}function C(e,t){i(e,t);O(e,t)}function L(e,t,r){var n=f(t);C(e,r?n.querySelector(r):n.firstElementChild)}function A(e,t,r,n){var i=m(t,"hx-swap");var a=m(t,"hx-select");if(i==="merge"){L(e,r,a)}else if(i==="outerHTML"){E(s(e),e,r,n,a);s(e).removeChild(e)}else if(i==="prepend"){E(e,e.firstChild,r,n,a)}else if(i==="prependBefore"){E(s(e),e,r,n,a)}else if(i==="append"){E(e,null,r,n,a)}else if(i==="appendAfter"){E(s(e),e.nextSibling,r,n,a)}else{e.innerHTML="";E(e,null,r,n,a)}}function H(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(!c(i)){i={value:i}}J(e,n,i)}}}else{J(e,t,[])}}}function M(e){var t=m(e,"hx-trigger");if(t){return t}else{if(u(e,"button")){return"click"}else if(u(e,"form")){return"submit"}else if(u(e,"input, textarea, select")){return"change"}else{return"click"}}}function N(a,e,o){var t=e.split(",");y(t,function(e){var t="";var r=50;var n=e.trim();if(n.indexOf(":")>0){var i=n.split(":");t=i[0];r=h(i[1])}else{t=n}setTimeout(function(){a.classList[o].call(a.classList,t)},r)})}function q(e,t,r){var n=M(e);var i=x(e);if(n.trim().indexOf("every ")===0){var a=n.split(/\s+/);var o=a[1];if(o){var s=h(o);i.timeout=setTimeout(function(){if(S(e)){le(e,t,r);q(e,t,g(e,"hx-"+t))}},s)}}}function I(e){return location.hostname===e.hostname&&d(e,"href")&&!d(e,"href").startsWith("#")}function R(e,t,r){if(e.tagName==="A"&&I(e)||e.tagName==="FORM"){t.boosted=true;var n,i;if(e.tagName==="A"){n="get";i=d(e,"href")}else{var a=d(e,"method");n=a?a.toLowerCase():"get";i=d(e,"action")}F(e,n,i,t,r,true)}}function F(a,o,s,e,t,u){var r=function(e){if(u)e.preventDefault();var t=x(e);var r=x(a);if(!t.handled){t.handled=true;if(g(a,"hx-trigger-once")==="true"){if(r.triggeredOnce){return}else{r.triggeredOnce=true}}if(g(a,"hx-trigger-changed-only")==="true"){if(r.lastValue===a.value){return}else{r.lastValue=a.value}}if(r.delayed){clearTimeout(r.delayed)}var n=g(a,"hx-trigger-delay");var i=function(){le(a,o,s,e.target)};if(n){r.delayed=setTimeout(i,h(n))}else{i()}}};e.trigger=t;e.eventListener=r;a.addEventListener(t,r)}function B(){if(!window["hxScrollHandler"]){var e=function(){y(p().querySelectorAll("[hx-trigger='reveal']"),function(e){X(e)})};window["hxScrollHandler"]=e;window.addEventListener("scroll",e)}}function X(e){var t=x(e);if(!t.revealed&&r(e)){t.revealed=true;le(e,t.verb,t.path)}}function k(e){if(!S(e)){e.sseSource.close();return true}}function P(t,e){var r={withCredentials:true};J(t,"initSSE.mx",r);var n=new EventSource(e);n.onerror=function(e){J(t,"sseError.mx",{error:e,source:n});k(t)};x(t).sseSource=n}function U(a){var o=x(a);if(!o.processed){o.processed=true;var s=M(a);var u=false;y(n,function(e){var t=g(a,"hx-"+e);if(t){o.path=t;o.verb=e;u=true;if(s.indexOf("sse:")===0){var r=s.substr(4);var n=l(a,function(e){return e.sseSource});if(n){var i=function(){if(!k(n)){if(S(a)){le(a,e,t)}else{n.sseSource.removeEventListener(r,i)}}};n.sseSource.addEventListener(r,i)}else{J(a,"noSSESourceError.mx")}}if(s==="revealed"){B();X(a)}else if(s==="load"){if(!o.loaded){o.loaded=true;le(a,e,t)}}else if(s.trim().indexOf("every ")===0){o.polling=true;q(a,e,t)}else{F(a,e,t,o,s)}}});if(!u&&m(a,"hx-boost")==="true"){R(a,o,s)}var e=g(a,"hx-sse-source");if(e){P(a,e)}var t=g(a,"hx-add-class");if(t){N(a,t,"add")}var r=g(a,"hx-remove-class");if(r){N(a,r,"remove")}}y(a.children,function(e){U(e)})}function j(e,t,r){var n=m(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,details:r}))}}function D(e,t){var r;if(window.CustomEvent&&typeof window.CustomEvent==="function"){r=new CustomEvent(e,{detail:t})}else{r=p().createEvent("CustomEvent");r.initCustomEvent(e,true,true,t)}return r}function J(e,t,r){r["elt"]=e;var n=D(t,r);if(HTMx.logger){HTMx.logger(e,t,r);if(t.indexOf("Error")>0){j(e,t,r)}}var i=e.dispatchEvent(n);var a=e.dispatchEvent(D("all.hx",{elt:e,originalDetails:r,originalEvent:n}));return i&&a}function e(e,t,r){var n,i,a;if(o(e)){n=p().body;i="all.hx";a=e}else if(o(t)){n=p().body;i=e;a=t}else{n=e;i=t;a=r}return n.addEventListener(i,a)}function V(){return Math.random().toString(36).substr(3,9)}function _(){var e=p().getElementsByClassName("hx-history-element");if(e.length>0){return e[0]}else{return p().body}}function z(e){localStorage.setItem("hx-history",JSON.stringify(e))}function G(){var e=localStorage.getItem("hx-history");var t;if(e){t=JSON.parse(e)}else{var r=V();t={current:r,slots:[r]};z(t)}return t}function W(){var e=G();var t=V();var r=e.slots;if(r.length>20){var n=r.shift();localStorage.removeItem("hx-history-"+n)}r.push(t);e.current=t;z(e)}function K(){var e=_();var t=G();history.replaceState({"hx-history-key":t.current},p().title,window.location.href);localStorage.setItem("hx-history-"+t.current,e.innerHTML)}function Q(e){var t=e["hx-history-key"];var r=localStorage.getItem("hx-history-"+t);var n=_();n.innerHTML="";E(n,null,r)}function Y(e){return m(e,"hx-push-url")==="true"||e.tagName==="A"&&x(e).boosted}function Z(e){if(Y(e)){K()}}function $(e,t){if(Y(e)){W();history.pushState({},"",t);K()}}function ee(e){re(e,"add")}function te(e){re(e,"remove")}function re(e,t){var r=m(e,"hx-indicator");if(r){var n=p().querySelectorAll(r)}else{n=[e]}y(n,function(e){e.classList[t].call(e.classList,"hx-show-indicator")})}function ne(e,t){for(var r=0;r<e.length;r++){var n=e[r];if(n.isSameNode(t)){return true}}return false}function ie(t,r,e){if(e==null||ne(t,e)){return}else{t.push(e)}var n=d(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(u(e,"form")){var o=e.elements;y(o,function(e){ie(t,r,e)})}}function ae(e){var t=[];var r={};ie(t,r,e);var n=g(e,"hx-include");if(n){var i=p().querySelectorAll(n);y(i,function(e){ie(t,r,e)})}ie(t,r,a(e,"form"));return r}function oe(e,t,r){if(e!==""){e+="&"}e+=encodeURIComponent(t)+"="+encodeURIComponent(r);return e}function se(e){var t="";for(var r in e){if(e.hasOwnProperty(r)){var n=e[r];if(Array.isArray(n)){y(n,function(e){t=oe(t,r,e)})}else{t=oe(t,r,n)}}}return t}function ue(e,t,r,n){e.setRequestHeader((n?"":"X-HX-")+t,r||"")}function le(i,e,a,t){var r=x(i);if(r.requestInFlight){return}else{r.requestInFlight=true}var o=function(){r.requestInFlight=false};var s=b(i);var n=m(i,"hx-prompt");if(n){var u=u(n);if(!J(i,"prompt.hx",{prompt:u,target:s}))return o()}var l=m(i,"hx-confirm");if(l){if(!confirm(l))return o()}var f=new XMLHttpRequest;var c=ae(i);if(!J(i,"values.hx",{values:c,target:s}))return o();if(e==="get"){var v=Object.keys(c).length===0;f.open("GET",a+(v?"":"?"+se(c)),true)}else{f.open("POST",a,true);ue(f,"Content-Type","application/x-www-form-urlencoded; charset=UTF-8",true);if(e!=="post"){ue(f,"X-HTTP-Method-Override",e.toUpperCase(),true)}}f.overrideMimeType("text/html");ue(f,"Request","true");ue(f,"Trigger-Id",d(i,"id"));ue(f,"Trigger-Name",d(i,"name"));ue(f,"Target-Id",d(s,"id"));ue(f,"Current-URL",p().location.href);if(u){ue(f,"Prompt",u)}if(t){ue(f,"Event-Target",d(t,"id"))}if(p().activeElement){ue(f,"Active-Element",d(p().activeElement,"id"));if(p().activeElement.value){ue(f,"Active-Element-Value",p().activeElement.value)}}f.onload=function(){try{if(!J(i,"beforeOnLoad.hx",{xhr:f,target:s}))return;Z(i,a);var e=this.getResponseHeader("X-HX-Trigger");H(i,e);$(i,a);if(this.status>=200&&this.status<400){if(this.status!==204){var t=this.response;if(!J(i,"beforeSwap.hx",{xhr:f,target:s}))return;s.classList.add("hx-swapping");var r=function(){try{A(s,i,t,function(){s.classList.remove("hx-swapping");K();J(i,"afterSwap.hx",{xhr:f,target:s})})}catch(e){J(i,"swapError.hx",{xhr:f,response:f.response,status:f.status,target:s});throw e}};var n=g(i,"hx-swap-delay");if(n){setTimeout(r,h(n))}else{r()}}}else{J(i,"responseError.hx",{xhr:f,response:f.response,status:f.status,target:s})}}catch(e){J(i,"onLoadError.hx",{xhr:f,response:f.response,status:f.status,target:s});throw e}finally{te(i);o();J(i,"afterOnLoad.hx",{xhr:f,response:f.response,status:f.status,target:s})}};f.onerror=function(){te(i);J(i,"loadError.hx",{xhr:f});o()};if(!J(i,"beforeRequest.hx",{xhr:f,values:c,target:s}))return o();ee(i);f.send(e==="get"?null:se(c))}function fe(e){if(p().readyState!=="loading"){e()}else{p().addEventListener("DOMContentLoaded",e)}}fe(function(){U(p().body);window.onpopstate=function(e){Q(e.state)}});function ce(e){return eval(e)}return{processElement:U,on:e,version:"0.0.1",_:ce}}(); |