almostnojs
Version:
A minimalist, dependency-free JavaScript framework for DOM manipulation, event handling, animations, state management, and HTTP requests.
4 lines (3 loc) • 6.16 kB
JavaScript
/* AlmostNo.js v1.1.3 Core */
(()=>{var O=Object.defineProperty;var g=(t,e)=>()=>(t&&(e=t(t=0)),e);var L=(t,e)=>{for(var n in e)O(t,n,{get:e[n],enumerable:!0})};var E,i,a=g(()=>{E=typeof window<"u"?window:global;if(!E.__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}}E.__AnJS__=t}i=E.__AnJS__});var R={};L(R,{default:()=>V,http:()=>_,request:()=>f});function G(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 B(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 f(t,e="GET",n=null,r={}){let{timeout:o=5e3,signal:s}=r,d=new URL(t,window.location.origin),l=n&&!["GET","DELETE"].includes(e),T=G(r.headers,l),m=l?k(n,T):void 0,F={method:e,headers:T,...m&&{body:m},...s&&{signal:s}},N=fetch(d.toString(),F).then(h=>{if(!h.ok)throw new Error(`HTTP ${h.status} at ${t}`);return(h.headers?.get("Content-Type")||"").includes("application/json")?h.json():h.text()});return B(N,o,t)}var _,V,w=g(()=>{a();i.prototype.request=function(t,e="GET",n=null,r={}){return f(t,e,n,r)};_={head:(t,e={})=>f(t,"HEAD",null,e),get:(t,e={})=>f(t,"GET",null,e),options:(t,e={})=>f(t,"OPTIONS",null,e),post:(t,e,n={})=>f(t,"POST",e,n),put:(t,e,n={})=>f(t,"PUT",e,n),patch:(t,e,n={})=>f(t,"PATCH",e,n),delete:(t,e={})=>f(t,"DELETE",null,e),abortController:()=>new AbortController},V=_});a();a();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}});a();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():""}});a();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=d=>{let l=e?d.target.closest(e):r;l&&r.contains(l)&&n.call(l,d)};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 u={},A={emit(t,e){u[t]?.forEach(n=>n(e))},listen(t,e){u[t]||(u[t]=[]),u[t].push(e)},forget(t,e){u[t]&&(u[t]=u[t].filter(n=>n!==e),u[t].length===0&&delete u[t])}};a();["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 U=t=>t.trim(),j=t=>{try{return JSON.parse(t)}catch{return null}},C=(t,e,n)=>(t-e)*(t-n)<=0,P=t=>typeof t=="function",x=t=>t!==null&&typeof t=="object",H=t=>typeof t=="string",M=t=>typeof t=="number"&&!isNaN(t),J=(t,e)=>t!==e&&t.contains(e),$=(t,e)=>{let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>t(...r),e)}},I=(t,e)=>{let n=0;return(...r)=>{let o=Date.now();o-n>=e&&(n=o,t(...r))}},D=(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:U,json:j,range:C,isFunction:P,isObject:x,isString:H,isNumber:M,contains:J,debounce:$,throttle:I,element:D};a();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 c(t){return new i(t)}["on","off","trigger"].forEach(t=>{c[t]=(...e)=>i.prototype[t].apply(c(),e)});FEATURE_HTTP&&Promise.resolve().then(()=>(w(),R)).then(t=>{Object.assign(c,t.http)});Object.assign(c,A,y,S);c.define=(t,e)=>customElements.define(t,e);typeof window<"u"&&(window.$=c);var lt=c;})();
window.$ = $;