Corrects the value of the slider based on the pointer event's position.
(event: PointerEvent)
| 440 | |
| 441 | /** Corrects the value of the slider based on the pointer event's position. */ |
| 442 | _fixValue(event: PointerEvent): void { |
| 443 | const xPos = event.clientX - this._slider._cachedLeft; |
| 444 | const width = this._slider._cachedWidth; |
| 445 | const step = this._slider.step === 0 ? 1 : this._slider.step; |
| 446 | const numSteps = Math.floor((this._slider.max - this._slider.min) / step); |
| 447 | const percentage = this._slider._isRtl() ? 1 - xPos / width : xPos / width; |
| 448 | |
| 449 | // To ensure the percentage is rounded to the necessary number of decimals. |
| 450 | const fixedPercentage = Math.round(percentage * numSteps) / numSteps; |
| 451 | |
| 452 | const impreciseValue = |
| 453 | fixedPercentage * (this._slider.max - this._slider.min) + this._slider.min; |
| 454 | const value = Math.round(impreciseValue / step) * step; |
| 455 | const prevValue = this.value; |
| 456 | |
| 457 | if (value === prevValue) { |
| 458 | // Because we prevented UI updates, if it turns out that the race |
| 459 | // condition didn't happen and the value is already correct, we |
| 460 | // have to apply the ui updates now. |
| 461 | this._slider._onValueChange(this); |
| 462 | this._slider.step > 0 |
| 463 | ? this._updateThumbUIByValue() |
| 464 | : this._updateThumbUIByPointerEvent(event, {withAnimation: this._slider._hasAnimation}); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | this.value = value; |
| 469 | this.valueChange.emit(this.value); |
| 470 | this._onChangeFn?.(this.value); |
| 471 | this._slider._onValueChange(this); |
| 472 | this._slider.step > 0 |
| 473 | ? this._updateThumbUIByValue() |
| 474 | : this._updateThumbUIByPointerEvent(event, {withAnimation: this._slider._hasAnimation}); |
| 475 | } |
| 476 | |
| 477 | _onPointerMove(event: PointerEvent): void { |
| 478 | // Again, does nothing if a step is defined because |
no test coverage detected