-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmf-conditional-fields.min.js
1 lines (1 loc) · 6.63 KB
/
mf-conditional-fields.min.js
1
"use strict";const mfConditionalFields=(e,t={})=>{switch(typeof e){case"string":e=document.querySelectorAll(e);break;case"object":!1===Array.isArray(e)&&Object.prototype.hasOwnProperty.call(e,"0")?(e=Object.fromEntries(Object.entries(e).filter((([e,t])=>!isNaN(e)&&void 0!==t.elements))),e=Object.values(e)):e=Array.isArray(e)?e:[e]}let n=t.rules||"inline",i=t.dynamic||!1,l=t.unsetHidden||!1,o=t.disableHidden||!1,r=t.debug||!1,a=t.depth||3,d=[],s=[],u=[],f={initField:(e,t)=>{let n=e.getAttribute("data-conditional-rules");if(n.length>0){n=JSON.parse(n);let i="container"in n?n.container:"",l="action"in n?n.action:"show",o="logic"in n?n.logic:"or",r="rules"in n?n.rules:[];if("object"==typeof r&&void 0===r.length&&(r=[r]),r.length>0){for(let e=0;r.length>e;e++)if("group"in r[e])for(let n=0;r[e].group.length>n;n++)"name"in r[e].group[n]&&!s[t].includes(r[e].group[n].name)&&s[t].push(r[e].group[n].name);else"name"in r[e]&&!s[t].includes(r[e].name)&&s[t].push(r[e].name);e.removeAttribute("data-conditional-rules"),e.mfConditionalContainerSelector=i,e.mfConditionalAction=l,e.mfConditionalLogic=o,e.mfConditionalRules=r,e.mfConditionalFormIndex=t,f.updateField(e)}}},updateField:(e,t=1)=>{let n=e.mfConditionalFormIndex,i=e.mfConditionalAction,l=e.mfConditionalLogic,o=e.mfConditionalRules,r=!1;if(o.length>0)for(let e=0;o.length>e;e++){let t=!1;if("group"in o[e]){let i=o[e].relation||"and",l=!1;for(let r=0;o[e].group.length>r;r++){if(l=f.evaluateRule(o[e].group[r],n),0==l&&"and"==i){t=!1;break}if(l&&"or"==i){t=!0;break}t=l}}else t=f.evaluateRule(o[e],n);if(!1===t&&"and"==l){r=!1;break}if(t&&"or"==l){r=!0;break}r=t}r||(i="hide"==i?"show":"show"==i?"hide":"disable"==i?"enable":"enable"==i?"disable":"none"),f.toggleField(e,i,t)},toggleField:(e,t,n)=>{let i=e.mfConditionalFormIndex,r=e.name,d=e.mfConditionalContainerSelector,u=null;if(n<a&&s[i].includes(r)){let e=f.getDependantField(r,i);if(e.length>0)for(let i=0;e.length>i;i++){let l=e[i].mfConditionalAction;"hide"==t&&"disable"!==l&&"enable"!==l?f.toggleField(e[i],"hide",++n):f.updateField(e[i],++n)}}"hide"==t?(""==d?e.setAttribute("hidden",!0):(u=e.closest(""+d),u&&u.setAttribute("hidden",!0)),o&&e.setAttribute("disabled","disabled"),l&&("checkbox"==e.type||"radio"==e.type?e.checked=!1:e.value="")):"disable"==t?e.setAttribute("disabled","disabled"):"enable"==t?e.hasAttribute("disabled")&&e.removeAttribute("disabled"):"show"==t&&(""==d?e.removeAttribute("hidden"):(u=e.closest(""+d),u&&u.removeAttribute("hidden")),o&&e.removeAttribute("disabled"))},getDependantField:(e,t)=>{let n=[];if(void 0!==d[t])for(let i=0;d[t].length>i;i++)if("mfConditionalRules"in d[t][i]){let l=d[t][i].mfConditionalRules.some((t=>{if("group"in t){for(let n=0;t.group.length>n;n++)if(t.group[n].name===e)return!0;return!1}return t.name===e}));void 0!==l&&!1!==l&&n.push(d[t][i])}return n},evaluateRule:(t,n)=>{let i=t.name,l=t.operator,o=t.value;if(s[n].includes(i)){let t,r,a,d=e[n].querySelectorAll('[name="'+i+'"]');if(d.length>0){if(t=d[0].type,"radio"!==t&&"checkbox"!==t&&(d=d[0]),"radio"==t||"checkbox"==t){r=[];for(let e=0;e<d.length;e++)d[e].checked&&r.push(d[e].value),e===d.length-1&&(r=r.join("|"))}else if("select-multiple"==t){r=[];for(let e=0;e<d.options.length;e++)d.options[e].selected&&r.push(d.options[e].value);r=r.join("|"),console.log(r)}else r=d.value;a=f.compareValues(l,r,o)}return a}return!1},compareValues:(e,t,n)=>{switch(t=t?t.toString().toLowerCase():"",n=n?n.toString().toLowerCase():"",e){case"is":return n===t;case"isnot":return n!==t;case"greaterthan":return!isNaN(t)&&!isNaN(n)&&Number(t)>Number(n);case"lessthan":return!isNaN(t)&&!isNaN(n)&&Number(t)<Number(n);case"contains":return t.includes(n);case"doesnotcontain":return!t.includes(n);case"beginswith":return t.startsWith(n);case"doesnotbeginwith":return!t.startsWith(n);case"endswith":return t.endsWith(n);case"doesnotendwith":return!t.endsWith(n);case"isempty":return""===t;case"isnotempty":return""!==t}return!1},updateForm:async(t,i="add")=>{if(void 0===e[t])return!1;if("add"==i)try{let i,l;if(i=await new Promise(((i,l)=>{let o=[];if("inline"==n)o=e[t].querySelectorAll("[data-conditional-rules]");else{for(let i=0;n.length>i;i++)if("field"in n[i]){let l=e[t].elements[n[i].field];l=l instanceof RadioNodeList?Array.from(l):[l];for(let e=0;l.length>e;e++)if(void 0!==l[e]){let t=l[e];t.setAttribute("data-conditional-rules",JSON.stringify(n[i])),o.push(t)}delete n[i].field}n=null}o.length>0?(d[t]=d[t].concat(Array.prototype.slice.call(o)),i(o)):l("No conditional fields found on step 1")})),l=await new Promise(((e,n)=>{if(i.length>0)for(let n=0;i.length>n;n++)f.initField(i[n],t),n===i.length-1&&e();else n("No conditional fields to initialize on step 2")})),s.length>0)for(let n=0;s[t].length>n;n++)if(!u[t].includes(s[t][n])){let i=e[t].querySelectorAll('[name="'+s[t][n]+'"]');if(0===i.length&&(i=e[t].querySelectorAll('[type="checkbox"][name="'+s[t][n]+'[]"]'),i.length>0)){let e=s[t][n];s[t][n]=s[t][n]+"[]";let i=f.getDependantField(e,t);if(i.length>0)for(let t=0;i.length>t;t++)if(i[t].mfConditionalRules.length>0){let n=!1;for(let l=0;i[t].mfConditionalRules.length>l;l++)if("group"in i[t].mfConditionalRules[l])for(let o=0;i[t].mfConditionalRules[l].group.length>o;o++)i[t].mfConditionalRules[l].group[o].name==e&&(i[t].mfConditionalRules[l].group[o].name=e+"[]",n=!0);else i[t].mfConditionalRules[l].name==e&&(i[t].mfConditionalRules[l].name=e+"[]",n=!0);n&&f.updateField(i[t])}}if(i.length>0){for(let e=0;i.length>e;e++)i[e].mfConditionalFormIndex=t,i[e].addEventListener("change",f.fieldListener,!1);u[t].push(s[t][n])}i=null}return!0}catch(n){if(r){let i="formIndex: "+t;void 0!==e[t].getAttribute("id")&&(i="formId: "+e[t].getAttribute("id")),console.info(`${i} => ${n}`)}return!1}else if("remove"==i)return d[t]=d[t].filter((n=>void 0!==e[t].elements[""+n.name])),s[t]=s[t].filter((n=>void 0!==e[t].elements[""+n])),u[t]=u[t].filter((n=>void 0!==e[t].elements[""+n])),!0;return!1},fieldListener:e=>{let t=f.getDependantField(e.target.name,e.target.mfConditionalFormIndex);if(t.length>0)for(let e=0;t.length>e;e++)f.updateField(t[e])},formListener:e=>{let t=e.target.mfConditionalFormIndex,n=e.detail.action;f.updateForm(t,n)}};if("block"==n){let e=document.getElementById("rules-mf-conditional-fields");if(void 0===typeof e)return r&&console.warn("The rules element could not be found."),!1;n=JSON.parse(e.innerHTML)}if("inline"!==n&&"object"!=typeof n)return r&&console.warn("The supplied rules or rule type is not valid."),!1;if(!(e.length>0))return r&&console.warn("The supplied conditional form was not found"),!1;for(let t=0;e.length>t;t++)d.push([]),s.push([]),u.push([]),e[t].mfConditionalFormIndex=t,f.updateForm(t);if(i)for(let t=0;e.length>t;t++)e[t].addEventListener("mfConditionalFormUpdated",f.formListener,!1)};