| 1 |
/* phpcs:ignoreFile */ |
| 2 |
(function () { |
| 3 |
function syncFormData() { |
| 4 |
const embeddedSelector = 'script.ivyforms-ssr-data[type="application/json"]'; |
| 5 |
const embedded = document.querySelectorAll(embeddedSelector); |
| 6 |
embedded.forEach(function (script) { |
| 7 |
try { |
| 8 |
const txt = script.textContent || script.innerText || ''; |
| 9 |
let parsed; |
| 10 |
try { |
| 11 |
parsed = JSON.parse(txt); |
| 12 |
} catch (e) { |
| 13 |
parsed = null; |
| 14 |
} |
| 15 |
if (parsed && typeof window.wpIvyFormDataList === 'object') { |
| 16 |
const counter = script.getAttribute('data-ivyforms-counter') || (parsed.counter || null); |
| 17 |
if (counter) { |
| 18 |
window.wpIvyFormDataList[counter] = parsed; |
| 19 |
} else { |
| 20 |
for (let k in parsed) { |
| 21 |
if (Object.prototype.hasOwnProperty.call(parsed, k)) { |
| 22 |
window.wpIvyFormDataList[k] = parsed[k]; |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
} catch (e) { |
| 28 |
// ignore |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
const wrappers = document.querySelectorAll('[data-ivyforms-data]'); |
| 33 |
wrappers.forEach(function (wrapper) { |
| 34 |
try { |
| 35 |
const raw = wrapper.getAttribute('data-ivyforms-data'); |
| 36 |
const data = tryParse(raw); |
| 37 |
if (data && typeof window.wpIvyFormDataList === 'object') { |
| 38 |
for (let k in data) { |
| 39 |
if (Object.prototype.hasOwnProperty.call(data, k)) { |
| 40 |
window.wpIvyFormDataList[k] = data[k]; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
} catch (e) { |
| 45 |
// ignore |
| 46 |
} |
| 47 |
}); |
| 48 |
|
| 49 |
disableFormInteractivity(); |
| 50 |
} |
| 51 |
|
| 52 |
function tryParse(raw) { |
| 53 |
if (!raw || typeof raw !== 'string') return null; |
| 54 |
try { |
| 55 |
return JSON.parse(raw); |
| 56 |
} catch (e) { |
| 57 |
try { |
| 58 |
let decoded = raw.replace(/"/g, '"'); |
| 59 |
decoded = decoded.replace(/&/g, '&'); |
| 60 |
decoded = decoded.replace(/'/g, "'"); |
| 61 |
decoded = decoded.replace(/</g, '<'); |
| 62 |
decoded = decoded.replace(/>/g, '>'); |
| 63 |
return JSON.parse(decoded); |
| 64 |
} catch (e2) { |
| 65 |
try { |
| 66 |
if (/^[A-Za-z0-9+\/=\n\r]+$/.test(raw) && raw.length > 50) { |
| 67 |
const txt = atob(raw); |
| 68 |
return JSON.parse(txt); |
| 69 |
} |
| 70 |
} catch (e3) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
return null; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
function disableFormInteractivity() { |
| 79 |
const wrapperSelector = '.ivyforms-block-editor-preview, [data-ivyforms-data]'; |
| 80 |
const wrappers = document.querySelectorAll(wrapperSelector); |
| 81 |
|
| 82 |
wrappers.forEach(function (wrapper) { |
| 83 |
const forms = wrapper.querySelectorAll('form'); |
| 84 |
if (forms.length === 0) { |
| 85 |
const standaloneSelector = 'input, textarea, select, button, a[href], [tabindex]'; |
| 86 |
const standalone = wrapper.querySelectorAll(standaloneSelector); |
| 87 |
applyDisableToElements(standalone); |
| 88 |
} |
| 89 |
|
| 90 |
forms.forEach(function (form) { |
| 91 |
if (!form.__ivyformsPreviewBound) { |
| 92 |
form.addEventListener('submit', function (e) { |
| 93 |
e.preventDefault(); |
| 94 |
e.stopPropagation(); |
| 95 |
return false; |
| 96 |
}, true); |
| 97 |
form.__ivyformsPreviewBound = true; |
| 98 |
} |
| 99 |
|
| 100 |
const elemsSelector = 'input, textarea, select, button, a[href], [tabindex]'; |
| 101 |
const elems = form.querySelectorAll(elemsSelector); |
| 102 |
applyDisableToElements(elems); |
| 103 |
}); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
function applyDisableToElements(elements) { |
| 108 |
elements.forEach(function (el) { |
| 109 |
if (el.hasAttribute('data-ivyforms-ignore')) { |
| 110 |
return; |
| 111 |
} |
| 112 |
if (el.hasAttribute('data-ivyforms-preview-disabled')) { |
| 113 |
return; |
| 114 |
} |
| 115 |
el.setAttribute('data-ivyforms-preview-disabled', '1'); |
| 116 |
try { |
| 117 |
if (el.hasAttribute('tabindex')) { |
| 118 |
el.setAttribute('data-ivyforms-original-tabindex', el.getAttribute('tabindex')); |
| 119 |
} |
| 120 |
el.setAttribute('tabindex', '-1'); |
| 121 |
} catch (e) { |
| 122 |
// ignore |
| 123 |
} |
| 124 |
|
| 125 |
const tag = (el.tagName || '').toLowerCase(); |
| 126 |
if (tag === 'a') { |
| 127 |
if (el.hasAttribute('href')) { |
| 128 |
el.setAttribute('data-ivyforms-original-href', el.getAttribute('href')); |
| 129 |
el.removeAttribute('href'); |
| 130 |
} |
| 131 |
} else if (tag === 'button' || tag === 'input' || tag === 'select' || tag === 'textarea') { |
| 132 |
try { |
| 133 |
if ('disabled' in el) { |
| 134 |
el.disabled = true; |
| 135 |
} else { |
| 136 |
el.setAttribute('aria-disabled', 'true'); |
| 137 |
} |
| 138 |
} catch (e) { |
| 139 |
// ignore |
| 140 |
} |
| 141 |
} else { |
| 142 |
el.setAttribute('aria-hidden', 'true'); |
| 143 |
} |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
if (document.readyState === 'loading') { |
| 148 |
document.addEventListener('DOMContentLoaded', syncFormData); |
| 149 |
} else { |
| 150 |
syncFormData(); |
| 151 |
} |
| 152 |
|
| 153 |
const observer = new MutationObserver(function () { |
| 154 |
syncFormData(); |
| 155 |
}); |
| 156 |
|
| 157 |
observer.observe(document.body, { |
| 158 |
childList: true, |
| 159 |
subtree: true |
| 160 |
}); |
| 161 |
})(); |
| 162 |
|