2 changed files with 39 additions and 1 deletions
@ -0,0 +1,38 @@ |
|||||||
|
(function (Drupal) { |
||||||
|
"use strict"; |
||||||
|
|
||||||
|
Drupal.behaviors.nouisliderYearFormat = { |
||||||
|
attach: function (context) { |
||||||
|
// 1. Target the slider element
|
||||||
|
const sliderEl = document.getElementById("year"); |
||||||
|
if (!sliderEl) return; |
||||||
|
|
||||||
|
// 2. Use a small function to attach the rounding logic
|
||||||
|
const applyRounding = () => { |
||||||
|
if (sliderEl.noUiSlider && !sliderEl.dataset.yearFormatApplied) { |
||||||
|
sliderEl.dataset.yearFormatApplied = "true"; |
||||||
|
|
||||||
|
sliderEl.noUiSlider.on("update", function (values, handle) { |
||||||
|
const inputId = |
||||||
|
handle === 0 ? "nouislider-input-from" : "nouislider-input-to"; |
||||||
|
const input = document.getElementById(inputId); |
||||||
|
if (input) { |
||||||
|
// Round the string value ("1971.00" -> 1971)
|
||||||
|
input.value = Math.round(parseFloat(values[handle])); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
// 3. Try immediately, then periodically until the slider is ready
|
||||||
|
applyRounding(); |
||||||
|
const retry = setInterval(() => { |
||||||
|
applyRounding(); |
||||||
|
if (sliderEl.dataset.yearFormatApplied) clearInterval(retry); |
||||||
|
}, 100); |
||||||
|
|
||||||
|
// Stop trying after 3 seconds
|
||||||
|
setTimeout(() => clearInterval(retry), 3000); |
||||||
|
}, |
||||||
|
}; |
||||||
|
})(Drupal); |
||||||
Loading…
Reference in new issue