@popovandrii/ui-elements
Version:
User interface elements. Color scheme. Elements with complex logic Java Script. Contrasting UI elements.
2 lines (1 loc) • 9.95 kB
JavaScript
(function(d,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(d=typeof globalThis<"u"?globalThis:d||self,h(d.UiElements={}))})(this,function(d){"use strict";var M=Object.defineProperty;var F=d=>{throw TypeError(d)};var V=(d,h,f)=>h in d?M(d,h,{enumerable:!0,configurable:!0,writable:!0,value:f}):d[h]=f;var w=(d,h,f)=>V(d,typeof h!="symbol"?h+"":h,f),B=(d,h,f)=>h.has(d)||F("Cannot "+f);var x=(d,h,f)=>(B(d,h,"read from private field"),f?f.call(d):h.get(d)),S=(d,h,f)=>h.has(d)?F("Cannot add the same private member more than once"):h instanceof WeakSet?h.add(d):h.set(d,f);var $=(d,h,f)=>(B(d,h,"access private method"),f);var A,k,g,K,L,_;class h{constructor(e={}){S(this,g);w(this,"selectors");w(this,"spinBoxes");S(this,A,(e,i,s,a=0,t=0)=>{e==a||e<a?(i.classList.add(this.selectors.disabledBtn),i.disabled=!0):(i.classList.remove(this.selectors.disabledBtn),i.disabled=!1),t!==0&&(e==t||e>t?(s.classList.add(this.selectors.disabledBtn),s.disabled=!0):(s.classList.remove(this.selectors.disabledBtn),s.disabled=!1))});S(this,k,(e,i)=>{const s=e.getAttribute(`data-${i}`);return s===null||s.trim()===""||isNaN(Number(s))?0:Number(s)});const i={main:"UIsp",btn:"UIsp__btn",input:"UIsp__input",disabledBtn:"disabled"};this.selectors={...i,...e},this.spinBoxes=document.querySelectorAll(`.${this.selectors.main}`),$(this,g,K).call(this)}}A=new WeakMap,k=new WeakMap,g=new WeakSet,K=function(){this.spinBoxes.forEach(e=>{const i=e.querySelectorAll(`.${this.selectors.btn}`),s=i[0],a=i[1],t=e.querySelector(`.${this.selectors.input}`),u=Number(e.dataset.step),l=Number(e.dataset.min),r=x(this,k).call(this,e,"max"),b=n=>{e.setAttribute("aria-valuenow",String(n)),e.setAttribute("aria-valuetext",`${n} items`)};Number(t.value)<=l&&(t.value=l.toFixed(u)),r!==0?(Number(t.value)>=r&&(t.value=r.toFixed(u)),r&&e.setAttribute("aria-valuemax",r.toFixed(u))):t.value=Number(t.value).toFixed(u),l&&e.setAttribute("aria-valuemin",l.toFixed(u)),x(this,A).call(this,Number(t.value),s,a,l,r),b(t.value);let c=null;const m=(n=1)=>{t.value=String(Math.abs(Number(t.value)));let o=parseFloat(t.value)||0;o=o+1*n/Math.pow(10,u),o>r&&r!==0&&(o=r),t.value=o.toFixed(u),x(this,A).call(this,Number(t.value),s,a,l,r),b(t.value)},p=(n=1)=>{t.value=String(Math.abs(Number(t.value)));let o=parseFloat(t.value)||0;o=o-1*n/Math.pow(10,u),o<l&&(o=l),t.value=o.toFixed(u),x(this,A).call(this,Number(t.value),s,a,l,r),b(t.value)},v=(n,o=150)=>{c===null&&(c=window.setInterval(n,o))},y=()=>{c!==null&&(clearInterval(c),c=null)};a.addEventListener("mousedown",n=>{const o=n.shiftKey?3:1;v(()=>m(o))}),a.addEventListener("touchstart",()=>v(m)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(n=>{a.addEventListener(n,y)}),a.addEventListener("click",n=>{const o=n.shiftKey?3:1;c===null&&m(o)}),s.addEventListener("click",n=>{const o=n.shiftKey?3:1;c===null&&p(o)}),s.addEventListener("mousedown",n=>{const o=n.shiftKey?3:1;v(()=>p(o),100)}),s.addEventListener("touchstart",()=>v(p,100)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(n=>{s.addEventListener(n,y)}),t.addEventListener("keydown",n=>{const o=n.key,z=n.shiftKey?3:1;if(["Backspace","Delete","ArrowLeft","ArrowRight","Tab","Enter","Home","End"].includes(o)||(n.ctrlKey||n.metaKey)&&["a","c","v","x"].includes(o.toLowerCase()))return;if(["e","+","-"].includes(o)){n.preventDefault();return}if(o==="ArrowUp"||o==="ArrowDown"){n.preventDefault();const U=parseFloat(t.value)||0,D=1*z/Math.pow(10,u);let I=o==="ArrowUp"?U+D:U-D;I<l&&(I=l),t.value=I.toFixed(u),Number(t.value)<l&&(t.value=l.toFixed(u)),Number(t.value)>r&&r!==0&&(t.value=r.toFixed(u)),x(this,A).call(this,Number(t.value),s,a,l,r),b(t.value);return}const C=o===","?".":o,q=/^[0-9]$/.test(C),N=C===".",T=t.value.includes(".");(u===0&&!q||u>0&&!(q||N)||N&&T)&&n.preventDefault()}),t.addEventListener("change",()=>{Number(t.value)<l&&(t.value=l.toFixed(u)),Number(t.value)>r&&r!==0?t.value=r.toFixed(u):t.value=Number(t.value).toFixed(u),x(this,A).call(this,Number(t.value),s,a,l,r)})})};class f{constructor(e={}){S(this,L);w(this,"selectors");w(this,"main");const i={main:"UIsw",label:"UIsw-label"};this.selectors={...i,...e},this.main=document.querySelectorAll(`.${this.selectors.main}`),$(this,L,_).call(this)}}L=new WeakSet,_=function(){this.main.forEach(e=>{const i=e.querySelector(`.${this.selectors.label}`),s=e.querySelector("input");i&&i.id&&e.setAttribute("aria-labelledby",i.id),s&&(s.checked?e.setAttribute("aria-checked","true"):e.setAttribute("aria-checked","false"),e.addEventListener("click",()=>{s.checked=!s.checked,e.setAttribute("aria-checked",String(s.checked))}),e.addEventListener("keydown",t=>{t.key==="ArrowRight"?(s.checked=!0,a(String(s.checked)),t.preventDefault()):t.key==="ArrowLeft"&&(s.checked=!1,a(String(s.checked)),t.preventDefault())}));const a=t=>{e.setAttribute("aria-checked",String(t))}})};class E{constructor(e={}){w(this,"selectors");w(this,"main");w(this,"itemArrowInitialized",new WeakSet);const i={idPrefix:"UI-option-",main:"UIselect",selected:"UIselect-selected",arrow:"UIselect-arrow",optionsList:"UIselect-options",search:"UIselect-options__search",items:"UIselect-options__items",excludedItems:["divider","test"]};this.selectors={...i,...e},this.main=document.querySelectorAll(`.${this.selectors.main}`),this.event(),E.initGlobalListener(this.selectors)}filterExcluded(e,i){return Array.from(e).filter(s=>!i.some(a=>typeof a=="string"?s.classList.contains(a)||s.id===a:s===a))}filterSearch(e,i){const s=i.trim().toLowerCase();return e.filter(a=>{var l,r;const t=((l=a.dataset.value)==null?void 0:l.toLowerCase())||"",u=((r=a.textContent)==null?void 0:r.toLowerCase())||"";return t.includes(s)||u.includes(s)})}event(){this.main.forEach(e=>{const i=e.querySelector(`.${this.selectors.main} input[type='hidden']`);try{if(!i)throw new Error('<input type="hidden" name="YouUniqueId">')}catch(m){return console.warn("Not found:",m.message)}const s=e.querySelector(`.${this.selectors.selected}`),a=e.querySelector(`.${this.selectors.arrow}`),t=e.querySelector(`.${this.selectors.optionsList}`),u=e.querySelector(`.${this.selectors.search} input`);a&&a.addEventListener("click",()=>{this.toggle(e,t)}),s.addEventListener("click",()=>{this.toggle(e,t)}),e.addEventListener("click",()=>{this.itemsPosition(t)});const l=t.querySelectorAll(`.${this.selectors.items} ul li`),r=this.filterExcluded(l,this.selectors.excludedItems),b=e.querySelector("[aria-selected='true']");b&&this.defaultSelect(e,b,s,i);var c=[];u&&u.addEventListener("input",m=>{const v=m.target.value.trim();s&&(c=this.filterSearch(r,v),v?(l.forEach(y=>y.remove()),c.forEach(y=>{var n;(n=t.querySelector(`.${this.selectors.optionsList} ul`))==null||n.appendChild(y)})):l.forEach(y=>{var n;(n=t.querySelector(`.${this.selectors.optionsList} ul`))==null||n.appendChild(y)}),this.itemArrow(e,t,s,i))}),this.itemArrow(e,t,s,i),this.items(e,t,r,s,i)})}itemArrow(e,i,s,a){if(this.itemArrowInitialized.has(e))return;this.itemArrowInitialized.add(e);let t=-1;const u=s.textContent?s.textContent:"",l=e.querySelector(`.${this.selectors.search} input`);e.addEventListener("keydown",r=>{l&&l.focus();const b=Array.from(i.querySelectorAll(`.${this.selectors.optionsList} ul li`)),c=this.filterExcluded(b,this.selectors.excludedItems),m=c.length;if(m!==0)if(r.key==="ArrowDown"){r.preventDefault(),e.getAttribute("aria-expanded")==="false"&&this.toggle(e,i),t=(t+1)%m,s.textContent=c[t].textContent,c.forEach(v=>v.removeAttribute("aria-selected")),c[t].setAttribute("aria-selected","true");const p=c[t].id||`${this.selectors.idPrefix}${t}`;e.setAttribute("aria-activedescendant",p),c[t].scrollIntoView({block:"nearest"})}else if(r.key==="ArrowUp"){r.preventDefault(),t=(t-1+m)%m,s.textContent=c[t].textContent,c.forEach(v=>v.removeAttribute("aria-selected")),c[t].setAttribute("aria-selected","true");const p=c[t].id||`${this.selectors.idPrefix}${t}`;e.setAttribute("aria-activedescendant",p),c[t].scrollIntoView({block:"nearest"})}else if(r.key==="Enter")if(r.preventDefault(),t>=0){s.textContent=c[t].textContent,c.forEach(v=>v.removeAttribute("aria-selected")),c[t].setAttribute("aria-selected","true");const p=c[t].id||`${this.selectors.idPrefix}${t}`;e.setAttribute("aria-activedescendant",p),a.value=String(c[t].dataset.value),this.costomEvent(e,a.value),this.close(e,i)}else this.toggle(e,i);else r.key==="Escape"&&(e.getAttribute("aria-activedescendant")||(s.textContent=u),this.close(e,i))})}itemsPosition(e){const i=e.querySelector('[aria-selected="true"]');i&&i.scrollIntoView({block:"nearest"})}items(e,i,s,a,t){s.forEach((u,l)=>{u.addEventListener("click",()=>{const r=s[l];if(r){a.textContent=r.textContent,s.forEach(c=>c.removeAttribute("aria-selected")),r.setAttribute("aria-selected","true");const b=r.id||`${this.selectors.idPrefix}${l}`;e.setAttribute("aria-expanded","false"),e.setAttribute("aria-activedescendant",b),t.value=String(s[l].dataset.value),this.costomEvent(e,t.value),this.close(e,i)}})})}defaultSelect(e,i,s,a){i&&(a.setAttribute("value",i.dataset.value??""),s.textContent=i.textContent??"",e.setAttribute("aria-activedescendant",i.id||""))}costomEvent(e,i){e.dispatchEvent(new CustomEvent("change",{detail:{val:i},bubbles:!0}))}toggle(e,i){E.closeAll(this.selectors),i.hidden?(i.hidden=!1,e.setAttribute("aria-expanded","true")):this.close(e,i)}close(e,i){i.hidden=!0,e.setAttribute("aria-expanded","false")}static closeAll(e){document.querySelectorAll(`.${e.main}`).forEach(i=>{const s=i.querySelector(`.${e.optionsList}`);s.hidden=!0,i.setAttribute("aria-expanded","false")})}static initGlobalListener(e){window.__UIselectGlobalClickInitialized||(document.addEventListener("click",i=>{const s=i.target;[...document.querySelectorAll(`.${e.main}`)].some(t=>t.contains(s))||E.closeAll(e)}),window.__UIselectGlobalClickInitialized=!0)}}d.Select=E,d.SpinBox=h,d.Switch=f,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});