budgit/assets/js/selectbox.min.js
2026-01-18 22:23:21 +00:00

1 line
8.1 KiB
JavaScript

(()=>{(function(){"use strict";function v(t){if(t._tui)return;t._tui=!0;let e=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"value");e?.set&&Object.defineProperty(t,"value",{get:e.get,set(c){let o=this.value;e.set.call(this,c),o!==c&&this.dispatchEvent(new Event("input",{bubbles:!0}))},configurable:!0})}function A(t){let e=t.querySelector('input[type="hidden"]'),c=t.getAttribute("data-tui-selectbox-content-id"),o=document.getElementById(c);if(!e||!o)return;let n=t.getAttribute("data-tui-selectbox-multiple")==="true",i=e.value?n?e.value.split(","):[e.value]:[];o.querySelectorAll(".select-item").forEach(s=>{let l=s.getAttribute("data-tui-selectbox-value")||"",a=i.includes(l),u=s.getAttribute("data-tui-selectbox-selected")==="true";if(a!==u)if(s.setAttribute("data-tui-selectbox-selected",a.toString()),a){s.classList.add("bg-accent","text-accent-foreground");let r=s.querySelector(".select-check");r&&r.classList.replace("opacity-0","opacity-100")}else{s.classList.remove("bg-accent","text-accent-foreground");let r=s.querySelector(".select-check");r&&r.classList.replace("opacity-100","opacity-0")}})}function b(t){let e=t.querySelector(".select-value"),c=t.querySelector('input[type="hidden"]'),o=t.getAttribute("data-tui-selectbox-content-id"),n=document.getElementById(o);if(!n&&c&&c.value){e.textContent=c.value,e.classList.remove("text-muted-foreground");return}if(!e||!n)return;let i=t.getAttribute("data-tui-selectbox-multiple")==="true",s=t.getAttribute("data-tui-selectbox-show-pills")==="true",l=e.getAttribute("data-tui-selectbox-placeholder")||"Select...",a=n.querySelectorAll('.select-item[data-tui-selectbox-selected="true"]');if(a.length===0){e.textContent=l,e.classList.add("text-muted-foreground"),c&&(c.value="");return}if(e.classList.remove("text-muted-foreground"),i){if(s){e.innerHTML="";let r=document.createElement("div");r.className="flex flex-wrap gap-1 items-center min-h-[1.5rem]";let h=[];Array.from(a).forEach(d=>{let p=document.createElement("span");p.className="inline-flex items-center gap-1 px-2 py-0.5 text-xs rounded-md bg-primary text-primary-foreground";let g=document.createElement("span");g.textContent=d.querySelector(".select-item-text")?.textContent||"",p.appendChild(g);let f=document.createElement("button");f.className="ml-0.5 hover:text-destructive focus:outline-none",f.type="button",f.innerHTML="\xD7",f.setAttribute("data-tui-selectbox-pill-remove",""),f.setAttribute("data-tui-selectbox-value",d.getAttribute("data-tui-selectbox-value")),p.appendChild(f),h.push(p)}),h.forEach(d=>r.appendChild(d)),e.appendChild(r),requestAnimationFrame(()=>{let d=e.offsetWidth;if(r.scrollWidth>d-10&&a.length>3){let g=t.getAttribute("data-tui-selectbox-selected-count-text")||"{n} items selected";e.textContent=g.replace("{n}",a.length)}})}else{let r=t.getAttribute("data-tui-selectbox-selected-count-text")||"{n} items selected";e.textContent=r.replace("{n}",a.length)}let u=Array.from(a).map(r=>r.getAttribute("data-tui-selectbox-value")||"");c&&(c.value=u.join(","))}else{let u=a[0],r=u.querySelector(".select-item-text")?.textContent||"";e.textContent=r,c&&(c.value=u.getAttribute("data-tui-selectbox-value")||"")}}function x(t){let e=t.value.toLowerCase().trim(),c=t.closest("[data-tui-popover-id]");c&&c.querySelectorAll(".select-item").forEach(o=>{let n=o.querySelector(".select-item-text")?.textContent.toLowerCase()||"",i=o.getAttribute("data-tui-selectbox-value")?.toLowerCase()||"",s=!e||n.includes(e)||i.includes(e);o.style.display=s?"":"none"})}function m(t){if(t.getAttribute("data-tui-selectbox-disabled")==="true")return;let e=t.closest("[data-tui-popover-id]"),c=e?.id,o=document.querySelector(`[data-tui-selectbox-content-id="${c}"]`);if(!o)return;let n=o.getAttribute("data-tui-selectbox-multiple")==="true",i=t.getAttribute("data-tui-selectbox-selected")==="true";if(n||e.querySelectorAll(".select-item").forEach(l=>{l.setAttribute("data-tui-selectbox-selected","false"),l.classList.remove("bg-accent","text-accent-foreground");let a=l.querySelector(".select-check");a&&a.classList.replace("opacity-100","opacity-0")}),t.setAttribute("data-tui-selectbox-selected",(!i).toString()),i){t.classList.remove("bg-accent","text-accent-foreground");let l=t.querySelector(".select-check");l&&l.classList.replace("opacity-100","opacity-0")}else{t.classList.add("bg-accent","text-accent-foreground");let l=t.querySelector(".select-check");l&&l.classList.replace("opacity-0","opacity-100")}b(o);let s=o.querySelector('input[type="hidden"]');s&&s.dispatchEvent(new Event("change",{bubbles:!0})),!n&&window.closePopover&&(window.closePopover(c),setTimeout(()=>o.focus(),50))}function S(){document.querySelectorAll(".select-container").forEach(t=>{let e=t.querySelector("button.select-trigger");e&&b(e)})}document.addEventListener("click",t=>{if(t.target.matches("[data-tui-selectbox-pill-remove]")){t.stopPropagation();let o=t.target.getAttribute("data-tui-selectbox-value"),i=t.target.closest("button.select-trigger")?.getAttribute("data-tui-selectbox-content-id"),l=document.getElementById(i)?.querySelector(`.select-item[data-tui-selectbox-value="${o}"]`);l&&m(l);return}let e=t.target.closest(".select-item");if(e){t.preventDefault(),m(e);return}let c=t.target.closest("button.select-trigger");if(c){let o=c.getAttribute("data-tui-selectbox-content-id"),n=document.getElementById(o),i=n?.querySelector("[data-tui-selectbox-search]");i&&setTimeout(()=>{n.style.display!=="none"&&i.focus()},50)}}),document.addEventListener("input",t=>{if(t.target.matches("[data-tui-selectbox-search]")){x(t.target);return}if(t.target.matches("[data-tui-selectbox-hidden-input]")){let e=t.target.closest(".select-trigger");e&&(A(e),b(e))}}),document.addEventListener("keydown",t=>{let e=document.activeElement;if(e?.matches("button.select-trigger")&&(t.key.length===1||t.key==="Backspace")){t.preventDefault();let o=e.getAttribute("data-tui-selectbox-content-id");e.click(),setTimeout(()=>{let n=document.getElementById(o)?.querySelector("[data-tui-selectbox-search]");n&&(n.focus(),t.key!=="Backspace"&&(n.value=t.key))},50)}let c=e?.closest("[data-tui-popover-id]");if(c?.querySelector(".select-item")){if(t.key==="ArrowDown"||t.key==="ArrowUp"){t.preventDefault();let o=Array.from(c.querySelectorAll(".select-item")).filter(s=>s.style.display!=="none");if(o.length===0)return;let n=c.querySelector(".select-item:focus"),i=0;if(n){let s=o.indexOf(n);i=t.key==="ArrowDown"?(s+1)%o.length:(s-1+o.length)%o.length}o[i].focus()}else if(t.key==="Enter"&&e?.matches(".select-item"))t.preventDefault(),m(e);else if(t.key==="Escape"){let o=c.querySelector("[data-tui-selectbox-search]");if(e?.matches(".select-item"))o?.focus();else if(e===o&&window.closePopover){window.closePopover(c.id);let n=document.querySelector(`[data-tui-selectbox-content-id="${c.id}"]`);setTimeout(()=>n?.focus(),50)}}}}),document.addEventListener("mouseover",t=>{let e=t.target.closest(".select-item");if(!e||e.getAttribute("data-tui-selectbox-disabled")==="true")return;let c=e.closest("[data-tui-popover-id]");c&&(c.querySelectorAll(".select-item").forEach(o=>{o.getAttribute("data-tui-selectbox-selected")!=="true"&&o.classList.remove("bg-accent","text-accent-foreground")}),e.getAttribute("data-tui-selectbox-selected")!=="true"&&e.classList.add("bg-accent","text-accent-foreground"))}),document.addEventListener("reset",t=>{t.target.matches("form")&&t.target.querySelectorAll(".select-container").forEach(e=>{let c=e.querySelector("button.select-trigger"),o=c?.getAttribute("data-tui-selectbox-content-id"),n=document.getElementById(o);if(n){n.querySelectorAll(".select-item").forEach(s=>{s.setAttribute("data-tui-selectbox-selected","false"),s.classList.remove("bg-accent","text-accent-foreground");let l=s.querySelector(".select-check");l&&l.classList.replace("opacity-100","opacity-0")});let i=n.querySelector("[data-tui-selectbox-search]");i&&(i.value="",x(i))}c&&b(c)})});function y(){document.querySelectorAll(".select-container").forEach(t=>{let e=t.querySelector("button.select-trigger");if(e&&!e.hasAttribute("data-initialized")){e.setAttribute("data-initialized","true");let c=e.querySelector('input[type="hidden"]');c&&v(c),b(e)}})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",y):y(),new MutationObserver(y).observe(document.body,{childList:!0,subtree:!0})})();})();