-
Notifications
You must be signed in to change notification settings - Fork 0
/
GStime.min.js
2 lines (2 loc) · 5.61 KB
/
GStime.min.js
1
2
/* GSTime v.1.3 */
!function(t){"use strict";let e="undefined"!=typeof document;function n(t){if(!(this instanceof n))return new n(t);this.elements=[],e&&("string"==typeof t?t.startsWith("<")&&t.endsWith(">")?this.elements=[this.createHTML(t)]:this.elements=Array.from(document.querySelectorAll(t)):t instanceof Element?this.elements=[t]:(t instanceof NodeList||Array.isArray(t))&&(this.elements=Array.from(t)))}n.prototype.createHTML=function(t){let e=document.createElement("div");return e.innerHTML=t.trim(),e.firstChild},n.prototype.each=function(t){return this.elements.forEach((e,n)=>t.call(e,n,e)),this},n.prototype.on=function(t,e){return this.each(function(){this.addEventListener(t,e,!1)})},n.prototype.off=function(t,e){return this.each(function(){this.removeEventListener(t,e,!1)})},n.prototype.val=function(t){return void 0===t?this.elements[0]?this.elements[0].value:void 0:this.each(function(){this.value=t})},n.prototype.append=function(t){return this.each(function(){this.insertAdjacentHTML("beforeend",t)})},n.prototype.prepend=function(t){return this.each(function(){this.insertAdjacentHTML("afterbegin",t)})},n.prototype.html=function(t){return void 0===t?this.elements[0]?this.elements[0].innerHTML:void 0:this.each(function(){this.innerHTML=t})},n.prototype.css=function(t,e){return"string"==typeof t&&void 0===e?this.elements[0]?getComputedStyle(this.elements[0])[t]:void 0:this.each(function(){"object"==typeof t?Object.assign(this.style,t):this.style[t]=e})},n.prototype.hide=function(){return this.css("display","none")},n.prototype.show=function(){return this.css("display","")},n.prototype.toggle=function(){return this.each(function(){this.style.display="none"===this.style.display?"":"none"})},n.prototype.clone=function(){return new n(this.elements.map(t=>t.cloneNode(!0)))},n.prototype.animate=function(t,e,n){let i=performance.now(),o=this.elements.map(e=>Object.fromEntries(Object.keys(t).map(t=>[t,parseFloat(getComputedStyle(e)[t])||0,]))),r=s=>{let a=Math.min((s-i)/e,1);this.elements.forEach((e,n)=>{for(let i in t){let r=o[n][i],s=parseFloat(t[i]),h=r+(s-r)*a;e.style[i]=`${h}px`}}),a<1?requestAnimationFrame(r):"function"==typeof n&&n()};return requestAnimationFrame(r),this},n.prototype.fadeOut=function(t,e){return this.animate({opacity:0},t,()=>{this.hide(),"function"==typeof e&&e()})},n.prototype.fadeIn=function(t,e){return this.show().css("opacity",0).animate({opacity:1},t,e)},n.prototype.hasClass=function(t){return!!this.elements[0]&&this.elements[0].classList.contains(t)},n.prototype.addClass=function(t){return this.each(function(){this.classList.add(t)})},n.prototype.removeClass=function(t){return this.each(function(){this.classList.remove(t)})},n.prototype.toggleClass=function(t){return this.each(function(){this.classList.toggle(t)})},n.prototype.attr=function(t,e){return void 0===e?this.elements[0]?this.elements[0].getAttribute(t):void 0:this.each(function(){this.setAttribute(t,e)})},n.prototype.removeAttr=function(t){return this.each(function(){this.removeAttribute(t)})},n.prototype.width=function(t){return void 0===t?this.elements[0]?this.elements[0].offsetWidth:void 0:this.css("width","number"==typeof t?`${t}px`:t)},n.prototype.height=function(t){return void 0===t?this.elements[0]?this.elements[0].offsetHeight:void 0:this.css("height","number"==typeof t?`${t}px`:t)},n.prototype.offset=function(){if(!this.elements[0])return;let t=this.elements[0].getBoundingClientRect();return{top:t.top+window.pageYOffset,left:t.left+window.pageXOffset}},n.prototype.position=function(){return this.elements[0]?{top:this.elements[0].offsetTop,left:this.elements[0].offsetLeft}:void 0},n.ajax=function(t){return new Promise((e,n)=>{if(!t||void 0===t.url){n(Error("A URL is required for the ajax request"));return}let i={method:"GET",headers:{},...t};i.data&&("GET"===i.method.toUpperCase()?i.url+=`?${new URLSearchParams(i.data)}`:"object"==typeof i.data?i.headers["Content-Type"]?i.headers["Content-Type"].includes("application/x-www-form-urlencoded")&&(i.body=new URLSearchParams(i.data).toString()):(i.headers["Content-Type"]="application/json",i.body=JSON.stringify(i.data)):i.body=i.data,delete i.data),fetch(i.url,i).then(t=>{if(!t.ok)throw Error(`HTTP error! status: ${t.status}`);let e=t.headers.get("content-type");return e&&e.includes("application/json")?t.json():e&&e.includes("text")?t.text():t.blob()}).then(t=>e(t)).catch(t=>n(t))})},n.prototype.after=function(t){return this.each(function(){this.insertAdjacentHTML("afterend",t)})},n.prototype.before=function(t){return this.each(function(){this.insertAdjacentHTML("beforebegin",t)})},n.prototype.remove=function(){return this.each(function(){this.parentNode.removeChild(this)})},n.prototype.parent=function(){let t=this.elements.map(t=>t.parentNode).filter(Boolean);return new n(t)},n.prototype.children=function(){let t=this.elements.flatMap(t=>Array.from(t.children));return new n(t)},n.prototype.siblings=function(){let t=this.elements.flatMap(t=>Array.from(t.parentNode.children).filter(e=>e!==t));return new n(t)},n.prototype.find=function(t){let e=this.elements.flatMap(e=>Array.from(e.querySelectorAll(t)));return new n(e)},n.prototype.closest=function(t){let e=this.elements.map(e=>e.closest(t)).filter(Boolean);return new n(e)},n.each=function(t,e){if(Array.isArray(t))t.forEach((t,n)=>e.call(t,n,t));else for(let n in t)t.hasOwnProperty(n)&&e.call(t[n],n,t[n]);return t},n.map=function(t,e){if(Array.isArray(t))return t.map(e);{let n={};for(let i in t)t.hasOwnProperty(i)&&(n[i]=e.call(t[i],i,t[i]));return n}},n.ready=function(t){e&&("loading"!==document.readyState?t():document.addEventListener("DOMContentLoaded",t))},t.GStime=n,t.$=function(t){return new n(t)}}("undefined"!=typeof window?window:this);