Files
KSP-MGA-Planner/dist/main/editor/time-selector.js
2022-12-16 18:39:53 +01:00

44 lines
1.5 KiB
JavaScript

import { KSPTime } from "../time/time.js";
export class TimeSelector {
constructor(namePrefix, config) {
this.config = config;
this.yearInput = document.getElementById(`${namePrefix}-year`);
this.dayInput = document.getElementById(`${namePrefix}-day`);
this.hourInput = document.getElementById(`${namePrefix}-hour`);
this.selector = document.getElementById(`${namePrefix}-time`);
this.selector.oninput = () => this.validate();
this.time = KSPTime(0, this.config.time);
this.validate();
}
get dateSeconds() {
return this.time.dateSeconds;
}
input(action) {
this.onChange = action;
this.selector.oninput = () => this.onChange();
}
update() {
const { year, day, hour } = this.time.displayYDHMS;
this.yearInput.value = year.toString();
this.dayInput.value = day.toString();
this.hourInput.value = hour.toString();
}
validate() {
let year = parseInt(this.yearInput.value);
let day = parseInt(this.dayInput.value);
let hour = parseInt(this.hourInput.value);
if (isNaN(year) || isNaN(day) || isNaN(hour)) {
return false;
}
else {
this.time.displayYDHMS = { year, day, hour, minute: 0, second: 0 };
this.update();
}
return true;
}
setToDefault() {
this.time.dateSeconds = this.time.defaultDate;
this.update();
}
}