reminder-bot/static/js/interval.js

95 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-04-03 20:53:28 +00:00
function get_interval(element) {
let months = element.querySelector('input[name="interval_months"]').value;
let days = element.querySelector('input[name="interval_days"]').value;
let hours = element.querySelector('input[name="interval_hours"]').value;
let minutes = element.querySelector('input[name="interval_minutes"]').value;
let seconds = element.querySelector('input[name="interval_seconds"]').value;
return {
months: parseInt(months) || null,
2022-12-10 16:21:43 +00:00
days: parseInt(days) || null,
2022-04-03 20:53:28 +00:00
seconds:
2022-12-10 16:21:43 +00:00
(parseInt(hours) || 0) * 3600 +
2022-04-03 20:53:28 +00:00
(parseInt(minutes) || 0) * 60 +
(parseInt(seconds) || 0) || null,
};
}
function update_interval(element) {
2022-03-27 17:03:42 +00:00
let months = element.querySelector('input[name="interval_months"]');
let days = element.querySelector('input[name="interval_days"]');
let hours = element.querySelector('input[name="interval_hours"]');
let minutes = element.querySelector('input[name="interval_minutes"]');
let seconds = element.querySelector('input[name="interval_seconds"]');
let interval = get_interval(element);
if (interval.months === null && interval.days === null && interval.seconds === null) {
months.value = "";
days.value = "";
hours.value = "";
minutes.value = "";
seconds.value = "";
} else {
months.value = months.value.padStart(1, "0");
days.value = days.value.padStart(1, "0");
hours.value = hours.value.padStart(2, "0");
minutes.value = minutes.value.padStart(2, "0");
seconds.value = seconds.value.padStart(2, "0");
if (seconds.value >= 60) {
let quotient = Math.floor(seconds.value / 60);
let remainder = seconds.value % 60;
seconds.value = String(remainder).padStart(2, "0");
minutes.value = String(Number(minutes.value) + Number(quotient)).padStart(
2,
"0"
);
}
if (minutes.value >= 60) {
let quotient = Math.floor(minutes.value / 60);
let remainder = minutes.value % 60;
2022-03-27 17:03:42 +00:00
minutes.value = String(remainder).padStart(2, "0");
hours.value = String(Number(hours.value) + Number(quotient)).padStart(2, "0");
}
2022-03-27 17:03:42 +00:00
}
}
const $intervalGroup = document.querySelector(".interval-group");
2022-03-27 17:03:42 +00:00
document.querySelector(".interval-group").addEventListener(
"blur",
(ev) => {
2022-04-03 20:53:28 +00:00
if (ev.target.nodeName !== "BUTTON") update_interval($intervalGroup);
2022-03-27 17:03:42 +00:00
},
true
);
$intervalGroup.querySelector("button.clear").addEventListener("click", () => {
2022-03-27 18:17:30 +00:00
$intervalGroup.querySelectorAll("input").forEach((el) => {
el.value = "";
});
2022-03-27 17:03:42 +00:00
});
document.addEventListener("remindersLoaded", (event) => {
for (reminder of event.detail) {
2022-03-27 18:17:30 +00:00
let $intervalGroup = reminder.node.querySelector(".interval-group");
$intervalGroup.addEventListener(
"blur",
(ev) => {
if (ev.target.nodeName !== "BUTTON") update_interval($intervalGroup);
},
true
);
2022-03-27 17:03:42 +00:00
2022-03-27 18:17:30 +00:00
$intervalGroup.querySelector("button.clear").addEventListener("click", () => {
$intervalGroup.querySelectorAll("input").forEach((el) => {
el.value = "";
});
2022-03-27 17:03:42 +00:00
});
}
});