Current section

Files

Jump to
primer_live priv static primer-live-prompt.min.js
Raw

priv/static/primer-live-prompt.min.js

"use strict";(()=>{var r="[data-prompt]",w="[data-prompt-content]",p="[data-content]",b="[data-touch]",c='input[type="checkbox"]',k="label",y="ismounted",g="touch",E="ismodal",T="isescapable",v="focusfirst",h=t=>(t==null?void 0:t.dataset[g])!==void 0,f=t=>(t==null?void 0:t.dataset[E])!==void 0,x=t=>(t==null?void 0:t.dataset[T])!==void 0;function L(t){let e=t==null?void 0:t.closest(r);if(!e)return null;let a=e.querySelector(c);return a||null}function m(t){let e=null;if(typeof t=="string"){let a=document.querySelector(t);a&&(e=a.querySelector(c))}else e=L(t);return e}function M(t){let e=t.closest(r);if(!e)throw new Error("Prompt element ".concat(r," not found"));let a=(e==null?void 0:e.querySelector(w))||null;if(!a)throw new Error("Prompt element 'data-prompt-content' not found");let n=a==null?void 0:a.querySelector(p);if(!n)throw new Error("Prompt element ".concat(p," not found"));let o=(a==null?void 0:a.querySelector(b))||null,i=(e==null?void 0:e.querySelector(k))||null;return{root:e,touchLayer:o,content:n,toggle:i}}function u({checkbox:t,state:e,elements:a,options:n,onDidShow:o}){switch(t.dataset[y]="true",e){case"showing":delete t.dataset.ishiding,n.willShow&&n.willShow(a);break;case"endShowing":o&&o(a),n.didShow&&n.didShow(a);break;case"hiding":t.checked=!1,t.dataset.ishiding="true",n.willHide&&n.willHide(a);break;case"endHiding":delete t.dataset.ishiding,n.didHide&&n.didHide(a);break;default:break}}function l(t){var n;if(!t.target)return;let e=t.target;if(e&&e instanceof HTMLElement&&!h(e))return;let a=e.closest(r);a&&a instanceof HTMLElement&&f(a)||(n=m(e))==null||n.click()}function P(t){if(t.key==="Escape"){let a=Array.from(document.querySelectorAll("".concat(r," > ").concat(c,":checked"))).reverse()[0];if(a instanceof HTMLElement){let n=a.closest(r);x(n)&&a.click()}}}function H({root:t}){let e=t.querySelector(p);e&&S(t,e)}function S(t,e){let a=t.dataset[v];if(a){let n=e.querySelector(a);n&&n.focus()}}function d(t,e,a){let n=m(t);if(n){if(n.checked&&e==="show"||!n.checked&&e==="hide")return;a&&(n.options=a),n.click()}}var s={isInited:!1,init:function(){s.isInited||(window.addEventListener("keydown",P),s.isInited=!0)},mounted:function(){this.init()},updated:function(){this.init()},change:function(t,e={}){var o,i;let a=m(t);if(!a||!(a instanceof HTMLInputElement))return;let n=M(a);a.checked?(o=n.touchLayer)==null||o.addEventListener("click",l):(i=n.touchLayer)==null||i.removeEventListener("click",l),a.addEventListener("transitionend",function(C){u({checkbox:a,state:a.checked?"endShowing":"endHiding",elements:n,options:e,onDidShow:H})},{once:!0}),u({checkbox:a,state:a.checked?"showing":"hiding",elements:n,options:e})},hide:function(t){if(typeof t!="string"){let e=t,a=e.closest(r);if(h(e)&&f(a))return}d(t,"hide")},show:function(t){d(t,"show")},toggle:function(t){d(t,"toggle")}};typeof window<"u"&&(window.Prompt=s);})();