-
Notifications
You must be signed in to change notification settings - Fork 0
/
appropriate.min.js
2 lines (2 loc) · 6.62 KB
/
appropriate.min.js
1
2
/*! Appropriate.js v1.1.0 | (c) AlexNottaBen | https://raw.githubusercontent.com/AlexNottaBen/Appropriate.js/main/LICENSE */
"use strict";class AppropriateObject extends Array{onready(e){return this.forEach((t=>{t.addEventListener("DOMContentLoaded",e)})),this}onload(e){return this.forEach((t=>{t.addEventListener("load",e)})),this}onclick(e){return this.forEach((t=>{t.addEventListener("click",e)})),this}onmouseover(e){return this.forEach((t=>{t.addEventListener("mouseover",e)})),this}onmouseout(e){return this.forEach((t=>{t.addEventListener("mouseout",e)})),this}onsubmit(e){return this.forEach((t=>{t.addEventListener("submit",e)})),this}on(e,t){return this.forEach((r=>{r.addEventListener(e,t)})),this}listen(e,t,r){return this.forEach((s=>{s.addEventListener(e,(e=>{e.target.matches(t)&&r(e)}))})),this}blur(){return this.forEach((e=>{e.blur()})),this}focus(){return this.forEach((e=>{e.focus()})),this}get(e){return this.length>0?this[e]:void 0}value(e){return e?(this.forEach((t=>{t.value=e})),this):this[0].value}next(){return this.map((e=>e.nextElementSibling)).filter((e=>null!==e))}prev(){return this.map((e=>e.previousElementSibling)).filter((e=>null!==e))}addClass(e){return this.forEach((t=>{t.classList.add(e)})),this}removeClass(e){return this.forEach((t=>{t.classList.remove(e)})),this}hasClass(e){return Array.from(this).every((t=>t.classList.contains(e)))}toggleClass(e){return this.forEach((t=>{t.classList.toggle(e)})),this}style(e,t){if(t){const r=e.replace(/(-[a-z])/,(e=>e.replace("-","").toUpperCase()));return this.forEach((e=>e.style[r]=t)),this}{const t=this[0];return window.getComputedStyle(t).getPropertyValue(e)}}first(){return new AppropriateObject(this[0])}last(){return new AppropriateObject(this[this.length-1])}text(e){return e?(this.forEach((t=>{t.innerText=e})),this):this.length>0?this[0].innerText:void 0}html(e){return e?(this.forEach((t=>{t.innerHTML=e})),this):this.length>0?this[0].innerHTML:void 0}append(e){return this.forEach((t=>{t.insertAdjacentHTML("beforeend",e)})),this}prepend(e){return this.forEach((t=>{t.insertAdjacentHTML("afterbegin",e)})),this}after(e){return this.forEach((t=>{t.insertAdjacentHTML("afterend",e)})),this}before(e){return this.forEach((t=>{t.insertAdjacentHTML("beforebegin",e)})),this}parent(){const e=this[0].parentElement;return new AppropriateObject(e)}children(){const e=Array.from(this[0].children);return new AppropriateObject(e)}find(e){return new AppropriateObject(this[0].querySelector(e))}findAll(e){return new AppropriateObject(this[0].querySelectorAll(e))}findElement(e){return this[0].querySelector(e)}findAllElements(e){return this[0].querySelectorAll(e)}fadeIn(e=500,t=(()=>{})){return this.forEach((r=>{r.style.opacity=0,r.style.display="",r.offsetHeight,r.style.transition=`opacity ${e}ms`,r.style.opacity=1,setTimeout((()=>{r.style.transition="",t()}),e)})),this}fadeOut(e=500,t=(()=>{})){return this.forEach((r=>{r.style.opacity=1,r.style.transition=`opacity ${e}ms`,r.style.opacity=0,setTimeout((()=>{r.style.transition="",t()}),e)})),this}hide(){return this.forEach((e=>{e.style.visibility="hidden"})),this}show(){return this.forEach((e=>{e.style.visibility="visible"})),this}clear(){return this.forEach((e=>{e.innerHTML=""})),this}remove(){return this.forEach((e=>{e instanceof Element&&e.remove()})),this}none(){return this.forEach((e=>{e.style.display="none"})),this}block(){return this.forEach((e=>{e.style.display="block"})),this}inline(){return this.forEach((e=>{e.style.display="inline"})),this}inlineBlock(){return this.forEach((e=>{e.style.display="inline-block"})),this}slideUp(e=500,t=(()=>{})){return this.forEach((r=>{r.style.overflow="hidden",r.style.height=r.scrollHeight+"px",r.offsetHeight,r.style.transition=`height ${e}ms`,r.style.height=0,setTimeout((()=>{r.style.display="none",r.style.height="",r.style.overflow="",r.style.transition="",t()}),e)})),this}slideDown(e=500,t=(()=>{})){return this.forEach((r=>{r.style.removeProperty("display");let s=window.getComputedStyle(r).display;"none"===s&&(s="block"),r.style.display=s;let i=r.scrollHeight;r.style.overflow="hidden",r.style.height=0,r.offsetHeight,r.style.transition=`height ${e}ms`,r.style.height=i+"px",setTimeout((()=>{r.style.height="",r.style.overflow="",r.style.transition="",t()}),e)})),this}each(e){return this.forEach(e),this}property(e,t){this.forEach((r=>{if(void 0===t)return r[e];r[e]=t}))}attribute(e,t){if(!t)return this.length>0?this[0].getAttribute(e):void 0;this.forEach((r=>{r.setAttribute(e,t)}))}animate(e,t=500,r=(()=>{})){return this.forEach((s=>{const i=performance.now(),n={};for(const t in e)n[t]=parseFloat(getComputedStyle(s)[t])||0;const o=function(h){const a=h-i,c=Math.min(a/t,1);for(const t in e){const r=n[t],i=r+(parseFloat(e[t])-r)*c;s.style[t]=i+("opacity"===t?"":"px")}c<1?requestAnimationFrame(o):"function"==typeof r&&r.call(s)};requestAnimationFrame(o)})),this}}const createAppropriateObject=function(e){return"string"==typeof e||e instanceof String?new AppropriateObject(...document.querySelectorAll(e)):new AppropriateObject(e)},select=createAppropriateObject,$=createAppropriateObject,requests={get:({url:e,echo:t=!1,data:r={},mode:s="cors",cache:i="default",headers:n={},credentials:o="same-origin",redirect:h="follow",referrerPolicy:a="no-referrer",beforeSend:c=(()=>{}),success:l=(()=>{}),except:d=(()=>{}),always:u=(()=>{})})=>{c();const f=Object.entries(r).map((([e,t])=>`${e}=${t}`)).join("&");fetch(`${e}?${f}`,{method:"GET",headers:n,mode:s,cache:i,credentials:o,redirect:h,referrerPolicy:a}).then((e=>{if(e.ok)return e.json();throw new Error(e.status)})).then((e=>(l(e),e))).catch((e=>{d(e)})).finally((()=>{u()}))},json:({url:e,method:t="POST",data:r={},echo:s=!1,mode:i="cors",cache:n="default",headers:o={},credentials:h="same-origin",redirect:a="follow",referrerPolicy:c="no-referrer",beforeSend:l=(()=>{}),success:d=(()=>{}),except:u=(()=>{}),always:f=(()=>{})})=>{l(),fetch(e,{method:t,headers:{"Content-Type":"application/json;charset=utf-8",...o},mode:i,cache:n,credentials:h,redirect:a,referrerPolicy:c,body:JSON.stringify(r)}).then((e=>{if(e.ok)return e.json();throw new Error(e.status)})).then((e=>(d(e),e))).catch((e=>{u(e)})).finally((()=>{f()}))},form:({url:e,method:t="POST",data:r={},echo:s=!1,mode:i="cors",cache:n="default",headers:o={},credentials:h="same-origin",redirect:a="follow",referrerPolicy:c="no-referrer",beforeSend:l=(()=>{}),success:d=(()=>{}),except:u=(()=>{}),always:f=(()=>{})})=>{l(),fetch(e,{method:t,headers:{"Content-Type":"application/x-www-form-urlencoded",...o},mode:i,cache:n,credentials:h,redirect:a,referrerPolicy:c,body:new URLSearchParams(r)}).then((e=>{if(e.ok)return e.json();throw new Error(e.status)})).then((e=>(d(e),e))).catch((e=>{u(e)})).finally((()=>{f()}))}};requests.args=requests.get;