(function (Drupal) { "use strict"; Drupal.behaviors.nouisliderYearFormat = { // Weight needs to be higher than the module's behavior so we run after it. // Drupal behaviors don't have explicit weight in this API, but attaching // after DOM ready is fine since once() ensures the module ran first. attach: function (context, settings) { const sliderEl = context.querySelector("#year.noUi-target"); if (!sliderEl || !sliderEl.noUiSlider) return; if (sliderEl.dataset.yearFormatApplied) return; sliderEl.dataset.yearFormatApplied = "true"; const inputFrom = document.getElementById("nouislider-input-from"); const inputTo = document.getElementById("nouislider-input-to"); // The module's update listener gives inputs the raw float (e.g. 1971.00) // because no top-level format is set on the slider. We add our own // update listener that overwrites the input value with a rounded integer. // 'update' fires on drag but does NOT trigger navigation — only 'set' does. sliderEl.noUiSlider.on("update", function (values, handle) { const rounded = Math.round(parseFloat(values[handle])); if (handle === 0 && inputFrom) inputFrom.value = rounded; if (handle === 1 && inputTo) inputTo.value = rounded; }); // Fix the current displayed values in the inputs immediately const current = sliderEl.noUiSlider.get(); if (inputFrom) inputFrom.value = Math.round(parseFloat(current[0])); if (inputTo) inputTo.value = Math.round(parseFloat(current[1])); }, }; })(Drupal);