From 1a8d628be59caf9c6527d0f79bfa87beabdd4ac9 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Sat, 8 Sep 2018 07:22:03 -0500 Subject: [PATCH] Clamp number input from user --- manage/config-dialog.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/manage/config-dialog.js b/manage/config-dialog.js index 46bf7f0e..32101edd 100644 --- a/manage/config-dialog.js +++ b/manage/config-dialog.js @@ -310,11 +310,26 @@ function configDialog(style) { function updateVarOnChange() { this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0'; + if (this.type === 'number') { + this.value = this.va.value = clampValue(this.value, this.va.range); + } if (this.type === 'range') { $('.current-value', this.closest('.config-range')).textContent = this.va.value; } } + // Clamp input[type=number] to a valid range + function clampValue(value, [min = 0, max = 100, step]) { + if (value < min) { + return min; + } else if (value > max) { + return max; + } + const remainder = value % (step || 1); + // Don't restrict to integer values if step is undefined. + return typeof step !== 'undefined' && remainder !== 0 ? value - remainder : value; + } + function updateVarOnInput(event, debounced = false) { if (debounced) { event.target.dispatchEvent(new Event('change', {bubbles: true}));