UNPKG

@ramstack/alpinegear-bound

Version:

@ramstack/alpinegear-bound provides the 'x-bound' Alpine.js directive, which allows for two-way binding of input elements and their associated data properties.

1 lines 4.04 kB
!function(){"use strict";const e=(...e)=>console.warn("alpinegear.js:",...e),t=Array.isArray,n=e=>null==e,i=e=>"checkbox"===e.type||"radio"===e.type,a=e=>t(e)?e:[e],r=(e,t)=>e==t,o=(e,t)=>e.findIndex(e=>e==t),c=(e,t)=>e.includes(t),s=(e,t,n,i)=>(e.addEventListener(t,n,i),()=>e.removeEventListener(t,n,i)),u=e=>"object"==typeof e?JSON.parse(JSON.stringify(e)):e;function l(e,...t){const n=e(...t);return()=>{let e;return n(t=>e=t),t=e,"function"==typeof t?.get?e.get():e;var t}}function d(e,...t){const n=e(...t);t[t.length-1]=`${t.at(-1)} = __val`;const i=e(...t);return e=>{let t;n(e=>t=e),function(e){return"function"==typeof e?.set}(t)?t.set(e):i(()=>{},{scope:{__val:e}})}}const f=Symbol();let p;function h(e,t,n=null){const{effect:i,release:a}=Alpine;let r,o,c,s=!1;const u=i(()=>{r=e(),s||(n?.deep&&JSON.stringify(r),o=r),(s||(n?.immediate??1))&&(c=setTimeout(()=>{t(r,o),o=r})),s=!0});return()=>{clearTimeout(c),a(u)}}const v=new Map("value,checked,files,innerHTML,innerText,textContent,videoHeight,videoWidth,naturalHeight,naturalWidth,clientHeight,clientWidth,offsetHeight,offsetWidth,indeterminate,open,group".split(",").map(e=>[e.trim().toLowerCase(),e.trim()]));function g({directive:g,entangle:m,evaluateLater:b,mapAttributes:k,mutateDom:x,prefixed:y}){k(e=>({name:e.name.replace(/^&/,y("bound:")),value:e.value})),g("bound",(g,{expression:k,value:y,modifiers:T},{effect:L,cleanup:w})=>{if(!y)return void e("x-bound directive expects the presence of a bound property name");const E=g.tagName.toUpperCase();k=k?.trim();const H=v.get(y.trim().replace("-","").toLowerCase());k||=H;const _=l(b,g,k),A=d(b,g,k),S=()=>r(g[H],_())||x(()=>g[H]=_()),W=()=>A((e=>"number"===e.type||"range"===e.type)(g)?function(e){return""===e?null:+e}(g[H]):g[H]);let C;switch(H){case"value":!function(){switch(E){case"INPUT":case"TEXTAREA":n(_())&&W(),L(S),w(s(g,"input",W)),C=!0;break;case"SELECT":setTimeout(()=>{n(_())&&W(),L(()=>function(e,t){for(const n of e.options)n.selected=o(t,n.value)>=0}(g,a(_()??[]))),w(s(g,"change",()=>A(function(e){return e.multiple?[...e.selectedOptions].map(e=>e.value):e.value}(g))))}),C=!0}}();break;case"checked":i(g)&&(L(S),w(s(g,"change",W)),C=!0);break;case"files":"file"===g.type&&(_()instanceof FileList||W(),L(S),w(s(g,"input",W)),C=!0);break;case"innerHTML":case"innerText":case"textContent":g.isContentEditable&&(n(_())&&W(),L(S),w(s(g,"input",W)),C=!0);break;case"videoHeight":case"videoWidth":O("VIDEO","resize");break;case"naturalHeight":case"naturalWidth":O("IMG","load");break;case"clientHeight":case"clientWidth":case"offsetHeight":case"offsetWidth":w(function(e,t){return p??=new ResizeObserver(e=>{for(const t of e)for(const e of t.target[f]?.values()??[])e(t)}),e[f]??=new Set,e[f].add(t),p.observe(e),()=>{e[f].delete(t),e[f].size||(p.unobserve(e),e[f]=null)}}(g,W)),C=!0;break;case"indeterminate":"checkbox"===g.type&&(n(_())&&W(),L(S),w(s(g,"change",W)),C=!0);break;case"open":!function(){const[e,t]=["DETAILS"===E,"DIALOG"===E];(e||t)&&((t||n(_()))&&W(),e&&L(S),w(s(g,"toggle",W)),C=!0)}();break;case"group":i(g)&&(g.name||x(()=>g.name=k),L(()=>x(()=>function(e,n){e.checked=t(n)?o(n,e.value)>=0:r(e.value,n)}(g,_()??[]))),w(s(g,"input",()=>A(function(e,t){if("radio"===e.type)return e.value;t=a(t);const n=o(t,e.value);return e.checked?n>=0||t.push(e.value):n>=0&&t.splice(n,1),t}(g,_())))),C=!0)}if(!C){const t=c(T,"in")?"in":c(T,"out")?"out":"inout",n=k===y?((e,t)=>{for(;e&&!t(e);)e=(e._x_teleportBack??e).parentElement;return e})(g.parentNode,e=>e._x_dataStack):g;if(!g._x_dataStack)return void e("x-bound directive requires the presence of the x-data directive to bind component properties");if(!n)return void e(`x-bound directive cannot find the parent scope where the '${y}' property is defined`);const i={get:l(b,n,k),set:d(b,n,k)},a={get:l(b,g,y),set:d(b,g,y)};switch(t){case"in":w(h(()=>i.get(),e=>a.set(u(e))));break;case"out":w(h(()=>a.get(),e=>i.set(u(e))));break;default:w(m(i,a))}}function O(e,t){E===e&&(W(),w(s(g,t,W)),C=!0)}})}s(document,"alpine:init",()=>Alpine.plugin(g))}();