Files
KSP-MGA-Planner/dist/main/editor/integer-input.js
Krafpy 824af087c1 Modified file structure.
Modified the file structure to have the `index.html` at the root
of the repository. Needed for Github Pages.
2021-08-15 21:31:25 +02:00

38 lines
1.0 KiB
JavaScript

export class IntegerInput {
constructor(id) {
this.element = document.getElementById(id);
this.min = parseInt(this.element.min);
this.max = parseInt(this.element.max);
this.element.onchange = () => this.validate();
}
get value() {
return parseInt(this.element.value);
}
set value(num) {
this.element.value = num.toString();
}
validate() {
let num = parseInt(this.element.value);
if (!isNaN(num)) {
num = this._clamp(num);
this.element.value = num.toString();
return true;
}
return false;
}
_clamp(num) {
if (!isNaN(this.min) && !isNaN(this.max))
return THREE.MathUtils.clamp(num, this.min, this.max);
return num;
}
assertValidity() {
if (!this.validate())
throw "Invalid numeric input.";
}
setMinMax(min, max) {
this.min = min;
this.max = max;
this.value = this._clamp(this.value);
}
}