// Configure your import map in config/importmap.rb. Read more: // https://github.com/rails/importmap-rails import "@hotwired/turbo-rails" import "./turbo_actions.js" // TODO: access `data-` attributes through // `keyname in`/`dataset.keyname`/`delete dataset.keyname` // Show page if hidden for testing purposes. function showPage(event) { document.documentElement.style.visibility="visible" } document.addEventListener('turbo:load', showPage) //
event handlers. function detailsChange(event) { var target = event.currentTarget var count = target.querySelectorAll('input:checked:not([disabled])').length var span = target.querySelector('summary > span') var button = target.querySelector('button') if (count > 0) { span.textContent = count + ' selected'; Turbo.StreamElement.prototype.enableElement(button) } else { span.textContent = span.getAttribute('data-prompt') Turbo.StreamElement.prototype.disableElement(button) } } window.detailsChange = detailsChange // Close open
when focus lost. function detailsClose(event) { if (!event.relatedTarget || event.relatedTarget.closest("details") != event.currentTarget) { event.currentTarget.removeAttribute("open") } } window.detailsClose = detailsClose window.detailsObserver = new MutationObserver((mutations) => { mutations[0].target.dispatchEvent(new Event('change', {bubbles: true})) }); // Validation of multiple-submit forms with different field requirements. function formValidate(event) { var id = event.submitter.getAttribute("data-validate") if (!id) return; var input = document.getElementById(id) if (!input.checkValidity()) { input.reportValidity() event.preventDefault() } } window.formValidate = formValidate // Pattern for number precision verification. Should accept all number formats // accepted by Kernel#Float. Lookahead checks the number of significant digits // in mantissa, following expression makes sure there is only one dot. const numberRegExp = /^\-?(?=(?:\.?0)*(?:\.?\d){1,15}?(?:\.?0)*\.?(?:[eE]|$))(?\d*\.?\d+|\d+\.)(?:[eE]\-?\d{1,3})?$/ // Validation of input[type=number] value precision. function numberValidate(event) { var input = event.target input.setCustomValidity("") if (!input.validity.valid) return var match = numberRegExp.exec(input.value) if (!match || (parseFloat(input.value) == 0.0 && match.groups['mantissa'].match(/[1-9]/))) { // TODO: move message to locales input.setCustomValidity("Please select a value within range of double precision floating-point format.") } } window.numberValidate = numberValidate // Keyboard event processing handlers. function formProcessKey(event) { switch (event.key) { case "Escape": event.currentTarget.querySelector("a[name=cancel]").click() break // TODO: allow new line inside