-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamon.min.js
1 lines (1 loc) · 5.24 KB
/
jamon.min.js
1
'use strict';(function(a,b){if('function'==typeof define&&define.amd)define([],b);else if('object'==typeof module&&module.exports)module.exports=b();else{const c=b();a.Jamon=c,'undefined'==typeof a.$&&'undefined'==typeof a.$$&&(a.$=c.get,a.$$=c.getAll)}})(this,function(){function a(u){return u.replace(/-([a-z])/g,(v,w)=>w.toUpperCase())}function b(u){return u.replace(/([A-Z])/g,v=>'-'+v.toLowerCase())}function c(u){return u?(u+'').trim().split(' '):[]}function d(u){return u===void 0}function f(u){return'string'==typeof u}function g(u){return Array.isArray(u)||u instanceof NodeList||u instanceof HTMLCollection}function h(u,v,w){const x=c(v);return x.length&&u.forEach(y=>{'toggle'===w?x.forEach(z=>y.classList.toggle(z)):y.classList[w](...x)}),u}function i(u,v,w){return d(w)?u[0]?u[0][v]:void 0:(null===w?u.forEach(x=>delete x[v]):u.forEach(x=>x[v]=w),u)}function j(u,v){const w=u[0];if(w&&'function'==typeof w.getBoundingClientRect)return w.getBoundingClientRect()[v]}function k(u,v,w,x){return u.forEach(y=>{const z=u.indexOf(y)?m(v,!0):v;g(v)?y[w](...z):y[w](z),l(y)}),x?u:v}function l(u,v){'prepend'===v||'append'===v?u.normalize():u.parentNode&&u.parentNode.normalize()}function m(u,v=!0){const w=new t;return u.forEach(x=>w.push(x.cloneNode(v))),w}function n(u,v){let x,w=u[r];return d(w)&&(w=new Map,u[r]=w),w.has(v)?x=w.get(v):(x=function(y){const z=y.target;z.matches(v)&&u.call(z,y)},w.set(v,x)),x}function o(u,v=''){return`${u}|${v}`}function p(u,v,w){const x=w?'querySelector':'querySelectorAll';let A,y=u.id,z=!1;return y||(z=!0,y='jamon-temporary-id',u.id=y),v=`#${y} ${v}`,A=u[x](v),z&&u.removeAttribute('id'),A}let q='hidden';const r=Symbol(),s=Symbol();class t extends Array{static create(u,v){const w=document.createElement(u);return d(v)||Object.entries(v).forEach(([x,y])=>w[x]=y),t.of(w)}static get hiddenClassName(){return q}static set hiddenClassName(u){q=u}static get(u){if(d(u))return new t;if(f(u)){const v=document.querySelector(u);return v?t.of(v):new t}return u===window||u instanceof Node&&[1,3,9,11].includes(u.nodeType)?t.of(u):g(u)?u.length?t.of(u[0]):new t:void 0}static getAll(u){return d(u)?new t:f(u)?t.from(document.querySelectorAll(u)):u instanceof t?u:g(u)?u.length?t.from(u):new t:t.of(u)}*items(){for(const u of this)yield t.of(u)}addClass(u){return h(this,u,'add')}removeClass(u){return h(this,u,'remove')}toggleClass(u){return h(this,u,'toggle')}hasClass(u){return u&&this[0]&&this[0].classList.contains((u+'').trim())}show(){return h(this,q,'remove')}hide(){return h(this,q,'add')}toggle(){return h(this,q,'toggle')}val(u){return i(this,'value',u)}html(u){return i(this,'innerHTML',null===u?'':u)}text(u){return i(this,'textContent',null===u?'':u)}prop(u,v){return i(this,u,v)}attr(u,v){if(d(v)){const w=this[0];return w&&w.hasAttribute(u)?w.getAttribute(u):void 0}return null===v?this.forEach(w=>w.removeAttribute(u)):this.forEach(w=>w.setAttribute(u,v)),this}css(u,v){if(d(v)){const w=this[0];return w?window.getComputedStyle(w).getPropertyValue(b(u)):void 0}return this.forEach(w=>w.style[a(u)]=v+''),this}data(u,v){if(d(v)){const w=this[0];return w?w.dataset[u]:void 0}return null===v?(this.forEach(w=>delete w.dataset[u]),this):(this.forEach(w=>w.dataset[u]=v),this)}width(){return j(this,'width')}height(){return j(this,'height')}offset(){const u=this[0];return{left:u.offsetLeft,top:u.offsetTop}}position(u){const v=this[0].getBoundingClientRect();return d(u)?{left:v.left,top:v.top}:(this.forEach(w=>{const x=w.offsetParent||document.body,y=x.getBoundingClientRect(),z=getComputedStyle(w),A=w.style,B=u.left,C=u.top;let D,E;'static'===z.position?(D=w.offsetLeft,E=w.offsetTop,A.position='relative'):(D=w.offsetLeft-(parseFloat(z.left)||0),E=w.offsetTop-(parseFloat(z.top)||0)),d(B)||(A.left=B-D-y.left+'px'),d(C)||(A.top=C-E-y.top+'px')}),this)}findOne(u){const v=new t;return this.some(w=>{const x=p(w,u,!0);if(x)return v.push(x),!0}),v}findAll(u){const v=new t;return this.forEach(w=>{const x=p(w,u);x.length&&v.push(...x)}),v}parent(){const u=new t;return this.forEach(v=>{const w=v.parentElement;w&&!u.includes(w)&&u.push(w)}),u}children(){const u=new t;return this.forEach(v=>u.push(...Array.from(v.children))),u}closest(u){const v=new t;return this.forEach(w=>{const x='function'==typeof w.closest&&w.closest(u);x&&!v.includes(x)&&v.push(x)}),v}prepend(u){return k(this,u,'prepend',!0)}prependTo(u){return k(u,this,'prepend')}append(u){return k(this,u,'append',!0)}appendTo(u){return k(u,this,'append')}before(u){return k(this,u,'before',!0)}insertBefore(u){return k(u,this,'before')}after(u){return k(this,u,'after',!0)}insertAfter(u){return k(u,this,'after')}replaceWith(u){return k(this,u,'replaceWith',!0)}replace(u){return k(u,this,'replaceWith')}clone(u){return m(this,u)}remove(){return this.forEach(u=>{const v=u.parentNode;u.remove(),v&&v.normalize()}),this}on(u,v,w){return d(w)?(w=v,v=void 0):w=n(w,v),c(u).forEach(x=>{const y=o(x,v);this.forEach(z=>{let A=z[s];d(A)&&(A=new Map,z[s]=A);let B=A.get(y);d(B)&&(B=new Set,A.set(y,B)),B.has(w)||(B.add(w),z.addEventListener(x,w))})}),this}off(u,v,w){return d(w)?(w=v,v=void 0):w=w[r].get(v),c(u).forEach(x=>{const y=o(x,v);this.forEach(z=>{z[s].get(y).delete(w),z.removeEventListener(x,w)})}),this}trigger(u,v={}){return v=Object.assign(v,{bubbles:!0,cancelable:!0}),this.forEach(w=>w.dispatchEvent(new CustomEvent(u,v))),this}}return t});