/** * @copyright (C) 2018 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt */class Showon{constructor(d){const t=this;this.container=d||document,this.fields={},this.showonFields=[].slice.call(this.container.querySelectorAll("[data-showon]")),this.showonFields.length&&(this.showonFields.forEach(i=>{if(i.hasAttribute("data-showon-initialised"))return;i.setAttribute("data-showon-initialised","");const s=i.getAttribute("data-showon")||"",e=JSON.parse(s);let h;e.length&&(h=[].slice.call(t.container.querySelectorAll(`[name="${e[0].field}"], [name="${e[0].field}[]"]`)),this.fields[e[0].field]||(this.fields[e[0].field]={origin:[],targets:[]}),h.forEach(n=>{this.fields[e[0].field].origin.indexOf(n)===-1&&this.fields[e[0].field].origin.push(n)}),this.fields[e[0].field].targets.push(i),e.length>1&&e.forEach((n,o)=>{o!==0&&(h=[].slice.call(t.container.querySelectorAll(`[name="${n.field}"], [name="${n.field}[]"]`)),this.fields[e[0].field]||(this.fields[e[0].field]={origin:[],targets:[]}),h.forEach(a=>{this.fields[e[0].field].origin.indexOf(a)===-1&&this.fields[e[0].field].origin.push(a)}),this.fields[e[0].field].targets.indexOf(i)===-1&&this.fields[e[0].field].targets.push(i))}))}),this.linkedOptions=this.linkedOptions.bind(this),Object.keys(this.fields).forEach(i=>{this.fields[i].origin.length&&this.fields[i].origin.forEach(s=>{t.linkedOptions(i),s.addEventListener("change",()=>{t.linkedOptions(i)}),s.addEventListener("keyup",()=>{t.linkedOptions(i)}),s.addEventListener("click",()=>{t.linkedOptions(i)})})}))}linkedOptions(d){this.fields[d].targets.forEach(t=>{const i=JSON.parse(t.getAttribute("data-showon"))||[];let s=!0,e;i.forEach((h,n)=>{const o=h||{};o.valid=0,this.fields[d].origin.forEach(a=>{if(a.name.replace("[]","")!==h.field)return;const f=a.id;if(a.getAttribute("type")&&["checkbox","radio"].includes(a.getAttribute("type").toLowerCase())){if(!a.checked)return;e=document.getElementById(f).value}else a.nodeName==="SELECT"&&a.hasAttribute("multiple")?e=Array.from(a.querySelectorAll("option:checked")).map(r=>r.value):(e=document.getElementById(f).value,e===null&&a.tagName.toLowerCase()==="select"&&(e=[]));typeof e!="object"&&(e=JSON.parse(`["${e}"]`)),e.forEach(r=>{o.sign==="="&&o.values.indexOf(r)!==-1&&(o.valid=1),o.sign==="!="&&o.values.indexOf(r)===-1&&(o.valid=1)})}),o.op===""?o.valid===0&&(s=!1):(o.op==="AND"&&o.valid+i[n-1].valid<2&&(s=!1,o.valid=0),o.op==="OR"&&o.valid+i[n-1].valid>0&&(s=!0,o.valid=1))}),t.tagName!=="option"?s?(t.classList.remove("hidden"),t.dispatchEvent(new CustomEvent("joomla:showon-show",{bubbles:!0}))):(t.classList.add("hidden"),t.dispatchEvent(new CustomEvent("joomla:showon-hide",{bubbles:!0}))):t.disabled=!s})}}if(!window.Joomla)throw new Error("Joomla API is not properly initialized");Joomla.Showon||(Joomla.Showon={initialise:l=>new Showon(l)}),Joomla.Showon.initialise(document);const getMatchedParents=(l,d)=>{let t=l,i;const s=[];for(;t;)i=t.matches&&t.matches(d)?t:null,i&&s.unshift(i),t=t.parentNode;return s};document.addEventListener("joomla:updated",({target:l})=>{if(l.classList.contains("subform-repeatable-group")){const d=[].slice.call(l.querySelectorAll("[data-showon]"));if(d.length){const t=[],i=[];getMatchedParents(l,".subform-repeatable-group").forEach(s=>{t.push(new RegExp(`\\[${s.dataset.baseName}X\\]`,"g")),i.push(`[${s.dataset.group}]`)}),d.forEach(s=>{let{showon:e}=s.dataset;t.forEach((h,n)=>{e=e.replace(h,i[n])}),s.dataset.showon=e})}}Joomla.Showon.initialise(l)});