{"version":3,"sources":["field-values.js"],"names":["hide","show","onLoad","context","arguments","length","undefined","document","input","duration","querySelectorAll","forEach","dependsOn","dependsOnValues","isVisible","dependsOnNames","dataset","dependsOnName","split","every","n","toUpperCase","field","fieldName","dependsOnValue","_toConsumableArray","concat","some","f","type","checked","v","value","element","defaultChecked","defaultValue","HTMLInputElement","HTMLTextAreaElement","dispatchEvent","Event","bubbles","disabled","addEventListener","e","target"],"mappings":"+9BAAAA,KAAAC,IAAA,KAAA,WAAA,SAAAC,SAAA,IAAAC,EAAA,EAAAC,UAAAC,QAAAC,KAAAA,IAAAF,UAAA,GAAAA,UAAA,GAAAG,SAEAL,EAAAA,iBAAAA,mBAAAA,EAAAA,QAAAA,SAAAA,GAIA,SAAAM,EAAA,GAAA,IAAAC,EAAA,EAAAL,UAAAC,QAAAC,KAAAA,IAAA,EAAA,EAAA,IAMAH,EAAAO,iBAAA,wBAAA,EAAAC,QAAA,SAAAC,GACA,IAMAC,EAGAC,EATAC,EAAAH,EAAAI,QAAAC,cAAAC,MAAA,GAAA,EAGAH,EAAAI,MAAA,SAAAC,GAAA,OAAAA,EAAAC,YAAA,IAAAC,EAAAN,QAAAO,UAAAF,YAAA,CAAA,CAAA,IAGAR,EAAAD,EAAAI,QAAAQ,eAAAN,MAAA,GAAA,IAGAJ,EAAAC,EAAAI,MAAA,SAAAC,GAIA,OAFAK,mBAAAtB,EAAAO,iBAAA,qBAAAgB,OAAAN,EAAA,MAAA,CAAA,CAAA,EAEAO,KAAA,SAAAC,GAEA,MAAA,CAAA,EAAA,aAAAA,EAAAC,MAAA,UAAAD,EAAAC,MAAAD,EAAAE,UAGAjB,EAAAc,KAAA,SAAAI,GAAA,MAAA,KAAAH,EAAAI,QAAA,MAAAD,GAAAA,EAAAV,YAAA,IAAAO,EAAAI,MAAAX,YAAA,EAAA,CAAA,CACA,CAAA,CACA,CAAA,GAIApB,KAEAD,MAFAY,EAAAH,CAAA,EAIAG,EAAAF,iBAAA,yBAAA,EAAAC,QAAA,SAAAsB,GAEAnB,IACA,aAAAmB,EAAAJ,MAAA,UAAAI,EAAAJ,KACAI,EAAAH,QAAAG,EAAAC,eAEAD,EAAAD,MAAAC,EAAAE,aAGAF,aAAAG,kBAAA,aAAAH,EAAAJ,MAAA,UAAAI,EAAAJ,MAAAI,aAAAI,oBACAJ,EAAAK,cAAA,IAAAC,MAAA,QAAA,CAAAC,QAAA,CAAA,CAAA,CAAA,CAAA,EAEAP,EAAAK,cAAA,IAAAC,MAAA,SAAA,CAAAC,QAAA,CAAA,CAAA,CAAA,CAAA,GAIAP,EAAAQ,SAAA,CAAA3B,CACA,CAAA,EACA,CAAA,CACA,CAxDAP,EAAAA,iBAAAA,QAAAA,WAAAA,OAAAA,EAAAA,CAAAA,CAAAA,EAKAC,EAAA,CAAA,CAoDA,CAAA,CACA,CA5DAN,OAAA,EAEAK,SAAAmC,iBAAA,oBAAA,SAAAC,GAAA,OAAAzC,OAAAyC,EAAAC,MAAA,CAAA,CAAA,SAJA1C,MA8DA","file":"field-values.js","sourcesContent":["import { hide, show } from 'ui-utils';\r\n\r\nonLoad();\r\n\r\ndocument.addEventListener('DOMContentUpdated', e => onLoad(e.target));\r\n\r\nexport function onLoad(context = document) {\r\n context.querySelectorAll('[data-field-name]').forEach(field => {\r\n field.addEventListener('input', () => input());\r\n input(0);\r\n\r\n function input(duration = 400) {\r\n context.querySelectorAll('[data-depends-on-name]').forEach(dependsOn => {\r\n const dependsOnNames = dependsOn.dataset.dependsOnName.split(';');\r\n\r\n // Make sure the current element depends on the field. This saves you checking when not necessary.\r\n if (dependsOnNames.every(n => n.toUpperCase() !== field.dataset.fieldName.toUpperCase()))\r\n return;\r\n\r\n const dependsOnValues = dependsOn.dataset.dependsOnValue.split(';');\r\n\r\n // Now make sure every value matches.\r\n const isVisible = dependsOnNames.every(n => {\r\n // This may return more than one .e.g radio buttons.\r\n const fields = [...context.querySelectorAll(`[data-field-name=\"${n}\" i]`)];\r\n\r\n return fields.some(f => {\r\n // Ignore un-checked checkbox/radio buttons.\r\n if ((f.type === 'checkbox' || f.type === 'radio') && !f.checked)\r\n return false;\r\n\r\n return dependsOnValues.some(v => f.value !== '' && (v === '*' || v.toUpperCase() === f.value.toUpperCase()));\r\n });\r\n });\r\n\r\n // Show the dependable field if all it's dependencies have a matching value, else hide it.\r\n if (isVisible)\r\n show(dependsOn, duration);\r\n else\r\n hide(dependsOn, duration);\r\n\r\n dependsOn.querySelectorAll('input, select, textarea').forEach(element => {\r\n // Reset the value (if applicable). This makes sure the personalisation price is not included when the field is hidden.\r\n if (!isVisible) {\r\n if (element.type === 'checkbox' || element.type === 'radio')\r\n element.checked = element.defaultChecked;\r\n else\r\n element.value = element.defaultValue;\r\n\r\n // Note: For input (not checkboxes or radio buttons) and textarea elements fire the input event, else fire change to make sure any values model bound with Vue.js are updated.\r\n if ((element instanceof HTMLInputElement && element.type !== 'checkbox' && element.type !== 'radio') || element instanceof HTMLTextAreaElement)\r\n element.dispatchEvent(new Event('input', { bubbles: true }));\r\n else\r\n element.dispatchEvent(new Event('change', { bubbles: true }));\r\n }\r\n\r\n // Disable the hidden form element (if hidden) otherwise the value is still submitted, e.g. check box lists.\r\n element.disabled = !isVisible;\r\n });\r\n });\r\n }\r\n });\r\n}"]}