|
| 1 | +// Places each time field next to the date field it belongs to, and snaps |
| 2 | +// typed values to the configured interval. |
| 3 | +// |
| 4 | +// Redmine renders no hook between the date inputs and their labels, so the |
| 5 | +// fields are rendered in a detached container and moved here. The targets are |
| 6 | +// Redmine's own wrapper ids (start_date_area / due_date_area). If either is |
| 7 | +// missing, the field is left where it was rendered rather than lost. |
| 8 | +(function () { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + // Marks that this script actually loaded. The stylesheet only hides the |
| 12 | + // detached container when this class is present, so a failed or disabled |
| 13 | + // script leaves the fields visible where they were rendered instead of |
| 14 | + // hiding them forever. |
| 15 | + document.documentElement.classList.add('issue-datetime-js'); |
| 16 | + |
| 17 | + function snapToStep(input) { |
| 18 | + var step = parseInt(input.getAttribute('step'), 10); |
| 19 | + if (!step || !input.value) return; |
| 20 | + |
| 21 | + var parts = input.value.split(':'); |
| 22 | + if (parts.length < 2) return; |
| 23 | + |
| 24 | + var hours = parseInt(parts[0], 10); |
| 25 | + var mins = parseInt(parts[1], 10); |
| 26 | + if (!isFinite(hours) || !isFinite(mins)) return; |
| 27 | + |
| 28 | + var minutes = hours * 60 + mins; |
| 29 | + var stepMinutes = step / 60; |
| 30 | + if (!stepMinutes) return; |
| 31 | + |
| 32 | + var snapped = Math.round(minutes / stepMinutes) * stepMinutes; |
| 33 | + // Snapping up from the last slot of the day would roll over to 00:00 the |
| 34 | + // next day, which the date field cannot express; clamp instead. |
| 35 | + if (snapped > 23 * 60 + 59) { |
| 36 | + snapped = Math.floor((23 * 60 + 59) / stepMinutes) * stepMinutes; |
| 37 | + } |
| 38 | + var hh = String(Math.floor(snapped / 60)).padStart(2, '0'); |
| 39 | + var mm = String(snapped % 60).padStart(2, '0'); |
| 40 | + var value = hh + ':' + mm; |
| 41 | + if (value !== input.value) input.value = value; |
| 42 | + } |
| 43 | + |
| 44 | + function relocate(container) { |
| 45 | + var fields = container.querySelectorAll('[data-issue-datetime-target]'); |
| 46 | + Array.prototype.forEach.call(fields, function (field) { |
| 47 | + var target = document.getElementById(field.dataset.issueDatetimeTarget); |
| 48 | + if (!target) return; |
| 49 | + |
| 50 | + var wrapper = document.createElement('span'); |
| 51 | + wrapper.className = 'issue-datetime-inline'; |
| 52 | + wrapper.appendChild(field); |
| 53 | + target.appendChild(wrapper); |
| 54 | + }); |
| 55 | + // Whatever could not be relocated stays visible in place. |
| 56 | + if (!container.querySelector('[data-issue-datetime-target]')) { |
| 57 | + container.parentNode.removeChild(container); |
| 58 | + } else { |
| 59 | + container.classList.remove('issue-datetime-pending'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function init() { |
| 64 | + var container = document.getElementById('issue-datetime-fields'); |
| 65 | + if (container) relocate(container); |
| 66 | + |
| 67 | + document.querySelectorAll('input.issue-datetime-time').forEach(function (input) { |
| 68 | + input.addEventListener('change', function () { snapToStep(input); }); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (document.readyState === 'loading') { |
| 73 | + document.addEventListener('DOMContentLoaded', init); |
| 74 | + } else { |
| 75 | + init(); |
| 76 | + } |
| 77 | +})(); |
0 commit comments