-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jBase.js
7 lines (7 loc) · 4.22 KB
/
jBase.js
1
2
3
4
5
6
7
/*!
* jBase © 2024 by Sven Minio is licensed under Attribution-NonCommercial-ShareAlike 4.0 International.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/
* Link to Work: https://github.com/k37z3r/jBase
* Link to Creator Profile: https://github.com/k37z3r
*/
class JBase extends Array{constructor(e){if(super(),e==document||e instanceof HTMLElement||e==window)this.push(e);else{let a=document.querySelectorAll(e);for(let i=0;i<a.length;i )this.push(a[i])}}ready(z){this[0].addEventListener("readystatechange",(e=>{if("complete"===this[0].readyState)return z(),!0}))}each(z){if(z&&"function"==typeof z){for(let i=0;i<this.length;i )z(this[i],i);return this}}addClass(o){return this.each((function(e){e.classList.add(o)})),this}removeClass(o){return this.each((function(e){e.classList.remove(o)})),this}val(o){return o?(this[0].value=o,this):this[0].value}hasClass(o){return this.each((function(e){e.classList.contains(o)})),this}css(o){return this.each((function(e){Object.assign(e.style,o)})),this}attr(a,v=null){return v?(this[0].setAttribute(a,v),this):this[0].getAttribute(a)}removeAttr(o){return this.each((function(e){e.removeAttribute(o)})),this}html(o){return o?this.each((function(e){e.innerHTML=o})):this.each((function(e){return e.innerHTML})),this}append(o){return this.each((function(e){e.append(o)})),this}toggleSlide(o={}){let d,t,i,a;return d=JBase_hasObject(o,"timer")?"slow"==o.timer?"800ms":"fast"==o.timer?"200ms":"moderate"==o.timer?"500ms":o.timer:"500ms",t=JBase_hasObject(o,"transition")&&"ease"==o.transition?"ease":"linear",JBase_hasObject(o,"transform")&&JBase_hasObject(o.transform,"in")&&JBase_hasObject(o.transform,"out")?(i=o.transform.in,a=o.transform.out):(i="translateY(0%)",a="translateY(100%)"),this.each((function(e){"true"==e.getAttribute("status")?(e.setAttribute("status","false"),e.style.transform=a,e.style.transitionTimingFunction=t,e.style.transition=d):(e.setAttribute("status","true"),e.style.transform=i,e.style.transitionTimingFunction=t,e.style.transition=d)})),this}prepend(o){return this.each((function(e){e.prepend(o)})),this}hide(){return this.each((function(e){e.style.display="none"})),this}show(){return this.each((function(e){e.style.display="block"})),this}on(a,c,z=null,s=null){let r=a.split(",");return null!=z?this.each((function(e){r.forEach((t=>{t=t.trim(),e.addEventListener(t,(function(t){t.target.matches(c ", " c " *")&&z.apply(t.target.closest(c),arguments)}),!1)}))})):(z=c,this.each((function(e){r.forEach((t=>{t=t.trim(),s?e.addEventListener(t,z,s):e.addEventListener(t,z,!1)}))}))),this}disable(o){return o?this.each((function(e){e.disabled=o})):this.each((function(e){return e.disabled})),this}doScroll(o={}){let t,l,b;return t=JBase_hasObject(o,"top")?o.top:0,l=JBase_hasObject(o,"left")?o.left:0,b=JBase_hasObject(o,"behavior")?o.behavior:"smooth",this.each((function(e){e.scroll({top:t,left:l,behavior:b})})),this}toggleClass(f=null,s=null){return f&&this.each((function(e){sc?e.classList.contains(s)?(e.classList.add(f),e.classList.remove(s)):(e.classList.add(s),e.classList.remove(f)):e.classList.contains(f)?e.classList.remove(f):e.classList.add(f)})),this}click(){return this.each((function(e){e.click()})),this}focus(o={}){let p,v;return this.each((function(e){p=!!JBase_hasObject(o,"scroll")&&!1===o.scroll,v=!JBase_hasObject(o,"visible")||o.visible,e.focus({preventScroll:p,focusVisible:v})})),this}}const $=function(e){return new JBase(e)},JBase_hasObject=function(a,s){const o=Object.getOwnPropertyNames(a);for(let i=0;i<o.length;i )if(o[i].toLowerCase()==s.toLowerCase())return!0;return!1};$.ajax=function(o){let p,u=o.url,t="get",success=function(){},fail=function(){},x=new XMLHttpRequest;o.success&&(success=o.success),o.fail&&(fail=o.fail),x.onerror=function(error){return fail(error)},x.onload=function(){if(4==this.readyState&&200==this.status){let r;try{r=JSON.parse(this.responseText)}catch(e){r=this.responseText}return success(r)}return fail(this.status)},o&&(t=o.type,"data"in o&&(p=new URLSearchParams(o.data).toString())),t&&"POST"==t.toUpperCase()?(x.open("POST",u,!0),x.setRequestHeader("Content-type","application/x-www-form-urlencoded"),x.send(p)):t&&"GET"!=t.toUpperCase()||(x.open("GET",`${u}?${p}`,!0),x.setRequestHeader("X-Requested-With","XMLHttpRequest"),x.send())};