/* ========================================================================== mai-kit.js v1.0 — Mission Acts International side-element kit Pairs with mai-kit.css. No dependencies, no tracking, no network calls. LOAD: (after the page's own script) WHAT IT DOES (each piece is opt-in by markup; nothing happens without it) 1. Theme & motion sync — reads the same localStorage keys the pages use ("mai-theme", "mai-motion"), follows changes made in other tabs, and tells embedded iframes the current theme via postMessage. Pages that already run the FluidSite script keep full control; the kit only applies the stored value when the page didn't. 2. Forms [data-maik-validate] — inline, accessible validation using the browser's own rules (required, type=email, pattern, min/maxlength), error summary, busy submit button, character counters, honeypot check. 3. Embeds [data-maik-embed] — loading veil, slow-load fallback link, lazy loading of data-src iframes. 4. Strips [data-maik-dismiss] — dismissible notices remembered per id. 5. Dialogs [data-maik-open] — open/close native elements. 6. Copy [data-maik-copy] — copy text (emails, phone numbers, links). 7. Toasts — window.MAIKit.toast("Saved", "ok"|"err"|"info"). 8. Reveal [.maik-reveal] — fade-in, only when the page has none. PUBLIC API: window.MAIKit = { init(root), toast(msg, kind), setTheme(t), validate(form) } Re-run MAIKit.init(container) after injecting new markup. ========================================================================== */ (function () { 'use strict'; if (window.MAIKit) { return; } // load-once guard var doc = document.documentElement; doc.classList.add('maik-js'); var store = { get: function (k) { try { return localStorage.getItem(k); } catch (e) { return null; } }, set: function (k, v) { try { localStorage.setItem(k, v); } catch (e) {} } }; var reduceMotion = function () { return doc.getAttribute('data-motion') === 'paused' || (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches); }; /* ---------- 1. Theme & motion sync ---------- */ var currentTheme = function () { var t = doc.getAttribute('data-theme'); if (t) { return t; } return (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? 'dark' : 'light'; }; var broadcastTheme = function () { var msg = { type: 'mai-theme', theme: currentTheme() }; Array.prototype.forEach.call(document.querySelectorAll('iframe[data-maik-theme-sync]'), function (f) { try { f.contentWindow.postMessage(msg, '*'); } catch (e) {} }); }; var setTheme = function (t) { if (t !== 'dark' && t !== 'light') { return; } var sysDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; if (t === (sysDark ? 'dark' : 'light')) { doc.removeAttribute('data-theme'); } else { doc.setAttribute('data-theme', t); } var btn = document.getElementById('theme-toggle'); if (btn) { btn.setAttribute('aria-pressed', t === 'dark' ? 'true' : 'false'); } }; // Apply stored values only if the page script didn't already var storedTheme = store.get('mai-theme'); if (!doc.hasAttribute('data-theme') && (storedTheme === 'dark' || storedTheme === 'light')) { setTheme(storedTheme); } if (store.get('mai-motion') === 'paused' && !doc.hasAttribute('data-motion')) { doc.setAttribute('data-motion', 'paused'); } // Follow changes made in another tab/window window.addEventListener('storage', function (e) { if (e.key === 'mai-theme' && e.newValue) { setTheme(e.newValue); } if (e.key === 'mai-motion') { if (e.newValue === 'paused') { doc.setAttribute('data-motion', 'paused'); } else { doc.removeAttribute('data-motion'); } } }); // Tell opted-in iframes whenever the theme changes on this page if ('MutationObserver' in window) { new MutationObserver(broadcastTheme).observe(doc, { attributes: true, attributeFilter: ['data-theme'] }); } // Inside an iframe: accept theme messages from a missionacts.com parent window.addEventListener('message', function (e) { var ok = /(^|\.)missionacts\.com$/.test((e.origin || '').replace(/^https?:\/\//, '').split(':')[0]) || e.origin === window.location.origin; if (ok && e.data && e.data.type === 'mai-theme') { setTheme(e.data.theme); } }); /* ---------- 2. Forms ---------- */ var fieldOf = function (el) { return el.closest('.maik-field, .maik-fieldset'); }; var labelText = function (el) { var f = fieldOf(el); var l = f && (f.querySelector('.maik-label') || f.querySelector('legend')); return l ? l.textContent.replace(/\*|\(optional\)/g, '').trim() : (el.name || 'This field'); }; var lcfirst = function (t) { return /^[A-Z][a-z]/.test(t) ? t.charAt(0).toLowerCase() + t.slice(1) : t; }; var messageFor = function (el) { var v = el.validity, name = labelText(el); if (el.dataset.maikMsg) { return el.dataset.maikMsg; } if (v.valueMissing) { if (el.type === 'checkbox' || el.type === 'radio' || el.tagName === 'SELECT') { return 'Please choose an option for \u201C' + name + '\u201D' + (/[?.!]$/.test(name) ? '' : '.'); } return 'Please fill in ' + lcfirst(name.replace(/[?:.\s]+$/, '')) + '.'; } if (v.typeMismatch && el.type === 'email') { return 'Enter an email address like [email protected].'; } if (v.typeMismatch && el.type === 'url') { return 'Enter a full web address starting with https://.'; } if (v.typeMismatch && el.type === 'tel') { return 'Enter a phone number using digits only.'; } if (v.tooShort) { return lcfirst(name) === name ? name + ' needs at least ' : name + ' needs at least ' + el.minLength + ' characters.'; } if (v.tooLong) { return name + ' must be ' + el.maxLength + ' characters or fewer.'; } if (v.patternMismatch) { return el.title || 'Please check the format of ' + name.toLowerCase() + '.'; } if (v.rangeUnderflow || v.rangeOverflow) { return name + ' must be between ' + el.min + ' and ' + el.max + '.'; } return el.validationMessage || 'Please check this field.'; }; var ensureError = function (f, id) { var err = f.querySelector('.maik-error'); if (!err) { err = document.createElement('p'); err.className = 'maik-error'; f.appendChild(err); } if (!err.id) { err.id = id + '-error'; } return err; }; var checkField = function (el) { var f = fieldOf(el); if (!f || el.disabled || el.type === 'hidden' || el.closest('.maik-hp')) { return true; } var valid = el.checkValidity(); var group = f.classList.contains('maik-fieldset') ? f.querySelectorAll('input') : [el]; var err = ensureError(f, el.id || el.name || ('maik-' + Math.random().toString(36).slice(2, 8))); f.classList.toggle('is-invalid', !valid); Array.prototype.forEach.call(group, function (g) { if (valid) { g.removeAttribute('aria-invalid'); } else { g.setAttribute('aria-invalid', 'true'); } var d = (g.getAttribute('aria-describedby') || '').split(' ').filter(Boolean); if (d.indexOf(err.id) === -1) { d.push(err.id); g.setAttribute('aria-describedby', d.join(' ')); } }); err.textContent = valid ? '' : messageFor(el); return valid; }; var validate = function (form) { var bad = [], seen = {}; Array.prototype.forEach.call(form.elements, function (el) { if (!el.willValidate) { return; } if ((el.type === 'radio' || el.type === 'checkbox') && el.name && seen[el.name]) { return; } seen[el.name] = true; if (!checkField(el)) { bad.push(el); } }); var sum = form.querySelector('.maik-summary'); if (sum) { if (bad.length) { var list = sum.querySelector('ul') || sum.appendChild(document.createElement('ul')); list.innerHTML = ''; bad.forEach(function (el) { var li = document.createElement('li'), a = document.createElement('a'); if (!el.id) { el.id = 'maik-f-' + Math.random().toString(36).slice(2, 8); } a.href = '#' + el.id; a.textContent = messageFor(el); a.addEventListener('click', function (e) { e.preventDefault(); el.focus(); }); li.appendChild(a); list.appendChild(li); }); sum.hidden = false; sum.setAttribute('tabindex', '-1'); sum.focus(); } else { sum.hidden = true; } } else if (bad.length) { bad[0].focus(); } return bad.length === 0; }; var initForm = function (form) { if (form.dataset.maikReady) { return; } form.dataset.maikReady = '1'; form.setAttribute('novalidate', ''); // we show our own messages form.addEventListener('blur', function (e) { var el = e.target; if (!el.willValidate) { return; } var f = fieldOf(el); if (el.value !== '' || (f && f.classList.contains('is-invalid'))) { checkField(el); } }, true); form.addEventListener('input', function (e) { var f = fieldOf(e.target); if (f && f.classList.contains('is-invalid')) { checkField(e.target); } }); form.addEventListener('change', function (e) { if (e.target.type === 'radio' || e.target.type === 'checkbox') { checkField(e.target); } }); // Character counters:

Array.prototype.forEach.call(form.querySelectorAll('[data-maik-count-for]'), function (c) { var el = form.querySelector('#' + c.getAttribute('data-maik-count-for')); if (!el) { return; } var max = el.maxLength > 0 ? el.maxLength : parseInt(c.getAttribute('data-max') || '0', 10); var upd = function () { var n = el.value.length; c.textContent = max ? n + ' / ' + max : n + ' characters'; c.classList.toggle('is-over', max > 0 && n > max); }; c.setAttribute('aria-live', 'polite'); el.addEventListener('input', upd); upd(); }); form.addEventListener('submit', function (e) { // Honeypot: real people never fill .maik-hp inputs var hp = form.querySelector('.maik-hp input, .maik-hp textarea'); if (hp && hp.value) { e.preventDefault(); return; } if (!validate(form)) { e.preventDefault(); return; } var btn = form.querySelector('[type="submit"]'); if (btn) { btn.classList.add('is-busy'); btn.setAttribute('aria-disabled', 'true'); if (btn.dataset.maikBusy) { btn.dataset.maikLabel = btn.textContent; btn.textContent = btn.dataset.maikBusy; } } // Let pages handle async submits: form.addEventListener('maik:valid', …) var ev; try { ev = new CustomEvent('maik:valid', { cancelable: true }); } catch (x) { ev = null; } if (ev && !form.dispatchEvent(ev)) { e.preventDefault(); } }); }; // Helper for async handlers: MAIKit.done(form, ok, message) var done = function (form, ok, message) { var btn = form.querySelector('[type="submit"]'); if (btn) { btn.classList.remove('is-busy'); btn.removeAttribute('aria-disabled'); if (btn.dataset.maikLabel) { btn.textContent = btn.dataset.maikLabel; } } if (message) { toast(message, ok ? 'ok' : 'err'); } if (ok) { form.reset(); } }; /* ---------- 3. Embeds ---------- */ var initEmbed = function (box) { if (box.dataset.maikReady) { return; } box.dataset.maikReady = '1'; var frame = box.querySelector('iframe, video'); if (!frame) { return; } var slowAfter = parseInt(box.getAttribute('data-maik-slow') || '8000', 10); var mark = function () { box.classList.add('is-loaded'); box.classList.remove('is-slow'); if (frame.hasAttribute('data-maik-theme-sync')) { broadcastTheme(); } }; frame.addEventListener('load', mark); frame.addEventListener('loadeddata', mark); var start = function () { if (frame.dataset.src && !frame.getAttribute('src')) { frame.setAttribute('src', frame.dataset.src); } setTimeout(function () { if (!box.classList.contains('is-loaded')) { box.classList.add('is-slow'); } }, slowAfter); }; if (frame.dataset.src && 'IntersectionObserver' in window) { var io = new IntersectionObserver(function (en) { if (en[0].isIntersecting) { io.disconnect(); start(); } }, { rootMargin: '300px' }); io.observe(box); } else { start(); } }; /* ---------- 4. Dismissible strips ---------- */ var initStrip = function (btn) { var strip = btn.closest('[data-maik-strip]'); if (!strip || btn.dataset.maikReady) { return; } btn.dataset.maikReady = '1'; var key = 'maik-dismiss-' + (strip.id || strip.getAttribute('data-maik-strip')); if (store.get(key)) { strip.hidden = true; return; } btn.addEventListener('click', function () { strip.hidden = true; store.set(key, '1'); }); }; /* ---------- 5. Dialogs ---------- */ var initDialogTrigger = function (btn) { if (btn.dataset.maikReady) { return; } btn.dataset.maikReady = '1'; btn.addEventListener('click', function () { var dlg = document.getElementById(btn.getAttribute('data-maik-open')); if (!dlg) { return; } if (typeof dlg.showModal === 'function') { dlg.showModal(); } else { dlg.setAttribute('open', ''); } dlg.addEventListener('close', function () { btn.focus(); }, { once: true }); }); }; document.addEventListener('click', function (e) { var c = e.target.closest('[data-maik-close]'); if (c) { var d = c.closest('dialog'); if (d) { d.close ? d.close() : d.removeAttribute('open'); } return; } if (e.target.tagName === 'DIALOG' && e.target.classList.contains('maik-dialog')) { var r = e.target.getBoundingClientRect(); // click on backdrop closes if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) { e.target.close(); } } }); /* ---------- 6. Copy ---------- */ var initCopy = function (btn) { if (btn.dataset.maikReady) { return; } btn.dataset.maikReady = '1'; var label = btn.textContent; btn.addEventListener('click', function () { var text = btn.getAttribute('data-maik-copy'); var ok = function () { btn.classList.add('is-done'); btn.textContent = 'Copied'; setTimeout(function () { btn.classList.remove('is-done'); btn.textContent = label; }, 1800); }; if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(text).then(ok, function () {}); } else { var t = document.createElement('textarea'); t.value = text; t.setAttribute('readonly', ''); t.style.position = 'fixed'; t.style.opacity = '0'; document.body.appendChild(t); t.select(); try { document.execCommand('copy'); ok(); } catch (x) {} document.body.removeChild(t); } }); }; /* ---------- 7. Toasts ---------- */ var toastHost; var toast = function (msg, kind) { if (!toastHost) { toastHost = document.createElement('div'); toastHost.className = 'maik-toasts'; toastHost.setAttribute('role', 'status'); toastHost.setAttribute('aria-live', 'polite'); document.body.appendChild(toastHost); } var t = document.createElement('div'); t.className = 'maik-toast' + (kind ? ' maik-toast--' + kind : ''); t.textContent = msg; toastHost.appendChild(t); setTimeout(function () { t.classList.add('is-out'); setTimeout(function () { t.remove(); }, reduceMotion() ? 0 : 320); }, 4200); }; /* ---------- 8. Reveal (only for .maik-reveal; pages keep their own .maif-reveal) ---------- */ var revealIO = ('IntersectionObserver' in window && !reduceMotion()) ? new IntersectionObserver(function (en) { en.forEach(function (x) { if (x.isIntersecting) { x.target.classList.add('is-in'); revealIO.unobserve(x.target); } }); }, { threshold: 0.1 }) : null; var initReveal = function (el) { if (revealIO) { revealIO.observe(el); } else { el.classList.add('is-in'); } }; /* ---------- Init ---------- */ var each = function (root, sel, fn) { Array.prototype.forEach.call(root.querySelectorAll(sel), fn); }; var init = function (root) { root = root || document; each(root, 'form[data-maik-validate]', initForm); each(root, '[data-maik-embed]', initEmbed); each(root, '[data-maik-strip] [data-maik-dismiss]', initStrip); each(root, '[data-maik-open]', initDialogTrigger); each(root, '[data-maik-copy]', initCopy); each(root, '.maik-reveal', initReveal); }; window.MAIKit = { init: init, toast: toast, setTheme: setTheme, validate: validate, done: done, version: '1.0' }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { init(); }); } else { init(); } })();