UNPKG

@twobirds/microcomponents

Version:

Micro Components Organization Class

1 lines 18.9 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.selectors=t():e.selectors=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{$d:()=>C,$t:()=>H});var r=new Function("try {return this===window;}catch(e){ return false;}");let n=new Set;r()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>n.add(e)));class s{#e;#t=!1;#r=!1;constructor(e,t={},r="l"){this.#e=e,this.data=t,this.bubble=r}get type(){return this.#e}get stopped(){return this.#t}get immediateStopped(){return this.#r}stopPropagation(){this.#t=!0}stopImmediatePropagation(){this.stopPropagation(),this.#r=!0}}var a=new Function("try {return this===window;}catch(e){ return false;}");let o=new Set;function i(e,t){let r=[];return e.forEach((e=>{if(e&&e instanceof HTMLElement){const n=e?._mc;!t&&n?r=r.concat(Object.values(e._mc)):t&&n&&n[t]&&r.push(n[t])}})),r}function c(e,t,r=!0){let n=[];for(;e;){if(e._mc&&(t?t&&e._mc[t]&&n.push(e._mc[t]):n=n.concat(Object.values(e._mc)),r))return n;e=e.parentNode}return n}function l(e){return e&&e instanceof HTMLElement}function u(e,t,r,n=!1,s=!1){let a={once:!1};n&&(a.capture=n),s&&(a.once=s),e.addEventListener(t,r,a)}function h(e,t,r){e.removeEventListener(t,r)}function f(e,t,r={}){let n=e;if(!n._mc){let t=b?.debug;b.debug=!1,n._mc=new b(e),b.debug=t,delete n._mc._mc}const s=n._mc;return t.length&&!s[t]&&(s[t]=r,e instanceof HTMLElement&&!e.getAttribute("_mc")&&e.setAttribute("_mc","")),r}function d(e,t){if(!t)return;const r=e?._mc||{};r&&r[t]&&delete r[t],!Object.keys(r).length&&e instanceof HTMLElement&&e.hasAttribute("_mc")&&e.removeAttribute("_mc")}function p(e,t,r={},n="l"){let a=e?._mc;if(!a)return;let o=t instanceof s?new s(t.type,t.data,"l"):new s(t,r,"l"),i="string"!=typeof t?t:new s(t,r,n);[...Object.values(a)].forEach((e=>{e?.trigger&&"function"==typeof e?.trigger&&e?.trigger(o)})),/[ud]/.test(i.bubble)&&m(e,i)}function m(e,t){/[ud]/.test(t.bubble)&&setTimeout((()=>{if(-1===t.bubble.indexOf("l")&&(t.bubble+="l"),t.bubble.indexOf("u")>-1){let r=c(e,"",!0)?.[0].target;r&&b.trigger(r,t)}t.bubble.indexOf("d")>-1&&new Set(e._mc.children().map((e=>e?.target))).forEach((e=>{e&&b.trigger(e,t)}))}),0)}a()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>o.add(e)));class y{#n;constructor(e,t=!1){var r;this.#n=new WeakRef(e||{}),(r=this).constructor!==b&&setTimeout((()=>{r.trigger("Init")}),0),function(e){if(!a()||e.constructor===b)return;const t=e.target;if(t instanceof HTMLElement){let r=e;Object.getOwnPropertyNames(r.constructor.prototype).filter((e=>/^on[A-Z]|one[A-Z]/.test(e)&&"function"==typeof r.constructor.prototype[e])).forEach((n=>{let s=/^one/.test(n),a=n.replace(/^on[e]{0,1}/,""),i=a.toLowerCase();s&&Object.defineProperty(e,"on"+a,{configurable:!0,enumerable:!1,writable:!0,value:e=>{r.constructor.prototype[n].bind(r)(e)}}),o.has("on"+i)&&u(t,i,(e=>{r.trigger(a,e)}),!1,s)}))}}(this)}get target(){return this.#n.deref()}}class g extends y{_mc={};constructor(e){super(e)}static trigger=p;trigger(e,t={},r="l"){let n=this;if(n.constructor===g||n.constructor===b){let a=e instanceof s?new s(e.type,e.data,"l"):new s(e,t,"l"),o="string"!=typeof e?e:new s(e,t,r);return[...Object.getOwnPropertyNames(n)].forEach((e=>{n[e]?.trigger&&"function"==typeof n[e]?.trigger&&n[e]?.trigger(a)})),void(/[ud]/.test(o.bubble)&&m(n.target,o))}if(n instanceof g||n instanceof b){const n=this,a="string"!=typeof e?e:new s(e,t,r),o="on"+a.type[0].toUpperCase()+a.type.slice(1);if(a.bubble.indexOf("l")>-1&&(a.immediateStopped||"function"!=typeof n[o]||(n[o](a),n.hasOwnProperty(o)&&delete n[o])),a.stopped||"l"===a.bubble)return n;/[ud]/.test(a.bubble)&&m(n.target,a)}}}class b extends y{_mc={};constructor(e,t=!1){super(e)}static add=f;static remove=d;static trigger=p;trigger(e,t={},r){g.prototype.trigger.call(this,e,t,r)}parent(e=""){let t=this.target;return l(t)?c(t.parentNode,e,!0):[]}ancestors(e=""){let t=this?.target;return l(t)?c(this.target.parentNode,e,!1):[]}children(e=""){let t=this?.target;if(!l(t))return[];const r=Math.random().toString().replace(".",""),n='[_mc]:not([temp_id="'+r+'"] [_mc] [_mc])';t.setAttribute("temp_id",r);const s=i([...t.querySelectorAll(n)],e);return t.removeAttribute("temp_id"),s}descendants(e=""){let t=this?.target;return l(t)?i([...t.querySelectorAll("*[_mc]")],e):[]}}const E=new Map,w=new Map;function v(e){const t="./"+e.split("-").join("/")+".js",r=document.createElement("script");return r.setAttribute("src",t),r.setAttribute("blocking","render"),r.async=!0,r.setAttribute("type","module"),r.setAttribute("name",e),r.setAttribute("loading",""),u(r,"load",(e=>{r.removeAttribute("loading"),r.setAttribute("loaded","")})),u(r,"error",(e=>{console.error("could not load Custom Element code from",t)})),r}function S(){[...document.querySelectorAll(":not(:defined)")].filter((e=>!!e&&!customElements.get(e.tagName.toLowerCase())&&!E.has(e.tagName.toLowerCase()))).forEach((e=>{const t=e.tagName.toLowerCase(),r=(t.split("-").join("/"),v(t));u(r,"load",(()=>{E.set(t,customElements.get(e.tagName.toLowerCase()))})),E.set(t,"loading"),document.head.append(r)})),[...document.querySelectorAll('[_mc]:not([_mc=""]')].filter((e=>!!e)).forEach((e=>{const t=e.getAttribute("_mc")?.split(" ").filter((e=>!!e))||[],r=t.filter((e=>!w.has(e)));r?.forEach((e=>{const t=v(e);u(t,"load",(()=>{return t=document.head.querySelector(`script[name="${e}"]`),void setTimeout((function(){const e=t.getAttribute("src"),r=t.getAttribute("name"),n=document.createElement("script");n.async=!0,n.setAttribute("type","module");const s=`\n\n\t\t\timport _ from "${e}";\n\t\t\timport { DC } from "./microcomponents.js";\n\n\t\t\tconst elements = Array.from(document.querySelectorAll('[_mc*="${r}"]')).filter( element => element instanceof HTMLElement );\n\n\t\t\telements.forEach((element) => {\n\t\t\t\t// add class to MCs repository\n\t\t\t\tautoload.state.MCs.set( '${r}', _ );\n\t\t\t\t// add instance to HTMLElement \n\t\t\t\tDC.add(element, _.name, new _(element));\n\t\t\t\t// remove instance name from elements "_mc" attribute\n\t\t\t\telement.setAttribute('_mc', element.getAttribute('_mc').replace('${r}', '').replace( /ww/g, ' ').trim()); \n\t\t\t});\n\n\t\t\tsetTimeout( () => {\n\t\t\t\tdocument.body.querySelector('script[name="${r}]"')?.remove();\n\t\t\t},0);\n\n\t\t`;n.innerHTML=s,document.body.append(n)}),0);var t})),w.set(e,"loading"),document.head.append(t)}));const n=t.filter((e=>w.has(e)&&!w.get(e).length));n?.forEach((t=>{setTimeout((()=>{b.add(e,t,new(_.state.MCs.get(t))(e)),e.getAttribute("_mc").replace("${elementName}","").replace(/\w\w/g," ").trim(),e.setAttribute("_mc")}),5)}))}))}let A=!1,O=null;function _(e=!0){return e?(A=e,O=new MutationObserver(S),setTimeout((()=>{O.observe(document.body,{childList:!0,subtree:!0}),S()}),5),window.autoload=window.autoload||_,A):(O?.disconnect(),O=null,A)}_.state={CEs:E,MCs:w};const x=e=>{let t=new Set;return e.forEach((e=>{(e=e.trim()).split(" ").forEach((e=>{""!==e&&t.add(e)}))})),Array.from(t)},T=e=>{let t=C("input,select,textarea",e).array,r={};return t.forEach((t=>{const n=t.getAttribute("name")||"undefined";if(n){let s;switch((e=>e instanceof HTMLInputElement?"input":e instanceof HTMLSelectElement?"select":e instanceof HTMLTextAreaElement?"textarea":"")(t)){case"input":s=new L(t,e),Object.defineProperty(r,n,{enumerable:!0,configurable:!0,get:()=>s.val,set:e=>{s.val=e}});break;case"select":s=new M(t),Object.defineProperty(r,n,{enumerable:!0,configurable:!0,get:()=>s.val,set:e=>{s.val=e}});break;case"textarea":s=new N(t),Object.defineProperty(r,n,{enumerable:!0,configurable:!0,get:()=>s.val,set:e=>{s.val=e}});break;default:console.warn("handler missing:",n,"for",t)}}})),r};class j{constructor(e){}}class L extends j{#s;#e;#a;constructor(e,t){super(e),this.#s=e,this.#e=e.type,this.#a=t}get val(){let e,t=this;switch(t.#e){case"radio":const r=C(`input[type="radio"][name="${this.#s.getAttribute("name")}"]`,this.#a).filter((e=>e.checked));return r.length?(e=r.array[0],e.value):"";case"checkbox":return t.#s.checked;default:return t.#s.value}return""}set val(e){let t=this;switch(t.#e){case"radio":const r=`input[type="radio"][name="${this.#s.getAttribute("name")}"]`,n=C(r,this.#a);if(!n.length)return;if(""===e)return void n.forEach((e=>{e.checked&&(e.checked=!1)}));const s=n.filter((t=>t.getAttribute("value")===e));if(!s.length)return;return void(s.array[0].checked=!0);case"checkbox":t.#s.checked=e;break;default:t.#s.value=e}}}class M extends j{#s;#o=!1;constructor(e){super(e),this.#s=e,this.#o="select-multiple"===e.type}get val(){const e=[];return C(this.#s.querySelectorAll("option")).filter((e=>e.selected)).forEach((t=>{t.selected&&e.push(t.value)})),this.#o?e:e.length?e[0]:""}set val(e){e="string"==typeof e?[e]:e,C(this.#s.querySelectorAll("option")).forEach((t=>{e.indexOf(t.value)>-1?(t.setAttribute("selected",!0),t.selected=!0):(t.removeAttribute("selected"),t.selected=!1)}))}}class N extends j{#s;constructor(e){super(e),this.#s=e}get val(){return this.#s.value}set val(e){this.#s.value=e}}class ${#i=new Set;get set(){return this.#i}get array(){return Array.from(this.#i)}set set(e){this.#i=e}get length(){return this.#i.size}constructor(e,t=document.body){if(new Set,!e)return this}$t(e){let t=new k;return this.#i.forEach((e=>{e._mc&&Object.values(e._mc).forEach((e=>{t.set.add(e)}))})),e&&t.filter((t=>t.nameSpace===e)),t}add(e,t){let r=this;return C(e,t).forEach((e=>{r.#i.add(e)})),r}addClass(...e){return e=x(e),this.forEach((t=>{t.classList.add(...e)})),this}after(e,t){let r=this,n=C(e,t);return n.length?(n.first().forEach((e=>{r.array.reverse().forEach((t=>{t.after(e)}))})),r):r}append(e,t){let r=this.array[0],n=C(e,t);return r&&n.forEach((e=>{r.append(e)})),this}appendTo(e,t){let r=C(e,t).array[0];return r&&this.forEach((e=>{r.append(e)})),this}attr(e,t){let r=this,n={};return r.length?arguments.length?"string"==typeof e?void 0===t?[...r.set][0].getAttribute(e):null===t?([...r.set].forEach((t=>{t.removeAttribute(e)})),r):("object"==typeof t&&(t=JSON.stringify(t)),[...r.set].forEach((r=>{r.setAttribute(e,t)})),r):"object"==typeof e?([...r.set].forEach((t=>{Object.keys(e).forEach((r=>{C(t).attr(r,e.key)}))})),r):r:(Array.from([...r.set][0].attributes).forEach((e=>{n[e.name]=e.value})),n):r}before(e,t){let r=this,n=C(e,t);return n.length?(n.first().forEach((e=>{r.forEach((t=>{t.before(e)}))})),r):r}children(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{Array.from(e.children).forEach((e=>{n.add(e)}))})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}descendants(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{e.querySelectorAll("*").forEach((e=>{n.add(e)}))})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}empty(){return this.filter((e=>!1))}first(e,t){let r=this,n=new Set,s=!1;return e&&(s=C(e,t)),r.#i.forEach((e=>{e.parentNode&&n.add(e.parentNode.children[0])})),r=C([...n]),e&&r.filter((e=>s.has(e))),r}has(e){return this.#i.has(e)}hasClass(...e){return e=x(e),this.every((e=>Array.from(e.classList).every((t=>e.contains(t)))))}html(e){let t=this;return e?(t.forEach((t=>{t.parentNode?t.innerHTML=e:(()=>{console.warn("Cannot access the .innerHTML of a HTMLElement that hasnt been inserted yet",t),console.trace(t,this),console.info("If you called this from inside a Web Component constructor, consider using the %c[connected] event callback!","color: blue;")})()})),t):t.array[0].innerHTML}last(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{let t=e.parentNode?.children;t&&n.add(Array.from(t).at(-1))})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}next(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{let t=e.parentNode?.children;if(t){let r=Array.from(t),s=r.indexOf(e)+1;s<r.length&&n.add(r.at(s))}})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}normalize(){return this.#i.forEach((e=>{e.normalize()})),this}off(e,t){let r=e.indexOf(" ")>-1?e.split(" "):[e];return this.array.forEach((e=>{r.forEach((function(r){h(e,r,t)}))})),this}on(e,t,r=!1,n=!1){let s=this,a=e.indexOf(" ")>-1?e.split(" ").filter((e=>""!==e)):[e];function o(){o.cb.apply(s,Array.from(arguments))}return o.cb=t,s.forEach((e=>{a.forEach((function(t){u(e,t,o,r,n)}))})),o}one(e,t,r=!1){let n=this,s=e.indexOf(" ")>-1?e.split(" ").filter((e=>""!==e)):[e];function a(){a.cb.apply(n,Array.from(arguments))}return a.cb=t,n.forEach((e=>{s.forEach((function(t){u(e,t,a,r,!0)}))})),a}parent(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{e.parentNode&&"HTML"!==e.parentNode.tagName&&n.add(e.parentNode)})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}parents(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{let t=e;for(;t.parentNode&&"HTML"!==t.parentNode.tagName;)t=t.parentNode,n.add(t)})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}prepend(e,t){let r=this.array[0],n=C(e,t);return r&&n.forEach((e=>{r.prepend(e)})),this}prev(e,t){let r=this,n=new Set;if(r.#i.forEach((e=>{let t=e.parentNode?.children;if(t){let r=Array.from(t),s=r.indexOf(e)-1;s>-1&&n.add(r.at(s))}})),r=C([...n]),e){let n=C(e,t);r.filter((e=>n.has(e)))}return r}removeClass(...e){return e=x(e),this.forEach((t=>{t.classList.remove(...e)})),this}text(e){let t=this;return e?(t.forEach((t=>{t.parentNode?t.innerText=e:(()=>{console.warn("Cannot access the .textContent of a HTMLElement that hasnt been inserted yet",t),console.trace(t,this),console.info("If you called this from inside a Web Component constructor, consider using the %c[connected] event callback!","color: blue;")})()})),t):t.array[0].innerText??""}toggleClass(...e){return e=x(e),this.forEach((t=>{t.classList.toggle(...e)})),this}trigger(e,t={},r=!1,n=!1,s=!1){let a,o={bubbles:r,cancelable:n,composed:s},i=e.indexOf(" ")>-1?e.split(" "):[e];return this.#i.forEach((e=>{i.forEach((function(r){a=new Event(r,o),a.data=t,e.dispatchEvent(a)}))})),this}val(e,t){if(!this.length)return this;const r=this;let n,s=T(r.array[0]);var a;return e?null!=(a=e)&&"object"==typeof a?(Object.assign(s,e),n=r):"string"==typeof e&&(void 0===t?n=s[e]:(s[e]=t,n=r)):n=Object.assign({},s),n}values(e){const t=this;let r={};if(!t.length)return console.warn("no dom selection to search inputs in"),{};let n=T(t.array[0]);return Object.keys(n).forEach((e=>{r[e]=n[e]})),0===Object.keys(r).length&&console.warn("no inputs in this dom selection",t.array[0],r),e&&Object.keys(e).forEach((t=>{n.hasOwnProperty(t)&&(console.log("key found",t),n[t]=e[t])})),structuredClone(Object.assign(n))}at(e){return C(this.array.at(e))}concat(e){let t=this;return(e instanceof $?e.array:e).forEach((e=>{t.#i.add(e)})),t}entries(){return this.array.entries()}every(e,t){return this.array.every(e,t)}filter(e,t){return this.#i=new Set(this.array.filter(e,t)),this}forEach(e,t){return this.array.forEach(e,t),this}includes(e){return-1!==this.array.indexOf(e,0)}indexOf(e,t){return this.array.indexOf(e,t)}map(e,t){return this.array.map(e,t)}pop(){return this.array.pop()}push(...e){return e.forEach((e=>{this.#i.add(e)})),this}shift(){let e=this,t=e.#i.size?e.array[0]:void 0;return t&&e.#i.delete(t),t}slice(e,t){return C(this.array.slice(e,t))}some(e,t){return this.array.some(e,t)}splice(e,t,...r){let n=this.array;return n.splice(e,t,...r),C(n)}}class k{#i=new Set;get set(){return this.#i}get array(){return Array.from(this.#i)}set set(e){this.#i=e}get length(){return this.#i.size}set length(e){}constructor(e,t=document.body){if(!e)return this;C(e,t).forEach((e=>{H(e).forEach((e=>{this.#i.add(e)}))}))}$d(e,t=document.body){let r=new $,n=new Set,s=!!e||!1,a=new Set;return s&&(a=C(e,t).set),this.#i.forEach((e=>{s&&!a.has(e._tg)||n.add(e._tg)})),r.set=n,r}children(e){let t=this,r=new Set;return t.#i.forEach((t=>{t.children(e).forEach((e=>{r.add(e)}))})),t.#i=r,t}descendants(e){let t=this,r=new Set;return t.#i.forEach((t=>{t.descendants(e).forEach((e=>{r.add(e)}))})),t.#i=r,t}first(e){let t=this,r=new Set;return t.#i.forEach((e=>{e.parent().children().$d().first().$t().forEach((e=>{r.add(e)}))})),t.#i=r,e&&t.filter((t=>t.nameSpace=e)),t}has(e){return this.#i.has(e)}last(e){let t=this,r=new Set;return t.#i.forEach((e=>{e.parent().children().$d().last().$t().forEach((e=>{r.add(e)}))})),t.#i=r,e&&t.filter((t=>t.nameSpace=e)),t}next(e){let t=this,r=new Set;return t.#i.forEach((e=>{let t,n=new Set;for(t=e._tg;null!==t.nextElementSibling;)t=t.nextElementSibling,n.add(t);H([...n]).$d().first().$t().forEach((e=>{r.add(e)}))})),t.#i=r,e&&t.filter((t=>t.nameSpace=e)),t}ns(e){return this.filter((t=>t.nameSpace===e))}off(e,t){let r=e.indexOf(" ")>-1?e.split(" ").filter((e=>""!==e)):[e];return this.$d().array.forEach((e=>{r.forEach((function(r){h(e,`[TB]${r}`,t)}))})),this}on(e,t,r=!1){let n=this,s=e.indexOf(" ")>-1?e.split(" ").filter((e=>""!==e)):[e];function a(){a.cb.apply(n,Array.from(arguments))}return a.cb=t,n.forEach((e=>{s.forEach((function(t){u(e._tg,`[TB]${t}`,a,!1,r)}))})),a}one(e,t){let r=this,n=e.indexOf(" ")>-1?e.split(" ").filter((e=>""!==e)):[e];function s(){s.cb.apply(r,Array.from(arguments))}return s.cb=t,r.forEach((e=>{n.forEach((function(t){u(e._tg,`[TB]${t}`,s,!1,!0)}))})),s}parent(e){let t=this,r=new Set;return t.#i.forEach((t=>{t.parent(e).forEach((e=>{r.add(e)}))})),t.#i=r,t}parents(e){let t=this,r=new Set;return t.#i.forEach((t=>{t.parents(e).forEach((e=>{r.add(e)}))})),t.#i=r,t}prev(e){let t=this,r=new Set;return t.#i.forEach((e=>{let t,n=new Set;for(t=e._tg;null!==t.previousElementSibling;)t=t.previousElementSibling,n.add(t);H([...n]).$d().last().$t().forEach((e=>{r.add(e)}))})),t.#i=r,e&&t.filter((t=>t.nameSpace=e)),t}trigger(e,t={},r="l"){return this.forEach((n=>{n.trigger(e,t,r)})),this}at(e){return H(this.array.at(e))}concat(e){let t=this;return e.forEach((e=>{t.#i.add(e)})),t}entries(){return this.array.entries()}every(e,t){return this.array.every(e,t)}filter(e,t){return H(this.array.filter(e,t))}forEach(e,t){return this.array.forEach(e,t),this}includes(e){return-1!==this.array.indexOf(e)}indexOf(e,t){return this.array.indexOf(e,t)}map(e,t){return this.array.map(e,t)}pop(){let e=this.array,t=e.pop();return this.#i=new Set(e),t}push(e){return this.#i.add(e),this}shift(){let e=this.array.shift();return this.#i.delete(e),e}slice(e,t){return H(this.array.slice(e,t))}some(e,t){return this.array.some(e,t)}splice(e,t,...r){let n=this.array.splice(e,t,...r);return H(n)}}const C=(e,t=document)=>{let r=new Set;return e instanceof Array?(e.forEach((e=>{C(e,t).forEach((e=>{r.add(e)}))})),new $([...r])):new $(e,t)},H=(e,t=document)=>C(e,t).$t();return t})()));