almostnojs
Version:
A minimalist, dependency-free JavaScript framework featuring tagged template rendering, DOM morphing, custom elements, state management, event handling, animations, and HTTP requests.
4 lines (3 loc) • 7.68 kB
JavaScript
/* AlmostNo.js v1.3.0 Extended */
(()=>{var U=Object.defineProperty;var d=(t,e)=>()=>(t&&(e=t(t=0)),e);var x=(t,e)=>{for(var n in e)U(t,n,{get:e[n],enumerable:!0})};var m,i,u=d(()=>{m=typeof window<"u"?window:global;if(!m.__AnJS__){class t extends Array{constructor(n){super(),n&&(n instanceof HTMLElement||n.nodeType===1?this.push(n):n instanceof NodeList||Array.isArray(n)?this.push(...n):typeof n=="string"&&this.push(...document.querySelectorAll(n)))}each(n){return this.forEach(n),this}get(n){return n===void 0?this:this.at(n)}clone(n=!0){return this[0]?this[0].cloneNode(n):null}}m.__AnJS__=t}i=m.__AnJS__});var V={};var w=d(()=>{u();Object.assign(i.prototype,{filter(t){return typeof t=="function"?new i([...this].filter(t)):new i([...this].filter(e=>e.matches(t)))},find(t){return new i(this.flatMap(e=>[...e.querySelectorAll(t)]))},first(){return new i(this.length?[this[0]]:[])},last(){return new i(this.length?[this[this.length-1]]:[])},even(){return new i(this.filter((t,e)=>!(e%2)))},odd(){return new i(this.filter((t,e)=>e%2))}})});var z={};var _=d(()=>{u();Object.assign(i.prototype,{next(){return new i(this[0]?.nextElementSibling?[this[0].nextElementSibling]:[])},prev(){return new i(this[0]?.previousElementSibling?[this[0].previousElementSibling]:[])},parent(){return new i(this[0]?.parentElement?[this[0].parentElement]:[])},children(){return new i(this[0]?[...this[0].children]:[])},siblings(){let t=this[0]?.parentElement;return new i(t?[...t.children].filter(e=>e!==this[0]):[])},closest(t){return new i(this[0]?.closest(t)?[this[0].closest(t)]:[])}})});var O={};x(O,{default:()=>X,http:()=>R,request:()=>a});function W(t={},e){let n={Accept:"application/json",...t};return e&&!n["Content-Type"]&&(n["Content-Type"]="application/json"),n}function K(t,e){return t instanceof FormData?(delete e["Content-Type"],t):e["Content-Type"]==="application/x-www-form-urlencoded"?new URLSearchParams(t).toString():JSON.stringify(t)}function Q(t,e,n){return e===0||e==null?t:Promise.race([t,new Promise((r,o)=>setTimeout(()=>o(new Error(`Request timed out: ${n}`)),e))])}function a(t,e="GET",n=null,r={}){let{timeout:o=5e3,signal:s}=r,E=new URL(t,window.location.origin),h=n&&!["GET","DELETE"].includes(e),g=W(r.headers,h),A=h?K(n,g):void 0,L={method:e,headers:g,...A&&{body:A},...s&&{signal:s}},j=fetch(E.toString(),L).then(c=>{if(!c.ok)throw new Error(`HTTP ${c.status} at ${t}`);return(c.headers?.get("Content-Type")||"").includes("application/json")?c.json():c.text()});return Q(j,o,t)}var R,X,F=d(()=>{u();i.prototype.request=function(t,e="GET",n=null,r={}){return a(t,e,n,r)};R={head:(t,e={})=>a(t,"HEAD",null,e),get:(t,e={})=>a(t,"GET",null,e),options:(t,e={})=>a(t,"OPTIONS",null,e),post:(t,e,n={})=>a(t,"POST",e,n),put:(t,e,n={})=>a(t,"PUT",e,n),patch:(t,e,n={})=>a(t,"PATCH",e,n),delete:(t,e={})=>a(t,"DELETE",null,e),abortController:()=>new AbortController},X=R});var Y={};var N=d(()=>{u();Object.assign(i.prototype,{animate(t,e=400,n="ease"){return this.each(r=>{r.style.transition=`all ${e}ms ${n}`,Object.assign(r.style,t),e>0&&setTimeout(()=>r.style.transition="",e)})},fade(t=+(this[0]?.style.opacity==="0"),e=400){return this.animate({opacity:t},e)},fadeIn(t){return this.fade(1,t)},fadeOut(t){return this.fade(0,t)}})});u();u();Object.assign(i.prototype,{content(t,e=!1){return t===void 0?this[0]?e?this[0].innerHTML:this[0].textContent:"":this.each(n=>e?n.innerHTML=t:n.textContent=t)},text(t){return this.content(t,!1)},html(t){return this.content(t,!0)},css(t,e){return e===void 0?this[0]?.style.getPropertyValue(t)||"":this.each(n=>n.style[t]=e)},class(t,e){return this.each(n=>n.classList[e===void 0?"toggle":e?"add":"remove"](t))},display(t){return this.each(e=>e.style.display=t?"":"none")},hide(){return this.display(!1)},show(){return this.display(!0)},remove(){return this.each(t=>t.remove())},empty(){return this.each(t=>t.innerHTML="")},insert(t,e="before"){let n={before:"beforeBegin",prepend:"afterBegin",append:"beforeEnd",after:"afterEnd"};return n[e]?this.each(r=>{if(typeof t=="string")return r.insertAdjacentHTML(n[e],t);(Array.isArray(t)?t:[t]).forEach(o=>r.insertAdjacentElement(n[e],o.cloneNode(!0)))}):this},prop(t,e){return arguments.length===1?this[0]?.[t]:this.each(n=>n[t]=e)},val(t){return arguments.length===0?this[0]?.value:this.each(e=>e.value=t)},has(t){return this[0]?.classList.contains(t)??!1},focus(){return this[0]?.focus(),this},blur(){return this[0]?.blur(),this}});u();Object.assign(i.prototype,{attr(t,e){return e===void 0?this[0]?.getAttribute(t):e===null?this.each(n=>n.removeAttribute(t)):this.each(n=>n.setAttribute(t,e))},id(t){return t===void 0?this.attr("id"):this.attr("id",t)},removeAttr(t){return this.attr(t,null)},serialize(){return this[0]instanceof HTMLFormElement?new URLSearchParams(new FormData(this[0])).toString():""}});u();var p=new WeakMap;Object.assign(i.prototype,{on(t,e,n){return typeof e=="function"?this.delegate(t,null,e):this.delegate(t,e,n)},off(t,e,n){return typeof e=="function"?this.undelegate(t,null,e):this.undelegate(t,e,n)},delegate(t,e,n){return this.each(r=>{p.has(r)||p.set(r,{});let o=p.get(r);o[t]||(o[t]=[]);let s=E=>{let h=e?E.target.closest(e):r;h&&r.contains(h)&&n.call(h,E)};o[t].push({selector:e,handler:n,delegateHandler:s}),r.addEventListener(t,s)})},undelegate(t,e,n){return this.each(r=>{if(!p.has(r))return;let o=p.get(r);o[t]&&(n?(o[t]=o[t].filter(s=>s.selector===e&&s.handler===n?(r.removeEventListener(t,s.delegateHandler),!1):!0),o[t].length===0&&delete o[t]):(o[t].forEach(s=>r.removeEventListener(t,s.delegateHandler)),delete o[t]),Object.keys(o).length===0&&p.delete(r))})},trigger(t){return this.each(e=>e.dispatchEvent(new Event(t,{bubbles:!0})))}});var f={},T={emit(t,e){f[t]?.forEach(n=>n(e))},listen(t,e){return f[t]||(f[t]=[]),f[t].push(e),()=>T.forget(t,e)},forget(t,e){f[t]&&(f[t]=f[t].filter(n=>n!==e),f[t].length===0&&delete f[t])}};u();["append","prepend","before","after"].forEach(t=>i.prototype[t]=function(e){return this.insert(e,t)});["click","change","submit","keydown","keyup","mouseover","mouseout"].forEach(t=>{i.prototype[t]=function(e){return e?this.on(t,e):this.trigger(t)}});var C=t=>t.trim(),J=t=>{try{return JSON.parse(t)}catch{return null}},M=(t,e,n)=>(t-e)*(t-n)<=0,P=t=>typeof t=="function",H=t=>t!==null&&typeof t=="object",I=t=>typeof t=="string",$=t=>typeof t=="number"&&!isNaN(t),D=(t,e)=>t!==e&&t.contains(e),G=(t,e)=>{let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>t(...r),e)}},B=(t,e)=>{let n=0;return(...r)=>{let o=Date.now();o-n>=e&&(n=o,t(...r))}},k=(t,e={},n=[])=>{let r=document.createElement(t);return Object.entries(e).forEach(([o,s])=>r.setAttribute(o,s)),n.forEach(o=>r.append(o instanceof Node?o:document.createTextNode(o))),r},y={trim:C,json:J,range:M,isFunction:P,isObject:H,isString:I,isNumber:$,contains:D,debounce:G,throttle:B,element:k};u();var b=function(t,e,n=!1){if(typeof t=="object")return typeof e=="boolean"&&(n=e),Object.keys(t).forEach(r=>b(r,t[r],n));!n&&i.prototype.hasOwnProperty(t)||(i.prototype[t]=e)},S={extend:b};typeof FEATURE_HTTP>"u"&&(globalThis.FEATURE_HTTP=!0);typeof FEATURE_FILTERING>"u"&&(globalThis.FEATURE_FILTERING=!0);typeof FEATURE_TRAVERSAL>"u"&&(globalThis.FEATURE_TRAVERSAL=!0);function l(t){return new i(t)}["on","off","trigger"].forEach(t=>{l[t]=(...e)=>i.prototype[t].apply(l(),e)});Promise.resolve().then(()=>(w(),V)).then(t=>Object.assign(l,t)),Promise.resolve().then(()=>(_(),z)).then(t=>Object.assign(l,t));FEATURE_HTTP&&Promise.resolve().then(()=>(F(),O)).then(t=>{Object.assign(l,t.http)});Promise.resolve().then(()=>N());Object.assign(l,T,y,S);l.define=(t,e)=>customElements.define(t,e);typeof window<"u"&&(window.$=l);var yt=l;})();
window.$ = $;