export function createTemplateSelectorLockController(templateSelect) { var locked = false; var armed = false; var lockInput = null; function clearLockInput() { if (lockInput && lockInput.parentNode) { lockInput.parentNode.removeChild(lockInput); } lockInput = null; } function reset() { locked = false; armed = false; clearLockInput(); if (!templateSelect) { return; } templateSelect.disabled = false; templateSelect.removeAttribute('aria-disabled'); templateSelect.removeAttribute('title'); templateSelect.classList.remove('is-locked'); } function arm() { armed = true; } function syncLockedValue() { if (lockInput) { lockInput.value = templateSelect.value; } } function lock() { if (!templateSelect || locked) { return; } locked = true; lockInput = lockInput || document.createElement('input'); lockInput.type = 'hidden'; lockInput.name = templateSelect.name; lockInput.value = templateSelect.value; templateSelect.insertAdjacentElement('afterend', lockInput); templateSelect.disabled = true; templateSelect.setAttribute('aria-disabled', 'true'); templateSelect.setAttribute('title', 'Template is locked after you edit its content.'); templateSelect.classList.add('is-locked'); } function markEdited() { if (!armed) { return false; } lock(); syncLockedValue(); return locked; } function sync() { if (!templateSelect) { return false; } if (locked) { syncLockedValue(); return true; } return false; } return { reset: reset, arm: arm, markEdited: markEdited, sync: sync, isLocked: function () { return locked; } }; }