Alas, poor clamp! I knew it well

This commit is contained in:
Rob Garrison 2018-09-20 04:25:45 -05:00
parent b0fbdfb6e9
commit 5f60c519ce

View File

@ -266,7 +266,8 @@ function configDialog(style) {
onfocus: va.type === 'number' ? selectAllOnFocus : null, onfocus: va.type === 'number' ? selectAllOnFocus : null,
onblur: va.type === 'number' ? updateVarOnBlur : null, onblur: va.type === 'number' ? updateVarOnBlur : null,
onchange: updateVarOnChange, onchange: updateVarOnChange,
oninput: updateVarOnInput oninput: updateVarOnInput,
required: true
}; };
if (typeof va.min === 'number') { if (typeof va.min === 'number') {
options.min = va.min; options.min = va.min;
@ -312,15 +313,8 @@ function configDialog(style) {
} }
} }
function updateVarOnBlur(e = {}) { function updateVarOnBlur() {
const value = Number(this.value); this.value = isDefault(this.va) ? this.va.default : this.va.value;
const clamped = clampValue(value, this.va);
if (clamped === value) {
this.va.value = value;
}
if (e.type === 'blur') {
this.value = clamped;
}
} }
function updateVarOnChange() { function updateVarOnChange() {
@ -328,29 +322,14 @@ function configDialog(style) {
this.va.value = Number(this.value); this.va.value = Number(this.value);
updateRangeCurrentValue(this.va, this.va.value); updateRangeCurrentValue(this.va, this.va.value);
} else if (this.type === 'number') { } else if (this.type === 'number') {
updateVarOnBlur.call(this); if (this.reportValidity()) {
this.va.value = Number(this.value);
}
} else { } else {
this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0'; this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0';
} }
} }
// Clamp input[type=number] to a valid range
function clampValue(value, va) {
// Don't restrict to integer values if step is undefined.
if (typeof va.step === 'number') {
const step = va.step || 1;
const scale = 10 ** (step.toString().split('.')[1] || '').length;
value = Math.round((value * scale) / (step * scale)) * (step * scale) / scale;
}
if (typeof va.min === 'number' && value < va.min) {
return va.min;
}
if (typeof va.max === 'number' && value > va.max) {
return va.max;
}
return value;
}
function updateRangeCurrentValue(va, value) { function updateRangeCurrentValue(va, value) {
const span = $('.current-value', va.input.closest('.config-range')); const span = $('.current-value', va.input.closest('.config-range'));
if (span) { if (span) {