function Ui(modules) { var connectedForms = {}; modules.messaging.addEventListener('open', refreshForms); modules.messaging.addEventListener('close', refreshForms); return { connectedForm: connectedForm, enableForm: enableForm }; function connectedForm(formId) { var root = document.getElementById(formId); connectedForms[formId] = { root: root, submits: root.querySelectorAll('[type=submit]'), enabled: false }; return root; } function enableForm(formId, enabled) { connectedForms[formId].enabled = enabled || undefined == enabled; refreshForm(connectedForms[formId]); } function refreshForms() { for(var key in connectedForms) { refreshForm(connectedForms[key]); } } function refreshForm(form) { form.submits.forEach(function(button) { button.disabled = !(modules.messaging.isOn() && form.enabled); }); } }