Skip to content

Commit

Permalink
Merge pull request #5667 from dmitrylyzo/fix-slider-float
Browse files Browse the repository at this point in the history
  • Loading branch information
thornbill authored Jun 6, 2024
2 parents 24f4833 + 7a88d5f commit 5495ef2
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/elements/emby-slider/emby-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
}
}

let supportsValueAutoSnap = false;
{
const slider = document.createElement('input');
slider.type = 'range';
slider.min = '-30';
slider.max = '30';
slider.step = '0.1';

slider.value = '0.30000000000000004';

supportsValueAutoSnap = slider.value === '0.3';

if (!supportsValueAutoSnap) {
console.debug('[EmbySlider] HTMLInputElement doesn\'t snap value - use workaround.');
}
}

/**
* Returns normalized slider step.
*
Expand Down Expand Up @@ -112,12 +129,45 @@ function mapValueToFraction(range, value) {
return Math.min(Math.max(fraction, 0), 1);
}

/**
* Returns value snapped to the slider step.
*
* @param {HTMLInputElement} range slider itself
* @param {number} value slider value
* @return {number} value snapped to the slider step
*/
function snapValue(range, value) {
if (range.step !== 'any') {
const min = parseFloat(range.min);
const step = normalizeSliderStep(range);
const decimals = Math.max(decimalCount(min), decimalCount(step));

value -= min;
value = Math.round(value / step) * step;
value += min;

value = parseFloat(value.toFixed(decimals));
}

return value;
}

/**
* Updates progress bar.
*
* @param {boolean} [isValueSet] update by 'valueset' event or by timer
*/
function updateValues(isValueSet) {
if (!!isValueSet && !supportsValueAutoSnap) {
const value = snapValue(this, parseFloat(this.value)).toString();

if (this.value !== value) {
this.value = value;

if (supportsValueSetOverride) return;
}
}

// Do not update values by 'valueset' in case of soft-implemented dragging
if (!!isValueSet && (!!this.keyboardDragging || !!this.touched)) {
return;
Expand Down Expand Up @@ -465,7 +515,7 @@ function finishKeyboardDragging(elem) {
function stepKeyboard(elem, delta) {
startKeyboardDragging(elem);

elem.value = Math.max(elem.min, Math.min(elem.max, parseFloat(elem.value) + delta));
elem.value = parseFloat(elem.value) + delta;

const event = new Event('input', {
bubbles: true,
Expand Down

0 comments on commit 5495ef2

Please sign in to comment.