UNPKG

abquery

Version:

a very lightweight library like JQuery with fewer features and somewhat different

2 lines (1 loc) 9.28 kB
const $=(e,t)=>({__init__(e,t){if(this.Type="abqueryObject",$.is_proto(e))return e;if($.typeof(e,"function"))return $.ready(e);if(this.num=1,this.__query__=document,t&&$.typeof(t,"number")?this.num=t:t&&$.is_html(t)&&(this.__query__=t),this.query=e||document,this.new=$.typeof(e,"string")&&$.is_new(e),this.new||$.is_html(this.query))(this.num>1||$.is_array(this.query))&&(this.many=!0),$.is_html(this.query)?this.$$=this.query:(this.$$=[...Array(this.num).keys()].reduce((e=>[...e,document.createElement(this.query.slice(1,-1))]),[]),1===this.num&&(this.$$=this.$$[0]));else{let e,[t,r]=$.clean(this.query,!0);r?(e=e=>this.__query__.querySelectorAll(e),this.many=!0):e=e=>this.__query__.querySelector(e),$.is_array(t)?(this.$$=t.reduce(((t,s)=>r?[...t,...e(s)]:[...t,e(s)]),[]),this.many=!0):this.$$=e(t)}return this.arr=Array.from(this.many?this.$$:[this.$$]),this.__get__(this.arr),this.__html_proto__(["id"])},__get__(e,t){if($.is_array($.clean(e))||$.is_array(e)){e=Object.assign($.is_array(e)?e:$.clean(e));for(let t in e)$.__get__(this,t,(()=>$(e[t])))}else{$.typeof(e,"string")&&t&&(e={[e]:t});for(let t in e)$.__set__(this,t,e[t])}return this},__set__(e,t){$.typeof(e,"string")&&t&&(e={[e]:t});for(let t in e)$.__set__(this,t,e[t]);return this},__define_prop__(e,t={_get:()=>this.prop(prop),_set:e=>this.prop(prop,e)}){$.typeof(e,"string")&&t&&(e={[e]:t});for(let t in e)$.__define_prop__(this,t,e[t]);return this},__html_proto__(e=[]){return this.__define_prop__([...Object.keys(HTMLElement.prototype),...e].filter((e=>!/^on/.test(e))).reduce(((e,t)=>["click","style","focus","blur"].includes(t)?{...e}:{...e,[t]:{_get:()=>this.prop(t),_set:e=>this.prop(t,e)}}),{}))},show({cls:e="",animate:t="abquery-show",delay:r=600,keep:s=!1}={},i=(()=>{})){return e?this.addClass(e):this.rmClass("abquery-d-none, d-none").rmCss("display"),this.addClass(t),setTimeout((()=>{s||this.rmClass(t)}),r),i(),this},hide({cls:e="",animate:t="abquery-hide",delay:r=600,keep:s=!1}={},i=(()=>{})){return this.addClass(t),setTimeout((()=>{e?this.rmClass(e):this.addClass("abquery-d-none"),s||this.rmClass(t)}),r),i(),this},toggleDisplay({cls:e=""}={}){return this.run((t=>{$(t).hasClass(e||["abquery-d-none","d-none"],{someClass:!0})?this.show({cls:e}):this.hide({cls:e})}))},addClass(e){return this.$run(((e,t)=>{t.trim()&&e.classList.add(t)}),e)},hasClass(e,{someClass:t=!1,someEl:r=!1}={}){return this.$runBool(((e,t)=>e.classList.contains(t)),e,{someArr:t,someEl:r})},rmClass(e){return this.$run(((e,t)=>{t.trim()&&e.classList.remove(t)}),e)},toggleClass(e){return this.$run(((e,t)=>{e.classList.toggle(t)}),e)},css(e,t,r=!1){const s=(e,t="!")=>[e.split(t)[0].trim(),r||!!e.split(t)[1]];return this.$set(((t,r,i)=>{if($.is_array($.clean(e))||void 0===i)return t.style.getPropertyValue(r);{const[e,n]=$.typeof(i,"number")?[i,!1]:s(i);t.style.setProperty(r,e,n?"important":"")}}),e,t)},rmCss(e){return this.$run(((e,t)=>{e.style.removeProperty(t)}),e)},attr(e,t){return this.$set(((t,r,s)=>{if($.is_array($.clean(e))||void 0===s)return t.getAttribute(r);t.setAttribute(r,s)}),e,t)},rmAttr(e){return this.$run(((e,t)=>{e.removeAttribute(t)}),e)},appendParent(e){return this.$run(((e,t)=>{$.e(t).appendChild(e)}),e)},detachParent(){return this.run((e=>{e.parentNode.removeChild(e)}))},append(e){return this.$run(((e,t)=>{e.appendChild($.e(t))}),e)},detach(e){return this.$run(((e,t)=>{e.removeChild($.e(t))}),e)},index(e){const t=this.arr.indexOf($.e(e));return t>=0?t:Error(console.error("cannot find index of arg"))},run(e,{delay:t=0,every:r=0}={}){const s=this.arr;return r?setInterval((()=>s.forEach(e)),r):t?setTimeout((()=>s.forEach(e)),t):s.forEach(e),this},$run(e,t){return t=$.clean(t),$.is_array(t)||(t=[t]),t.forEach((t=>this.run((r=>e(r,t))))),this},$runBool(e,t,{someArr:r=!1,someEl:s=!1}={}){$.is_array(t)||(t=[t]);const i=t=>s?this.arr.some((r=>e(r,t))):this.arr.every((r=>e(r,t)));return r?t.some(i):t.every(i)},$set(e,t,r){let s=[];t=$.clean(t);let i=$.typeof(t,"string");return this.run((n=>{if($.is_dict(t))for(let r in t)e(n,r,t[r]);else $.is_array(t)||void 0===r?(i&&(t=[t]),s=[...s,...t.reduce(((t,r)=>[...t,e(n,r)]),[])]):e(n,t,r)})),($.is_array(t)||i)&&void 0===r?1===this.arr.length&&i?s[0]:s:this},prop(e,t){return this.$set(((t,r,s)=>{if($.is_array($.clean(e))||void 0===s)return t[r];try{t[r]=s}catch(e){console.error(e)}}),e,t)},get class(){return this.prop("className")},get parent(){return $(this.arr[0].parentNode)},get parents(){let e=Array.from(this.arr.reduce(((e,t)=>e.add(t.parentNode)),new Set));return $(e)},get child(){return $(this.arr[0].firstElementChild)},get children(){let e=this.arr.reduce(((e,t)=>[...e,...Array.from(t.children)]),[]);return $(e)},get len(){return this.arr.length},get html(){return this.prop("innerHTML")},get text(){return this.prop("textContent")},get val(){let e=$.form_data(this.$$),t=Object.keys(e);return 1===t.length?e[t[0]]:e},set class(e){this.prop("className",e)},set html(e){this.prop("innerHTML",e)},set text(e){this.prop("textContent",e)},set val(e){this.run((t=>{t[$.form_value(t,!0)]=e}))},on(e,t){return this.$run(((e,r)=>{$.on(r,t,e)}),e)},hover(e){return this.on("mouseover,mouseout",e)},click(e){return this.on("click",e)},debounce(e,t,r){return $.typeof(e,"function")&&(void 0===t||$.typeof(t,"number"))?this.run($.debounce(e,t)):this.on(e,$.debounce(t,r))},throttle(e,t,r){return $.typeof(e,"function")&&(void 0===t||$.typeof(t,"number"))?this.run($.throttle(e,t)):this.on(e,$.throttle(t,r))},$(e,t){if($.is_new(e))return this.$new(e,t);const r=Array.from(this.arr.reduce(((t,r)=>new Set([...t,...$(e,r).arr])),new Set)).filter((e=>null!==e));if(!r)throw Error(`query:${e} is not in parent/s`);return 1===r.length?$(r[0]):$(r)},$new(e,t){let r=$(e,t);return this.append(r.$$),r}}.__init__(e,t));$.is_proto=e=>!!e&&e.Type===$().Type,$.is_array=e=>!!e&&([].__proto__===e.__proto__||NodeList===e.__proto__.constructor),$.is_html=(e,t=!0)=>{if(!e)return!1;const r=e=>e.__proto__.constructor.toString().includes("HTML");return t?($.is_array(e)||(e=[e]),r(e[0])):r(e)},$.is_new=e=>$.typeof(e,"string")&&/^<[a-z]+>$/.test(e),$.typeof=(e,t)=>typeof e===t,$.is_dict=e=>{if(!e)return!1;return{}.__proto__===e.__proto__},$.__get__=(e,t,r)=>{const s=$.typeof(r,"function")?r:()=>r;return Object.defineProperty(e,t,{get:s})},$.__set__=(e,t,r)=>Object.defineProperty(e,t,{set:r}),$.__define_prop__=(e,t,{_get:r,_set:s})=>{const i=$.typeof(r,"function")?r:()=>r;return Object.defineProperty(e,t,{get:i,set:s})},$.obj_text=(e,t="")=>{for(let r in e)t+=`${r}: ${e[r]}; `.trim();return t},$.clean=(e,t=!1)=>{if($.is_array(e))e=e.toString();else if(!$.typeof(e,"string"))return e;let r=!1;return"*"===e[0]&&(e=e.slice(1),r=!0),e=1===(e=e.split(",").reduce(((e,t)=>[...e,t.trim()]),[])).length?e[0]:e,t?[e,r]:e},$.e=e=>$.is_proto(e)?e.$$:e,$.form_data=e=>($.is_array(e)||(e=[e]),Array.from(e).reduce(((e,t)=>{let r=$.form_value(t);return null===r||"submit"===t.type?e:{...e,[t.name||t.id]:r}}),{})),$.form_value=(e,t=!1)=>"INPUT"===e.tagName||"TEXTAREA"===e.tagName?["radio","checkbox"].includes(e.type)?t?"checked":e.checked:"file"===e.type?!0===e.multiple?t?"files":e.files:t?"files[0]":e.files[0]:t?"value":e.value:"SELECT"===e.tagName?t?"select.option.selected":Array.from(e.options).filter((e=>e.selected)).map((e=>e.value)):null,$.debounce=(e,t=2e3)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>{e(...s)}),t)}},$.throttle=(e,t=2e3)=>{let r=0;return(...s)=>{const i=(new Date).getTime();if(!(i-r<t))return r=i,e(...s)}},$.styleElementId="abquery-stylesheet",$.css_prefix=e=>["","-webkit-","-moz-"].reduce(((t,r)=>t+`${r}${e.trim()};`),""),$.init_style_defaults=()=>{if($("abquery-init_style_defaults").$$)return;let e=`${$.css_prefix("animation: abquery-keyframe-show .6s cubic-bezier(0, 0.9, 0.3, 1.2) forwards")}\n opacity: 0;${$.css_prefix("transform: translateY(-4rem) scale(.8)")}`,t=`0% {${$.css_prefix("transform: scale(1)")}opacity: 1;}\n 20%{${$.css_prefix("transform: scale(.9)")}} 100% {${$.css_prefix("transform: none")}opacity: 0;}`;return $.add_keyframes("abquery-keyframe-show",`100%{opacity: 1;${$.css_prefix("transform: none")}}`),$.add_style(".abquery-show",e),$.add_keyframes("abquery-keyframe-hide",t),$.add_style(".abquery-hide",""+$.css_prefix("animation: abquery-keyframe-hide .6s ease-out")),$.add_style(".abquery-d-none","display:none !important").attr("abquery-init_style_defaults",!0)},$.gen_frames=(e,t)=>`\n @keyframes ${e} {${t}}\n @-webkit-keyframes ${e} {${t}}\n `,$.get_styles=(e=$.styleElementId)=>{let t=$("#"+e);return t.$$||(t=$.new("style").attr({type:"text/css",id:""+e})),t},$.add_keyframes=(e,t,r=$.styleElementId)=>{const s=$.get_styles(r);return s.appendParent(document.head).html+=$.gen_frames(e,t),s},$.add_style=(e,t,r)=>{const s=$.get_styles(r);return s.appendParent(document.head).html+=`${e} {${t}}`,s},$.on=(e,t,r=document)=>r.addEventListener(e,t),$.ready=e=>{$.on("DOMContentLoaded",e)},$.new=(e,t)=>$(`<${e}>`,t),$.id=e=>$(e.toString().split(",").reduce(((e,t)=>[...e,"#"+t.trim()]),[]).toString()),$.cls=e=>$("*"+e.toString().split(",").reduce(((e,t)=>[...e,"."+t.trim()]),[]).toString()),$.attrs=e=>$("*"+e.toString().split(",").reduce(((e,t)=>[...e,"["+t.trim()+"]"]),[]).toString()),window.$=$,$.init_style_defaults();